2 import org.tomlj.TomlParseResult
3 import org.tomlj.TomlTable
7 gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORIES.each { repository ->
10 if (gradle.mozconfig.substs.ALLOW_INSECURE_GRADLE_REPOSITORIES) {
11 allowInsecureProtocol = true
18 detekt_plugin = Versions.detekt
19 python_envs_plugin = Versions.python_envs_plugin
20 ksp_plugin = Versions.ksp_plugin
22 // Used in mobile/android/fenix/app/build.gradle
23 protobuf_plugin = FenixVersions.protobuf_plugin
27 classpath 'org.mozilla.apilint:apilint:0.5.2'
28 classpath 'com.android.tools.build:gradle:8.0.2' // Bug 1881001 for using AC version
29 classpath 'org.apache.commons:commons-exec:1.3'
30 classpath 'com.diffplug.spotless:spotless-plugin-gradle:6.25.0'
31 classpath 'org.tomlj:tomlj:1.1.0'
32 classpath ComponentsDependencies.tools_kotlingradle
34 // Used in mobile/android/fenix/app/build.gradle
35 classpath ComponentsDependencies.androidx_safeargs
36 classpath FenixDependencies.osslicenses_plugin
37 classpath FenixDependencies.tools_benchmarkgradle
38 classpath "org.mozilla.telemetry:glean-gradle-plugin:${Versions.mozilla_glean}"
39 classpath "${ApplicationServicesConfig.groupId}:tooling-nimbus-gradle:${ApplicationServicesConfig.version}"
44 id("io.gitlab.arturbosch.detekt").version("$detekt_plugin")
45 id("com.google.devtools.ksp").version("$ksp_plugin")
48 def tryInt = { string ->
52 if (string.isInteger()) {
53 return string as Integer
58 // Parses the Cargo.lock and returns the version for the given package name.
59 def getRustVersionFor(packageName) {
60 String version = null;
61 TomlParseResult result = Toml.parse(file("Cargo.lock").getText());
62 for (object in result.getArray("package").toList()) {
63 def table = (TomlTable) object
64 if (table.getString("name") == packageName) {
65 if (version != null) {
66 throw new StopExecutionException("Multiple versions for '${packageName}' found." +
67 " Ensure '${packageName}' is only included once.")
69 version = table.getString("version")
76 // Expose the per-object-directory configuration to all projects.
78 mozconfig = gradle.mozconfig
79 topsrcdir = gradle.mozconfig.topsrcdir
80 topobjdir = gradle.mozconfig.topobjdir
82 gleanVersion = Versions.mozilla_glean
83 if (gleanVersion != getRustVersionFor("glean")) {
84 throw new StopExecutionException("Mismatched Glean version, expected: ${gleanVersion}," +
85 " found ${getRustVersionFor("glean")}")
88 artifactSuffix = getArtifactSuffix()
89 versionName = getVersionName()
90 versionCode = computeVersionCode()
91 versionNumber = getVersionNumber()
92 buildId = getBuildId()
94 buildToolsVersion = mozconfig.substs.ANDROID_BUILD_TOOLS_VERSION
95 compileSdkVersion = tryInt(mozconfig.substs.ANDROID_TARGET_SDK)
96 targetSdkVersion = tryInt(mozconfig.substs.ANDROID_TARGET_SDK)
97 minSdkVersion = tryInt(mozconfig.substs.MOZ_ANDROID_MIN_SDK_VERSION)
98 manifestPlaceholders = [
99 ANDROID_PACKAGE_NAME: mozconfig.substs.ANDROID_PACKAGE_NAME,
100 ANDROID_TARGET_SDK: mozconfig.substs.ANDROID_TARGET_SDK,
101 MOZ_ANDROID_MIN_SDK_VERSION: mozconfig.substs.MOZ_ANDROID_MIN_SDK_VERSION,
106 gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORIES.each { repository ->
109 if (gradle.mozconfig.substs.ALLOW_INSECURE_GRADLE_REPOSITORIES) {
110 allowInsecureProtocol = true
116 // Use the semanticdb-javac and semanticdb-kotlinc plugins to generate semanticdb files for Searchfox
117 if (mozconfig.substs.ENABLE_MOZSEARCH_PLUGIN || mozconfig.substs.DOWNLOAD_ALL_GRADLE_DEPENDENCIES) {
118 def targetRoot = new File(topobjdir, "mozsearch_java_index")
119 def semanticdbJavacVersion = "com.sourcegraph:semanticdb-javac:0.9.6"
120 def semanticdbKotlincVersion = "com.sourcegraph:semanticdb-kotlinc:0.4.0"
123 def addDependencyToConfigurationIfExists = { configurationName, dependency ->
124 def configuration = configurations.findByName(configurationName)
125 if (configuration != null) {
126 dependencies.add(configurationName, dependency)
130 addDependencyToConfigurationIfExists("compileOnly", semanticdbJavacVersion)
131 addDependencyToConfigurationIfExists("testCompileOnly", semanticdbJavacVersion)
132 addDependencyToConfigurationIfExists("androidTestCompileOnly", semanticdbJavacVersion)
133 addDependencyToConfigurationIfExists("kotlinCompilerPluginClasspath", semanticdbKotlincVersion)
136 tasks.withType(JavaCompile) {
137 options.compilerArgs += [
138 "-Xplugin:semanticdb -sourceroot:${topsrcdir} -targetroot:${targetRoot}",
142 tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
143 compilerOptions.freeCompilerArgs.addAll([
144 "-P", "plugin:semanticdb-kotlinc:sourceroot=${topsrcdir}",
145 "-P", "plugin:semanticdb-kotlinc:targetroot=${targetRoot}",
150 task downloadDependencies() {
151 description 'Download all dependencies to the Gradle cache'
153 configurations.each { configuration ->
154 if (configuration.canBeResolved) {
155 configuration.allDependencies.each { dependency ->
157 configuration.files(dependency)
159 println("Could not resolve ${configuration.name} -> ${dependency.name}")
160 println(" > ${e.message}")
162 println(" >> ${e.cause}")
164 println(" >> ${e.cause.cause}")
176 buildDir "${topobjdir}/gradle/build"
178 // A stream that processes bytes line by line, prepending a tag before sending
179 // each line to Gradle's logging.
180 class TaggedLogOutputStream extends org.apache.commons.exec.LogOutputStream {
184 TaggedLogOutputStream(tag, logger) {
189 void processLine(String line, int level) {
190 logger.lifecycle("${this.tag} ${line}")
194 ext.geckoBinariesOnlyIf = { task ->
195 // Never when Gradle was invoked within `mach build`.
196 if ('1' == System.env.GRADLE_INVOKED_WITHIN_MACH_BUILD) {
197 rootProject.logger.lifecycle("Skipping task ${task.path} because: within `mach build`")
201 // Never for official builds.
202 if (mozconfig.substs.MOZILLA_OFFICIAL) {
203 rootProject.logger.lifecycle("Skipping task ${task.path} because: MOZILLA_OFFICIAL")
207 // Multi-l10n builds set `AB_CD=multi`, which isn't a valid locale, and
208 // `MOZ_CHROME_MULTILOCALE`. To avoid failures, if Gradle is invoked with
209 // either, we don't invoke Make at all; this allows a multi-locale omnijar
210 // to be consumed without modification.
211 if ('multi' == System.env.AB_CD || System.env.MOZ_CHROME_MULTILOCALE) {
212 rootProject.logger.lifecycle("Skipping task ${task.path} because: AB_CD=multi")
216 // Single-locale l10n repacks set `IS_LANGUAGE_REPACK=1` and handle resource
217 // and code generation themselves.
218 if ('1' == System.env.IS_LANGUAGE_REPACK) {
219 rootProject.logger.lifecycle("Skipping task ${task.path} because: IS_LANGUAGE_REPACK")
223 rootProject.logger.lifecycle("Executing task ${task.path}")
227 // Non-official versions are like "61.0a1", where "a1" is the milestone.
228 // This simply strips that off, leaving "61.0" in this example.
229 def getAppVersionWithoutMilestone() {
230 return project.ext.mozconfig.substs.MOZ_APP_VERSION.replaceFirst(/a[0-9]/, "")
233 // This converts MOZ_APP_VERSION into an integer
236 // We take something like 58.1.2a1 and come out with 5800102
237 // This gives us 3 digits for the major number, and 2 digits
238 // each for the minor and build number. Beta and Release
240 // This must be synchronized with _compute_gecko_version(...) in /taskcluster/gecko_taskgraph/transforms/task.py
241 def computeVersionCode() {
242 String appVersion = getAppVersionWithoutMilestone()
244 // Split on the dot delimiter, e.g. 58.1.1a1 -> ["58, "1", "1a1"]
245 String[] parts = appVersion.split('\\.')
247 assert parts.size() == 2 || parts.size() == 3
250 int code = Integer.parseInt(parts[0]) * 100000
253 code += Integer.parseInt(parts[1]) * 100
256 if (parts.size() == 3) {
257 code += Integer.parseInt(parts[2])
263 def getVersionName() {
264 return "${mozconfig.substs.MOZ_APP_VERSION}-${mozconfig.substs.MOZ_UPDATE_CHANNEL}"
267 // Mimic Python: open(os.path.join(buildconfig.topobjdir, 'buildid.h')).readline().split()[2]
269 return file("${topobjdir}/buildid.h").getText('utf-8').split()[2]
272 def getVersionNumber() {
273 def appVersion = getAppVersionWithoutMilestone()
274 def parts = appVersion.split('\\.')
275 def version = parts[0] + "." + parts[1] + "." + getBuildId()
276 def substs = project.ext.mozconfig.substs
277 if (!substs.MOZILLA_OFFICIAL && !substs.MOZ_ANDROID_FAT_AAR_ARCHITECTURES) {
278 // Use -SNAPSHOT versions locally to enable the local GeckoView substitution flow.
279 version += "-SNAPSHOT"
284 def getArtifactSuffix() {
285 def substs = project.ext.mozconfig.substs
288 // Release artifacts don't specify the channel, for the sake of simplicity.
289 if (substs.MOZ_UPDATE_CHANNEL != 'release') {
290 suffix += "-${mozconfig.substs.MOZ_UPDATE_CHANNEL}"
296 class MachExec extends Exec {
298 // Bug 1543982: When invoking `mach build` recursively, the outer `mach
299 // build` itself modifies the environment, causing configure to run
300 // again. This tries to restore the environment that the outer `mach
301 // build` was invoked in. See the comment in
302 // $topsrcdir/settings.gradle.
303 project.ext.mozconfig.mozconfig.env.unmodified.each { k, v -> environment.remove(k) }
304 environment project.ext.mozconfig.orig_mozconfig.env.unmodified
305 environment 'MOZCONFIG', project.ext.mozconfig.substs.MOZCONFIG
309 task machBuildFaster(type: MachExec) {
310 onlyIf rootProject.ext.geckoBinariesOnlyIf
312 workingDir "${topsrcdir}"
314 commandLine mozconfig.substs.PYTHON3
315 args "${topsrcdir}/mach"
319 // Add `-v` if we're running under `--info` (or `--debug`).
320 if (project.logger.isEnabled(LogLevel.INFO)) {
324 // `path` is like `:machBuildFaster`.
325 standardOutput = new TaggedLogOutputStream("${path}>", logger)
326 errorOutput = standardOutput
329 task machStagePackage(type: MachExec) {
330 onlyIf rootProject.ext.geckoBinariesOnlyIf
332 dependsOn rootProject.machBuildFaster
334 workingDir "${topobjdir}"
336 // We'd prefer this to be a `mach` invocation, but `mach build
337 // mobile/android/installer/stage-package` doesn't work as expected.
338 commandLine mozconfig.substs.GMAKE
340 args "${topobjdir}/mobile/android/installer"
343 outputs.file "${topobjdir}/dist/geckoview/assets/omni.ja"
345 outputs.file "${topobjdir}/dist/geckoview/assets/${mozconfig.substs.ANDROID_CPU_ARCH}/libxul.so"
346 outputs.file "${topobjdir}/dist/geckoview/lib/${mozconfig.substs.ANDROID_CPU_ARCH}/libmozglue.so"
348 // Force running `stage-package`.
349 outputs.upToDateWhen { false }
351 // `path` is like `:machStagePackage`.
352 standardOutput = new TaggedLogOutputStream("${path}>", logger)
353 errorOutput = standardOutput
357 subprojects { project ->
358 tasks.withType(JavaCompile) {
359 // Add compiler args for all code except third-party code.
360 options.compilerArgs += [
361 // Turn on all warnings, except...
363 // Deprecation, because we do use deprecated API for compatibility.
364 "-Xlint:-deprecation",
365 // Serial, because we don't use Java serialization.
367 // Classfile, because javac has a bug with MethodParameters attributes
368 // with Java 7. https://bugs.openjdk.java.net/browse/JDK-8190452
371 // In GeckoView java projects only, turn all remaining warnings
372 // into errors unless marked by @SuppressWarnings.
373 def projectName = project.getName()
374 if (projectName.startsWith('geckoview')
375 || projectName == 'annotations'
376 || projectName == 'exoplayer2'
377 || projectName == 'messaging_example'
378 || projectName == 'port_messaging_example'
379 || projectName == 'test_runner'
381 options.compilerArgs += [
394 languageLevel = '1.8'
398 // Object directories take a huge amount of time for IntelliJ to index.
399 // Exclude them. Convention is that object directories start with obj.
400 // IntelliJ is clever and will not exclude the parts of the object
401 // directory that are referenced, if there are any. In practice,
402 // indexing the entirety of the tree is taking too long, so exclude all
404 def topsrcdirURI = file(topsrcdir).toURI()
405 excludeDirs += files(file(topsrcdir)
406 .listFiles({it.isDirectory()} as FileFilter)
407 .collect({topsrcdirURI.relativize(it.toURI()).toString()}) // Relative paths.
408 .findAll({!it.equals('mobile/')}))
410 // If topobjdir is below topsrcdir, hide only some portions of that tree.
411 def topobjdirURI = file(topobjdir).toURI()
412 if (!topsrcdirURI.relativize(topobjdirURI).isAbsolute()) {
413 excludeDirs -= file(topobjdir)
414 excludeDirs += files(file(topobjdir).listFiles())
415 excludeDirs -= file("${topobjdir}/gradle")
420 subprojects { project ->
421 // Perform spotless lint in GeckoView projects only.
422 def projectName = project.getName()
423 if (projectName.startsWith('geckoview')
424 || projectName == 'annotations'
425 || projectName == 'exoplayer2'
426 || projectName == 'messaging_example'
427 || projectName == 'port_messaging_example'
428 || projectName == 'test_runner'
430 apply plugin: "com.diffplug.spotless"
434 target project.fileTree(project.projectDir) {
436 exclude '**/thirdparty/**'
438 googleJavaFormat('1.17.0')
441 target project.fileTree(project.projectDir) {
443 exclude '**/thirdparty/**'
450 // Set the source and target compatibility for non-GeckoView projects only.
451 if (it.hasProperty('android')) {
454 sourceCompatibility JavaVersion.VERSION_17
455 targetCompatibility JavaVersion.VERSION_17
462 project.configurations.configureEach {
463 resolutionStrategy.capabilitiesResolution.withCapability("org.mozilla.telemetry:glean-native") {
464 def toBeSelected = candidates.find {
465 it.id instanceof ProjectComponentIdentifier && it.id.projectName.contains('geckoview')
467 if (toBeSelected != null) {
470 because 'use GeckoView Glean instead of standalone Glean'