summaryrefslogtreecommitdiffstats
path: root/build.gradle
blob: d537ade743e9090b86a5a05cbd09783852b8e3d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import org.tomlj.Toml
import org.tomlj.TomlParseResult
import org.tomlj.TomlTable

def tryInt = { string ->
    if (string == null) {
        return string
    }
    if (string.isInteger()) {
        return string as Integer
    }
    return string
}

// Parses the Cargo.lock and returns the version for the given package name.
def getRustVersionFor(packageName) {
    String version = null;
    TomlParseResult result = Toml.parse(file("Cargo.lock").getText());
    for (object in result.getArray("package").toList()) {
        def table = (TomlTable) object
        if (table.getString("name") == packageName) {
            if (version != null) {
                throw new StopExecutionException("Multiple versions for '${packageName}' found." +
                        " Ensure '${packageName}' is only included once.")
            }
            version = table.getString("version")
        }
    }
    return version
}

allprojects {
    // Expose the per-object-directory configuration to all projects.
    ext {
        mozconfig = gradle.mozconfig
        topsrcdir = gradle.mozconfig.topsrcdir
        topobjdir = gradle.mozconfig.topobjdir

        gleanVersion = "52.7.0"
        if (gleanVersion != getRustVersionFor("glean")) {
          throw new StopExecutionException("Mismatched Glean version, expected: ${gleanVersion}," +
              " found ${getRustVersionFor("glean")}")
        }

        artifactSuffix = getArtifactSuffix()
        versionName = getVersionName()
        versionCode = computeVersionCode()
        versionNumber = getVersionNumber()
        buildId = getBuildId()

        buildToolsVersion = mozconfig.substs.ANDROID_BUILD_TOOLS_VERSION
        compileSdkVersion = tryInt(mozconfig.substs.ANDROID_TARGET_SDK)
        targetSdkVersion = tryInt(mozconfig.substs.ANDROID_TARGET_SDK)
        minSdkVersion = tryInt(mozconfig.substs.MOZ_ANDROID_MIN_SDK_VERSION)
        manifestPlaceholders = [
            ANDROID_PACKAGE_NAME: mozconfig.substs.ANDROID_PACKAGE_NAME,
            ANDROID_TARGET_SDK: mozconfig.substs.ANDROID_TARGET_SDK,
            MOZ_ANDROID_MIN_SDK_VERSION: mozconfig.substs.MOZ_ANDROID_MIN_SDK_VERSION,
        ]
    }

    repositories {
        gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORIES.each { repository ->
            maven {
                url repository
                if (gradle.mozconfig.substs.ALLOW_INSECURE_GRADLE_REPOSITORIES) {
                    allowInsecureProtocol = true
                }
            }
        }
    }

    task downloadDependencies() {
        description 'Download all dependencies to the Gradle cache'
        doLast {
            configurations.each { configuration ->
                if (configuration.canBeResolved) {
                    configuration.allDependencies.each { dependency ->
                        try {
                            configuration.files(dependency)
                        } catch(e) {
                            println("Could not resolve ${configuration.name} -> ${dependency.name}")
                            println(" > ${e.message}")
                            if (e.cause) {
                                println(" >> ${e.cause}")
                                if (e.cause.cause) {
                                    println(" >> ${e.cause.cause}")
                                }
                            }
                            println("")
                        }
                    }
                }
            }
        }
    }
}

buildDir "${topobjdir}/gradle/build"

buildscript {
    repositories {
        gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORIES.each { repository ->
            maven {
                url repository
                if (gradle.mozconfig.substs.ALLOW_INSECURE_GRADLE_REPOSITORIES) {
                    allowInsecureProtocol = true
                }
            }
        }
    }

    ext.kotlin_version = '1.8.21'

    dependencies {
        classpath 'org.mozilla.apilint:apilint:0.5.2'
        classpath 'com.android.tools.build:gradle:7.4.2'
        classpath 'org.apache.commons:commons-exec:1.3'
        classpath 'com.diffplug.spotless:spotless-plugin-gradle:6.18.0'
        classpath 'org.tomlj:tomlj:1.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

// A stream that processes bytes line by line, prepending a tag before sending
// each line to Gradle's logging.
class TaggedLogOutputStream extends org.apache.commons.exec.LogOutputStream {
    String tag
    Logger logger

    TaggedLogOutputStream(tag, logger) {
        this.tag = tag
        this.logger = logger
    }

    void processLine(String line, int level) {
        logger.lifecycle("${this.tag} ${line}")
    }
}

ext.geckoBinariesOnlyIf = { task ->
    // Never when Gradle was invoked within `mach build`.
    if ('1' == System.env.GRADLE_INVOKED_WITHIN_MACH_BUILD) {
        rootProject.logger.lifecycle("Skipping task ${task.path} because: within `mach build`")
        return false
    }

    // Never for official builds.
    if (mozconfig.substs.MOZILLA_OFFICIAL) {
        rootProject.logger.lifecycle("Skipping task ${task.path} because: MOZILLA_OFFICIAL")
        return false
    }

    // Multi-l10n builds set `AB_CD=multi`, which isn't a valid locale, and
    // `MOZ_CHROME_MULTILOCALE`.  To avoid failures, if Gradle is invoked with
    // either, we don't invoke Make at all; this allows a multi-locale omnijar
    // to be consumed without modification.
    if ('multi' == System.env.AB_CD || System.env.MOZ_CHROME_MULTILOCALE) {
        rootProject.logger.lifecycle("Skipping task ${task.path} because: AB_CD=multi")
        return false
    }

    // Single-locale l10n repacks set `IS_LANGUAGE_REPACK=1` and handle resource
    // and code generation themselves.
    if ('1' == System.env.IS_LANGUAGE_REPACK) {
        rootProject.logger.lifecycle("Skipping task ${task.path} because: IS_LANGUAGE_REPACK")
        return false
    }

    rootProject.logger.lifecycle("Executing task ${task.path}")
    return true
}

// Non-official versions are like "61.0a1", where "a1" is the milestone.
// This simply strips that off, leaving "61.0" in this example.
def getAppVersionWithoutMilestone() {
    return project.ext.mozconfig.substs.MOZ_APP_VERSION.replaceFirst(/a[0-9]/, "")
}

// This converts MOZ_APP_VERSION into an integer
// version code.
//
// We take something like 58.1.2a1 and come out with 5800102
// This gives us 3 digits for the major number, and 2 digits
// each for the minor and build number. Beta and Release
//
// This must be synchronized with _compute_gecko_version(...) in /taskcluster/gecko_taskgraph/transforms/task.py
def computeVersionCode() {
    String appVersion = getAppVersionWithoutMilestone()

    // Split on the dot delimiter, e.g. 58.1.1a1 -> ["58, "1", "1a1"]
    String[] parts = appVersion.split('\\.')

    assert parts.size() == 2 || parts.size() == 3

    // Major
    int code = Integer.parseInt(parts[0]) * 100000

    // Minor
    code += Integer.parseInt(parts[1]) * 100

    // Build
    if (parts.size() == 3) {
        code += Integer.parseInt(parts[2])
    }

    return code;
}

def getVersionName() {
    return "${mozconfig.substs.MOZ_APP_VERSION}-${mozconfig.substs.MOZ_UPDATE_CHANNEL}"
}

// Mimic Python: open(os.path.join(buildconfig.topobjdir, 'buildid.h')).readline().split()[2]
def getBuildId() {
    return file("${topobjdir}/buildid.h").getText('utf-8').split()[2]
}

def getVersionNumber() {
    def appVersion = getAppVersionWithoutMilestone()
    def parts = appVersion.split('\\.')
    def version = parts[0] + "." + parts[1] + "." + getBuildId()
    def substs = project.ext.mozconfig.substs
    if (!substs.MOZILLA_OFFICIAL && !substs.MOZ_ANDROID_FAT_AAR_ARCHITECTURES) {
        // Use -SNAPSHOT versions locally to enable the local GeckoView substitution flow.
        version += "-SNAPSHOT"
    }
    return version
}

def getArtifactSuffix() {
    def substs = project.ext.mozconfig.substs

    def suffix = ""
    // Release artifacts don't specify the channel, for the sake of simplicity.
    if (substs.MOZ_UPDATE_CHANNEL != 'release') {
        suffix += "-${mozconfig.substs.MOZ_UPDATE_CHANNEL}"
    }

    return suffix
}

class MachExec extends Exec {
    def MachExec() {
        // Bug 1543982: When invoking `mach build` recursively, the outer `mach
        // build` itself modifies the environment, causing configure to run
        // again.  This tries to restore the environment that the outer `mach
        // build` was invoked in.  See the comment in
        // $topsrcdir/settings.gradle.
        project.ext.mozconfig.mozconfig.env.unmodified.each { k, v -> environment.remove(k) }
        environment project.ext.mozconfig.orig_mozconfig.env.unmodified
        environment 'MOZCONFIG', project.ext.mozconfig.substs.MOZCONFIG
    }
}

task machBuildFaster(type: MachExec) {
    onlyIf rootProject.ext.geckoBinariesOnlyIf

    workingDir "${topsrcdir}"

    commandLine mozconfig.substs.PYTHON3
    args "${topsrcdir}/mach"
    args 'build'
    args 'faster'

    // Add `-v` if we're running under `--info` (or `--debug`).
    if (project.logger.isEnabled(LogLevel.INFO)) {
        args '-v'
    }

    // `path` is like `:machBuildFaster`.
    standardOutput = new TaggedLogOutputStream("${path}>", logger)
    errorOutput = standardOutput
}

task machStagePackage(type: MachExec) {
    onlyIf rootProject.ext.geckoBinariesOnlyIf

    dependsOn rootProject.machBuildFaster

    workingDir "${topobjdir}"

    // We'd prefer this to be a `mach` invocation, but `mach build
    // mobile/android/installer/stage-package` doesn't work as expected.
    commandLine mozconfig.substs.GMAKE
    args '-C'
    args "${topobjdir}/mobile/android/installer"
    args 'stage-package'

    outputs.file "${topobjdir}/dist/geckoview/assets/omni.ja"

    outputs.file "${topobjdir}/dist/geckoview/assets/${mozconfig.substs.ANDROID_CPU_ARCH}/libxul.so"
    outputs.file "${topobjdir}/dist/geckoview/lib/${mozconfig.substs.ANDROID_CPU_ARCH}/libmozglue.so"

    // Force running `stage-package`.
    outputs.upToDateWhen { false }

    // `path` is like `:machStagePackage`.
    standardOutput = new TaggedLogOutputStream("${path}>", logger)
    errorOutput = standardOutput
}

afterEvaluate {
    subprojects { project ->
        tasks.withType(JavaCompile) {
            // Add compiler args for all code except third-party code.
            options.compilerArgs += [
                // Turn on all warnings, except...
                "-Xlint:all",
                // Deprecation, because we do use deprecated API for compatibility.
                "-Xlint:-deprecation",
                // Serial, because we don't use Java serialization.
                "-Xlint:-serial",
                // Classfile, because javac has a bug with MethodParameters attributes
                // with Java 7. https://bugs.openjdk.java.net/browse/JDK-8190452
                "-Xlint:-classfile",
                // Turn all remaining warnings into errors,
                // unless marked by @SuppressWarnings.
                "-Werror"]
        }
    }
}

apply plugin: 'idea'

idea {
    project {
        languageLevel = '1.8'
    }

    module {
        // Object directories take a huge amount of time for IntelliJ to index.
        // Exclude them.  Convention is that object directories start with obj.
        // IntelliJ is clever and will not exclude the parts of the object
        // directory that are referenced, if there are any.  In practice,
        // indexing the entirety of the tree is taking too long, so exclude all
        // but mobile/.
        def topsrcdirURI = file(topsrcdir).toURI()
        excludeDirs += files(file(topsrcdir)
            .listFiles({it.isDirectory()} as FileFilter)
            .collect({topsrcdirURI.relativize(it.toURI()).toString()}) // Relative paths.
            .findAll({!it.equals('mobile/')}))

        // If topobjdir is below topsrcdir, hide only some portions of that tree.
        def topobjdirURI = file(topobjdir).toURI()
        if (!topsrcdirURI.relativize(topobjdirURI).isAbsolute()) {
            excludeDirs -= file(topobjdir)
            excludeDirs += files(file(topobjdir).listFiles())
            excludeDirs -= file("${topobjdir}/gradle")
        }
    }
}

subprojects {
    apply plugin: "com.diffplug.spotless"

    spotless {
        java {
            target project.fileTree(project.projectDir) {
                include '**/*.java'
                exclude '**/thirdparty/**'
            }
            googleJavaFormat('1.17.0')
        }
        kotlin {
            target project.fileTree(project.projectDir) {
                include '**/*.kt'
                exclude '**/thirdparty/**'
            }
            ktlint('0.48.2')
        }
    }
}