blob: 58921f96c7343c6f714d3652baec6ec34d858f7c [file] [log] [blame]
Yigit Boyar19b41102016-11-20 10:46:32 -08001/*
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 Liutikas7f40a7e2017-10-27 17:55:06 -070016
Owen Gray74cc2592020-09-24 15:05:40 -040017import androidx.build.BuildOnServerKt
18import androidx.build.LibraryGroups
19import androidx.build.LibraryType
20import androidx.build.SupportConfig
21import androidx.build.SdkHelperKt
22import com.github.jengelman.gradle.plugins.shadow.tasks.ConfigureShadowRelocation
Yigit Boyar9b3d20b2020-07-05 10:48:05 -070023import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
24
Aurimas Liutikas526389b2018-02-27 14:01:24 -080025plugins {
Aurimas Liutikas65d3d602019-04-01 23:08:13 -070026 id("AndroidXPlugin")
27 id("kotlin")
Yigit Boyar562a37d2020-03-16 13:00:36 -070028 id("com.github.johnrengelman.shadow")
Aurimas Liutikas526389b2018-02-27 14:01:24 -080029}
Yigit Boyar19b41102016-11-20 10:46:32 -080030
31def antlrOut = "$buildDir/generated/antlr/grammar-gen/"
32sourceSets {
Jim Sproch9e38b4f2021-01-06 14:21:06 -080033 main.java.srcDirs += "src/main/grammar-gen"
Yigit Boyar19b41102016-11-20 10:46:32 -080034 main.java.srcDirs += antlrOut
35}
Sergey Vasilinets0af2c3f2017-07-20 12:48:16 -070036
Yigit Boyar562a37d2020-03-16 13:00:36 -070037configurations {
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
54shadowJar {
55 // set classifier to empty string so that it doesn't append anything to the jar.
Jim Sproch9e38b4f2021-01-06 14:21:06 -080056 archiveClassifier = ""
Yigit Boyar562a37d2020-03-16 13:00:36 -070057 configurations = [project.configurations.shadowed]
58 dependencies {
59 // antlr has dependencies on unrelated projects for its gui stuff, do not include them
Jim Sproch9e38b4f2021-01-06 14:21:06 -080060 exclude(dependency("org.abego.treelayout:.*"))
61 exclude(dependency("org.glassfish:.*"))
62 exclude(dependency("com.ibm.icu:.*"))
Yigit Boyar562a37d2020-03-16 13:00:36 -070063 }
64}
65
66jar {
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 Sproch9e38b4f2021-01-06 14:21:06 -080070 archiveClassifier = "before-jarjar"
Yigit Boyar562a37d2020-03-16 13:00:36 -070071}
72
73// relocate all shadow dependencies
74task relocateShadowJar(type: ConfigureShadowRelocation) {
75 target = tasks.shadowJar
76 prefix = "androidx.room.jarjarred"
77}
78
Jim Sproch9e38b4f2021-01-06 14:21:06 -080079tasks.shadowJar.dependsOn(tasks.relocateShadowJar)
Yigit Boyar562a37d2020-03-16 13:00:36 -070080
81configurations {
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 Boyar19b41102016-11-20 10:46:32 -080093dependencies {
Aurimas Liutikas8a1c0392019-12-02 11:31:04 -080094 implementation(project(":room:room-common"))
95 implementation(project(":room:room-migration"))
Yigit Boyar16d78292020-08-03 16:00:18 -070096 implementation(project(":room:room-compiler-processing"))
Aurimas Liutikas1a0e7b12021-05-04 12:55:40 -070097 implementation(libs.kotlinStdlib)
98 implementation(libs.autoCommon)
99 implementation(libs.autoValueAnnotations)
100 implementation(libs.javapoet)
Aurimas Liutikase1b84582021-04-23 14:17:30 -0700101 implementation(libs.kspApi)
Aurimas Liutikas1a0e7b12021-05-04 12:55:40 -0700102 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 Sproch9e38b4f2021-01-06 14:21:06 -0800109 testImplementation(projectOrArtifact(":paging:paging-common"))
Yigit Boyarf1dce272020-12-07 22:29:16 -0800110 testImplementation(project(":room:room-compiler-processing-testing"))
Aurimas Liutikas1a0e7b12021-05-04 12:55:40 -0700111 testImplementation(libs.junit)
112 testImplementation(libs.jsr250)
113 testImplementation(libs.mockitoCore)
114 testImplementation(libs.antlr4)
Jim Sproch9e38b4f2021-01-06 14:21:06 -0800115 testImplementation(fileTree(
Alan Viverettebb04f2e2020-04-09 17:13:37 -0400116 dir: "${SdkHelperKt.getSdkPath(project)}/platforms/$SupportConfig.COMPILE_SDK_VERSION/",
117 include : "android.jar"
Jim Sproch9e38b4f2021-01-06 14:21:06 -0800118 ))
119 testImplementation(fileTree(
Alan Viverettebb04f2e2020-04-09 17:13:37 -0400120 dir: "${new File(project(":room:room-runtime").buildDir, "libJar")}",
121 include : "*.jar"
Jim Sproch9e38b4f2021-01-06 14:21:06 -0800122 ))
123 testImplementation(fileTree(
Alan Viverettebb04f2e2020-04-09 17:13:37 -0400124 dir: "${new File(project(":sqlite:sqlite").buildDir, "libJar")}",
125 include : "*.jar"
Jim Sproch9e38b4f2021-01-06 14:21:06 -0800126 ))
Yigit Boyar19b41102016-11-20 10:46:32 -0800127}
128
Jim Sproch9e38b4f2021-01-06 14:21:06 -0800129def generateAntlrTask = task("generateAntlrGrammar", type: JavaExec) {
Yigit Boyar19b41102016-11-20 10:46:32 -0800130 def outFolder = file(antlrOut)
131 outputs.dir(outFolder)
Yigit Boyar922b65c2017-12-18 09:29:26 -0800132 inputs.file("$projectDir/SQLite.g4")
Aurimas Liutikas8a1c0392019-12-02 11:31:04 -0800133 classpath configurations.compileClasspath
Yigit Boyar19b41102016-11-20 10:46:32 -0800134 main "org.antlr.v4.Tool"
Alan Viverette6c563342018-03-08 18:02:39 -0500135 args "SQLite.g4", "-visitor", "-o", new File(outFolder, "androidx/room/parser").path,
136 "-package", "androidx.room.parser"
Yigit Boyar19b41102016-11-20 10:46:32 -0800137}
138
Yigit Boyar562a37d2020-03-16 13:00:36 -0700139/**
140 * Room compiler jarjars some dependencies. This task validates the published artifacts of room
141 * compiler to ensure dependencies are properly jarjarred.
142 */
143class 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 Riverad8d57ba2020-07-30 10:47:26 -0700211 // have a no-op output to make gradle happy w/ input/output checking.
Yigit Boyar562a37d2020-03-16 13:00:36 -0700212 result.write("ok\n")
213 }
214}
215
216def 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 Liutikas386ea1d2021-04-29 15:30:34 -0700229 it.dependsOn("publishMavenPublicationToMavenRepository")
Yigit Boyar562a37d2020-03-16 13:00:36 -0700230}
231
232// make sure we validate published artifacts on the build server.
233BuildOnServerKt.addToBuildOnServer(project, checkArtifactContentsTask)
234
Yigit Boyar19b41102016-11-20 10:46:32 -0800235tasks.findByName("compileKotlin").dependsOn(generateAntlrTask)
Jeff Gaston787e3c32020-08-10 16:51:40 -0400236tasks.findByName("sourceJar").dependsOn(generateAntlrTask)
Jeff Gastonfa1793f2021-04-29 13:06:20 -0400237tasks.findByName("compileKotlin").dependsOn(":room:room-runtime:jarRelease")
238tasks.findByName("compileKotlin").dependsOn(":sqlite:sqlite:jarRelease")
Yigit Boyar19b41102016-11-20 10:46:32 -0800239
Yigit Boyar9b3d20b2020-07-05 10:48:05 -0700240tasks.withType(KotlinCompile).configureEach {
241 kotlinOptions {
242 freeCompilerArgs += ["-Xopt-in=kotlin.RequiresOptIn",
Yigit Boyar186c4222021-03-01 16:53:38 -0800243 "-Xopt-in=kotlin.contracts.ExperimentalContracts",
244 "-Xopt-in=androidx.room.compiler.processing.ExperimentalProcessingApi"]
Yigit Boyar9b3d20b2020-07-05 10:48:05 -0700245 }
246}
247
Yigit Boyar40b5cb32021-01-25 22:47:35 -0800248tasks.withType(Test).configureEach {
249 it.systemProperty("androidx.room.compiler.processing.strict", "true")
250}
251
Aurimas Liutikas2ad31612019-04-01 04:23:03 -0700252androidx {
Aurimas Liutikasea5ee822017-11-06 12:52:28 -0800253 name = "Android Room Compiler"
Owen Gray74cc2592020-09-24 15:05:40 -0400254 type = LibraryType.COMPILER_PLUGIN
Aurimas Liutikas7f40a7e2017-10-27 17:55:06 -0700255 mavenGroup = LibraryGroups.ROOM
Aurimas Liutikasea5ee822017-11-06 12:52:28 -0800256 inceptionYear = "2017"
257 description = "Android Room annotation processor"
Yigit Boyard809f482017-09-13 16:02:16 -0700258}