Skip to content

Commit

Permalink
Don't add common compiler plugin artifacts to native targets
Browse files Browse the repository at this point in the history
Make native plugin configuration be non-transitive
  • Loading branch information
Anton Lakotka authored and TeamCityServer committed Mar 12, 2021
1 parent f339748 commit d023f09
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import kotlin.test.*

internal class CompilationSpecificPluginPath {
@Test
Expand Down Expand Up @@ -59,8 +56,8 @@ internal class CompilationSpecificPluginPath {
project.evaluate()

// Then each plugin classpath should have its own dependency
assertEquals(listOf("kotlin-scripting-compiler-embeddable", "jvmOnly"), project.subplugins("desktop"))
assertEquals(listOf("kotlin-scripting-compiler-embeddable", "jsOnly"), project.subplugins("web"))
assertEquals(listOf("jvmOnly"), project.subplugins("desktop"))
assertEquals(listOf("jsOnly"), project.subplugins("web"))

// And each compilation task should have its own plugin classpath
val compileDesktop = project.tasks.getByName("compileKotlinDesktop") as KotlinCompile
Expand All @@ -69,12 +66,17 @@ internal class CompilationSpecificPluginPath {
}

@Test
fun `native artifact should take precedence over regular artifact for native platform`() {
// Given common plugin but with native-specific artifact
class NativeSpecificPlugin : FakeSubPlugin("common", "native", { true })
fun `only native artifact should be taken for native platforms`() {
// Given plugin but with native-specific artifact
class NativeSpecificPlugin : FakeSubPlugin("common1", "native", { true })

// And plugin without native artifact but applicable on all platforms
class RegularPluginWithoutNativeArtifact : FakeSubPlugin("common2", null, { true })

// When applying these plugins
val project = buildProjectWithMPP {
plugins.apply(NativeSpecificPlugin::class.java)
plugins.apply(RegularPluginWithoutNativeArtifact::class.java)

kotlin {
jvm()
Expand All @@ -83,9 +85,27 @@ internal class CompilationSpecificPluginPath {
}
project.evaluate()

assertTrue("common" in project.subplugins("jvm"))
assertTrue("native" in project.subplugins("linuxX64"))
assertTrue("common" !in project.subplugins("linuxX64"))
// Expect jvm target have both artifacts
assertEquals(listOf("common1", "common2"), project.subplugins("jvm"))

// And native target should have only NativeSpecificPlugin artifacts
assertEquals(listOf("native"), project.subplugins("linuxX64"))
}

@Test
fun `native plugin configuration should not be transitive`() {
val project = buildProjectWithMPP {
kotlin {
jvm()
linuxX64()
}
}

val nativeConfig = project
.configurations
.getByName(pluginClassPathConfiguration("linuxX64", "main"))

assertFalse(nativeConfig.isTransitive)
}

@Test
Expand Down Expand Up @@ -144,6 +164,7 @@ internal class CompilationSpecificPluginPath {
.getByName(pluginClassPathConfiguration(target, compilation))
.allDependencies
.map { it.name }
.minus("kotlin-scripting-compiler-embeddable")


private fun buildProjectWithMPP(code: Project.() -> Unit): ProjectInternal {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ abstract class AbstractKotlinTargetConfigurator<KotlinTargetType : KotlinTarget>
val pluginConfiguration = configurations.maybeCreate(compilation.pluginConfigurationName).apply {
if (target.platformType == KotlinPlatformType.native) {
extendsFrom(configurations.getByName(NATIVE_COMPILER_PLUGIN_CLASSPATH_CONFIGURATION_NAME))
isTransitive = false
} else {
extendsFrom(target.project.commonKotlinPluginClasspath)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ class SubpluginEnvironment(
val pluginId = subplugin.getCompilerPluginId()
project.logger.kotlinDebug { "Loading subplugin $pluginId" }

val nativeArtifact = subplugin.getPluginArtifactForNative()
if (nativeArtifact != null && kotlinCompilation.platformType == KotlinPlatformType.native) {
project.addMavenDependency(kotlinCompilation.pluginConfigurationName, nativeArtifact)

if (kotlinCompilation.platformType == KotlinPlatformType.native) {
subplugin.getPluginArtifactForNative()?.let { artifact ->
project.addMavenDependency(kotlinCompilation.pluginConfigurationName, artifact)
}
} else {
project.addMavenDependency(kotlinCompilation.pluginConfigurationName, subplugin.getPluginArtifact())
}
Expand Down

0 comments on commit d023f09

Please sign in to comment.