From 78cc2365b0563588ab2f032fac952233496aa68c Mon Sep 17 00:00:00 2001 From: Yahor Berdnikau Date: Wed, 6 Apr 2022 16:08:53 +0200 Subject: [PATCH] Fix gradle api dependency version in variants Common SourceSet was forcing gradle api version to be the same in all plugin variants via 'extendsFrom(..)'. ^KT-49227 In Progress --- buildSrc/src/main/kotlin/GradleCommon.kt | 57 ++++++++++++++----- buildSrc/src/main/kotlin/artifacts.kt | 8 ++- .../build.gradle.kts | 6 +- .../org/jetbrains/kotlin/gradle/UpToDateIT.kt | 6 +- native/commonizer-api/build.gradle.kts | 6 +- 5 files changed, 52 insertions(+), 31 deletions(-) diff --git a/buildSrc/src/main/kotlin/GradleCommon.kt b/buildSrc/src/main/kotlin/GradleCommon.kt index 1c1cf17d6f1..aaae54cec9e 100644 --- a/buildSrc/src/main/kotlin/GradleCommon.kt +++ b/buildSrc/src/main/kotlin/GradleCommon.kt @@ -23,7 +23,7 @@ import org.gradle.kotlin.dsl.* import org.gradle.plugin.devel.plugins.JavaGradlePluginPlugin import org.jetbrains.dokka.DokkaVersion import org.jetbrains.dokka.gradle.DokkaTask -import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet +import org.jetbrains.kotlin.gradle.dsl.KotlinSingleTargetExtension import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import org.jetbrains.kotlin.project.model.KotlinPlatformTypeAttribute import plugins.configureDefaultPublishing @@ -95,9 +95,17 @@ fun Project.createGradleCommonSourceSet(): SourceSet { val commonSourceSet = sourceSets.create("common") { excludeGradleCommonDependencies(this) + // Adding Gradle API to separate configuration, so version will not leak into variants + val commonGradleApiConfiguration = configurations.create("commonGradleApiCompileOnly") { + isVisible = false + isCanBeConsumed = false + isCanBeResolved = true + } + configurations[compileClasspathConfigurationName].extendsFrom(commonGradleApiConfiguration) + dependencies { compileOnlyConfigurationName(kotlinStdlib()) - compileOnlyConfigurationName("dev.gradleplugins:gradle-api:7.2") + "commonGradleApiCompileOnly"("dev.gradleplugins:gradle-api:7.2") if (this@createGradleCommonSourceSet.name != "kotlin-gradle-plugin-api" && this@createGradleCommonSourceSet.name != "android-test-fixes" ) { @@ -190,11 +198,22 @@ fun Project.wireGradleVariantToCommonGradleVariant( ) { wireSourceSet.compileClasspath += commonSourceSet.output wireSourceSet.runtimeClasspath += commonSourceSet.output - @Suppress("deprecation") // Needs support from KGP - wireSourceSet.withConvention(KotlinSourceSet::class) { - val wireKotlinSourceSet = this - commonSourceSet.withConvention(KotlinSourceSet::class) { - wireKotlinSourceSet.dependsOn(this) + + // Allowing to use 'internal' classes/methods from common source code + (extensions.getByName("kotlin") as KotlinSingleTargetExtension).target.compilations.run { + getByName(wireSourceSet.name).associateWith(getByName(commonSourceSet.name)) + } + + // Common outputs will also produce '${project.name}.kotlin_module' file, so we need to avoid + // files clash + val compileTaskName = if (wireSourceSet.name == SourceSet.MAIN_SOURCE_SET_NAME) { + "compileKotlin" + } else { + "compile${wireSourceSet.name.capitalize()}Kotlin" + } + tasks.named(compileTaskName) { + kotlinOptions { + moduleName = "${this@wireGradleVariantToCommonGradleVariant.name}_${wireSourceSet.name}" } } @@ -251,14 +270,11 @@ fun Project.reconfigureMainSourcesSetForGradlePlugin( excludeGradleCommonDependencies(this) wireGradleVariantToCommonGradleVariant(this, commonSourceSet) - tasks.withType().configureEach { - if (name == jarTaskName) { - setupPublicJar(archiveBaseName.get()) - addEmbeddedRuntime() - } else if (name == sourcesJarTaskName) { - addEmbeddedSources() - } - } + // https://youtrack.jetbrains.com/issue/KT-51913 + configurations["default"].attributes.attribute( + TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE, + objects.named(TargetJvmEnvironment::class, "no-op") + ) plugins.withType().configureEach { this@reconfigureMainSourcesSetForGradlePlugin @@ -295,6 +311,17 @@ fun Project.reconfigureMainSourcesSetForGradlePlugin( } } } + + // Fix common sources visibility for tests + sourceSets.named(SourceSet.TEST_SOURCE_SET_NAME) { + compileClasspath += commonSourceSet.output + runtimeClasspath += commonSourceSet.output + } + + // Allowing to use 'internal' classes/methods from common source code + (extensions.getByName("kotlin") as KotlinSingleTargetExtension).target.compilations.run { + getByName(SourceSet.TEST_SOURCE_SET_NAME).associateWith(getByName(commonSourceSet.name)) + } } /** diff --git a/buildSrc/src/main/kotlin/artifacts.kt b/buildSrc/src/main/kotlin/artifacts.kt index ca91bda235e..41c943856aa 100644 --- a/buildSrc/src/main/kotlin/artifacts.kt +++ b/buildSrc/src/main/kotlin/artifacts.kt @@ -71,7 +71,13 @@ fun Jar.addEmbeddedRuntime() { project.configurations.findByName("embedded")?.let { embedded -> dependsOn(embedded) from { - embedded.map(project::zipTree) + embedded.map { + if (it.extension.equals("jar", ignoreCase = true)) { + project.zipTree(it) + } else { + it + } + } } } } diff --git a/libraries/tools/kotlin-gradle-plugin-idea/build.gradle.kts b/libraries/tools/kotlin-gradle-plugin-idea/build.gradle.kts index 3dfb03e5b33..b4bbea23001 100644 --- a/libraries/tools/kotlin-gradle-plugin-idea/build.gradle.kts +++ b/libraries/tools/kotlin-gradle-plugin-idea/build.gradle.kts @@ -14,11 +14,7 @@ dependencies { implementation(kotlinStdlib()) testImplementation(gradleApi()) testImplementation(gradleKotlinDsl()) - testImplementation(project(":kotlin-gradle-plugin")) { - capabilities { - requireCapability("org.jetbrains.kotlin:kotlin-gradle-plugin-common") - } - } + testImplementation(project(":kotlin-gradle-plugin")) testImplementation(project(":kotlin-gradle-statistics")) testImplementation(project(":kotlin-test:kotlin-test-junit")) diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/UpToDateIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/UpToDateIT.kt index 511206c6cbc..d0e5da71c74 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/UpToDateIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/UpToDateIT.kt @@ -99,11 +99,7 @@ class UpToDateIT : KGPBaseTest() { val originalPaths get() = originalCompilerCp.map { it.replace("\\", "/") }.joinToString(", ") { "'$it'" } override fun initProject(project: TestProject) = with(project) { - val pluginSuffix = if (project.gradleVersion < GradleVersion.version("7.0")) { - "kotlin_gradle_plugin" - } else { - "kotlin_gradle_plugin_gradle70" - } + val pluginSuffix = "kotlin_gradle_plugin" buildGradle.appendText( "\nafterEvaluate { println 'compiler_cp=' + compileKotlin.getDefaultCompilerClasspath\$$pluginSuffix().toList() }" ) diff --git a/native/commonizer-api/build.gradle.kts b/native/commonizer-api/build.gradle.kts index c5df9c162f9..b692443d14b 100644 --- a/native/commonizer-api/build.gradle.kts +++ b/native/commonizer-api/build.gradle.kts @@ -17,11 +17,7 @@ dependencies { testImplementation(commonDependency("junit:junit")) testImplementation(projectTests(":compiler:tests-common")) testRuntimeOnly(project(":native:kotlin-klib-commonizer")) - testImplementation(project(":kotlin-gradle-plugin")) { - capabilities { - requireCapability("org.jetbrains.kotlin:kotlin-gradle-plugin-common") - } - } + testImplementation(project(":kotlin-gradle-plugin")) testImplementation(project(":kotlin-gradle-statistics")) testImplementation(project(":kotlin-gradle-plugin-model")) testImplementation(gradleApi())