Yigit Boyar | 19b4110 | 2016-11-20 10:46:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Aurimas Liutikas | 7f40a7e | 2017-10-27 17:55:06 -0700 | [diff] [blame] | 16 | |
Owen Gray | 74cc259 | 2020-09-24 15:05:40 -0400 | [diff] [blame] | 17 | import androidx.build.BuildOnServerKt |
Owen Gray | 74cc259 | 2020-09-24 15:05:40 -0400 | [diff] [blame] | 18 | import androidx.build.LibraryType |
| 19 | import androidx.build.SupportConfig |
| 20 | import androidx.build.SdkHelperKt |
Aurimas Liutikas | 04a624d | 2021-08-10 14:12:14 -0700 | [diff] [blame] | 21 | import java.util.zip.ZipEntry |
| 22 | import java.util.zip.ZipFile |
Yigit Boyar | 9b3d20b | 2020-07-05 10:48:05 -0700 | [diff] [blame] | 23 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile |
| 24 | |
Aurimas Liutikas | 526389b | 2018-02-27 14:01:24 -0800 | [diff] [blame] | 25 | plugins { |
Aurimas Liutikas | 65d3d60 | 2019-04-01 23:08:13 -0700 | [diff] [blame] | 26 | id("AndroidXPlugin") |
| 27 | id("kotlin") |
Yigit Boyar | 562a37d | 2020-03-16 13:00:36 -0700 | [diff] [blame] | 28 | id("com.github.johnrengelman.shadow") |
Aurimas Liutikas | 526389b | 2018-02-27 14:01:24 -0800 | [diff] [blame] | 29 | } |
Yigit Boyar | 19b4110 | 2016-11-20 10:46:32 -0800 | [diff] [blame] | 30 | |
| 31 | def antlrOut = "$buildDir/generated/antlr/grammar-gen/" |
| 32 | sourceSets { |
Jim Sproch | 9e38b4f | 2021-01-06 14:21:06 -0800 | [diff] [blame] | 33 | main.java.srcDirs += "src/main/grammar-gen" |
Yigit Boyar | 19b4110 | 2016-11-20 10:46:32 -0800 | [diff] [blame] | 34 | main.java.srcDirs += antlrOut |
| 35 | } |
Sergey Vasilinets | 0af2c3f | 2017-07-20 12:48:16 -0700 | [diff] [blame] | 36 | |
Yigit Boyar | 562a37d | 2020-03-16 13:00:36 -0700 | [diff] [blame] | 37 | configurations { |
| 38 | /** |
| 39 | * shadowed is used for dependencies which we jarjar into the library jar instead of adding it |
| 40 | * as a pom dependency |
| 41 | */ |
| 42 | shadowed |
| 43 | // make sure shadowed dependencies show up as compileOnly so that normal compilation works |
| 44 | compileOnly.extendsFrom(shadowed) |
| 45 | // compiler tests run w/o shadowed classes so we should add those dependencies into test |
| 46 | // configuration |
| 47 | testCompile.extendsFrom(shadowed) |
| 48 | // for downstream tests, provide a configuration that includes the shadow output + other |
| 49 | // dependencies that are not shadowed |
| 50 | shadowAndImplementation.extendsFrom(shadow) |
| 51 | shadowAndImplementation.extendsFrom(implementation) |
| 52 | } |
| 53 | |
| 54 | shadowJar { |
| 55 | // set classifier to empty string so that it doesn't append anything to the jar. |
Jim Sproch | 9e38b4f | 2021-01-06 14:21:06 -0800 | [diff] [blame] | 56 | archiveClassifier = "" |
Yigit Boyar | 562a37d | 2020-03-16 13:00:36 -0700 | [diff] [blame] | 57 | configurations = [project.configurations.shadowed] |
| 58 | dependencies { |
| 59 | // antlr has dependencies on unrelated projects for its gui stuff, do not include them |
Jim Sproch | 9e38b4f | 2021-01-06 14:21:06 -0800 | [diff] [blame] | 60 | exclude(dependency("org.abego.treelayout:.*")) |
| 61 | exclude(dependency("org.glassfish:.*")) |
| 62 | exclude(dependency("com.ibm.icu:.*")) |
Yigit Boyar | 562a37d | 2020-03-16 13:00:36 -0700 | [diff] [blame] | 63 | } |
Aurimas Liutikas | 04a624d | 2021-08-10 14:12:14 -0700 | [diff] [blame] | 64 | relocate("org.antlr", "androidx.room.jarjarred.org.antlr") |
| 65 | relocate("org.stringtemplate", "androidx.room.jarjarred.org.stringtemplate") |
Yigit Boyar | 562a37d | 2020-03-16 13:00:36 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | jar { |
| 69 | // set a classifier on this one so that the output does not clash with the output from |
| 70 | // shadowJar task. We should never use this directly as it won't have the shadowed classes that |
| 71 | // are necessary to run. |
Jim Sproch | 9e38b4f | 2021-01-06 14:21:06 -0800 | [diff] [blame] | 72 | archiveClassifier = "before-jarjar" |
Yigit Boyar | 562a37d | 2020-03-16 13:00:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Yigit Boyar | 562a37d | 2020-03-16 13:00:36 -0700 | [diff] [blame] | 75 | configurations { |
| 76 | // replace the standard jar with the one built by 'shadowJar' in both api and runtime variants |
| 77 | apiElements.outgoing.artifacts.clear() |
| 78 | apiElements.outgoing.artifact(shadowJar) { |
| 79 | builtBy shadowJar |
| 80 | } |
| 81 | runtimeElements.outgoing.artifacts.clear() |
| 82 | runtimeElements.outgoing.artifact(shadowJar) { |
| 83 | builtBy shadowJar |
| 84 | } |
| 85 | } |
| 86 | |
Ember Rose | c662e1a | 2022-09-16 14:37:50 -0400 | [diff] [blame^] | 87 | androidx.configureAarAsJarForConfiguration("testImplementation") |
Aurimas Liutikas | 7d0ea332 | 2022-09-13 18:47:56 -0700 | [diff] [blame] | 88 | |
Yigit Boyar | 19b4110 | 2016-11-20 10:46:32 -0800 | [diff] [blame] | 89 | dependencies { |
Aurimas Liutikas | 8a1c039 | 2019-12-02 11:31:04 -0800 | [diff] [blame] | 90 | implementation(project(":room:room-common")) |
| 91 | implementation(project(":room:room-migration")) |
Yigit Boyar | 16d7829 | 2020-08-03 16:00:18 -0700 | [diff] [blame] | 92 | implementation(project(":room:room-compiler-processing")) |
Aurimas Liutikas | 1a0e7b1 | 2021-05-04 12:55:40 -0700 | [diff] [blame] | 93 | implementation(libs.kotlinStdlib) |
| 94 | implementation(libs.autoCommon) |
| 95 | implementation(libs.autoValueAnnotations) |
| 96 | implementation(libs.javapoet) |
Aurimas Liutikas | e1b8458 | 2021-04-23 14:17:30 -0700 | [diff] [blame] | 97 | implementation(libs.kspApi) |
Aurimas Liutikas | 1a0e7b1 | 2021-05-04 12:55:40 -0700 | [diff] [blame] | 98 | shadowed(libs.antlr4) |
| 99 | implementation(libs.sqliteJdbc) |
| 100 | implementation(libs.kotlinMetadataJvm) |
| 101 | implementation(libs.apacheCommonsCodec) |
| 102 | implementation(libs.intellijAnnotations) |
| 103 | testImplementation(libs.truth) |
| 104 | testImplementation(libs.autoValue) // to access the processor in tests |
Daniel Santiago Rivera | 7cb4041 | 2021-09-02 13:57:38 -0700 | [diff] [blame] | 105 | testImplementation(libs.autoServiceAnnotations) |
| 106 | testImplementation(libs.autoService) // to access the processor in tests |
Jim Sproch | 9e38b4f | 2021-01-06 14:21:06 -0800 | [diff] [blame] | 107 | testImplementation(projectOrArtifact(":paging:paging-common")) |
Yigit Boyar | f1dce27 | 2020-12-07 22:29:16 -0800 | [diff] [blame] | 108 | testImplementation(project(":room:room-compiler-processing-testing")) |
Aurimas Liutikas | 1a0e7b1 | 2021-05-04 12:55:40 -0700 | [diff] [blame] | 109 | testImplementation(libs.junit) |
| 110 | testImplementation(libs.jsr250) |
| 111 | testImplementation(libs.mockitoCore) |
| 112 | testImplementation(libs.antlr4) |
Yigit Boyar | 577a3a6 | 2021-09-13 18:40:16 -0700 | [diff] [blame] | 113 | testImplementation(libs.kotlinCompilerEmbeddable) |
Jim Sproch | 9e38b4f | 2021-01-06 14:21:06 -0800 | [diff] [blame] | 114 | testImplementation(fileTree( |
Alan Viverette | bb04f2e | 2020-04-09 17:13:37 -0400 | [diff] [blame] | 115 | dir: "${SdkHelperKt.getSdkPath(project)}/platforms/$SupportConfig.COMPILE_SDK_VERSION/", |
| 116 | include : "android.jar" |
Jim Sproch | 9e38b4f | 2021-01-06 14:21:06 -0800 | [diff] [blame] | 117 | )) |
Ember Rose | c662e1a | 2022-09-16 14:37:50 -0400 | [diff] [blame^] | 118 | testImplementationAarAsJar(project(":room:room-runtime")) |
| 119 | testImplementationAarAsJar(project(":sqlite:sqlite")) |
Kuan-Ying Chou | fa34a94 | 2021-12-20 18:34:56 +0000 | [diff] [blame] | 120 | testImplementation(project(":internal-testutils-common")) |
Yigit Boyar | 19b4110 | 2016-11-20 10:46:32 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Aurimas Liutikas | 6f9301c | 2021-11-05 10:28:38 -0700 | [diff] [blame] | 123 | def generateAntlrTask = task("generateAntlrGrammar", type: GenerateAntlrGrammar) { |
Aurimas Liutikas | 6f9301c | 2021-11-05 10:28:38 -0700 | [diff] [blame] | 124 | sqliteFile = file("$projectDir/SQLite.g4") |
Daz DeBoer | 0b80c76 | 2021-11-17 09:26:31 -0700 | [diff] [blame] | 125 | antlrClasspath = configurations.compileClasspath |
| 126 | outputDirectory = file(antlrOut) |
Yigit Boyar | 19b4110 | 2016-11-20 10:46:32 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Aurimas Liutikas | 6f9301c | 2021-11-05 10:28:38 -0700 | [diff] [blame] | 129 | @CacheableTask |
Daz DeBoer | 0b80c76 | 2021-11-17 09:26:31 -0700 | [diff] [blame] | 130 | abstract class GenerateAntlrGrammar extends DefaultTask { |
Aurimas Liutikas | 6f9301c | 2021-11-05 10:28:38 -0700 | [diff] [blame] | 131 | @PathSensitive(PathSensitivity.NONE) |
| 132 | @InputFile |
| 133 | File sqliteFile |
| 134 | |
Daz DeBoer | 0b80c76 | 2021-11-17 09:26:31 -0700 | [diff] [blame] | 135 | @Classpath |
| 136 | FileCollection antlrClasspath |
| 137 | |
Aurimas Liutikas | 6f9301c | 2021-11-05 10:28:38 -0700 | [diff] [blame] | 138 | @OutputDirectory |
| 139 | File outputDirectory |
| 140 | |
Daz DeBoer | 0b80c76 | 2021-11-17 09:26:31 -0700 | [diff] [blame] | 141 | @Inject |
| 142 | abstract ExecOperations getExecOperations() |
| 143 | |
| 144 | @Inject |
Aurimas Liutikas | 6f9301c | 2021-11-05 10:28:38 -0700 | [diff] [blame] | 145 | public GenerateAntlrGrammar() { |
| 146 | description("Generates Antlr Grammar used by room") |
| 147 | group("build") |
Daz DeBoer | 0b80c76 | 2021-11-17 09:26:31 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @TaskAction |
| 151 | void generateAntlrGrammar() { |
| 152 | execOperations.javaexec { |
| 153 | mainClass.set("org.antlr.v4.Tool") |
| 154 | classpath = antlrClasspath |
| 155 | args "SQLite.g4", |
| 156 | "-visitor", |
| 157 | "-o", new File(outputDirectory, "androidx/room/parser").path, |
| 158 | "-package", "androidx.room.parser" |
| 159 | } |
Aurimas Liutikas | 6f9301c | 2021-11-05 10:28:38 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Yigit Boyar | 562a37d | 2020-03-16 13:00:36 -0700 | [diff] [blame] | 163 | /** |
| 164 | * Room compiler jarjars some dependencies. This task validates the published artifacts of room |
| 165 | * compiler to ensure dependencies are properly jarjarred. |
| 166 | */ |
| 167 | class CheckArtifactTask extends DefaultTask { |
| 168 | @InputFiles |
| 169 | FileCollection artifactInputs = project.objects.fileCollection() |
| 170 | @InputFile |
| 171 | File pomFile |
| 172 | @OutputFile |
| 173 | File result = new File(project.buildDir, "checkArtifactOutput.txt") |
| 174 | /** |
| 175 | * Checks the publish task's artifacts to make sure the classes.jar does include jarjarred |
| 176 | * antlr classes. |
| 177 | */ |
| 178 | def validatePublishTaskOutputs() { |
| 179 | if (artifactInputs.files.isEmpty()) { |
| 180 | throw new GradleException("Couldn't find the classes.jar for the room-compiler " + |
| 181 | "artifact. Ensure that publish is setup properly.") |
| 182 | } |
| 183 | artifactInputs.forEach { |
| 184 | validateJarContents(it) |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Traverses the given jar file, looks for the classes that should be jarjarred and validates |
| 190 | * their location. |
| 191 | */ |
| 192 | def validateJarContents(File jarFile) { |
Aurimas Liutikas | 04a624d | 2021-08-10 14:12:14 -0700 | [diff] [blame] | 193 | Boolean found = false |
| 194 | ZipFile zip = new ZipFile(jarFile) |
| 195 | try { |
| 196 | for (Enumeration list = zip.entries(); list.hasMoreElements(); ) { |
| 197 | String entry = ((ZipEntry) list.nextElement()).name |
| 198 | if (!entry.endsWith(".class")) continue |
| 199 | if (entry.contains("org/antlr")) { |
| 200 | found = true |
| 201 | if (!entry.contains("androidx/room/jarjarred/org/antlr")) { |
| 202 | throw new GradleException("Any Antlr class included in the Room Compiler's" + |
| 203 | " jar file should be moved into androidx/room/jarjarred.\n" + |
| 204 | "Looks like $entry has not been moved") |
| 205 | } |
| 206 | } |
Daniel Santiago Rivera | 399f8f4 | 2021-08-11 07:33:45 -0700 | [diff] [blame] | 207 | if (!entry.startsWith("androidx/room/")) { |
Aurimas Liutikas | 04a624d | 2021-08-10 14:12:14 -0700 | [diff] [blame] | 208 | throw new GradleException("Found a class that is not in androidx.room " + |
| 209 | "package: $entry") |
Yigit Boyar | 562a37d | 2020-03-16 13:00:36 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
Aurimas Liutikas | 04a624d | 2021-08-10 14:12:14 -0700 | [diff] [blame] | 212 | } finally { |
| 213 | zip.close() |
Yigit Boyar | 562a37d | 2020-03-16 13:00:36 -0700 | [diff] [blame] | 214 | } |
| 215 | if (!found) { |
| 216 | throw new GradleException("Couldn't find any Antlr classes in room-compiler artifact" + |
| 217 | ".\n Antlr is jarjarred into room-compiler so there should be some files") |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Checks the generated pom file to ensure it does not depend on any jarjarred dependencies |
| 223 | * but still depends on others. |
| 224 | */ |
| 225 | def validatePomTaskOutputs() { |
| 226 | if (!pomFile.canRead()) { |
| 227 | throw new GradleException("Cannot find the pom file for room-compiler") |
| 228 | } |
| 229 | def pomContents = pomFile.newReader().text |
| 230 | if (pomContents.contains("antlr")) { |
| 231 | throw new GradleException("Room-compiler pom file should not depend on antlr.\n" + |
| 232 | "Pom Contents:\n $pomContents") |
| 233 | } |
| 234 | if(!pomContents.contains("<artifactId>kotlin-stdlib</artifactId>")) { |
| 235 | throw new GradleException("room-compiler should depend on kotlin stdlib.\n" + |
| 236 | "Pom Contents:\n $pomContents") |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | @TaskAction |
| 241 | def validate() { |
| 242 | result.write("fail\n") |
| 243 | validatePublishTaskOutputs() |
| 244 | validatePomTaskOutputs() |
Daniel Santiago Rivera | d8d57ba | 2020-07-30 10:47:26 -0700 | [diff] [blame] | 245 | // have a no-op output to make gradle happy w/ input/output checking. |
Yigit Boyar | 562a37d | 2020-03-16 13:00:36 -0700 | [diff] [blame] | 246 | result.write("ok\n") |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | def checkArtifactContentsTask = tasks.register("checkArtifactTask", CheckArtifactTask) { |
Aurimas Liutikas | 816e387 | 2022-07-19 11:33:48 -0700 | [diff] [blame] | 251 | def pomTask = (GenerateMavenPom) project.tasks.named("generatePomFileForMavenPublication").get() |
| 252 | it.pomFile = pomTask.destination |
| 253 | } |
| 254 | |
| 255 | afterEvaluate { |
| 256 | def publishTaskProvider = project.tasks.named("publishMavenPublicationToMavenRepository") |
| 257 | checkArtifactContentsTask.configure { |
| 258 | it.artifactInputs.from { |
| 259 | publishTaskProvider.map { |
| 260 | ((PublishToMavenRepository) it).getPublication().artifacts.matching { |
| 261 | it.classifier == null |
| 262 | }.collect { |
| 263 | it.file |
| 264 | } |
| 265 | } |
Yigit Boyar | 562a37d | 2020-03-16 13:00:36 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
Yigit Boyar | 562a37d | 2020-03-16 13:00:36 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // make sure we validate published artifacts on the build server. |
| 271 | BuildOnServerKt.addToBuildOnServer(project, checkArtifactContentsTask) |
| 272 | |
Yigit Boyar | 19b4110 | 2016-11-20 10:46:32 -0800 | [diff] [blame] | 273 | tasks.findByName("compileKotlin").dependsOn(generateAntlrTask) |
Jeff Gaston | 787e3c3 | 2020-08-10 16:51:40 -0400 | [diff] [blame] | 274 | tasks.findByName("sourceJar").dependsOn(generateAntlrTask) |
Yigit Boyar | 19b4110 | 2016-11-20 10:46:32 -0800 | [diff] [blame] | 275 | |
Yigit Boyar | 9b3d20b | 2020-07-05 10:48:05 -0700 | [diff] [blame] | 276 | tasks.withType(KotlinCompile).configureEach { |
| 277 | kotlinOptions { |
Daniel Santiago Rivera | ceb65a6 | 2021-06-09 10:03:57 -0700 | [diff] [blame] | 278 | freeCompilerArgs += [ |
Daniel Santiago Rivera | dc6ad79 | 2022-04-21 14:16:43 +0000 | [diff] [blame] | 279 | "-Xjvm-default=all", |
Jim Sproch | bcbd33a | 2022-01-12 14:45:37 -0800 | [diff] [blame] | 280 | "-opt-in=kotlin.contracts.ExperimentalContracts", |
| 281 | "-opt-in=androidx.room.compiler.processing.ExperimentalProcessingApi" |
Daniel Santiago Rivera | ceb65a6 | 2021-06-09 10:03:57 -0700 | [diff] [blame] | 282 | ] |
Yigit Boyar | 9b3d20b | 2020-07-05 10:48:05 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Yigit Boyar | 40b5cb3 | 2021-01-25 22:47:35 -0800 | [diff] [blame] | 286 | tasks.withType(Test).configureEach { |
| 287 | it.systemProperty("androidx.room.compiler.processing.strict", "true") |
Aurimas Liutikas | f58a25f | 2022-03-07 11:09:56 -0800 | [diff] [blame] | 288 | it.maxParallelForks(3) |
Dustin Lam | 10b44a6 | 2022-04-06 00:14:47 +0000 | [diff] [blame] | 289 | if (project.providers.environmentVariable("GITHUB_ACTIONS").present) { |
| 290 | // limit memory usage to avoid running out of memory in the docker container. |
| 291 | it.maxHeapSize("512m") |
| 292 | } |
Yigit Boyar | 40b5cb3 | 2021-01-25 22:47:35 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Aurimas Liutikas | 2ad3161 | 2019-04-01 04:23:03 -0700 | [diff] [blame] | 295 | androidx { |
Aurimas Liutikas | ea5ee82 | 2017-11-06 12:52:28 -0800 | [diff] [blame] | 296 | name = "Android Room Compiler" |
Daniel Santiago Rivera | 565946b | 2022-08-01 11:49:07 -0700 | [diff] [blame] | 297 | type = LibraryType.ANNOTATION_PROCESSOR |
Aurimas Liutikas | 7f40a7e | 2017-10-27 17:55:06 -0700 | [diff] [blame] | 298 | mavenGroup = LibraryGroups.ROOM |
Aurimas Liutikas | ea5ee82 | 2017-11-06 12:52:28 -0800 | [diff] [blame] | 299 | inceptionYear = "2017" |
| 300 | description = "Android Room annotation processor" |
Yigit Boyar | d809f48 | 2017-09-13 16:02:16 -0700 | [diff] [blame] | 301 | } |