Bug 1867656 - Refactor loading mozconfig extension property into its own gradle file...
[gecko.git] / mobile / android / gradle / mozconfig.gradle
blob742a53e74071edd9da724d7ce8f91a31736735ea
1 /* -*- Mode: Groovy; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 import groovy.json.JsonSlurper
8 // Loads the mach environment configurations into a gradle extension property.
10 apply from: file('./mach_env.gradle')
12 logger.lifecycle("mozconfig.gradle> Loading mach environment into a gradle extension property")
14 def commandLine = ["${topsrcdir}/mach", "environment", "--format", "json", "--verbose"]
15 if (System.env.GRADLE_MACH_PYTHON) {
16     commandLine.addAll(0, [System.env.GRADLE_MACH_PYTHON])
17 } else if (System.properties["os.name"].contains("Windows")) {
18     commandLine.addAll(0, ["python"])
21 def proc = commandLine.execute(machEnv(topsrcdir), new File(topsrcdir))
22 def standardOutput = new ByteArrayOutputStream()
23 def standardError = new ByteArrayOutputStream()
24 proc.consumeProcessOutput(standardOutput, standardError)
25 proc.waitFor()
27 // Only show the output if something went wrong.
28 if (proc.exitValue() != 0) {
29     logger.info("mozconfig.gradle> Error running ./mach environment: \n\n"
30             + "Process '${commandLine}' finished with non-zero exit value ${proc.exitValue()}:\n\n"
31             + "stdout:\n"
32             + "${standardOutput.toString()}\n\n"
33             + "stderr:\n"
34             + "${standardError.toString()}")
35     throw new StopExecutionException(
36             "Could not run ./mach environment. Try running ./mach build first.")
39 def slurper = new JsonSlurper()
40 def json = slurper.parseText(standardOutput.toString())
42 if (json.substs.MOZ_BUILD_APP != 'mobile/android') {
43     throw new GradleException("Building with Gradle is only supported for Fennec, i.e., MOZ_BUILD_APP == 'mobile/android'.")
46 // The Gradle instance is shared between settings.gradle and all the
47 // other build.gradle files (see
48 // http://forums.gradle.org/gradle/topics/define_extension_properties_from_settings_xml).
49 // We use this ext property to pass the per-object-directory mozconfig
50 // between scripts.  This lets us execute set-up code before we gradle
51 // tries to configure the project even once, and as a side benefit
52 // saves invoking |mach environment| multiple times.
53 gradle.ext.mozconfig = json
55 // Produced by `mach build`.  Bug 1543982: the mozconfig determined by `mach
56 // environment` above can be different because `mach build` itself sets certain
57 // critical environment variables including MOZ_OBJDIR, CC, and CXX.  We use
58 // this record to patch up the environment when we recursively invoke `mach
59 // build ...` commands from within Gradle.  This avoids invalidating configure
60 // based on the changed environment variables.
61 def orig = slurper.parse(new File(json.topobjdir, '.mozconfig.json'))
62 gradle.ext.mozconfig.orig_mozconfig = orig.mozconfig