Skip to content

Commit d11068b

Browse files
ALikhachevSpace Team
authored and
Space Team
committed
[Build] Align Gradle-related modules stdlib with the API version
This change also slightly reworks API and language version configuration. Instead of limiting them for all Kotlin compile tasks in the project, configure them granularly, only for "main" or other published parts. This allows to use fresh Kotlin for tests. Also fixes warnings in test code related to LV bump there. ^KT-61911 Fixed
1 parent 71b1ce1 commit d11068b

File tree

8 files changed

+107
-58
lines changed

8 files changed

+107
-58
lines changed

gradle/verification-metadata.xml

+12-6
Original file line numberDiff line numberDiff line change
@@ -4254,12 +4254,6 @@
42544254
<sha256 value="9dff2645dd3d2f9f0b807f960f0e6b203affa999c02ae5727aaf0a792ba111e8" origin="Generated by Gradle"/>
42554255
</artifact>
42564256
</component>
4257-
<component group="org.jetbrains.kotlin" name="kotlin-serialization-compiler-plugin-embeddable" version="2.1.0">
4258-
<artifact name="kotlin-serialization-compiler-plugin-embeddable-2.1.0.jar">
4259-
<md5 value="2788404aebafd09f657d905bba8d6e5f" origin="Generated by Gradle"/>
4260-
<sha256 value="c6032f2f87a9ae8ae7ad301987be1732c6cacfacb60eb89a77408fe5b2de7fc5" origin="Generated by Gradle"/>
4261-
</artifact>
4262-
</component>
42634257
<component group="org.jetbrains.kotlin" name="kotlin-serialization-compiler-plugin-embeddable" version="2.2.0-dev-12941">
42644258
<artifact name="kotlin-serialization-compiler-plugin-embeddable-2.2.0-dev-12941.jar">
42654259
<md5 value="0bec8191feb7f80f8dac4904af5d9e1f" origin="Generated by Gradle"/>
@@ -4272,6 +4266,12 @@
42724266
<sha256 value="115daea30b0d484afcf2360237b9d9537f48a4a2f03f3cc2a16577dfc6e90342" origin="Generated by Gradle"/>
42734267
</artifact>
42744268
</component>
4269+
<component group="org.jetbrains.kotlin" name="kotlin-stdlib" version="1.7.0">
4270+
<artifact name="kotlin-stdlib-1.7.0.jar">
4271+
<md5 value="400a2d86759907bef5b467fb258eb759" origin="Generated by Gradle"/>
4272+
<sha256 value="aa88e9625577957f3249a46cb6e166ee09b369e600f7a11d148d16b0a6d87f05" origin="Generated by Gradle"/>
4273+
</artifact>
4274+
</component>
42754275
<component group="org.jetbrains.kotlin" name="kotlin-stdlib" version="1.8.20">
42764276
<artifact name="kotlin-stdlib-1.8.20.jar">
42774277
<md5 value="2b3d65e24952649bf7534017c64b435e" origin="Generated by Gradle"/>
@@ -4358,6 +4358,12 @@
43584358
<sha256 value="644a7257c23b51a1fd5068960e40922e3e52c219f11ece3e040a3abc74823f22" origin="Generated by Gradle"/>
43594359
</artifact>
43604360
</component>
4361+
<component group="org.jetbrains.kotlin" name="kotlin-stdlib-common" version="1.7.0">
4362+
<artifact name="kotlin-stdlib-common-1.7.0.jar">
4363+
<md5 value="a0da9b73af82169a86df0c442dce3232" origin="Generated by Gradle"/>
4364+
<sha256 value="59c6ff64fe9a6604afce03e8aaa75f83586c6030ac71fb0b34ee7cdefed3618f" origin="Generated by Gradle"/>
4365+
</artifact>
4366+
</component>
43614367
<component group="org.jetbrains.kotlin" name="kotlin-stdlib-common" version="1.8.20">
43624368
<artifact name="kotlin-stdlib-common-1.8.20.jar">
43634369
<md5 value="af45a8873880f027f47d573e1483a6dc" origin="Generated by Gradle"/>

libraries/tools/kotlin-compose-compiler/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ if (!kotlinBuildProperties.isInJpsBuildIdeaSync) {
5252
dependencies {
5353
implementation(project())
5454
implementation(gradleKotlinDsl())
55+
implementation(project(":compiler:cli-common"))
5556
implementation(platform(libs.junit.bom))
5657
implementation(libs.junit.jupiter.api)
5758
implementation(project(":kotlin-test"))

libraries/tools/kotlin-gradle-plugin/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ if (!kotlinBuildProperties.isInJpsBuildIdeaSync) {
541541
}
542542

543543
functionalTestCompilation.configurations.pluginConfiguration.dependencies.add(
544-
dependencies.create("org.jetbrains.kotlin:kotlin-serialization-compiler-plugin-embeddable:${libs.versions.kotlin.`for`.gradle.plugins.compilation.get()}")
544+
dependencies.create("org.jetbrains.kotlin:kotlin-serialization-compiler-plugin-embeddable")
545545
)
546546
functionalTestCompilation.associateWith(kotlin.target.compilations.getByName(gradlePluginVariantForFunctionalTests.sourceSetName))
547547
functionalTestCompilation.associateWith(kotlin.target.compilations.getByName("common"))

libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/PropertiesBuildServiceTest.kt

-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ class PropertiesBuildServiceTest {
102102
is PropertiesBuildService.NullableStringGradleProperty -> properties.property(property, project).orNull
103103
is PropertiesBuildService.IntGradleProperty -> properties.property(property, project).get()
104104
is PropertiesBuildService.NullableBooleanGradleProperty -> properties.property(property, project).orNull
105-
else -> error("Unexpected property type ${property::class}")
106105
}
107106
)
108107
}

repo/gradle-build-conventions/buildsrc-compat/src/main/kotlin/GradleCommon.kt

+77-37
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.gradle.api.plugins.JavaBasePlugin
2121
import org.gradle.api.plugins.JavaLibraryPlugin
2222
import org.gradle.api.plugins.JavaPlugin
2323
import org.gradle.api.plugins.JavaPluginExtension
24+
import org.gradle.api.provider.Property
2425
import org.gradle.api.provider.Provider
2526
import org.gradle.api.publish.PublishingExtension
2627
import org.gradle.api.publish.maven.MavenPublication
@@ -38,10 +39,10 @@ import org.gradle.plugin.devel.tasks.ValidatePlugins
3839
import org.gradle.plugin.use.resolve.internal.ArtifactRepositoriesPluginResolver.PLUGIN_MARKER_SUFFIX
3940
import org.jetbrains.kotlin.gradle.dsl.KotlinSingleJavaTargetExtension
4041
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
42+
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
4143
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
4244
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
4345
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilerExecutionStrategy
44-
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
4546
import plugins.configureDefaultPublishing
4647
import plugins.configureKotlinPomAttributes
4748

@@ -157,7 +158,7 @@ fun Project.createGradleCommonSourceSet(): SourceSet {
157158
configurations[compileClasspathConfigurationName].extendsFrom(commonGradleApiConfiguration)
158159

159160
dependencies {
160-
compileOnlyConfigurationName(kotlinStdlib())
161+
compileOnlyConfigurationName("org.jetbrains.kotlin:kotlin-stdlib:${GradlePluginVariant.GRADLE_MIN.bundledKotlinVersion}.0")
161162
"commonGradleApiCompileOnly"(gradleApi())
162163
if (this@createGradleCommonSourceSet.name !in testPlugins) {
163164
compileOnlyConfigurationName(project(":kotlin-gradle-plugin-api")) {
@@ -177,11 +178,13 @@ fun Project.createGradleCommonSourceSet(): SourceSet {
177178
}
178179
}
179180
}
180-
181-
// Common outputs will also produce '${project.name}.kotlin_module' file, so we need to avoid
182-
// files clash
183-
tasks.named<KotlinJvmCompile>("compile${commonSourceSet.name.replaceFirstChar { it.uppercase() }}Kotlin") {
181+
val kotlinJvmTarget = (extensions.getByName("kotlin") as KotlinSingleJavaTargetExtension).target
182+
val compilation = kotlinJvmTarget.compilations.getByName(commonSourceSet.name)
183+
tasks.named<KotlinCompile>(compilation.compileKotlinTaskName) {
184+
// Common outputs will also produce '${project.name}.kotlin_module' file, so we need to avoid
185+
// files clash
184186
compilerOptions.moduleName.set("${this@createGradleCommonSourceSet.name}_${commonSourceSet.name}")
187+
configureGradleCompatibility()
185188
}
186189

187190
registerValidatePluginTasks(commonSourceSet)
@@ -305,7 +308,7 @@ fun Project.reconfigureMainSourcesSetForGradlePlugin(
305308
}
306309

307310
dependencies {
308-
"compileOnly"(kotlinStdlib())
311+
"compileOnly"("org.jetbrains.kotlin:kotlin-stdlib:${GradlePluginVariant.GRADLE_MIN}.0")
309312
// Decoupling gradle-api artifact from current project Gradle version. Later would be useful for
310313
// gradle plugin variants
311314
"compileOnly"("dev.gradleplugins:gradle-api:${GradlePluginVariant.GRADLE_MIN.gradleApiVersion}")
@@ -428,14 +431,20 @@ fun Project.reconfigureMainSourcesSetForGradlePlugin(
428431
}
429432
}
430433

434+
val kotlinJvmTarget = (extensions.getByName("kotlin") as KotlinSingleJavaTargetExtension).target
435+
val mainCompilation = kotlinJvmTarget.compilations.getByName(KotlinCompilation.MAIN_COMPILATION_NAME)
436+
tasks.named<KotlinCompile>(mainCompilation.compileKotlinTaskName) {
437+
configureGradleCompatibility()
438+
}
439+
431440
// Fix common sources visibility for tests
432441
sourceSets.named(SourceSet.TEST_SOURCE_SET_NAME) {
433442
compileClasspath += commonSourceSet.output
434443
runtimeClasspath += commonSourceSet.output
435444
}
436445

437446
// Allowing to use 'internal' classes/methods from common source code
438-
(extensions.getByName("kotlin") as KotlinSingleJavaTargetExtension).target.compilations.run {
447+
kotlinJvmTarget.compilations.run {
439448
getByName(SourceSet.TEST_SOURCE_SET_NAME).associateWith(getByName(commonSourceSet.name))
440449
}
441450
}
@@ -505,13 +514,16 @@ fun Project.createGradlePluginVariant(
505514
}
506515
}
507516

508-
// KT-52138: Make module name the same for all variants, so KSP could access internal methods/properties
509-
tasks.named<KotlinJvmCompile>("compile${variantSourceSet.name.replaceFirstChar { it.uppercase() }}Kotlin") {
517+
val kotlinJvmTarget = (extensions.getByName("kotlin") as KotlinSingleJavaTargetExtension).target
518+
val compilation = kotlinJvmTarget.compilations.getByName(variantSourceSet.name)
519+
tasks.named<KotlinCompile>(compilation.compileKotlinTaskName) {
520+
// KT-52138: Make module name the same for all variants, so KSP could access internal methods/properties
510521
compilerOptions.moduleName.set(this@createGradlePluginVariant.name)
522+
configureGradleCompatibility()
511523
}
512524

513525
dependencies {
514-
variantSourceSet.compileOnlyConfigurationName(kotlinStdlib())
526+
variantSourceSet.compileOnlyConfigurationName("org.jetbrains.kotlin:kotlin-stdlib:${GradlePluginVariant.GRADLE_MIN.bundledKotlinVersion}.0")
515527
if (variant == GradlePluginVariant.GRADLE_813) {
516528
// Workaround until 'dev.gradleplugins:gradle-api:8.13' will be published
517529
variantSourceSet.compileOnlyConfigurationName("org.jetbrains.intellij.deps:gradle-api:${variant.gradleApiVersion}")
@@ -546,37 +558,65 @@ private fun Project.commonVariantAttributes(): Action<Configuration> = Action<Co
546558
}
547559
}
548560

549-
fun Project.configureKotlinCompileTasksGradleCompatibility() {
550-
tasks.withType<KotlinCompile>().configureEach {
551-
compilerOptions {
552-
if (!kotlinBuildProperties.isInJpsBuildIdeaSync) {
553-
// check https://docs.gradle.org/current/userguide/compatibility.html#kotlin for Kotlin-Gradle versions matrix
554-
@Suppress("DEPRECATION", "DEPRECATION_ERROR") // we can't use language version greater than 1.8 as our minimal supported Gradle 7.6 embeds Kotlin 1.7.10
555-
languageVersion.set(KotlinVersion.KOTLIN_1_7)
556-
@Suppress("DEPRECATION", "DEPRECATION_ERROR") // we can't use api version greater than 1.7 as our minimal supported Gradle version 7.6 uses kotlin-stdlib 1.7
557-
apiVersion.set(KotlinVersion.KOTLIN_1_7)
558-
}
559-
freeCompilerArgs.addAll(
560-
listOf(
561-
"-Xskip-prerelease-check",
562-
"-Xsuppress-version-warnings",
563-
// We have to override the default value for `-Xsam-conversions` to `class`
564-
// otherwise the compiler would compile lambdas using invokedynamic,
565-
// such lambdas are not serializable so are not compatible with Gradle configuration cache.
566-
// It doesn't lead to a significant difference in binaries sizes, and previously (before LV 1.5) the `class` value was set by default.
567-
"-Xsam-conversions=class",
568-
)
569-
)
561+
/**
562+
* Configures the JVM compile task to produce binaries compatible with [GradlePluginVariant.GRADLE_MIN].
563+
*/
564+
fun KotlinCompile.configureGradleCompatibility() {
565+
configureRunViaKotlinBuildToolsApi()
566+
compilerOptions {
567+
if (!project.kotlinBuildProperties.isInJpsBuildIdeaSync) {
568+
val variant = GradlePluginVariant.GRADLE_MIN
569+
// we should keep control of the language version for compatibility with bundled Kotlin compiler for Gradle Kotlin scripts.
570+
languageVersion.set(KotlinVersion.fromVersion(variant.bundledKotlinVersion))
571+
// we should not use stdlib symbols not available in the bundled Kotlin runtime
572+
apiVersion.set(KotlinVersion.fromVersion(variant.bundledKotlinVersion))
570573
}
574+
freeCompilerArgs.addAll(
575+
listOf(
576+
"-Xskip-prerelease-check",
577+
"-Xsuppress-version-warnings",
578+
// We have to override the default value for `-Xsam-conversions` to `class`
579+
// otherwise the compiler would compile lambdas using invokedynamic,
580+
// such lambdas are not serializable so are not compatible with Gradle configuration cache.
581+
// It doesn't lead to a significant difference in binaries sizes, and previously (before LV 1.5) the `class` value was set by default.
582+
"-Xsam-conversions=class",
583+
)
584+
)
571585
}
572-
configureRunViaKotlinBuildToolsApi()
573586
}
574587

575-
fun Project.configureRunViaKotlinBuildToolsApi() {
576-
project.extra["kotlin.compiler.runViaBuildToolsApi"] = true
577-
tasks.withType<KotlinCompile>().configureEach {
578-
compilerExecutionStrategy.set(KotlinCompilerExecutionStrategy.IN_PROCESS)
588+
/**
589+
* Configures the main JVM compile task in the project to use specific setup for compatibility with [GradlePluginVariant.GRADLE_MIN]
590+
* If you need to configure it for specific tasks, please use [configureGradleCompatibility] and [configureBuildToolsApiVersionForGradleCompatibility].
591+
*/
592+
fun Project.configureKotlinCompileTasksGradleCompatibility() {
593+
tasks.named("compileKotlin", KotlinCompile::class.java) {
594+
configureGradleCompatibility()
579595
}
596+
configureBuildToolsApiVersionForGradleCompatibility()
597+
}
598+
599+
/**
600+
* Configures the task to execute the Kotlin compiler via the Kotlin Build Tools API.
601+
*
602+
* This way, we use an older bootstrap compiler controlled by the `kotlin-for-gradle-plugins-compilation`
603+
* version catalog entry to ensure compatibility with older language versions
604+
*
605+
* It's using reflection to access internal property instead of the `kotlin.compiler.runViaBuildToolsApi` property to avoid using it for test source sets.
606+
* This way, we can the latest AV/LV in test code as well as dependencies built with them.
607+
*/
608+
private fun KotlinCompile.configureRunViaKotlinBuildToolsApi() {
609+
val runViaBuildToolsMethod = this::class.java.getMethod("getRunViaBuildToolsApi\$kotlin_gradle_plugin_common")
610+
@Suppress("UNCHECKED_CAST") val runViaBuildTools = runViaBuildToolsMethod.invoke(this) as Property<Boolean>
611+
runViaBuildTools.set(true)
612+
compilerExecutionStrategy.set(KotlinCompilerExecutionStrategy.IN_PROCESS)
613+
}
614+
615+
/**
616+
* Configures the build tools API version for the project to use Kotlin compiler of the version
617+
* that can produce binaries of [GradlePluginVariant.bundledKotlinVersion] for [GradlePluginVariant.GRADLE_MIN]
618+
*/
619+
fun Project.configureBuildToolsApiVersionForGradleCompatibility() {
580620
afterEvaluate {
581621
val gradlePluginsBuildToolsApiClasspath by rootProject.buildscript.configurations
582622
configurations.findByName("kotlinBuildToolsApiClasspath")?.let {

repo/gradle-build-conventions/buildsrc-compat/src/main/kotlin/gradle-plugin-common-configuration.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ val signPublication = !version.toString().contains("-SNAPSHOT") &&
1717
(project.gradle.startParameter.taskNames.contains("publishPlugins") || signLibraryPublication)
1818

1919
configureCommonPublicationSettingsForGradle(signPublication)
20-
configureKotlinCompileTasksGradleCompatibility()
20+
configureBuildToolsApiVersionForGradleCompatibility()
2121
addBomCheckTask()
2222
extensions.extraProperties["kotlin.stdlib.default.dependency"] = "false"
2323

repo/gradle-build-conventions/buildsrc-compat/src/main/kotlin/gradle-plugin-dependency-configuration.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ plugins {
1414
}
1515

1616
configureCommonPublicationSettingsForGradle(signLibraryPublication)
17-
configureKotlinCompileTasksGradleCompatibility()
17+
configureBuildToolsApiVersionForGradleCompatibility()
1818
addBomCheckTask()
1919
extensions.extraProperties["kotlin.stdlib.default.dependency"] = "false"
2020

repo/gradle-build-conventions/gradle-plugins-common/src/main/kotlin/gradle/GradlePluginVariant.kt

+14-11
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,27 @@ package gradle
77

88
/**
99
* Gradle's plugins common variants.
10+
* See also the compatibility matrix: https://docs.gradle.org/current/userguide/compatibility.html#kotlin
1011
*
11-
* [minimalSupportedGradleVersion] - minimal Gradle version that is supported in this variant
12+
* [minimalSupportedGradleVersion] - Minimal Gradle version that is supported in this variant
1213
* [gradleApiVersion] - Gradle API dependency version. Usually should be the same as [minimalSupportedGradleVersion].
13-
* [gradleApiJavadocUrl] - Gradle URL for the given API. Last enum entry should always point to 'current'.
14+
* [gradleApiJavadocUrl] - Gradle URL for the given API.The last enum entry should always point to 'current'.
15+
* [bundledKotlinVersion] - The version of the bundled Kotlin. Used to control the kotlin-stdlib version and the values of `-api-version`, `-language-version` arguments
1416
*/
1517
enum class GradlePluginVariant(
1618
val sourceSetName: String,
1719
val minimalSupportedGradleVersion: String,
1820
val gradleApiVersion: String,
1921
val gradleApiJavadocUrl: String,
22+
val bundledKotlinVersion: String,
2023
) {
21-
GRADLE_MIN("main", "7.6", "7.6", "https://docs.gradle.org/7.6.1/javadoc/"),
22-
GRADLE_80("gradle80", "8.0", "8.0", "https://docs.gradle.org/8.0.2/javadoc/"),
23-
GRADLE_81("gradle81", "8.1", "8.1", "https://docs.gradle.org/8.1.1/javadoc/"),
24-
GRADLE_82("gradle82", "8.2", "8.2", "https://docs.gradle.org/8.2.1/javadoc/"),
25-
GRADLE_85("gradle85", "8.5", "8.5", "https://docs.gradle.org/8.5/javadoc/"),
26-
GRADLE_86("gradle86", "8.6", "8.6", "https://docs.gradle.org/8.6/javadoc/"),
27-
GRADLE_88("gradle88", "8.8", "8.8", "https://docs.gradle.org/8.8/javadoc/"),
28-
GRADLE_811("gradle811", "8.11", "8.11", "https://docs.gradle.org/8.11/javadoc/"),
29-
GRADLE_813("gradle813", "8.13", "8.13", "https://docs.gradle.org/current/javadoc/"),
24+
GRADLE_MIN("main", "7.6", "7.6", "https://docs.gradle.org/7.6.1/javadoc/", "1.7"),
25+
GRADLE_80("gradle80", "8.0", "8.0", "https://docs.gradle.org/8.0.2/javadoc/", "1.8"),
26+
GRADLE_81("gradle81", "8.1", "8.1", "https://docs.gradle.org/8.1.1/javadoc/", "1.8"),
27+
GRADLE_82("gradle82", "8.2", "8.2", "https://docs.gradle.org/8.2.1/javadoc/", "1.8"),
28+
GRADLE_85("gradle85", "8.5", "8.5", "https://docs.gradle.org/8.5/javadoc/", "1.9"),
29+
GRADLE_86("gradle86", "8.6", "8.6", "https://docs.gradle.org/8.6/javadoc/", "1.9"),
30+
GRADLE_88("gradle88", "8.8", "8.8", "https://docs.gradle.org/8.8/javadoc/", "1.9"),
31+
GRADLE_811("gradle811", "8.11", "8.11", "https://docs.gradle.org/8.11/javadoc/", "2.0"),
32+
GRADLE_813("gradle813", "8.13", "8.13", "https://docs.gradle.org/current/javadoc/", "2.0"),
3033
}

0 commit comments

Comments
 (0)