Fix syncing languageSettings for KotlinNativeLink task

Task registration may happen within another configuration call leading
to prohibited 'afterEvaluate {}' block error. Fixed it by moving out
sync to separate configuration call.

^KT-54113 Fixed
This commit is contained in:
Yahor Berdnikau
2022-09-21 16:42:01 +02:00
committed by Space
parent d7ef5efa6a
commit ad44ee8904
4 changed files with 64 additions and 12 deletions
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.native
import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.testbase.*
import org.junit.jupiter.api.DisplayName
@DisplayName("KotlinNativeLink task tests")
@NativeGradlePluginTests
internal class KotlinNativeLinkIT : KGPBaseTest() {
@DisplayName("KT-54113: afterEvaluate to sync languageSettings should run out of configuration methods scope")
@GradleTest
fun shouldSyncLanguageSettingsSafely(gradleVersion: GradleVersion) {
nativeProject("native-link-simple", gradleVersion) {
build("tasks")
}
}
}
@@ -0,0 +1,16 @@
plugins {
id 'org.jetbrains.kotlin.multiplatform'
}
repositories {
mavenLocal()
mavenCentral()
}
kotlin {
<SingleNativeTarget>("host")
targets.named("host").configure {
binaries.test("integrationTests")
}
}
@@ -0,0 +1,8 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
fun main() {
println("Hello, Kotlin/Native!")
}
@@ -73,18 +73,6 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget> : AbstractKotl
)
}
runOnceAfterEvaluated("Sync language settings for NativeLinkTask") {
result.configure {
// We propagate compilation free args to the link task for now (see KT-33717).
val defaultLanguageSettings = binary.compilation.languageSettings as? DefaultLanguageSettingsBuilder
if (defaultLanguageSettings != null) {
it.toolOptions.freeCompilerArgs.addAll(
defaultLanguageSettings.freeCompilerArgs
)
}
}
}
if (binary !is TestExecutable) {
tasks.named(binary.compilation.target.artifactsTaskName).dependsOn(result)
@@ -96,6 +84,18 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget> : AbstractKotl
}
}
private fun Project.syncLanguageSettingsToLinkTask(binary: NativeBinary) {
tasks.named(binary.linkTaskName, KotlinNativeLink::class.java).configure {
// We propagate compilation free args to the link task for now (see KT-33717).
val defaultLanguageSettings = binary.compilation.languageSettings as? DefaultLanguageSettingsBuilder
if (defaultLanguageSettings != null) {
it.toolOptions.freeCompilerArgs.addAll(
defaultLanguageSettings.freeCompilerArgs
)
}
}
}
private fun Project.createFrameworkArtifact(
binary: Framework,
linkTask: TaskProvider<KotlinNativeLink>
@@ -317,6 +317,11 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget> : AbstractKotl
target.binaries.all {
project.createLinkTask(it)
}
project.runOnceAfterEvaluated("Sync language settings for NativeLinkTask") {
target.binaries.all { binary ->
project.syncLanguageSettingsToLinkTask(binary)
}
}
target.binaries.withType(Executable::class.java).all {
project.createRunTask(it)