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