From 143d864b98cb69dc16f83574c9b6b6275d20e44e Mon Sep 17 00:00:00 2001 From: Tadeas Kriz Date: Mon, 13 May 2024 16:03:13 -0400 Subject: [PATCH] Add test for XCFrameworks using the old DSL. --- .../skie/test/base/BaseGradleTests.kt | 21 +++++- ...naryDsl_Framework_AllDarwinPresetsTests.kt | 4 ++ ...inBinaryDsl_Framework_SinglePresetTests.kt | 4 ++ ...inBinaryDsl_Framework_SingleTargetTests.kt | 4 ++ ...BinaryDsl_XCFramework_SingleTargetTests.kt | 66 +++++++++++++++++++ .../test/trait/gradle/BuildGradleBuilder.kt | 19 ++++-- .../test/resources/templates/basic/main.swift | 4 +- 7 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/basic/KotlinBinaryDsl_XCFramework_SingleTargetTests.kt diff --git a/test-runner/src/test/kotlin/co/touchlab/skie/test/base/BaseGradleTests.kt b/test-runner/src/test/kotlin/co/touchlab/skie/test/base/BaseGradleTests.kt index 6dd69565..31e9f2fd 100644 --- a/test-runner/src/test/kotlin/co/touchlab/skie/test/base/BaseGradleTests.kt +++ b/test-runner/src/test/kotlin/co/touchlab/skie/test/base/BaseGradleTests.kt @@ -108,6 +108,8 @@ abstract class BaseGradleTests: TestUtilsTrait, GradleBuildFileBuilderTrait { this += "-parse-as-library" // Workaround for missing symbol when compiling with Coroutines for MacosArm64 this += listOf("-Xlinker", "-dead_strip") + // Add the framework parent path as `rpath` so it gets loaded correctly when run + this += listOf("-Xlinker", "-rpath", "-Xlinker", frameworkParentPath) } val result = command.joinToString(" ").execute(testProjectDir) @@ -120,8 +122,23 @@ abstract class BaseGradleTests: TestUtilsTrait, GradleBuildFileBuilderTrait { return result } - fun runSwift() { - // TODO: Implement running binaries + fun runSwift( + assertResult: ((CommandResult) -> Unit)? = { + assertEquals(0, it.exitCode) + }, + ): CommandResult { + val command = buildList { + this += "./swift_executable" + } + + val result = command.joinToString(" ").execute(testProjectDir) + if (!isCI) { + println(result.stdOut) + } + + assertResult?.invoke(result) + + return result } fun commonMain(fqdn: String): File { diff --git a/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/basic/KotlinBinaryDsl_Framework_AllDarwinPresetsTests.kt b/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/basic/KotlinBinaryDsl_Framework_AllDarwinPresetsTests.kt index 21bbdfd0..701cd168 100644 --- a/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/basic/KotlinBinaryDsl_Framework_AllDarwinPresetsTests.kt +++ b/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/basic/KotlinBinaryDsl_Framework_AllDarwinPresetsTests.kt @@ -34,6 +34,10 @@ class KotlinBinaryDsl_Framework_AllDarwinPresetsTests: BaseGradleTests() { KotlinTarget.Native.Darwin.targets.forEach { target -> buildSwift(target, Templates.basic, builtFrameworkParentDir(target, buildConfiguration, isArtifactDsl = false)) + + if (target is KotlinTarget.Native.MacOS) { + runSwift() + } } } } diff --git a/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/basic/KotlinBinaryDsl_Framework_SinglePresetTests.kt b/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/basic/KotlinBinaryDsl_Framework_SinglePresetTests.kt index 94d929a3..ac11d58f 100644 --- a/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/basic/KotlinBinaryDsl_Framework_SinglePresetTests.kt +++ b/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/basic/KotlinBinaryDsl_Framework_SinglePresetTests.kt @@ -35,6 +35,10 @@ class KotlinBinaryDsl_Framework_SinglePresetTests: BaseGradleTests() { preset.targets.forEach { target -> buildSwift(target, Templates.basic, builtFrameworkParentDir(target, buildConfiguration, isArtifactDsl = false)) + + if (target is KotlinTarget.Native.MacOS) { + runSwift() + } } } } diff --git a/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/basic/KotlinBinaryDsl_Framework_SingleTargetTests.kt b/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/basic/KotlinBinaryDsl_Framework_SingleTargetTests.kt index c23837cb..e316b79d 100644 --- a/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/basic/KotlinBinaryDsl_Framework_SingleTargetTests.kt +++ b/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/basic/KotlinBinaryDsl_Framework_SingleTargetTests.kt @@ -36,5 +36,9 @@ class KotlinBinaryDsl_Framework_SingleTargetTests: BaseGradleTests() { runGradle() buildSwift(target, Templates.basic, builtFrameworkParentDir(target, buildConfiguration, isArtifactDsl = false)) + + if (target is KotlinTarget.Native.MacOS) { + runSwift() + } } } diff --git a/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/basic/KotlinBinaryDsl_XCFramework_SingleTargetTests.kt b/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/basic/KotlinBinaryDsl_XCFramework_SingleTargetTests.kt new file mode 100644 index 00000000..cd3f8977 --- /dev/null +++ b/test-runner/src/test/kotlin/co/touchlab/skie/test/suite/gradle/basic/KotlinBinaryDsl_XCFramework_SingleTargetTests.kt @@ -0,0 +1,66 @@ +package co.touchlab.skie.test.suite.gradle.basic + +import co.touchlab.skie.test.annotation.MatrixTest +import co.touchlab.skie.test.annotation.filter.Smoke +import co.touchlab.skie.test.annotation.type.GradleTests +import co.touchlab.skie.test.base.BaseGradleTests +import co.touchlab.skie.test.runner.BuildConfiguration +import co.touchlab.skie.test.template.Templates +import co.touchlab.skie.test.util.KotlinTarget +import co.touchlab.skie.test.util.KotlinVersion +import co.touchlab.skie.test.util.LinkMode +import org.gradle.testkit.runner.TaskOutcome +import kotlin.test.assertEquals + +@Suppress("ClassName") +@Smoke +@GradleTests +class KotlinBinaryDsl_XCFramework_SingleTargetTests: BaseGradleTests() { + @MatrixTest + fun `single target`( + kotlinVersion: KotlinVersion, + target: KotlinTarget.Native.Darwin, + buildConfiguration: BuildConfiguration, + linkMode: LinkMode, + ) { + rootBuildFile(kotlinVersion) { + kotlin { + target(target) + + registerNativeFrameworks(kotlinVersion, buildConfiguration, linkMode, includeXcframework = true) + } + + workaroundFatFrameworkConfigurationIfNeeded(kotlinVersion) + } + + copyToCommonMain(Templates.basic) + + runGradle( + arguments = arrayOf("assembleXCFramework"), + assertResult = { result -> + assertEquals(TaskOutcome.SUCCESS, result.task(":assembleXCFramework")?.outcome) + }, + ) + + val frameworkParentName = when (target) { + KotlinTarget.Native.Ios.SimulatorArm64 -> "ios-arm64-simulator" + KotlinTarget.Native.Ios.X64 -> "ios-x86_64-simulator" + KotlinTarget.Native.Ios.Arm64 -> "ios-arm64" + KotlinTarget.Native.Tvos.SimulatorArm64 -> "tvos-arm64-simulator" + KotlinTarget.Native.Tvos.X64 -> "tvos-x86_64-simulator" + KotlinTarget.Native.Tvos.Arm64 -> "tvos-arm64" + KotlinTarget.Native.MacOS.Arm64 -> "macos-arm64" + KotlinTarget.Native.MacOS.X64 -> "macos-x86_64" + } + + buildSwift( + target, + Templates.basic, + frameworkParentPath = "build/XCFrameworks/${buildConfiguration.name.lowercase()}/gradle_test.xcframework/$frameworkParentName", + ) + + if (target is KotlinTarget.Native.MacOS) { + runSwift() + } + } +} diff --git a/test-runner/src/test/kotlin/co/touchlab/skie/test/trait/gradle/BuildGradleBuilder.kt b/test-runner/src/test/kotlin/co/touchlab/skie/test/trait/gradle/BuildGradleBuilder.kt index f55412ec..980fb2e3 100644 --- a/test-runner/src/test/kotlin/co/touchlab/skie/test/trait/gradle/BuildGradleBuilder.kt +++ b/test-runner/src/test/kotlin/co/touchlab/skie/test/trait/gradle/BuildGradleBuilder.kt @@ -48,10 +48,12 @@ class BuildGradleBuilder( appendLines(""" tasks.withType().configureEach { - configurations.getByName( - name.substringAfter("link").replaceFirstChar { it.lowercase() } - ).attributes { - attribute(Attribute.of("fat-framework", String::class.java), "true") + if (name.startsWith("link")) { + configurations.getByName( + name.substringAfter("link").replaceFirstChar { it.lowercase() } + ).attributes { + attribute(Attribute.of("fat-framework", String::class.java), "true") + } } } """.trimIndent()) @@ -76,6 +78,7 @@ class BuildGradleBuilder( builderIndentationLevel += 1 block() builderIndentationLevel -= 1 + appendIndentation() builder.appendLine("}") } @@ -141,9 +144,14 @@ class BuildGradleBuilder( kotlinVersion: KotlinVersion, buildConfiguration: BuildConfiguration, linkMode: LinkMode, + includeXcframework: Boolean = false, ) { imports.add("org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget") imports.add("org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType") + if (includeXcframework) { + imports.add("org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework") + +"val xcframework = XCFramework()" + } "targets.withType" { "binaries" { @@ -153,6 +161,9 @@ class BuildGradleBuilder( if (kotlinVersion.needsOldLinker) { +"""linkerOpts += "-ld64"""" } + if (includeXcframework) { + +"xcframework.add(this)" + } } } } diff --git a/test-runner/src/test/resources/templates/basic/main.swift b/test-runner/src/test/resources/templates/basic/main.swift index cd35a66f..2bf7392f 100644 --- a/test-runner/src/test/resources/templates/basic/main.swift +++ b/test-runner/src/test/resources/templates/basic/main.swift @@ -4,6 +4,7 @@ import gradle_test @main struct Main { static func main() async throws { + print("Running Swift code") let enumValues: [BasicEnum] = [.a, .b, .c] for (index, value) in enumValues.enumerated() { switch value { @@ -16,7 +17,8 @@ struct Main { } } - // TODO: Add the rest + // TODO: Add assertions for the rest of SKIE features + print("Swift code completed successfully") } }