[Gradle Native] Made definitionFile Optional for cinterop task

^KT-62800 Fixed
This commit is contained in:
Dmitrii Krasnov
2023-12-13 17:52:21 +01:00
committed by Space Team
parent 013b5e3780
commit cef46a9dd1
7 changed files with 107 additions and 5 deletions
@@ -9,6 +9,8 @@ import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.testbase.*
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.condition.OS
import kotlin.io.path.createDirectories
import kotlin.io.path.createFile
@NativeGradlePluginTests
class CinteropIT : KGPBaseTest() {
@@ -75,4 +77,65 @@ class CinteropIT : KGPBaseTest() {
}
}
}
@DisplayName("KT-62800: passing header to cinterop instead of passing def file")
@GradleTest
fun cinteropWithExplicitPassingHeader(gradleVersion: GradleVersion) {
nativeProject("cinterop-with-header", gradleVersion = gradleVersion) {
val dummyHeaderPath = projectPath.resolve("libs").resolve("include").resolve("dummy.h").toFile().canonicalPath
build(":assemble") {
assertTasksExecuted(":cinteropCinteropNative")
extractNativeTasksCommandLineArgumentsFromOutput(":cinteropCinteropNative", toolName = NativeToolKind.C_INTEROP) {
assertCommandLineArgumentsContainSequentially("-header", dummyHeaderPath)
assertCommandLineArgumentsContainSequentially("-Ilibs/include")
assertCommandLineArgumentsContainSequentially("-pkg", "cinterop")
assertCommandLineArgumentsDoNotContain("-def")
}
}
}
}
@DisplayName("KT-62800: check that optional .def file in cinterop works well with configuration cache")
@GradleTestVersions(minVersion = TestVersions.Gradle.G_8_1) // Gradle supports checking file existence with configuration cache only since 8.1 version
@GradleTest
fun cinteropWithOptionalDefFileAndConfigurationCache(gradleVersion: GradleVersion) {
nativeProject(
"cinterop-with-header",
gradleVersion = gradleVersion,
buildOptions = defaultBuildOptions.copy(
configurationCache = true
)
) {
val dummyHeaderPath = projectPath.resolve("libs").resolve("include").resolve("dummy.h").toFile().canonicalPath
// first build with non-existing .def file and configuration cache enabled
build(":assemble") {
assertTasksExecuted(":cinteropCinteropNative")
extractNativeTasksCommandLineArgumentsFromOutput(":cinteropCinteropNative", toolName = NativeToolKind.C_INTEROP) {
assertCommandLineArgumentsContainSequentially("-header", dummyHeaderPath)
assertCommandLineArgumentsContainSequentially("-Ilibs/include")
assertCommandLineArgumentsContainSequentially("-pkg", "cinterop")
assertCommandLineArgumentsDoNotContain("-def")
}
}
// adding cinterop.def to default path
val cinteropDefFile = projectPath.resolve("src")
.resolve("nativeInterop")
.resolve("cinterop")
.createDirectories()
.resolve("cinterop.def")
.createFile()
// second build with existing .def file and configuration cache enabled
build(":assemble") {
assertTasksExecuted(":cinteropCinteropNative")
extractNativeTasksCommandLineArgumentsFromOutput(":cinteropCinteropNative", toolName = NativeToolKind.C_INTEROP) {
assertCommandLineArgumentsContainSequentially("-header", dummyHeaderPath)
assertCommandLineArgumentsContainSequentially("-Ilibs/include")
assertCommandLineArgumentsContainSequentially("-pkg", "cinterop")
assertCommandLineArgumentsContainSequentially("-def", cinteropDefFile.toFile().canonicalPath)
}
}
}
}
}
@@ -31,7 +31,7 @@ class NativeIncrementalCompilationIT : KGPBaseTest() {
)
@DisplayName("KT-63742: Check that kotlinNativeLink task passes all required args for cache orchestration and ic")
@GradleTestVersions(minVersion = TestVersions.Gradle.G_7_4) // DefaultResolvedComponentResult is supported after 7.4 only
@GradleTestVersions(minVersion = TestVersions.Gradle.G_7_4) // DefaultResolvedComponentResult with configuration cache is supported only after 7.4
@GradleTest
fun checkArgumentsForIncrementalCache(gradleVersion: GradleVersion) {
nativeProject("native-incremental-simple", gradleVersion) {
@@ -0,0 +1,25 @@
plugins {
kotlin("multiplatform")
}
repositories {
mavenCentral()
mavenLocal()
}
kotlin {
<SingleNativeTarget>("native") {
binaries {
executable()
}
compilations.getByName("main") {
cinterops {
val cinterop by creating {
headers("libs/include/dummy.h")
compilerOpts.add("-Ilibs/include")
packageName("cinterop")
}
}
}
}
}
@@ -0,0 +1,8 @@
import cinterop.dummyFunction
@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
fun nativeMainUsingCInterop() = dummyFunction()
fun main() {
}
@@ -27,7 +27,7 @@ import java.io.File
import javax.inject.Inject
abstract class DefaultCInteropSettings @Inject internal constructor(
private val params: Params
private val params: Params,
) : CInteropSettings {
internal data class Params(
@@ -35,7 +35,7 @@ abstract class DefaultCInteropSettings @Inject internal constructor(
val identifier: CInteropIdentifier,
val dependencyConfigurationName: String,
val interopProcessingTaskName: String,
val services: Services
val services: Services,
) {
open class Services @Inject constructor(
val providerFactory: ProviderFactory,
@@ -74,9 +74,11 @@ abstract class DefaultCInteropSettings @Inject internal constructor(
@Deprecated("Deprecated. Please, use definitionFile.", ReplaceWith("definitionFile"))
val defFileProperty: Property<File> = params.services.objectFactory.property<File>().convention(
params.services.projectLayout.projectDirectory.file("src/nativeInterop/cinterop/$name.def").asFile
getDefaultCinteropDefinitionFile().takeIf { it.exists() }
)
private fun getDefaultCinteropDefinitionFile(): File = params.services.projectLayout.projectDirectory.file("src/nativeInterop/cinterop/$name.def").asFile
val definitionFile: RegularFileProperty = params.services.objectFactory.fileProperty().convention(
params.services.projectLayout.file(defFileProperty)
)
@@ -1097,6 +1097,7 @@ abstract class CInteropProcess @Inject internal constructor(params: Params) :
@get:InputFile
@get:PathSensitive(PathSensitivity.RELATIVE)
@get:NormalizeLineEndings
@get:Optional
abstract val definitionFile: RegularFileProperty
@get:Internal
@@ -1158,7 +1159,9 @@ abstract class CInteropProcess @Inject internal constructor(params: Params) :
addArg("-o", outputFile.absolutePath)
addArgIfNotNull("-target", konanTarget.visibleName)
addArgIfNotNull("-def", definitionFile.getFile().canonicalPath)
if (definitionFile.isPresent) {
addArgIfNotNull("-def", definitionFile.getFile().canonicalPath)
}
addArgIfNotNull("-pkg", packageName)
addFileArgs("-header", headers)