Bug 1860954 replace AsyncCubebTask with NS_NewRunnableFunction() r=pehrsons
[gecko.git] / build.gradle
blobff0483968c48559b52f386ac7aed641f45e1d7b6
1 import org.tomlj.Toml
2 import org.tomlj.TomlParseResult
3 import org.tomlj.TomlTable
5 def tryInt = { string ->
6     if (string == null) {
7         return string
8     }
9     if (string.isInteger()) {
10         return string as Integer
11     }
12     return string
15 // Parses the Cargo.lock and returns the version for the given package name.
16 def getRustVersionFor(packageName) {
17     String version = null;
18     TomlParseResult result = Toml.parse(file("Cargo.lock").getText());
19     for (object in result.getArray("package").toList()) {
20         def table = (TomlTable) object
21         if (table.getString("name") == packageName) {
22             if (version != null) {
23                 throw new StopExecutionException("Multiple versions for '${packageName}' found." +
24                         " Ensure '${packageName}' is only included once.")
25             }
26             version = table.getString("version")
27         }
28     }
29     return version
32 allprojects {
33     // Expose the per-object-directory configuration to all projects.
34     ext {
35         mozconfig = gradle.mozconfig
36         topsrcdir = gradle.mozconfig.topsrcdir
37         topobjdir = gradle.mozconfig.topobjdir
39         gleanVersion = "54.0.0"
40         if (gleanVersion != getRustVersionFor("glean")) {
41           throw new StopExecutionException("Mismatched Glean version, expected: ${gleanVersion}," +
42               " found ${getRustVersionFor("glean")}")
43         }
45         artifactSuffix = getArtifactSuffix()
46         versionName = getVersionName()
47         versionCode = computeVersionCode()
48         versionNumber = getVersionNumber()
49         buildId = getBuildId()
51         buildToolsVersion = mozconfig.substs.ANDROID_BUILD_TOOLS_VERSION
52         compileSdkVersion = tryInt(mozconfig.substs.ANDROID_TARGET_SDK)
53         targetSdkVersion = tryInt(mozconfig.substs.ANDROID_TARGET_SDK)
54         minSdkVersion = tryInt(mozconfig.substs.MOZ_ANDROID_MIN_SDK_VERSION)
55         manifestPlaceholders = [
56             ANDROID_PACKAGE_NAME: mozconfig.substs.ANDROID_PACKAGE_NAME,
57             ANDROID_TARGET_SDK: mozconfig.substs.ANDROID_TARGET_SDK,
58             MOZ_ANDROID_MIN_SDK_VERSION: mozconfig.substs.MOZ_ANDROID_MIN_SDK_VERSION,
59         ]
60     }
62     repositories {
63         gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORIES.each { repository ->
64             maven {
65                 url repository
66                 if (gradle.mozconfig.substs.ALLOW_INSECURE_GRADLE_REPOSITORIES) {
67                     allowInsecureProtocol = true
68                 }
69             }
70         }
71     }
73     task downloadDependencies() {
74         description 'Download all dependencies to the Gradle cache'
75         doLast {
76             configurations.each { configuration ->
77                 if (configuration.canBeResolved) {
78                     configuration.allDependencies.each { dependency ->
79                         try {
80                             configuration.files(dependency)
81                         } catch(e) {
82                             println("Could not resolve ${configuration.name} -> ${dependency.name}")
83                             println(" > ${e.message}")
84                             if (e.cause) {
85                                 println(" >> ${e.cause}")
86                                 if (e.cause.cause) {
87                                     println(" >> ${e.cause.cause}")
88                                 }
89                             }
90                             println("")
91                         }
92                     }
93                 }
94             }
95         }
96     }
99 buildDir "${topobjdir}/gradle/build"
101 buildscript {
102     repositories {
103         gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORIES.each { repository ->
104             maven {
105                 url repository
106                 if (gradle.mozconfig.substs.ALLOW_INSECURE_GRADLE_REPOSITORIES) {
107                     allowInsecureProtocol = true
108                 }
109             }
110         }
111     }
113     ext.kotlin_version = '1.8.21'
115     dependencies {
116         classpath 'org.mozilla.apilint:apilint:0.5.2'
117         classpath 'com.android.tools.build:gradle:7.4.2'
118         classpath 'org.apache.commons:commons-exec:1.3'
119         classpath 'com.diffplug.spotless:spotless-plugin-gradle:6.18.0'
120         classpath 'org.tomlj:tomlj:1.1.0'
121         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
122     }
125 // A stream that processes bytes line by line, prepending a tag before sending
126 // each line to Gradle's logging.
127 class TaggedLogOutputStream extends org.apache.commons.exec.LogOutputStream {
128     String tag
129     Logger logger
131     TaggedLogOutputStream(tag, logger) {
132         this.tag = tag
133         this.logger = logger
134     }
136     void processLine(String line, int level) {
137         logger.lifecycle("${this.tag} ${line}")
138     }
141 ext.geckoBinariesOnlyIf = { task ->
142     // Never when Gradle was invoked within `mach build`.
143     if ('1' == System.env.GRADLE_INVOKED_WITHIN_MACH_BUILD) {
144         rootProject.logger.lifecycle("Skipping task ${task.path} because: within `mach build`")
145         return false
146     }
148     // Never for official builds.
149     if (mozconfig.substs.MOZILLA_OFFICIAL) {
150         rootProject.logger.lifecycle("Skipping task ${task.path} because: MOZILLA_OFFICIAL")
151         return false
152     }
154     // Multi-l10n builds set `AB_CD=multi`, which isn't a valid locale, and
155     // `MOZ_CHROME_MULTILOCALE`.  To avoid failures, if Gradle is invoked with
156     // either, we don't invoke Make at all; this allows a multi-locale omnijar
157     // to be consumed without modification.
158     if ('multi' == System.env.AB_CD || System.env.MOZ_CHROME_MULTILOCALE) {
159         rootProject.logger.lifecycle("Skipping task ${task.path} because: AB_CD=multi")
160         return false
161     }
163     // Single-locale l10n repacks set `IS_LANGUAGE_REPACK=1` and handle resource
164     // and code generation themselves.
165     if ('1' == System.env.IS_LANGUAGE_REPACK) {
166         rootProject.logger.lifecycle("Skipping task ${task.path} because: IS_LANGUAGE_REPACK")
167         return false
168     }
170     rootProject.logger.lifecycle("Executing task ${task.path}")
171     return true
174 // Non-official versions are like "61.0a1", where "a1" is the milestone.
175 // This simply strips that off, leaving "61.0" in this example.
176 def getAppVersionWithoutMilestone() {
177     return project.ext.mozconfig.substs.MOZ_APP_VERSION.replaceFirst(/a[0-9]/, "")
180 // This converts MOZ_APP_VERSION into an integer
181 // version code.
183 // We take something like 58.1.2a1 and come out with 5800102
184 // This gives us 3 digits for the major number, and 2 digits
185 // each for the minor and build number. Beta and Release
187 // This must be synchronized with _compute_gecko_version(...) in /taskcluster/gecko_taskgraph/transforms/task.py
188 def computeVersionCode() {
189     String appVersion = getAppVersionWithoutMilestone()
191     // Split on the dot delimiter, e.g. 58.1.1a1 -> ["58, "1", "1a1"]
192     String[] parts = appVersion.split('\\.')
194     assert parts.size() == 2 || parts.size() == 3
196     // Major
197     int code = Integer.parseInt(parts[0]) * 100000
199     // Minor
200     code += Integer.parseInt(parts[1]) * 100
202     // Build
203     if (parts.size() == 3) {
204         code += Integer.parseInt(parts[2])
205     }
207     return code;
210 def getVersionName() {
211     return "${mozconfig.substs.MOZ_APP_VERSION}-${mozconfig.substs.MOZ_UPDATE_CHANNEL}"
214 // Mimic Python: open(os.path.join(buildconfig.topobjdir, 'buildid.h')).readline().split()[2]
215 def getBuildId() {
216     return file("${topobjdir}/buildid.h").getText('utf-8').split()[2]
219 def getVersionNumber() {
220     def appVersion = getAppVersionWithoutMilestone()
221     def parts = appVersion.split('\\.')
222     def version = parts[0] + "." + parts[1] + "." + getBuildId()
223     def substs = project.ext.mozconfig.substs
224     if (!substs.MOZILLA_OFFICIAL && !substs.MOZ_ANDROID_FAT_AAR_ARCHITECTURES) {
225         // Use -SNAPSHOT versions locally to enable the local GeckoView substitution flow.
226         version += "-SNAPSHOT"
227     }
228     return version
231 def getArtifactSuffix() {
232     def substs = project.ext.mozconfig.substs
234     def suffix = ""
235     // Release artifacts don't specify the channel, for the sake of simplicity.
236     if (substs.MOZ_UPDATE_CHANNEL != 'release') {
237         suffix += "-${mozconfig.substs.MOZ_UPDATE_CHANNEL}"
238     }
240     return suffix
243 class MachExec extends Exec {
244     def MachExec() {
245         // Bug 1543982: When invoking `mach build` recursively, the outer `mach
246         // build` itself modifies the environment, causing configure to run
247         // again.  This tries to restore the environment that the outer `mach
248         // build` was invoked in.  See the comment in
249         // $topsrcdir/settings.gradle.
250         project.ext.mozconfig.mozconfig.env.unmodified.each { k, v -> environment.remove(k) }
251         environment project.ext.mozconfig.orig_mozconfig.env.unmodified
252         environment 'MOZCONFIG', project.ext.mozconfig.substs.MOZCONFIG
253     }
256 task machBuildFaster(type: MachExec) {
257     onlyIf rootProject.ext.geckoBinariesOnlyIf
259     workingDir "${topsrcdir}"
261     commandLine mozconfig.substs.PYTHON3
262     args "${topsrcdir}/mach"
263     args 'build'
264     args 'faster'
266     // Add `-v` if we're running under `--info` (or `--debug`).
267     if (project.logger.isEnabled(LogLevel.INFO)) {
268         args '-v'
269     }
271     // `path` is like `:machBuildFaster`.
272     standardOutput = new TaggedLogOutputStream("${path}>", logger)
273     errorOutput = standardOutput
276 task machStagePackage(type: MachExec) {
277     onlyIf rootProject.ext.geckoBinariesOnlyIf
279     dependsOn rootProject.machBuildFaster
281     workingDir "${topobjdir}"
283     // We'd prefer this to be a `mach` invocation, but `mach build
284     // mobile/android/installer/stage-package` doesn't work as expected.
285     commandLine mozconfig.substs.GMAKE
286     args '-C'
287     args "${topobjdir}/mobile/android/installer"
288     args 'stage-package'
290     outputs.file "${topobjdir}/dist/geckoview/assets/omni.ja"
292     outputs.file "${topobjdir}/dist/geckoview/assets/${mozconfig.substs.ANDROID_CPU_ARCH}/libxul.so"
293     outputs.file "${topobjdir}/dist/geckoview/lib/${mozconfig.substs.ANDROID_CPU_ARCH}/libmozglue.so"
295     // Force running `stage-package`.
296     outputs.upToDateWhen { false }
298     // `path` is like `:machStagePackage`.
299     standardOutput = new TaggedLogOutputStream("${path}>", logger)
300     errorOutput = standardOutput
303 afterEvaluate {
304     subprojects { project ->
305         tasks.withType(JavaCompile) {
306             // Add compiler args for all code except third-party code.
307             options.compilerArgs += [
308                 // Turn on all warnings, except...
309                 "-Xlint:all",
310                 // Deprecation, because we do use deprecated API for compatibility.
311                 "-Xlint:-deprecation",
312                 // Serial, because we don't use Java serialization.
313                 "-Xlint:-serial",
314                 // Classfile, because javac has a bug with MethodParameters attributes
315                 // with Java 7. https://bugs.openjdk.java.net/browse/JDK-8190452
316                 "-Xlint:-classfile",
317                 // Turn all remaining warnings into errors,
318                 // unless marked by @SuppressWarnings.
319                 "-Werror"]
320         }
321     }
324 apply plugin: 'idea'
326 idea {
327     project {
328         languageLevel = '1.8'
329     }
331     module {
332         // Object directories take a huge amount of time for IntelliJ to index.
333         // Exclude them.  Convention is that object directories start with obj.
334         // IntelliJ is clever and will not exclude the parts of the object
335         // directory that are referenced, if there are any.  In practice,
336         // indexing the entirety of the tree is taking too long, so exclude all
337         // but mobile/.
338         def topsrcdirURI = file(topsrcdir).toURI()
339         excludeDirs += files(file(topsrcdir)
340             .listFiles({it.isDirectory()} as FileFilter)
341             .collect({topsrcdirURI.relativize(it.toURI()).toString()}) // Relative paths.
342             .findAll({!it.equals('mobile/')}))
344         // If topobjdir is below topsrcdir, hide only some portions of that tree.
345         def topobjdirURI = file(topobjdir).toURI()
346         if (!topsrcdirURI.relativize(topobjdirURI).isAbsolute()) {
347             excludeDirs -= file(topobjdir)
348             excludeDirs += files(file(topobjdir).listFiles())
349             excludeDirs -= file("${topobjdir}/gradle")
350         }
351     }
354 subprojects {
355     apply plugin: "com.diffplug.spotless"
357     spotless {
358         java {
359             target project.fileTree(project.projectDir) {
360                 include '**/*.java'
361                 exclude '**/thirdparty/**'
362             }
363             googleJavaFormat('1.17.0')
364         }
365         kotlin {
366             target project.fileTree(project.projectDir) {
367                 include '**/*.kt'
368                 exclude '**/thirdparty/**'
369             }
370             ktlint('0.48.2')
371         }
372     }