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 if (!ext.hasProperty("topsrcdir")) {
15 ext.topsrcdir = file(buildscript.getSourceFile()).getParentFile().getParentFile().getParentFile().getParentFile().absolutePath
18 def commandLine = ["${topsrcdir}/mach", "environment", "--format", "json", "--verbose"]
19 if (System.env.GRADLE_MACH_PYTHON) {
20 commandLine.addAll(0, [System.env.GRADLE_MACH_PYTHON])
21 } else if (System.properties["os.name"].contains("Windows")) {
22 commandLine.addAll(0, ["python"])
25 def proc = commandLine.execute(machEnv(topsrcdir), new File(topsrcdir))
26 def standardOutput = new ByteArrayOutputStream()
27 def standardError = new ByteArrayOutputStream()
28 proc.consumeProcessOutput(standardOutput, standardError)
31 // Only show the output if something went wrong.
32 if (proc.exitValue() != 0) {
33 logger.info("mozconfig.gradle> Error running ./mach environment: \n\n"
34 + "Process '${commandLine}' finished with non-zero exit value ${proc.exitValue()}:\n\n"
36 + "${standardOutput.toString()}\n\n"
38 + "${standardError.toString()}")
39 throw new StopExecutionException(
40 "Could not run ./mach environment. Try running ./mach build first.")
43 def outputString = standardOutput.toString().normalize()
44 // Ignore possible lines of output from pip installing packages,
45 // so only start at what looks like the beginning of a JSON object
46 if (outputString.lastIndexOf("\n") != -1) {
47 outputString = outputString.substring(outputString.lastIndexOf("\n") + 1)
49 def slurper = new JsonSlurper()
52 json = slurper.parseText(outputString)
54 logger.info("mozconfig.gradle> Failed to parse JSON output from ./mach environment: \n\n" +
56 throw new StopExecutionException(
57 "Failed to parse JSON output from ./mach environment.\n\n" + outputString);
60 if (json.substs.MOZ_BUILD_APP != 'mobile/android') {
61 throw new GradleException("Building with Gradle is only supported for Firefox for Android, i.e., MOZ_BUILD_APP == 'mobile/android'.")
64 // The Gradle instance is shared between settings.gradle and all the
65 // other build.gradle files (see
66 // http://forums.gradle.org/gradle/topics/define_extension_properties_from_settings_xml).
67 // We use this ext property to pass the per-object-directory mozconfig
68 // between scripts. This lets us execute set-up code before we gradle
69 // tries to configure the project even once, and as a side benefit
70 // saves invoking |mach environment| multiple times.
71 gradle.ext.mozconfig = json
73 // Produced by `mach build`. Bug 1543982: the mozconfig determined by `mach
74 // environment` above can be different because `mach build` itself sets certain
75 // critical environment variables including MOZ_OBJDIR, CC, and CXX. We use
76 // this record to patch up the environment when we recursively invoke `mach
77 // build ...` commands from within Gradle. This avoids invalidating configure
78 // based on the changed environment variables.
79 def orig = slurper.parse(new File(json.topobjdir, '.mozconfig.json'))
80 gradle.ext.mozconfig.orig_mozconfig = orig.mozconfig