xref: /aosp_15_r20/external/moshi/moshi-kotlin-codegen/build.gradle.kts (revision 238ab3e782f339ab327592a602fa7df0a3f729ad)
1 /*
2  * Copyright (C) 2020 Square, Inc.
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  *    https://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  */
16 
17 import com.github.jengelman.gradle.plugins.shadow.tasks.ConfigureShadowRelocation
18 import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransformer
19 import com.vanniktech.maven.publish.JavadocJar.Empty
20 import com.vanniktech.maven.publish.KotlinJvm
21 import com.vanniktech.maven.publish.MavenPublishBaseExtension
22 import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
23 
<lambda>null24 plugins {
25   kotlin("jvm")
26   id("com.google.devtools.ksp")
27   id("com.vanniktech.maven.publish.base")
28   alias(libs.plugins.mavenShadow)
29 }
30 
<lambda>null31 tasks.withType<KotlinCompile>().configureEach {
32   kotlinOptions {
33     @Suppress("SuspiciousCollectionReassignment")
34     freeCompilerArgs += listOf(
35       "-opt-in=com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview",
36       "-opt-in=com.squareup.moshi.kotlin.codegen.api.InternalMoshiCodegenApi",
37     )
38     if (this@configureEach.name == "compileTestKotlin") {
39       freeCompilerArgs += "-opt-in=org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi"
40     }
41   }
42 }
43 
44 // --add-opens for kapt to work. KGP covers this for us but local JVMs in tests do not
<lambda>null45 tasks.withType<Test>().configureEach {
46   jvmArgs(
47     "--add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
48     "--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
49     "--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
50     "--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
51     "--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED",
52     "--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
53     "--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
54     "--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
55     "--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
56     "--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"
57   )
58 }
59 
60 val shade: Configuration = configurations.maybeCreate("compileShaded")
61 configurations.getByName("compileOnly").extendsFrom(shade)
<lambda>null62 dependencies {
63   implementation(project(":moshi"))
64   implementation(kotlin("reflect"))
65   shade(libs.kotlinxMetadata) {
66     exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib")
67   }
68   api(libs.kotlinpoet)
69   shade(libs.kotlinpoet.metadata) {
70     exclude(group = "org.jetbrains.kotlin")
71     exclude(group = "com.squareup", module = "kotlinpoet")
72     exclude(group = "com.google.guava")
73   }
74   implementation(libs.kotlinpoet.ksp)
75   implementation(libs.guava)
76   implementation(libs.asm)
77   implementation(platform(libs.kotlin.bom))
78 
79   implementation(libs.autoService)
80   ksp(libs.autoService.ksp)
81 
82   // KSP deps
83   compileOnly(libs.ksp)
84   compileOnly(libs.ksp.api)
85   compileOnly(libs.kotlin.annotationProcessingEmbeddable)
86   compileOnly(libs.kotlin.compilerEmbeddable)
87   compileOnly(platform(libs.kotlin.bom))
88   // Always force the latest KSP version to match the one we're compiling against
89   testImplementation(libs.ksp)
90   testImplementation(libs.ksp.api)
91   testImplementation(libs.kotlin.annotationProcessingEmbeddable)
92   testImplementation(libs.kotlin.compilerEmbeddable)
93   testImplementation(libs.kotlinCompileTesting.ksp)
94   testImplementation(platform(libs.kotlin.bom))
95 
96   // Copy these again as they're not automatically included since they're shaded
97   testImplementation(project(":moshi"))
98   testImplementation(kotlin("reflect"))
99   testImplementation(libs.kotlinpoet.metadata)
100   testImplementation(libs.kotlinpoet.ksp)
101   testImplementation(libs.junit)
102   testImplementation(libs.truth)
103   testImplementation(libs.kotlinCompileTesting)
104 }
105 
<lambda>null106 val relocateShadowJar = tasks.register<ConfigureShadowRelocation>("relocateShadowJar") {
107   target = tasks.shadowJar.get()
108 }
109 
<lambda>null110 val shadowJar = tasks.shadowJar.apply {
111   configure {
112     dependsOn(relocateShadowJar)
113     archiveClassifier.set("")
114     configurations = listOf(shade)
115     relocate("com.squareup.kotlinpoet.metadata", "com.squareup.moshi.kotlinpoet.metadata")
116     relocate(
117       "com.squareup.kotlinpoet.classinspector",
118       "com.squareup.moshi.kotlinpoet.classinspector"
119     )
120     relocate("kotlinx.metadata", "com.squareup.moshi.kotlinx.metadata")
121     transformers.add(ServiceFileTransformer())
122   }
123 }
124 
<lambda>null125 artifacts {
126   runtimeOnly(shadowJar)
127   archives(shadowJar)
128 }
129 
<lambda>null130 configure<MavenPublishBaseExtension> {
131   configure(KotlinJvm(javadocJar = Empty()))
132 }
133