KT-50106 Add gradle property with global K/N linker flags

This commit is contained in:
konstantin.tskhovrebov
2021-12-07 18:05:37 +01:00
committed by teamcity
parent ff85d54c93
commit 003cb156bf
6 changed files with 79 additions and 2 deletions
@@ -7,8 +7,11 @@ package org.jetbrains.kotlin.gradle.native
import org.jetbrains.kotlin.gradle.BaseGradleIT
import org.jetbrains.kotlin.gradle.GradleVersionRequired
import org.jetbrains.kotlin.gradle.native.GeneralNativeIT.Companion.withNativeCommandLineArguments
import org.jetbrains.kotlin.konan.target.HostManager
import org.junit.Assume
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import kotlin.test.Test
class NativeLibraryDslIT : BaseGradleIT() {
@@ -70,6 +73,36 @@ class NativeLibraryDslIT : BaseGradleIT() {
assertTasksNotExecuted(
":lib:compileKotlinLinuxX64"
)
withNativeCommandLineArguments(":shared:assembleMylibDebugSharedLibraryLinuxX64") {
assertFalse(it.contains("-Xfoo=bar"))
assertFalse(it.contains("-Xbaz=qux"))
assertTrue(it.contains("-Xmen=pool"))
}
assertFileExists("/shared/build/out/dynamic/linux_x64/debug/libmylib.so")
assertFileExists("/shared/build/out/dynamic/linux_x64/debug/libmylib_api.h")
}
}
}
@Test
fun `link shared library from single gradle module with additional link args`() {
with(Project("new-kn-library-dsl")) {
setupWorkingDir()
gradleProperties().appendText("\nkotlin.native.linkArgs=-Xfoo=bar -Xbaz=qux")
build(":shared:assembleMylibDebugSharedLibraryLinuxX64") {
assertSuccessful()
assertTasksExecuted(
":shared:compileKotlinLinuxX64",
":shared:assembleMylibDebugSharedLibraryLinuxX64"
)
assertTasksNotExecuted(
":lib:compileKotlinLinuxX64"
)
withNativeCommandLineArguments(":shared:assembleMylibDebugSharedLibraryLinuxX64") {
assertTrue(it.contains("-Xfoo=bar"))
assertTrue(it.contains("-Xbaz=qux"))
assertTrue(it.contains("-Xmen=pool"))
}
assertFileExists("/shared/build/out/dynamic/linux_x64/debug/libmylib.so")
assertFileExists("/shared/build/out/dynamic/linux_x64/debug/libmylib_api.h")
}
@@ -158,6 +158,40 @@ class NativePlatformLibsIT : BaseGradleIT() {
}
}
@Test
fun testLinkerArgsViaGradleProperties() {
with(Project("native-platform-libraries")) {
setupWorkingDir()
gradleProperties().apply {
configureJvmMemory()
appendText("\nkotlin.native.linkArgs=-Xfoo=bar -Xbaz=qux")
}
gradleBuildScript().modify(::transformBuildScriptWithPluginsDsl)
gradleBuildScript().appendText("""
kotlin.linuxX64() {
binaries.sharedLib {
freeCompilerArgs += "-Xmen=pool"
}
}
""".trimIndent())
build("linkDebugSharedLinuxX64") {
assertSuccessful()
assertTasksExecuted(
":compileKotlinLinuxX64",
":linkDebugSharedLinuxX64"
)
assertContains("-Xfoo=bar")
assertContains("-Xbaz=qux")
assertContains("-Xmen=pool")
assertContains("w: Flag is not supported by this version of the compiler: -Xfoo=bar")
assertContains("w: Flag is not supported by this version of the compiler: -Xbaz=qux")
assertContains("w: Flag is not supported by this version of the compiler: -Xmen=pool")
assertFileExists("/build/bin/linuxX64/debugShared/libnative_platform_libraries.so")
assertFileExists("/build/bin/linuxX64/debugShared/libnative_platform_libraries_api.h")
}
}
}
@Test
fun testNoGenerationForUnsupportedHost() {
deleteInstalledCompilers()
@@ -12,6 +12,9 @@ kotlin {
kotlinArtifacts {
Native.Library("mylib") {
target = linuxX64
kotlinOptions {
freeCompilerArgs += "-Xmen=pool"
}
}
Native.Library("myslib") {
target = linuxX64
@@ -292,6 +292,12 @@ internal class PropertiesProvider private constructor(private val project: Proje
val nativeJvmArgs: String?
get() = propertyWithDeprecatedVariant("kotlin.native.jvmArgs", "org.jetbrains.kotlin.native.jvmArgs")
/**
* Allows a user to specify free compiler arguments for K/N linker.
*/
val nativeLinkArgs: List<String>
get() = property("kotlin.native.linkArgs").orEmpty().split(' ').filterNot { it.isBlank() }
/**
* Forces to run a compilation in a separate JVM.
*/
@@ -520,7 +520,8 @@ constructor(
override val additionalCompilerOptions: Provider<Collection<String>> = project.provider {
kotlinOptions.freeCompilerArgs +
compilation.kotlinOptions.freeCompilerArgs +
((languageSettings as? DefaultLanguageSettingsBuilder)?.freeCompilerArgs ?: emptyList())
((languageSettings as? DefaultLanguageSettingsBuilder)?.freeCompilerArgs ?: emptyList()) +
PropertiesProvider(project).nativeLinkArgs
}
override val kotlinOptions: KotlinCommonToolOptions = NativeLinkOptions()
@@ -111,7 +111,7 @@ open class KotlinNativeLinkArtifactTask @Inject constructor(
override var allWarningsAsErrors: Boolean = false
override var suppressWarnings: Boolean = false
override var verbose: Boolean = false
override var freeCompilerArgs: List<String> = listOf()
override var freeCompilerArgs: List<String> = PropertiesProvider(project).nativeLinkArgs
}
fun kotlinOptions(fn: KotlinCommonToolOptions.() -> Unit) {