mozconfig.gradle (4533B)
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/. */ 5 6 import groovy.json.JsonSlurper 7 8 // Loads the mach environment configurations into a gradle extension property. 9 10 apply from: file('./mach_env.gradle') 11 12 logger.lifecycle("mozconfig.gradle> Loading mach environment into a gradle extension property") 13 14 if (!ext.hasProperty("topsrcdir")) { 15 ext.topsrcdir = file(buildscript.getSourceFile()).getParentFile().getParentFile().getParentFile().getParentFile().absolutePath 16 } 17 18 def command = ["${topsrcdir}/mach", "environment", "--format", "json", "--verbose"] 19 if (System.env.GRADLE_MACH_PYTHON) { 20 command.addAll(0, [System.env.GRADLE_MACH_PYTHON]) 21 } else if (System.properties["os.name"].contains("Windows")) { 22 command.addAll(0, ["python"]) 23 } 24 25 def proc = providers.exec { 26 workingDir = new File(topsrcdir) 27 environment = machEnv(topsrcdir) 28 commandLine = command 29 ignoreExitValue = true 30 } 31 32 def result = proc.result.get().exitValue 33 def standardOutput = proc.standardOutput.asText.get() 34 // Only show the output if something went wrong. 35 if (result != 0) { 36 logger.info("mozconfig.gradle> Error running ./mach environment: \n\n" 37 + "Process '${command}' finished with non-zero exit value ${result}:\n\n" 38 + "stdout:\n" 39 + "${standardOutput}\n\n" 40 + "stderr:\n" 41 + "${proc.standardError.asText.get()}") 42 throw new StopExecutionException( 43 "Could not run ./mach environment. Try running ./mach build first.") 44 } 45 46 def outputString = standardOutput.toString().normalize() 47 // Ignore possible lines of output from pip installing packages, 48 // so only start at what looks like the beginning of a JSON object 49 if (outputString.lastIndexOf("\n") != -1) { 50 outputString = outputString.substring(outputString.lastIndexOf("\n") + 1) 51 } 52 def slurper = new JsonSlurper() 53 def json; 54 try { 55 json = slurper.parseText(outputString) 56 } catch (ignored) { 57 logger.info("mozconfig.gradle> Failed to parse JSON output from ./mach environment: \n\n" + 58 outputString); 59 throw new StopExecutionException( 60 "Failed to parse JSON output from ./mach environment.\n\n" + outputString); 61 } 62 63 if (json.substs.MOZ_BUILD_APP != 'mobile/android') { 64 throw new GradleException("Building with Gradle is only supported for Firefox for Android, i.e., MOZ_BUILD_APP == 'mobile/android'.") 65 } 66 67 // The Gradle instance is shared between settings.gradle and all the 68 // other build.gradle files (see 69 // http://forums.gradle.org/gradle/topics/define_extension_properties_from_settings_xml). 70 // We use this ext property to pass the per-object-directory mozconfig 71 // between scripts. This lets us execute set-up code before we gradle 72 // tries to configure the project even once, and as a side benefit 73 // saves invoking |mach environment| multiple times. 74 gradle.ext.mozconfig = json 75 76 // Produced by `mach build`. Bug 1543982: the mozconfig determined by `mach 77 // environment` above can be different because `mach build` itself sets certain 78 // critical environment variables including MOZ_OBJDIR, CC, and CXX. We use 79 // this record to patch up the environment when we recursively invoke `mach 80 // build ...` commands from within Gradle. This avoids invalidating configure 81 // based on the changed environment variables. 82 def orig = slurper.parse(new File(json.topobjdir, '.mozconfig.json')) 83 gradle.ext.mozconfig.orig_mozconfig = orig.mozconfig 84 85 // While we're here, read our VCS configuration when available. This is usually 86 // present in the `mozconfig`, but the source of truth is `source-repo.h`, which 87 // incorporates environment variables after configure-time. 88 gradle.ext.mozconfig.source_repo = [:] 89 def sourceRepoFile = file("${gradle.mozconfig.topobjdir}/source-repo.h") 90 if (sourceRepoFile.canRead()) { 91 def defines = ["MOZ_SOURCE_STAMP", "MOZ_SOURCE_REPO", "MOZ_SOURCE_URL"] 92 93 sourceRepoFile.eachLine { line -> 94 defines.forEach { define -> 95 def matcher = line =~ ~/^#define ${define}\s(.*)/ 96 if (matcher.find()) { 97 gradle.ext.mozconfig.source_repo[define] = matcher.group(1).trim() 98 } 99 } 100 } 101 102 if (gradle.ext.mozconfig.substs.MOZILLA_OFFICIAL) { 103 logger.info("mozconfig.gradle> Parsed source-repo.h: ${gradle.ext.mozconfig.source_repo}"); 104 } 105 }