blob: 323459efd7dbc368c06e492742a91ddc76d026bf [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
Aurimas Liutikas04a624d2021-08-10 14:12:14 -070022import java.util.zip.ZipEntry
23import java.util.zip.ZipFile
Yigit Boyar9b3d20b2020-07-05 10:48:05 -070024import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
25
Aurimas Liutikas526389b2018-02-27 14:01:24 -080026plugins {
Aurimas Liutikas65d3d602019-04-01 23:08:13 -070027 id("AndroidXPlugin")
28 id("kotlin")
Yigit Boyar562a37d2020-03-16 13:00:36 -070029 id("com.github.johnrengelman.shadow")
Aurimas Liutikas526389b2018-02-27 14:01:24 -080030}
Yigit Boyar19b41102016-11-20 10:46:32 -080031
32def antlrOut = "$buildDir/generated/antlr/grammar-gen/"
33sourceSets {
Jim Sproch9e38b4f2021-01-06 14:21:06 -080034 main.java.srcDirs += "src/main/grammar-gen"
Yigit Boyar19b41102016-11-20 10:46:32 -080035 main.java.srcDirs += antlrOut
36}
Sergey Vasilinets0af2c3f2017-07-20 12:48:16 -070037
Yigit Boyar562a37d2020-03-16 13:00:36 -070038configurations {
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
55shadowJar {
56 // set classifier to empty string so that it doesn't append anything to the jar.
Jim Sproch9e38b4f2021-01-06 14:21:06 -080057 archiveClassifier = ""
Yigit Boyar562a37d2020-03-16 13:00:36 -070058 configurations = [project.configurations.shadowed]
59 dependencies {
60 // antlr has dependencies on unrelated projects for its gui stuff, do not include them
Jim Sproch9e38b4f2021-01-06 14:21:06 -080061 exclude(dependency("org.abego.treelayout:.*"))
62 exclude(dependency("org.glassfish:.*"))
63 exclude(dependency("com.ibm.icu:.*"))
Yigit Boyar562a37d2020-03-16 13:00:36 -070064 }
Aurimas Liutikas04a624d2021-08-10 14:12:14 -070065 relocate("org.antlr", "androidx.room.jarjarred.org.antlr")
66 relocate("org.stringtemplate", "androidx.room.jarjarred.org.stringtemplate")
Yigit Boyar562a37d2020-03-16 13:00:36 -070067}
68
69jar {
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 Sproch9e38b4f2021-01-06 14:21:06 -080073 archiveClassifier = "before-jarjar"
Yigit Boyar562a37d2020-03-16 13:00:36 -070074}
75
Yigit Boyar562a37d2020-03-16 13:00:36 -070076configurations {
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 Boyar19b41102016-11-20 10:46:32 -080088dependencies {
Aurimas Liutikas8a1c0392019-12-02 11:31:04 -080089 implementation(project(":room:room-common"))
90 implementation(project(":room:room-migration"))
Yigit Boyar16d78292020-08-03 16:00:18 -070091 implementation(project(":room:room-compiler-processing"))
Aurimas Liutikas1a0e7b12021-05-04 12:55:40 -070092 implementation(libs.kotlinStdlib)
93 implementation(libs.autoCommon)
94 implementation(libs.autoValueAnnotations)
95 implementation(libs.javapoet)
Aurimas Liutikase1b84582021-04-23 14:17:30 -070096 implementation(libs.kspApi)
Aurimas Liutikas1a0e7b12021-05-04 12:55:40 -070097 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 Rivera7cb40412021-09-02 13:57:38 -0700104 testImplementation(libs.autoServiceAnnotations)
105 testImplementation(libs.autoService) // to access the processor in tests
Jim Sproch9e38b4f2021-01-06 14:21:06 -0800106 testImplementation(projectOrArtifact(":paging:paging-common"))
Yigit Boyarf1dce272020-12-07 22:29:16 -0800107 testImplementation(project(":room:room-compiler-processing-testing"))
Aurimas Liutikas1a0e7b12021-05-04 12:55:40 -0700108 testImplementation(libs.junit)
109 testImplementation(libs.jsr250)
110 testImplementation(libs.mockitoCore)
111 testImplementation(libs.antlr4)
Yigit Boyar577a3a62021-09-13 18:40:16 -0700112 testImplementation(libs.kotlinCompilerEmbeddable)
Jim Sproch9e38b4f2021-01-06 14:21:06 -0800113 testImplementation(fileTree(
Alan Viverettebb04f2e2020-04-09 17:13:37 -0400114 dir: "${SdkHelperKt.getSdkPath(project)}/platforms/$SupportConfig.COMPILE_SDK_VERSION/",
115 include : "android.jar"
Jim Sproch9e38b4f2021-01-06 14:21:06 -0800116 ))
117 testImplementation(fileTree(
Alan Viverettebb04f2e2020-04-09 17:13:37 -0400118 dir: "${new File(project(":room:room-runtime").buildDir, "libJar")}",
119 include : "*.jar"
Jim Sproch9e38b4f2021-01-06 14:21:06 -0800120 ))
121 testImplementation(fileTree(
Alan Viverettebb04f2e2020-04-09 17:13:37 -0400122 dir: "${new File(project(":sqlite:sqlite").buildDir, "libJar")}",
123 include : "*.jar"
Jim Sproch9e38b4f2021-01-06 14:21:06 -0800124 ))
Yigit Boyar19b41102016-11-20 10:46:32 -0800125}
126
Aurimas Liutikas6f9301c2021-11-05 10:28:38 -0700127def generateAntlrTask = task("generateAntlrGrammar", type: GenerateAntlrGrammar) {
Yigit Boyar19b41102016-11-20 10:46:32 -0800128 def outFolder = file(antlrOut)
Aurimas Liutikas6f9301c2021-11-05 10:28:38 -0700129 outputDirectory = outFolder
130 sqliteFile = file("$projectDir/SQLite.g4")
Aurimas Liutikas8a1c0392019-12-02 11:31:04 -0800131 classpath configurations.compileClasspath
Alan Viverette6c563342018-03-08 18:02:39 -0500132 args "SQLite.g4", "-visitor", "-o", new File(outFolder, "androidx/room/parser").path,
133 "-package", "androidx.room.parser"
Yigit Boyar19b41102016-11-20 10:46:32 -0800134}
135
Aurimas Liutikas6f9301c2021-11-05 10:28:38 -0700136@CacheableTask
137abstract 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 Boyar562a37d2020-03-16 13:00:36 -0700152/**
153 * Room compiler jarjars some dependencies. This task validates the published artifacts of room
154 * compiler to ensure dependencies are properly jarjarred.
155 */
156class 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 Liutikas04a624d2021-08-10 14:12:14 -0700182 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 Rivera399f8f42021-08-11 07:33:45 -0700196 if (!entry.startsWith("androidx/room/")) {
Aurimas Liutikas04a624d2021-08-10 14:12:14 -0700197 throw new GradleException("Found a class that is not in androidx.room " +
198 "package: $entry")
Yigit Boyar562a37d2020-03-16 13:00:36 -0700199 }
200 }
Aurimas Liutikas04a624d2021-08-10 14:12:14 -0700201 } finally {
202 zip.close()
Yigit Boyar562a37d2020-03-16 13:00:36 -0700203 }
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 Riverad8d57ba2020-07-30 10:47:26 -0700234 // have a no-op output to make gradle happy w/ input/output checking.
Yigit Boyar562a37d2020-03-16 13:00:36 -0700235 result.write("ok\n")
236 }
237}
238
239def 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 Liutikas386ea1d2021-04-29 15:30:34 -0700252 it.dependsOn("publishMavenPublicationToMavenRepository")
Yigit Boyar562a37d2020-03-16 13:00:36 -0700253}
254
255// make sure we validate published artifacts on the build server.
256BuildOnServerKt.addToBuildOnServer(project, checkArtifactContentsTask)
257
Yigit Boyar19b41102016-11-20 10:46:32 -0800258tasks.findByName("compileKotlin").dependsOn(generateAntlrTask)
Jeff Gaston787e3c32020-08-10 16:51:40 -0400259tasks.findByName("sourceJar").dependsOn(generateAntlrTask)
Jeff Gastonfa1793f2021-04-29 13:06:20 -0400260tasks.findByName("compileKotlin").dependsOn(":room:room-runtime:jarRelease")
261tasks.findByName("compileKotlin").dependsOn(":sqlite:sqlite:jarRelease")
Yigit Boyar19b41102016-11-20 10:46:32 -0800262
Yigit Boyar9b3d20b2020-07-05 10:48:05 -0700263tasks.withType(KotlinCompile).configureEach {
264 kotlinOptions {
Daniel Santiago Riveraceb65a62021-06-09 10:03:57 -0700265 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 Boyar9b3d20b2020-07-05 10:48:05 -0700271 }
272}
273
Yigit Boyar40b5cb32021-01-25 22:47:35 -0800274tasks.withType(Test).configureEach {
275 it.systemProperty("androidx.room.compiler.processing.strict", "true")
276}
277
Aurimas Liutikas2ad31612019-04-01 04:23:03 -0700278androidx {
Aurimas Liutikasea5ee822017-11-06 12:52:28 -0800279 name = "Android Room Compiler"
Owen Gray74cc2592020-09-24 15:05:40 -0400280 type = LibraryType.COMPILER_PLUGIN
Aurimas Liutikas7f40a7e2017-10-27 17:55:06 -0700281 mavenGroup = LibraryGroups.ROOM
Aurimas Liutikasea5ee822017-11-06 12:52:28 -0800282 inceptionYear = "2017"
283 description = "Android Room annotation processor"
Yigit Boyard809f482017-09-13 16:02:16 -0700284}