-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathbuild.gradle
More file actions
403 lines (352 loc) · 14.6 KB
/
build.gradle
File metadata and controls
403 lines (352 loc) · 14.6 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import org.gradle.api.services.BuildServiceParameters
import org.tomlj.Toml
import org.tomlj.TomlParseResult
import org.tomlj.TomlTable
buildscript {
repositories {
gradle.mozconfig.substs.GRADLE_MAVEN_REPOSITORIES.each { repository ->
maven {
url = repository
if (gradle.mozconfig.substs.ALLOW_INSECURE_GRADLE_REPOSITORIES) {
allowInsecureProtocol = true
}
}
}
}
dependencies {
classpath libs.android.gradle.plugin
classpath libs.tomlj
// Used in mobile/android/fenix/app/build.gradle
classpath libs.androidx.benchmark.gradle
classpath libs.androidx.navigation.safeargs
classpath libs.osslicenses.plugin
classpath libs.mozilla.glean.gradle.plugin
}
}
plugins {
id 'ApkSizePlugin'
id "mozac.ConfigPlugin"
id 'org.mozilla.conventions.mach-tasks'
alias(libs.plugins.android.library) apply false
alias(libs.plugins.dependency.analysis)
alias(libs.plugins.detekt)
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.kotlin.compose) apply false
alias(libs.plugins.ksp)
alias(libs.plugins.spotless)
}
def tryInt = { string ->
if (string == null) {
return string
}
if (string.isInteger()) {
return string as Integer
}
return string
}
abstract class VerifyGleanVersionTask extends DefaultTask {
@InputFile
final RegularFileProperty source = project.objects.fileProperty().convention(project.layout.projectDirectory.file("Cargo.lock"))
@Input
String expectedVersion = project.ext.gleanVersion
@OutputFile
final RegularFileProperty outputFile = project.objects.fileProperty()
@TaskAction
void verifyGleanVersion() {
def foundVersion = getRustVersionFor(source.get().asFile, "glean")
if (expectedVersion != foundVersion) {
throw new GradleException("Mismatched Glean version, expected: '${expectedVersion}'," +
" found '${foundVersion}'")
} else {
logger.lifecycle("verifyGleanVersion> expected version matches found version '${foundVersion}'")
}
outputFile.get().asFile.text = "glean-${foundVersion}"
}
// Parses the Cargo.lock and returns the version for the given package name.
static String getRustVersionFor(file, packageName) {
String version = null;
TomlParseResult result = Toml.parse(file.getText());
for (object in result.getArray("package").toList()) {
def table = (TomlTable) object
if (table.getString("name") == packageName) {
if (version != null) {
throw new GradleException("Multiple versions for '${packageName}' found." +
" Ensure '${packageName}' is only included once.")
}
version = table.getString("version")
}
}
return version
}
}
tasks.register("verifyGleanVersion", VerifyGleanVersionTask) {
outputFile.convention(layout.buildDirectory.file("glean/verifyGleanVersion.marker"))
}
allprojects {
// Expose the per-object-directory configuration to all projects.
ext {
mozconfig = gradle.mozconfig
topsrcdir = gradle.mozconfig.topsrcdir
topobjdir = gradle.mozconfig.topobjdir
gleanVersion = libs.versions.glean.get() // Verification done in verifyGleanVersion task
artifactSuffix = getArtifactSuffix()
versionName = getVersionName()
versionCode = computeVersionCode()
versionNumber = getVersionNumber()
buildId = getBuildId()
buildToolsVersion = mozconfig.substs.ANDROID_BUILD_TOOLS_VERSION
compileSdkMajorVersion = tryInt(mozconfig.substs.ANDROID_COMPILE_SDK_MAJOR)
compileSdkMinorVersion = tryInt(mozconfig.substs.ANDROID_COMPILE_SDK_MINOR)
minSdkVersion = tryInt(mozconfig.substs.MOZ_ANDROID_MIN_SDK_VERSION)
targetSdkVersion = tryInt(mozconfig.substs.ANDROID_TARGET_SDK)
manifestPlaceholders = [
ANDROID_TARGET_SDK: mozconfig.substs.ANDROID_TARGET_SDK,
MOZ_ANDROID_MIN_SDK_VERSION: mozconfig.substs.MOZ_ANDROID_MIN_SDK_VERSION,
]
}
afterEvaluate {
if (it.hasProperty('android')) {
android {
buildToolsVersion gradle.mozconfig.substs.ANDROID_BUILD_TOOLS_VERSION
testOptions {
unitTests.includeAndroidResources = true
}
}
}
}
// Use the semanticdb-javac and semanticdb-kotlinc plugins to generate semanticdb files for Searchfox
if (mozconfig.substs.ENABLE_MOZSEARCH_PLUGIN || mozconfig.substs.DOWNLOAD_ALL_GRADLE_DEPENDENCIES) {
def targetRoot = new File(topobjdir, "mozsearch_java_index")
afterEvaluate {
def addDependencyToConfigurationIfExists = { configurationName, dependency ->
def configuration = configurations.findByName(configurationName)
if (configuration != null) {
dependencies.add(configurationName, dependency)
}
}
addDependencyToConfigurationIfExists("compileOnly", libs.semanticdb.java)
addDependencyToConfigurationIfExists("testCompileOnly", libs.semanticdb.java)
addDependencyToConfigurationIfExists("androidTestCompileOnly", libs.semanticdb.java)
addDependencyToConfigurationIfExists("kotlinCompilerPluginClasspath", libs.semanticdb.kotlin)
}
tasks.withType(JavaCompile) {
options.compilerArgs += [
"-Xplugin:semanticdb -sourceroot:${topsrcdir} -targetroot:${targetRoot}",
]
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
compilerOptions.freeCompilerArgs.addAll([
"-P", "plugin:semanticdb-kotlinc:sourceroot=${topsrcdir}".toString(),
"-P", "plugin:semanticdb-kotlinc:targetroot=${targetRoot}".toString(),
])
}
}
}
// Non-official versions are like "61.0a1" or "61.0b1", where "a1" and "b1"
// are the milestone.
// This simply strips that off, leaving "61.0" in this example.
def getAppVersionWithoutMilestone() {
return project.ext.mozconfig.substs.MOZ_APP_VERSION.replaceFirst(/[ab][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
}
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"]
// In GeckoView java projects only, turn all remaining warnings
// into errors unless marked by @SuppressWarnings.
def projectName = project.getName()
if (projectName.startsWith('geckoview')
|| projectName == 'annotations'
|| projectName == 'messaging_example'
|| projectName == 'port_messaging_example'
|| projectName == 'test_runner'
) {
options.compilerArgs += [
"-Werror"
]
}
}
project.configurations.configureEach {
// Dependencies can't depend on a different major version of Glean than A-C itself.
resolutionStrategy.eachDependency { details ->
if (details.requested.group == 'org.mozilla.telemetry'
&& details.requested.name.contains('glean') ) {
def requested = details.requested.version.tokenize(".")
def defined = project.ext.gleanVersion.tokenize(".")
// Check the major version
if (requested[0] != defined[0]) {
throw new AssertionError("Cannot resolve to a single Glean version. Requested: ${details.requested.version}, A-C uses: ${libs.mozilla.glean}")
} else {
// Enforce that all (transitive) dependencies are using the defined Glean version
details.useVersion project.ext.gleanVersion
}
}
}
resolutionStrategy.capabilitiesResolution.withCapability("org.mozilla.telemetry:glean-native") {
def toBeSelected = candidates.find { it.id instanceof ModuleComponentIdentifier && it.id.module.contains('geckoview') }
if (toBeSelected != null) {
select(toBeSelected)
}
because 'use GeckoView Glean instead of standalone Glean'
}
}
}
}
subprojects { project ->
// Perform spotless lint in GeckoView projects only.
// Sync with spotless_projects in mobile/android/geckoview/gradle.configure.
def projectName = project.getName()
if (projectName.startsWith('geckoview')
|| projectName == 'annotations'
|| projectName == 'messaging_example'
|| projectName == 'port_messaging_example'
|| projectName == 'test_runner'
) {
apply plugin: "com.diffplug.spotless"
spotless {
lineEndings = com.diffplug.spotless.LineEnding.UNIX
java {
target project.fileTree(project.projectDir) {
include '**/*.java'
exclude '**/thirdparty/**'
}
googleJavaFormat(libs.versions.google.java.format.get())
}
kotlin {
target project.fileTree(project.projectDir) {
include '**/*.kt'
exclude '**/thirdparty/**'
}
ktlint("${libs.versions.ktlint.get()}").setEditorConfigPath("${topsrcdir}/mobile/android/geckoview/.editorconfig")
}
}
// Work around https://github.com/diffplug/spotless/issues/1958 by
// explicitly depending on Spotless plugin dependencies.
project.configurations {
googleJavaFormat
}
project.dependencies {
googleJavaFormat libs.google.java.format
}
}
afterEvaluate {
if (it.hasProperty('android')) {
kotlin {
jvmToolchain(config.jvmTargetCompatibility)
}
}
if (it.hasProperty('android')
&& (project.projectDir.absolutePath.contains("android-components")
|| projectName == "fenix"
|| projectName == "focus-android")
) {
dependencies {
lintChecks project.project(':components:tooling-lint')
}
android {
// Copied from subbproject's build.gradle
lint {
baseline = file("${project.projectDir}/lint-baseline.xml")
}
}
}
}
project.configurations.configureEach {
resolutionStrategy.capabilitiesResolution.withCapability("org.mozilla.telemetry:glean-native") {
def toBeSelected = candidates.find {
it.id instanceof ProjectComponentIdentifier && it.id.projectName.contains('geckoview')
}
if (toBeSelected != null) {
select(toBeSelected)
}
because 'use GeckoView Glean instead of standalone Glean'
}
}
}
// These are convenience helpers to run lints over all the subprojects
def mobileProjects = [":fenix", ":focus-android", ":android-components"]
tasks.register("lint") {
group = "verification"
description = "Runs lint on all mobile projects."
mobileProjects.each { dependsOn "${it}:lint" }
}
tasks.register("ktlint") {
group = "verification"
description = "Runs ktlint on all mobile projects."
mobileProjects.each { dependsOn "${it}:ktlint" }
}
tasks.register("ktlintFormat") {
group = "formatting"
description = "Runs ktlintFormat on all mobile projects."
mobileProjects.each { dependsOn "${it}:ktlintFormat" }
}
tasks.named("detekt").configure {
// NOTE: We are replacing the default 'detekt' task.
description = "Runs detekt on all mobile projects."
mobileProjects.each { dependsOn "${it}:detekt" }
}
tasks.register("lint-a-c") {
group = "verification"
description = "(Deprecated) alias for :android-components:lint"
dependsOn ":android-components:lint"
}