xref: /aosp_15_r20/external/kotlinpoet/build.gradle.kts (revision 3c321d951dd070fb96f8ba59e952ffc3131379a0)
1 /*
2  * Copyright (C) 2019 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 import com.diffplug.gradle.spotless.SpotlessExtension
17 import org.gradle.api.tasks.testing.logging.TestExceptionFormat
18 import org.jetbrains.dokka.gradle.DokkaTask
19 import org.jetbrains.kotlin.gradle.dsl.JvmTarget
20 import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
21 import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
22 import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
23 
<lambda>null24 plugins {
25   alias(libs.plugins.kotlin.multiplatform) apply false
26   alias(libs.plugins.kotlin.jvm) apply false
27   alias(libs.plugins.ksp) apply false
28   alias(libs.plugins.dokka) apply false
29   alias(libs.plugins.spotless) apply false
30   alias(libs.plugins.mavenPublish) apply false
31   alias(libs.plugins.kotlinBinaryCompatibilityValidator)
32 }
33 
<lambda>null34 allprojects {
35   // Note that the group name for publishing is "com.squareup" and is declared in gradle.properties. It's set to a
36   // different value here to disambiguate the Maven coordinates of the :interop:javapoet submodule and the JavaPoet
37   // dependency.
38   group = "com.squareup.kotlinpoet"
39   version = property("VERSION_NAME") as String
40 
41   repositories {
42     mavenCentral()
43   }
44 }
45 
<lambda>null46 subprojects {
47   tasks.withType<KotlinCompile> {
48     compilerOptions {
49       jvmTarget.set(JvmTarget.JVM_1_8)
50       freeCompilerArgs.add("-Xjvm-default=all")
51     }
52   }
53   // Ensure "org.gradle.jvm.version" is set to "8" in Gradle metadata.
54   tasks.withType<JavaCompile> {
55     options.release.set(8)
56   }
57 
58   if ("test" !in name && buildFile.exists()) {
59     apply(plugin = "org.jetbrains.dokka")
60     apply(plugin = "com.vanniktech.maven.publish")
61     pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
62       configure<KotlinMultiplatformExtension> {
63         explicitApi()
64       }
65     }
66     pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {
67       configure<KotlinProjectExtension> {
68         explicitApi()
69       }
70     }
71     afterEvaluate {
72       tasks.named<DokkaTask>("dokkaHtml") {
73         val projectFolder = project.path.trim(':').replace(':', '-')
74         outputDirectory.set(rootProject.rootDir.resolve("docs/1.x/$projectFolder"))
75         dokkaSourceSets.configureEach {
76           skipDeprecated.set(true)
77         }
78       }
79     }
80   }
81   apply(plugin = "com.diffplug.spotless")
82   configure<SpotlessExtension> {
83     kotlin {
84       target("**/*.kt")
85       ktlint(libs.versions.ktlint.get()).editorConfigOverride(
86         mapOf("ktlint_standard_filename" to "disabled"),
87       )
88       trimTrailingWhitespace()
89       endWithNewline()
90 
91       licenseHeader(
92         """
93         |/*
94         | * Copyright (C) ${'$'}YEAR Square, Inc.
95         | *
96         | * Licensed under the Apache License, Version 2.0 (the "License");
97         | * you may not use this file except in compliance with the License.
98         | * You may obtain a copy of the License at
99         | *
100         | * https://www.apache.org/licenses/LICENSE-2.0
101         | *
102         | * Unless required by applicable law or agreed to in writing, software
103         | * distributed under the License is distributed on an "AS IS" BASIS,
104         | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
105         | * See the License for the specific language governing permissions and
106         | * limitations under the License.
107         | */
108         """.trimMargin(),
109       )
110     }
111   }
112 
113   // Only enable the extra toolchain tests on CI. Otherwise local development is broken on Apple Silicon macs
114   // because there are no matching toolchains for several older JDK versions.
115   if ("CI" in System.getenv()) {
116     fun Project.setupCheckTask(testTaskName: String) {
117       // Copied from https://github.com/square/retrofit/blob/master/retrofit/build.gradle#L28.
118       // Create a test task for each supported JDK. We check every "LTS" + current version.
119       val versionsToTest = listOf(8, 11, 17, 19)
120       for (majorVersion in versionsToTest) {
121         val jdkTest = tasks.register<Test>("testJdk$majorVersion") {
122           val javaToolchains = project.extensions.getByType(JavaToolchainService::class)
123           javaLauncher.set(
124             javaToolchains.launcherFor {
125               languageVersion.set(JavaLanguageVersion.of(majorVersion))
126               vendor.set(JvmVendorSpec.AZUL)
127             },
128           )
129 
130           description = "Runs the test suite on JDK $majorVersion"
131           group = LifecycleBasePlugin.VERIFICATION_GROUP
132 
133           // Copy inputs from normal Test task.
134           val testTask =
135             tasks.getByName<Test>(testTaskName)
136 
137           classpath = testTask.classpath
138           testClassesDirs = testTask.testClassesDirs
139 
140           testLogging {
141             exceptionFormat = TestExceptionFormat.FULL
142           }
143         }
144         tasks.named("check").configure {
145           dependsOn(jdkTest)
146         }
147       }
148     }
149     pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
150       setupCheckTask("jvmTest")
151     }
152     pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {
153       setupCheckTask("test")
154     }
155   }
156 }
157 
<lambda>null158 apiValidation {
159   nonPublicMarkers += "com.squareup.kotlinpoet.ExperimentalKotlinPoetApi"
160   ignoredProjects += listOf(
161     "interop", // Empty middle package
162     "test-processor", // Test only
163   )
164 }
165