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 |
| 22 | import com.github.jengelman.gradle.plugins.shadow.tasks.ConfigureShadowRelocation |
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 | } |
| 64 | } |
| 65 | |
| 66 | jar { |
| 67 | // set a classifier on this one so that the output does not clash with the output from |
| 68 | // shadowJar task. We should never use this directly as it won't have the shadowed classes that |
| 69 | // are necessary to run. |
Jim Sproch | 9e38b4f | 2021-01-06 14:21:06 -0800 | [diff] [blame] | 70 | archiveClassifier = "before-jarjar" |
Yigit Boyar | 562a37d | 2020-03-16 13:00:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // relocate all shadow dependencies |
| 74 | task relocateShadowJar(type: ConfigureShadowRelocation) { |
| 75 | target = tasks.shadowJar |
| 76 | prefix = "androidx.room.jarjarred" |
| 77 | } |
| 78 | |
Jim Sproch | 9e38b4f | 2021-01-06 14:21:06 -0800 | [diff] [blame] | 79 | tasks.shadowJar.dependsOn(tasks.relocateShadowJar) |
Yigit Boyar | 562a37d | 2020-03-16 13:00:36 -0700 | [diff] [blame] | 80 | |
| 81 | configurations { |
| 82 | // replace the standard jar with the one built by 'shadowJar' in both api and runtime variants |
| 83 | apiElements.outgoing.artifacts.clear() |
| 84 | apiElements.outgoing.artifact(shadowJar) { |
| 85 | builtBy shadowJar |
| 86 | } |
| 87 | runtimeElements.outgoing.artifacts.clear() |
| 88 | runtimeElements.outgoing.artifact(shadowJar) { |
| 89 | builtBy shadowJar |
| 90 | } |
| 91 | } |
| 92 | |
Yigit Boyar | 19b4110 | 2016-11-20 10:46:32 -0800 | [diff] [blame] | 93 | dependencies { |
Aurimas Liutikas | 8a1c039 | 2019-12-02 11:31:04 -0800 | [diff] [blame] | 94 | implementation(project(":room:room-common")) |
| 95 | implementation(project(":room:room-migration")) |
Yigit Boyar | 16d7829 | 2020-08-03 16:00:18 -0700 | [diff] [blame] | 96 | implementation(project(":room:room-compiler-processing")) |
Aurimas Liutikas | 1a0e7b1 | 2021-05-04 12:55:40 -0700 | [diff] [blame] | 97 | implementation(libs.kotlinStdlib) |
| 98 | implementation(libs.autoCommon) |
| 99 | implementation(libs.autoValueAnnotations) |
| 100 | implementation(libs.javapoet) |
Aurimas Liutikas | e1b8458 | 2021-04-23 14:17:30 -0700 | [diff] [blame] | 101 | implementation(libs.kspApi) |
Aurimas Liutikas | 1a0e7b1 | 2021-05-04 12:55:40 -0700 | [diff] [blame] | 102 | shadowed(libs.antlr4) |
| 103 | implementation(libs.sqliteJdbc) |
| 104 | implementation(libs.kotlinMetadataJvm) |
| 105 | implementation(libs.apacheCommonsCodec) |
| 106 | implementation(libs.intellijAnnotations) |
| 107 | testImplementation(libs.truth) |
| 108 | testImplementation(libs.autoValue) // to access the processor in tests |
Jim Sproch | 9e38b4f | 2021-01-06 14:21:06 -0800 | [diff] [blame] | 109 | testImplementation(projectOrArtifact(":paging:paging-common")) |
Yigit Boyar | f1dce27 | 2020-12-07 22:29:16 -0800 | [diff] [blame] | 110 | testImplementation(project(":room:room-compiler-processing-testing")) |
Aurimas Liutikas | 1a0e7b1 | 2021-05-04 12:55:40 -0700 | [diff] [blame] | 111 | testImplementation(libs.junit) |
| 112 | testImplementation(libs.jsr250) |
| 113 | testImplementation(libs.mockitoCore) |
| 114 | testImplementation(libs.antlr4) |
Jim Sproch | 9e38b4f | 2021-01-06 14:21:06 -0800 | [diff] [blame] | 115 | testImplementation(fileTree( |
Alan Viverette | bb04f2e | 2020-04-09 17:13:37 -0400 | [diff] [blame] | 116 | dir: "${SdkHelperKt.getSdkPath(project)}/platforms/$SupportConfig.COMPILE_SDK_VERSION/", |
| 117 | include : "android.jar" |
Jim Sproch | 9e38b4f | 2021-01-06 14:21:06 -0800 | [diff] [blame] | 118 | )) |
| 119 | testImplementation(fileTree( |
Alan Viverette | bb04f2e | 2020-04-09 17:13:37 -0400 | [diff] [blame] | 120 | dir: "${new File(project(":room:room-runtime").buildDir, "libJar")}", |
| 121 | include : "*.jar" |
Jim Sproch | 9e38b4f | 2021-01-06 14:21:06 -0800 | [diff] [blame] | 122 | )) |
| 123 | testImplementation(fileTree( |
Alan Viverette | bb04f2e | 2020-04-09 17:13:37 -0400 | [diff] [blame] | 124 | dir: "${new File(project(":sqlite:sqlite").buildDir, "libJar")}", |
| 125 | include : "*.jar" |
Jim Sproch | 9e38b4f | 2021-01-06 14:21:06 -0800 | [diff] [blame] | 126 | )) |
Yigit Boyar | 19b4110 | 2016-11-20 10:46:32 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Jim Sproch | 9e38b4f | 2021-01-06 14:21:06 -0800 | [diff] [blame] | 129 | def generateAntlrTask = task("generateAntlrGrammar", type: JavaExec) { |
Yigit Boyar | 19b4110 | 2016-11-20 10:46:32 -0800 | [diff] [blame] | 130 | def outFolder = file(antlrOut) |
| 131 | outputs.dir(outFolder) |
Yigit Boyar | 922b65c | 2017-12-18 09:29:26 -0800 | [diff] [blame] | 132 | inputs.file("$projectDir/SQLite.g4") |
Aurimas Liutikas | 8a1c039 | 2019-12-02 11:31:04 -0800 | [diff] [blame] | 133 | classpath configurations.compileClasspath |
Yigit Boyar | 19b4110 | 2016-11-20 10:46:32 -0800 | [diff] [blame] | 134 | main "org.antlr.v4.Tool" |
Alan Viverette | 6c56334 | 2018-03-08 18:02:39 -0500 | [diff] [blame] | 135 | args "SQLite.g4", "-visitor", "-o", new File(outFolder, "androidx/room/parser").path, |
| 136 | "-package", "androidx.room.parser" |
Yigit Boyar | 19b4110 | 2016-11-20 10:46:32 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Yigit Boyar | 562a37d | 2020-03-16 13:00:36 -0700 | [diff] [blame] | 139 | /** |
| 140 | * Room compiler jarjars some dependencies. This task validates the published artifacts of room |
| 141 | * compiler to ensure dependencies are properly jarjarred. |
| 142 | */ |
| 143 | class CheckArtifactTask extends DefaultTask { |
| 144 | @InputFiles |
| 145 | FileCollection artifactInputs = project.objects.fileCollection() |
| 146 | @InputFile |
| 147 | File pomFile |
| 148 | @OutputFile |
| 149 | File result = new File(project.buildDir, "checkArtifactOutput.txt") |
| 150 | /** |
| 151 | * Checks the publish task's artifacts to make sure the classes.jar does include jarjarred |
| 152 | * antlr classes. |
| 153 | */ |
| 154 | def validatePublishTaskOutputs() { |
| 155 | if (artifactInputs.files.isEmpty()) { |
| 156 | throw new GradleException("Couldn't find the classes.jar for the room-compiler " + |
| 157 | "artifact. Ensure that publish is setup properly.") |
| 158 | } |
| 159 | artifactInputs.forEach { |
| 160 | validateJarContents(it) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Traverses the given jar file, looks for the classes that should be jarjarred and validates |
| 166 | * their location. |
| 167 | */ |
| 168 | def validateJarContents(File jarFile) { |
| 169 | FileTree jarFiles = project.zipTree(jarFile) |
| 170 | def found = false |
| 171 | jarFiles.files.each { |
| 172 | if (it.path.contains("/org/antlr")) { |
| 173 | found = true |
| 174 | if (!it.path.contains("androidx/room/jarjarred/org/antlr")) { |
| 175 | throw new GradleException("Any Antlr class included in the Room Compiler's" + |
| 176 | " jar file should be moved into androidx/room/jarjarred.\n" + |
| 177 | "Looks like $it has not been moved") |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | if (!found) { |
| 182 | throw new GradleException("Couldn't find any Antlr classes in room-compiler artifact" + |
| 183 | ".\n Antlr is jarjarred into room-compiler so there should be some files") |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Checks the generated pom file to ensure it does not depend on any jarjarred dependencies |
| 189 | * but still depends on others. |
| 190 | */ |
| 191 | def validatePomTaskOutputs() { |
| 192 | if (!pomFile.canRead()) { |
| 193 | throw new GradleException("Cannot find the pom file for room-compiler") |
| 194 | } |
| 195 | def pomContents = pomFile.newReader().text |
| 196 | if (pomContents.contains("antlr")) { |
| 197 | throw new GradleException("Room-compiler pom file should not depend on antlr.\n" + |
| 198 | "Pom Contents:\n $pomContents") |
| 199 | } |
| 200 | if(!pomContents.contains("<artifactId>kotlin-stdlib</artifactId>")) { |
| 201 | throw new GradleException("room-compiler should depend on kotlin stdlib.\n" + |
| 202 | "Pom Contents:\n $pomContents") |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | @TaskAction |
| 207 | def validate() { |
| 208 | result.write("fail\n") |
| 209 | validatePublishTaskOutputs() |
| 210 | validatePomTaskOutputs() |
Daniel Santiago Rivera | d8d57ba | 2020-07-30 10:47:26 -0700 | [diff] [blame] | 211 | // 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] | 212 | result.write("ok\n") |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | def checkArtifactContentsTask = tasks.register("checkArtifactTask", CheckArtifactTask) { |
| 217 | it.artifactInputs.from { |
| 218 | ((PublishToMavenRepository) project.tasks |
| 219 | .named("publishMavenPublicationToMavenRepository").get()).getPublication() |
| 220 | .artifacts.matching { |
| 221 | it.classifier == null |
| 222 | }.collect { |
| 223 | it.file |
| 224 | } |
| 225 | } |
| 226 | def pomTask = (GenerateMavenPom) project.tasks |
| 227 | .named("generatePomFileForMavenPublication").get() |
| 228 | it.pomFile = pomTask.destination |
Aurimas Liutikas | 386ea1d | 2021-04-29 15:30:34 -0700 | [diff] [blame] | 229 | it.dependsOn("publishMavenPublicationToMavenRepository") |
Yigit Boyar | 562a37d | 2020-03-16 13:00:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // make sure we validate published artifacts on the build server. |
| 233 | BuildOnServerKt.addToBuildOnServer(project, checkArtifactContentsTask) |
| 234 | |
Yigit Boyar | 19b4110 | 2016-11-20 10:46:32 -0800 | [diff] [blame] | 235 | tasks.findByName("compileKotlin").dependsOn(generateAntlrTask) |
Jeff Gaston | 787e3c3 | 2020-08-10 16:51:40 -0400 | [diff] [blame] | 236 | tasks.findByName("sourceJar").dependsOn(generateAntlrTask) |
Jeff Gaston | fa1793f | 2021-04-29 13:06:20 -0400 | [diff] [blame] | 237 | tasks.findByName("compileKotlin").dependsOn(":room:room-runtime:jarRelease") |
| 238 | tasks.findByName("compileKotlin").dependsOn(":sqlite:sqlite:jarRelease") |
Yigit Boyar | 19b4110 | 2016-11-20 10:46:32 -0800 | [diff] [blame] | 239 | |
Yigit Boyar | 9b3d20b | 2020-07-05 10:48:05 -0700 | [diff] [blame] | 240 | tasks.withType(KotlinCompile).configureEach { |
| 241 | kotlinOptions { |
| 242 | freeCompilerArgs += ["-Xopt-in=kotlin.RequiresOptIn", |
Yigit Boyar | 186c422 | 2021-03-01 16:53:38 -0800 | [diff] [blame] | 243 | "-Xopt-in=kotlin.contracts.ExperimentalContracts", |
| 244 | "-Xopt-in=androidx.room.compiler.processing.ExperimentalProcessingApi"] |
Yigit Boyar | 9b3d20b | 2020-07-05 10:48:05 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Yigit Boyar | 40b5cb3 | 2021-01-25 22:47:35 -0800 | [diff] [blame] | 248 | tasks.withType(Test).configureEach { |
| 249 | it.systemProperty("androidx.room.compiler.processing.strict", "true") |
| 250 | } |
| 251 | |
Aurimas Liutikas | 2ad3161 | 2019-04-01 04:23:03 -0700 | [diff] [blame] | 252 | androidx { |
Aurimas Liutikas | ea5ee82 | 2017-11-06 12:52:28 -0800 | [diff] [blame] | 253 | name = "Android Room Compiler" |
Owen Gray | 74cc259 | 2020-09-24 15:05:40 -0400 | [diff] [blame] | 254 | type = LibraryType.COMPILER_PLUGIN |
Aurimas Liutikas | 7f40a7e | 2017-10-27 17:55:06 -0700 | [diff] [blame] | 255 | mavenGroup = LibraryGroups.ROOM |
Aurimas Liutikas | ea5ee82 | 2017-11-06 12:52:28 -0800 | [diff] [blame] | 256 | inceptionYear = "2017" |
| 257 | description = "Android Room annotation processor" |
Yigit Boyar | d809f48 | 2017-09-13 16:02:16 -0700 | [diff] [blame] | 258 | } |