Skip to content

Commit a8fee4d

Browse files
committed
Don't override compilation freeCompilerArgs in link task
Calling '.addAll()' was replacing convention value leading for inability to add/modify Kotlin/Native link task freeCompilerArgs via KotlinCompilation.options.freeCompilerArgs. For now convention was replaced by normal value. ^KT-56280 Fixed
1 parent e2b4317 commit a8fee4d

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/native/KotlinNativeLinkIT.kt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.gradle.native
88
import org.gradle.util.GradleVersion
99
import org.jetbrains.kotlin.gradle.testbase.*
1010
import org.junit.jupiter.api.DisplayName
11+
import kotlin.io.path.appendText
1112

1213
@DisplayName("KotlinNativeLink task tests")
1314
@NativeGradlePluginTests
@@ -20,4 +21,50 @@ internal class KotlinNativeLinkIT : KGPBaseTest() {
2021
build("tasks")
2122
}
2223
}
24+
25+
@DisplayName("KT-56280: should propagate freeCompilerArgs from compilation")
26+
@GradleTest
27+
fun shouldUseCompilationFreeCompilerArgs(gradleVersion: GradleVersion) {
28+
nativeProject("native-link-simple", gradleVersion) {
29+
buildGradle.appendText(
30+
"""
31+
|
32+
|kotlin {
33+
| targets.named("host").configure {
34+
| binaries.executable()
35+
| }
36+
|
37+
| targets.withType(org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget.class) {
38+
| compilations.main.kotlinOptions {
39+
| freeCompilerArgs += ["-e", "main"]
40+
| }
41+
| }
42+
|}
43+
""".trimMargin()
44+
)
45+
46+
build("linkReleaseExecutableHost") {
47+
val linkTaskOutput = output
48+
.substringAfter("Task :linkReleaseExecutableHost")
49+
.substringBefore("Task :linkHost")
50+
assert(linkTaskOutput.isNotEmpty()) {
51+
"Could not get :linkReleaseExecutableHost task output!"
52+
}
53+
54+
val args = linkTaskOutput
55+
.substringAfterLast("Transformed arguments = [")
56+
.substringBefore("]")
57+
.lines()
58+
.map { it.trim() }
59+
assert(
60+
args.isNotEmpty() &&
61+
args.contains("-e") &&
62+
args.contains("main")
63+
) {
64+
printBuildOutput()
65+
"Link task arguments does not contain '-e main'!"
66+
}
67+
}
68+
}
69+
}
2370
}

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/KotlinNativeTargetConfigurator.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget> : AbstractKotl
7070
val konanPropertiesBuildService = KonanPropertiesBuildService.registerIfAbsent(project.gradle)
7171
it.konanPropertiesService.set(konanPropertiesBuildService)
7272
it.usesService(konanPropertiesBuildService)
73-
it.toolOptions.freeCompilerArgs.convention(
74-
compilationCompilerOptions.options.freeCompilerArgs
75-
)
73+
it.toolOptions.freeCompilerArgs.value(compilationCompilerOptions.options.freeCompilerArgs)
74+
it.toolOptions.freeCompilerArgs.addAll(providers.provider { PropertiesProvider(project).nativeLinkArgs })
7675
}
7776

7877

@@ -90,7 +89,7 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget> : AbstractKotl
9089
tasks.named(binary.linkTaskName, KotlinNativeLink::class.java).configure {
9190
// We propagate compilation free args to the link task for now (see KT-33717).
9291
val defaultLanguageSettings = binary.compilation.languageSettings as? DefaultLanguageSettingsBuilder
93-
if (defaultLanguageSettings != null) {
92+
if (defaultLanguageSettings != null && defaultLanguageSettings.freeCompilerArgs.isNotEmpty()) {
9493
it.toolOptions.freeCompilerArgs.addAll(
9594
defaultLanguageSettings.freeCompilerArgs
9695
)

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeLink.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ constructor(
5757

5858
final override val toolOptions: KotlinCommonCompilerToolOptions = objectFactory
5959
.newInstance<KotlinCommonCompilerToolOptionsDefault>()
60-
.apply {
61-
freeCompilerArgs.addAll(PropertiesProvider(project).nativeLinkArgs)
62-
}
6360

6461
init {
6562
@Suppress("DEPRECATION")

0 commit comments

Comments
 (0)