-
Notifications
You must be signed in to change notification settings - Fork 226
/
build.gradle
322 lines (281 loc) · 12.1 KB
/
build.gradle
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
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.build = [
ndkVersion: "27.2.12479018", // Keep it in sync in TC Dockerfile.
// In general, we should aim to keep these in sync with AC
compileSdkVersion: 34,
targetSdkVersion: 34,
minSdkVersion: 21,
jvmTargetCompatibility: 17,
]
repositories {
mavenCentral()
google()
maven {
url "https://plugins.gradle.org/m2/"
}
maven {
name "Mozilla"
url "https://maven.mozilla.org/maven2"
content {
// Improve performance: only check moz maven for mozilla deps.
includeGroupByRegex "org\\.mozilla\\..*"
}
}
}
dependencies {
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath libs.kotlin.gradle.plugin
classpath libs.mozilla.glean.gradle.plugin
classpath libs.mozilla.rust.android.gradle
classpath libs.tools.android.plugin
// Yes, this is unusual. We want to access some host-specific
// computation at build time.
classpath libs.jna
}
}
plugins {
alias libs.plugins.detekt
alias libs.plugins.gradle.download.task // Downloading libs/archives from Taskcluster.
alias libs.plugins.protobuf.gradle apply false
}
allprojects {
repositories {
google()
mavenCentral()
maven {
name "Mozilla"
url "https://maven.mozilla.org/maven2"
content {
// Improve performance: only check moz maven for mozilla deps.
includeGroupByRegex "org\\.mozilla\\..*"
}
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
// Avoid Gradle namespace collision. This is here, rather than in `buildscript
// { ... }`, to avoid issues with importing.
import com.sun.jna.Platform as DefaultPlatform
// If this is `null`, we use libs from the source directory.
// Check if there are any changes to `libs` since `main`, and if not,
// use the sha to download the artifacts from taskcluster.
//
// Note we pass the path to the git-dir so that this still works when
// used as a dependency substitution from e.g. android-components.
ext.libsGitSha = "git --git-dir=${rootProject.rootDir}/.git diff --name-only main -- :/libs".execute().text.allWhitespace ?
"git --git-dir=${rootProject.rootDir}/.git rev-parse HEAD:libs".execute().text.trim() : null
// Use in-tree libs from the source directory in CI or if the git SHA is unset; otherwise use
// downloaded libs.
// XX: Since we switched the libs directory to be `darwin-x86-64` and `darwin-aarch64` in https://github.com/mozilla/application-services/pull/4792
// we have mismatch with the libs built on CI, since they have the libraries under `darwin`.
// Temporarily, we are disabling downloaded libs
// we should enable them once our CI builds libs the same way as our local environments
// see: https://github.com/mozilla/application-services/issues/4828
// def useDownloadedLibs = !System.getenv('CI') && ext.libsGitSha != null
def useDownloadedLibs = false
if (useDownloadedLibs) {
task downloadAndroidLibs(type: Download) {
src "https://firefox-ci-tc.services.mozilla.com/api/index/v1/task/project.application-services.application-services.build.libs.android.${rootProject.ext.libsGitSha}/artifacts/public/build/android.tar.gz"
dest new File(buildDir, "libs.android.${rootProject.ext.libsGitSha}.tar.gz")
doFirst {
if (it.dest.exists()) {
throw new StopExecutionException("File to download already exists: ${it.dest.path}")
}
}
overwrite true
}
task untarAndroidLibs(dependsOn: downloadAndroidLibs, type: Copy) {
from tarTree(downloadAndroidLibs.dest)
into rootProject.buildDir
}
task downloadDesktopLibs(type: Download) {
src {
switch (DefaultPlatform.RESOURCE_PREFIX) {
case 'darwin-x86-64':
case 'darwin-aarch64':
return "https://firefox-ci-tc.services.mozilla.com/api/index/v1/task/project.application-services.application-services.build.libs.desktop.macos.${rootProject.ext.libsGitSha}/artifacts/public/build/macos.tar.gz"
case 'linux-x86-64':
return "https://firefox-ci-tc.services.mozilla.com/api/index/v1/task/project.application-services.application-services.build.libs.desktop.linux.${rootProject.ext.libsGitSha}/artifacts/public/build/linux.tar.gz"
default:
throw new GradleException("Unknown host platform '${DefaultPlatform.RESOURCE_PREFIX}'. " +
"Set `ext.libsGitSha = null` in ${rootProject.rootDir}/build.gradle and build your own libs. " +
"If you don't want to build your own libs for Android, you can untar\n\n${downloadAndroidLibs.src}\n\nat top-level to populate `libs/android/`. " +
"You'll need build your own libs for your host platform in order to be able to build and run unit tests.")
}
}
dest {
switch (DefaultPlatform.RESOURCE_PREFIX) {
case 'darwin-x86-64':
case 'darwin-aarch64':
return new File(buildDir, "libs.desktop.macos.${rootProject.ext.libsGitSha}.tar.gz")
case 'linux-x86-64':
return new File(buildDir, "libs.desktop.linux.${rootProject.ext.libsGitSha}.tar.gz")
default:
throw new GradleException("Unknown host platform '${DefaultPlatform.RESOURCE_PREFIX}'. " +
"Set `ext.libsGitSha = null` in ${rootProject.rootDir}/build.gradle and build your own libs.")
}
}
doFirst {
if (it.dest.exists()) {
throw new StopExecutionException("File to download already exists: ${it.dest.path}")
}
}
overwrite true
}
task untarDesktopLibs(dependsOn: downloadDesktopLibs, type: Copy) {
from tarTree(downloadDesktopLibs.dest)
into rootProject.buildDir
}
subprojects { project ->
afterEvaluate {
android.libraryVariants.all { v ->
v.preBuildProvider.configure {
dependsOn(rootProject.untarAndroidLibs)
dependsOn(rootProject.untarDesktopLibs)
}
}
}
}
}
// Additionally, we require `--locked` in CI, but not for local builds.
// Unlike the above, this can't be overridden by `local.properties` (mainly
// because doing so seems pointless, not for any security reason)
ext.extraCargoBuildArguments = []
if (System.getenv("CI")) {
ext.extraCargoBuildArguments = ["--locked"]
}
// The Cargo targets to invoke. The mapping from short name to target
// triple is defined by the `rust-android-gradle` plugin.
// They can be overwritten in `local.properties` by the `rust.targets`
// attribute.
ext.rustTargets = [
'arm',
'arm64',
'x86_64',
'x86',
]
// Generate libs for our current platform so we can run unit tests.
switch (DefaultPlatform.RESOURCE_PREFIX) {
case 'darwin-x86-64':
ext.nativeRustTarget = 'darwin-x86-64'
break
case 'darwin-aarch64':
ext.nativeRustTarget = 'darwin-aarch64'
break
case 'linux-x86-64':
ext.nativeRustTarget = 'linux-x86-64'
break
}
ext.rustTargets += ext.nativeRustTarget
ext.libsRootDir = useDownloadedLibs ? rootProject.buildDir : rootProject.rootDir
subprojects {
group = "org.mozilla.appservices"
apply plugin: 'maven-publish'
// Kotlin settings applicable to all modules.
afterEvaluate{
if (it.hasProperty('android')) {
android {
// This shouldn't be needed anymore with AGP 8.1.0+
// https://kotlinlang.org/docs/gradle-configure-project.html#gradle-java-toolchains-support
compileOptions {
sourceCompatibility rootProject.ext.build.jvmTargetCompatibility
targetCompatibility rootProject.ext.build.jvmTargetCompatibility
}
buildFeatures {
buildConfig true
}
publishing {
singleVariant('release') {
withSourcesJar()
}
}
}
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions.allWarningsAsErrors = true
}
}
// This allows to invoke Gradle like `./gradlew publishToRootProjectBuildDir` (equivalent to
// `./gradlew publish`) and also `./gradlew publishToProjectBuildDir`.
publishing {
repositories {
maven {
name = "rootProjectBuildDir"
url = "file://${project.rootProject.buildDir}/maven"
}
maven {
name = "projectBuildDir"
url = "file://${project.buildDir}/maven"
}
}
}
}
// Configure some environment variables, per toolchain, that will apply during
// the Cargo build. We assume that the `libs/` directory has been populated
// before invoking Gradle (or Cargo).
ext.cargoExec = { spec, toolchain ->
spec.environment("NSS_STATIC", "1")
spec.environment("NSS_DIR", new File(rootProject.ext.libsRootDir, "libs/${toolchain.folder}/nss").absolutePath)
}
detekt {
input = files(
fileTree(dir: "${projectDir}/components", excludes: ["external", "**/generated", "**/templates"]),
"${projectDir}/gradle-plugin",
"buildSrc"
)
buildUponDefaultConfig = true
config = files("${projectDir}/.detekt.yml")
reports {
xml.enabled = false
}
}
tasks.withType(io.gitlab.arturbosch.detekt.Detekt) {
exclude(".*test.*,.*/resources/.*,.*/tmp/.*,.*/build/.*")
}
configurations {
ktlint
}
dependencies {
ktlint(libs.ktlint) {
attributes {
attribute(Bundling.BUNDLING_ATTRIBUTE, getObjects().named(Bundling, Bundling.EXTERNAL))
}
}
}
tasks.register("ktlint", JavaExec) {
group = "verification"
description = "Check Kotlin code style."
classpath = configurations.ktlint
main = "com.pinterest.ktlint.Main"
args "${projectDir}/components/**/*.kt", "${projectDir}/gradle-plugin/**/*.kt", "buildSrc/**/*.kt", "!**/build", "!**/templates/**", "!components/external", "!**/generated/**"
}
tasks.register("ktlintFormat", JavaExec) {
group = "formatting"
description = "Fix Kotlin code style deviations."
classpath = configurations.ktlint
main = "com.pinterest.ktlint.Main"
args "-F", "${projectDir}/components/**/*.kt", "${projectDir}/gradle-plugin/**/*.kt", "buildSrc/**/*.kt", "!**/build", "!**/templates/**", "!components/external", "!**/generated/**"
// Fix for newer Java from https://github.com/pinterest/ktlint/issues/1195#issuecomment-1009027802
jvmArgs("--add-opens", "java.base/java.lang=ALL-UNNAMED")
}
// Extremely unsophisticated way to publish a local development version while hiding implementation details.
//
// This shells out to a python script that tries to detect whether the working directory has changed since the last
// time it was run, and it so then it shells out to `./gradlew publishToMavenLocal -Plocal=<timestamp>` to publish
// a new version of of the code with an auto-incrementing version number.
//
// It would be nice to implement this natively in gradle using gradle's own change-detection facilities, but I don't know
// enough about gradle to take that on. At least this approach gives a nice stable `./gradlew autoPublishForLocalDevelopment`
// interface for consumers.
task autoPublishForLocalDevelopment(type: Exec) {
commandLine "./automation/publish_to_maven_local_if_modified.py"
}
task printNdkVersion {
doLast {
println project.ext.build.ndkVersion
}
}