Fix adding associated compilation for Kotlin Native.

Kotlin native does not support 'compileOnly' or 'runtimeOnly'
configurations. In this case associated compilations are added as
'implementation'.

^KT-45911 Fixed
This commit is contained in:
Yahor Berdnikau
2021-04-19 12:00:01 +02:00
parent 57c4f79c1c
commit 9e9450caa2
5 changed files with 117 additions and 22 deletions
@@ -4,6 +4,7 @@
*/ */
package org.jetbrains.kotlin.gradle package org.jetbrains.kotlin.gradle
import org.gradle.api.logging.LogLevel
import org.jetbrains.kotlin.gradle.native.GeneralNativeIT.Companion.checkNativeCommandLineArguments import org.jetbrains.kotlin.gradle.native.GeneralNativeIT.Companion.checkNativeCommandLineArguments
import org.jetbrains.kotlin.gradle.native.GeneralNativeIT.Companion.containsSequentially import org.jetbrains.kotlin.gradle.native.GeneralNativeIT.Companion.containsSequentially
import org.gradle.api.logging.configuration.WarningMode import org.gradle.api.logging.configuration.WarningMode
@@ -693,7 +694,8 @@ class NewMultiplatformIT : BaseGradleIT() {
} }
@Test @Test
fun testLanguageSettingsClosureForKotlinDsl() = with(transformNativeTestProjectWithPluginDsl("sample-lib-gradle-kotlin-dsl", gradleVersion, "new-mpp-lib-and-app")) { fun testLanguageSettingsClosureForKotlinDsl() =
with(transformNativeTestProjectWithPluginDsl("sample-lib-gradle-kotlin-dsl", gradleVersion, "new-mpp-lib-and-app")) {
gradleBuildScript().appendText( gradleBuildScript().appendText(
"\n" + """ "\n" + """
kotlin.sourceSets.all { kotlin.sourceSets.all {
@@ -1712,4 +1714,55 @@ class NewMultiplatformIT : BaseGradleIT() {
assertEquals("org.sample.one:foo", jsManifest[KLIB_PROPERTY_UNIQUE_NAME]) assertEquals("org.sample.one:foo", jsManifest[KLIB_PROPERTY_UNIQUE_NAME])
} }
} }
@Test
fun testNativeCompilationShouldNotProduceAnyWarningsForAssociatedCompilations() {
with(Project("native-common-dependencies-warning", minLogLevel = LogLevel.INFO)) {
setupWorkingDir()
build("help") {
assertSuccessful()
assertNotContains("A compileOnly dependency is used in the Kotlin/Native target '${detectNativeEnabledCompilation()}':")
}
}
}
@Test
fun testNativeCompilationShouldProduceWarningOnCompileOnlyCommonDependency() {
with(Project("native-common-dependencies-warning", minLogLevel = LogLevel.INFO)) {
setupWorkingDir()
gradleBuildScript().modify {
it.replaceFirst("//compileOnly:", "")
}
build("help") {
assertSuccessful()
assertContains("A compileOnly dependency is used in the Kotlin/Native target '${detectNativeEnabledCompilation()}':")
}
}
}
@Test
fun testNativeCompilationCompileOnlyDependencyWarningCouldBeDisabled() {
with(Project("native-common-dependencies-warning", minLogLevel = LogLevel.INFO)) {
setupWorkingDir()
gradleBuildScript().modify {
it.replaceFirst("//compileOnly:", "")
}
projectDir.resolve("gradle.properties").writeText(
"""
kotlin.native.ignoreIncorrectDependencies = true
""".trimIndent()
)
build("help") {
assertSuccessful()
assertNotContains("A compileOnly dependency is used in the Kotlin/Native target '${detectNativeEnabledCompilation()}':")
}
}
}
private fun detectNativeEnabledCompilation(): String = when {
HostManager.hostIsLinux -> "linuxX64"
HostManager.hostIsMingw -> "mingwX64"
HostManager.hostIsMac -> "macosX64"
else -> throw AssertionError("Host ${HostManager.host} is not supported for this test")
}
} }
@@ -0,0 +1,18 @@
plugins {
kotlin("multiplatform")
}
repositories {
mavenLocal()
mavenCentral()
}
kotlin {
linuxX64()
mingwX64()
macosX64()
}
dependencies {
//compileOnly: commonMainCompileOnly("org.jetbrains.kotlin:kotlin-stdlib")
}
@@ -0,0 +1,11 @@
pluginManagement {
repositories {
mavenLocal()
mavenCentral()
gradlePluginPortal()
}
plugins {
id("org.jetbrains.kotlin.multiplatform") version "${extra["kotlin_version"]}"
}
}
@@ -254,7 +254,6 @@ abstract class AbstractKotlinCompilation<T : KotlinCommonOptions>(
protected open fun addAssociateCompilationDependencies(other: KotlinCompilation<*>) { protected open fun addAssociateCompilationDependencies(other: KotlinCompilation<*>) {
with(target.project) { with(target.project) {
dependencies.add( dependencies.add(
compileOnlyConfigurationName, compileOnlyConfigurationName,
project.files(Callable { other.output.classesDirs }) project.files(Callable { other.output.classesDirs })
@@ -104,6 +104,20 @@ class KotlinNativeCompilation(
val binariesTaskName: String val binariesTaskName: String
get() = lowerCamelCaseName(target.disambiguationClassifier, compilationName, "binaries") get() = lowerCamelCaseName(target.disambiguationClassifier, compilationName, "binaries")
override fun addAssociateCompilationDependencies(other: KotlinCompilation<*>) {
with(target.project) {
// Kotlin native does not support either 'compileOnly' or 'runtimeOnly' configurations
dependencies.add(
implementationConfigurationName,
project.files({ other.output.classesDirs })
)
configurations.named(implementationConfigurationName).configure {
it.extendsFrom(configurations.findByName(other.implementationConfigurationName))
}
}
}
} }
class KotlinSharedNativeCompilation(override val target: KotlinMetadataTarget, val konanTargets: List<KonanTarget>, name: String) : class KotlinSharedNativeCompilation(override val target: KotlinMetadataTarget, val konanTargets: List<KonanTarget>, name: String) :