Bug 1725074 - Allow attribs with the same names as uniforms. r=gfx-reviewers,lsalzman
[gecko.git] / substitute-local-geckoview.gradle
blob3231d8036a14af95a3b55d2a27378ba84462026f
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // Substitute a local GeckoView AAR into a consuming Gradle project.
6 //
7 // To use this, in a consuming Gradle project's `/build.gradle` add a stanza like:
8 //
9 // ext.topsrcdir = '/absolute/path/to/mozilla-central'; apply from: "${ext.topsrcdir}/substitute-local-geckoview.gradle"
11 // The object directory will be determined using `mach environment` and will agree with `./mach
12 // gradle` and Android Studio.  Or, specify the exact object directory with a stanza like:
14 // ext.topsrcdir = '/absolute/path/to/mozilla-central'
15 // ext.topobjdir = '/absolute/path/to/objdir'
16 // apply from: "${ext.topsrcdir}/substitute-local-geckoview.gradle"
18 // Substitution works with artifact and non-artifact builds.
20 // If you get errors about .jar files not being found, ensure that the consuming
21 // application is using a recent Android-Gradle plugin (say, 3.4+).  There were
22 // issues with Jetifier, and issues with .jar vs. .aar extensions, in older
23 // versions.
25 import groovy.json.JsonSlurper
27 def log(message) {
28     logger.lifecycle("[substitute-local-geckoview] ${message}")
31 def warn(message) {
32     logger.warn("[substitute-local-geckoview] Warning: ${message}")
35 if (!project.ext.has('topsrcdir')) {
36     throw new GradleException("ext.topsrcdir must be specified to substitute for a local GeckoView")
39 /**
40  * Loads the mozconfig and returns any variables derived from it, avoiding side effects.
41  *
42  * This method is relatively slow because it calls mach, which starts a python interpreter, will
43  * becomes very slow if called for numerous subprojects. Therefore, it should only be called once
44  * per build.
45  */
46 def loadMozconfig() {
47     apply from: "${topsrcdir}/mobile/android/gradle/mach_env.gradle"
49     // Cribbed from https://hg.mozilla.org/mozilla-central/file/tip/settings.gradle.  When run in
50     // topobjdir, `mach environment` correctly finds the mozconfig corresponding to that object
51     // directory.
52     def commandLine = ["${topsrcdir}/mach", "environment", "--format", "json", "--verbose"]
53     def proc = commandLine.execute(
54             machEnv(topsrcdir),
55             new File(ext.has('topobjdir') ? ext.get('topobjdir') : topsrcdir))
56     def standardOutput = new ByteArrayOutputStream()
57     proc.consumeProcessOutput(standardOutput, standardOutput)
58     proc.waitFor()
60     // Only show the output if something went wrong.
61     if (proc.exitValue() != 0) {
62         throw new GradleException("Process '${commandLine}' finished with non-zero exit value ${proc.exitValue()}:\n\n${standardOutput.toString()}")
63     }
65     def slurper = new JsonSlurper()
66     def mozconfig = slurper.parseText(standardOutput.toString())
68     if (topsrcdir != mozconfig.topsrcdir) {
69         throw new GradleException("Specified topsrcdir ('${topsrcdir}') is not mozconfig topsrcdir ('${mozconfig.topsrcdir}')")
70     }
72     def topobjdir
73     if (ext.has('topobjdir')) {
74         topobjdir = ext.topobjdir
75     } else {
76         topobjdir = mozconfig.topobjdir
77         log("Found topobjdir ${topobjdir} from topsrcdir ${topsrcdir}")
78     }
80     if (mozconfig.substs.MOZ_BUILD_APP != 'mobile/android') {
81         throw new GradleException("Building with Gradle is only supported for GeckoView, i.e., MOZ_BUILD_APP == 'mobile/android'.")
82     }
84     log("Will substitute GeckoView (geckoview-{nightly,beta}) with local GeckoView (geckoview-default) from ${topobjdir}/gradle/build/mobile/android/geckoview/maven")
86     if (!mozconfig.substs.COMPILE_ENVIRONMENT) {
87         log("To update the local GeckoView, run `./mach gradle geckoview:publishWithGeckoBinariesDebugPublicationToMavenRepository` in ${topsrcdir}")
88     } else {
89         log("To update the local GeckoView, run `./mach build binaries && ./mach gradle geckoview:publishWithGeckoBinariesDebugPublicationToMavenRepository` in ${topsrcdir}")
90     }
92     return [mozconfig, topobjdir]
95 // This script is expected to be called for every subproject in the build (in ac, this is over 100)
96 // but loadMozconfig should only be called once per build (see the javadoc) so we store the output
97 // of that call as a global variable and re-use it when this script is called again.
98 def LOAD_MOZCONFIG_CACHE = "substitute-local-geckoview-mozconfig-cache"
99 if (!rootProject.ext.has(LOAD_MOZCONFIG_CACHE)) {
100     rootProject.ext.set(LOAD_MOZCONFIG_CACHE, loadMozconfig())
102 def (mozconfig, topobjdir) = rootProject.ext.get(LOAD_MOZCONFIG_CACHE)
104 repositories {
105     maven {
106         name "Local GeckoView Maven repository"
107         url "${topobjdir}/gradle/build/mobile/android/geckoview/maven"
108     }
111 configurations.all { config ->
112     // Like `geckoview-nightly` for a multi-architecture fat AAR or
113     // `geckoview-nightly-armeabi-v7a` for an architecture-specific AAR.
114     def geckoviewModules = [
115         'geckoview-nightly',
116         'geckoview-nightly-armeabi-v7a',
117         'geckoview-nightly-arm64-v8a',
118         'geckoview-nightly-x86',
119         'geckoview-nightly-x86_64',
120         'geckoview-beta',
121         'geckoview-beta-armeabi-v7a',
122         'geckoview-beta-arm64-v8a',
123         'geckoview-beta-x86',
124         'geckoview-beta-x86_64',
125     ]
127     def geckoviewOmniModules = [
128         'geckoview-nightly-omni',
129         'geckoview-nightly-omni-armeabi-v7a',
130         'geckoview-nightly-omni-arm64-v8a',
131         'geckoview-nightly-omni-x86',
132         'geckoview-nightly-omni-x86_64',
133         'geckoview-beta-omni',
134         'geckoview-beta-omni-armeabi-v7a',
135         'geckoview-beta-omni-arm64-v8a',
136         'geckoview-beta-omni-x86',
137         'geckoview-beta-omni-x86_64',
138     ]
140     if (config.isCanBeResolved()) {
141         config.resolutionStrategy { strategy ->
142             dependencySubstitution {
143                 all { dependency ->
144                     // We could restrict based on target architecture, but there doesn't seem to
145                     // be much advantage to doing so right now.
147                     if (!(dependency.requested instanceof ModuleComponentSelector)) {
148                         // We can only substitute for a module: we're never going to substitute
149                         // for a project.
150                         return
151                     }
153                     def group = dependency.requested.group
154                     def module = dependency.requested.module
155                     if (group == 'org.mozilla.geckoview'
156                           && (geckoviewModules.contains(module) || geckoviewOmniModules.contains(module))) {
157                         def name = ''
158                         def isLite = mozconfig.substs.MOZ_ANDROID_GECKOVIEW_LITE
160                         if (isLite) {
161                           name = 'geckoview-default'
162                         } else {
163                           name = 'geckoview-default-omni'
164                         }
166                         if (geckoviewModules.contains(module) && !isLite) {
167                           warn("Substituting a geckoview omni build into a lite dependency. Add ac_add_options --enable-geckoview-lite to ${mozconfig.mozconfig.path} to fix this.")
168                         } else if (geckoviewOmniModules.contains(module) && isLite) {
169                           // Substituting lite into omni is unlikely to work at
170                           // all so we just error out here.
171                           throw new GradleException("Substituting a geckoview lite build into an omni dependency. Remove ac_add_options --enable-geckoview-lite in ${mozconfig.mozconfig.path} to fix this.")
172                         }
174                         log("Substituting ${group}:${dependency.requested.module} with local GeckoView ${group}:${name} in ${config}")
176                         dependency.useTarget([group: group, name: name, version: '+'])
178                         // We substitute with a dynamic version ('+').  It seems that Gradle
179                         // discovers the underlying AAR is out of date correctly based on file
180                         // timestamp already, but let's try to avoid some class of cache
181                         // invalidation error while we're here.
182                         strategy.cacheDynamicVersionsFor 0, 'seconds'
183                         strategy.cacheChangingModulesFor 0, 'seconds'
184                     }
185                 }
186             }
187         }
188     }