1// Top-level build file where you can add configuration options common to all sub-projects/modules.
2
3
4buildscript {
5    repositories {
6        jcenter()
7    }
8    dependencies {
9        classpath 'com.android.tools.build:gradle:2.2.3'
10
11        // NOTE: Do not place your application dependencies here; they belong
12        // in the individual module build.gradle files
13    }
14}
15
16allprojects {
17    repositories {
18        jcenter()
19    }
20}
21
22def setupSkiaLibraryBuild(project, appVariants, appName) {
23    appVariants.all{ variant ->
24        def buildNativeLib = project.task("${variant.name}_BuildSkiaLib", type:Exec) {
25            workingDir '../../../..' // top-level skia directory
26            commandLine constructBuildCommand(project, variant, appName).split()
27        }
28        buildNativeLib.onlyIf { !project.hasProperty("suppressNativeBuild") }
29
30        def copyNativeLib = project.task("${variant.name}_CopySkiaLib", type:Copy) {
31            def fromDir = "../../../../" + getVariantOutDir(project, variant).skiaOut
32            def intoDir = getVariantOutDir(project, variant).androidOut
33            from fromDir
34            into intoDir
35            include "${appName}.so"
36        }
37
38        TaskCollection<Task> compileTask = project.tasks.matching {
39            //  println(it.name)
40            it.name.toLowerCase().contains("compile" + variant.name.toLowerCase()) &&
41                    it.name.toLowerCase().endsWith("ndk")
42        }
43        compileTask.findAll()*.dependsOn copyNativeLib
44        copyNativeLib.dependsOn buildNativeLib
45    }
46}
47
48def getLocalProperties() {
49    Properties properties = new Properties()
50    File propFile = project.rootProject.file('local.properties')
51    if (propFile.canRead()) {
52        properties.load(propFile.newDataInputStream())
53    }
54    propFile = project.rootProject.file('gradle.properties')
55    if (propFile.canRead()) {
56        properties.load(propFile.newDataInputStream())
57    }
58    return properties
59}
60
61def getVariantOutDir(project, variant) {
62    String variantPrefix = null
63    String androidLibDir = null
64    if (variant.name.startsWith("arm64")) {
65        variantPrefix = "arm64"
66        androidLibDir = "arm64-v8a"
67    } else if (variant.name.startsWith("arm")) {
68        variantPrefix = "arm"
69        androidLibDir = "armeabi-v7a"
70    } else if (variant.name.startsWith("x86_64")) {
71        variantPrefix = "x64"
72        androidLibDir = "x86_64"
73    } else if (variant.name.startsWith("x86")) {
74        variantPrefix = "x86"
75        androidLibDir = "x86"
76    } else if (variant.name.startsWith("mipsel")) {
77        variantPrefix = "mipsel"
78        androidLibDir = "mips"
79    } else if (variant.name.startsWith("mips64el")) {
80        variantPrefix = "mips64el"
81        androidLibDir = "mips64"
82    }
83
84    String skiaOutDir = null
85    String propName = "${variantPrefix}.out.dir"
86    if (project.hasProperty(propName)) {
87        skiaOutDir = project.getProperties().getAt(propName)
88    } else {
89        skiaOutDir = getLocalProperties().getProperty(propName, "missing_variant_out")
90    }
91
92    return [skiaOut: skiaOutDir,
93            androidOut: "src/main/libs/${androidLibDir}"]
94}
95
96def constructBuildCommand(project, variant, appName) {
97    String depotToolsDir = null
98    for (String entry : System.getenv("PATH").split(":")) {
99        if (entry.contains("depot_tools")) {
100            depotToolsDir = entry;
101            break;
102        }
103    }
104    if (depotToolsDir == null) {
105        depotToolsDir = getLocalProperties().getProperty('depot_tools.dir', null)
106    }
107
108    if (depotToolsDir == null) {
109        throw GradleScriptException("Depot Tools not found! Please update your path to include" +
110                " depot_tools or define depot_tools.dir in local.properties")
111    }
112
113    String out_dir = getVariantOutDir(project, variant).skiaOut
114    return "${depotToolsDir}/ninja -C $out_dir $appName"
115}
116