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