diff --git a/build-plugins/kmp-conventions/src/main/kotlin/aws/sdk/kotlin/gradle/kmp/ConfigureIosSimulator.kt b/build-plugins/kmp-conventions/src/main/kotlin/aws/sdk/kotlin/gradle/kmp/ConfigureIosSimulator.kt new file mode 100644 index 0000000..f7be606 --- /dev/null +++ b/build-plugins/kmp-conventions/src/main/kotlin/aws/sdk/kotlin/gradle/kmp/ConfigureIosSimulator.kt @@ -0,0 +1,60 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ +package aws.sdk.kotlin.gradle.kmp + +import org.gradle.api.Project +import org.gradle.api.tasks.Exec +import org.gradle.kotlin.dsl.withType +import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeSimulatorTest +import org.jetbrains.kotlin.konan.target.HostManager + +/** + * Disables standalone mode in simulator tests since it causes issues with TLS. + * This means we need to manage the simulator state ourselves (booting, shutting down). + * https://youtrack.jetbrains.com/issue/KT-38317 + */ +public fun Project.configureIosSimulatorTasks() { + val simulatorDeviceName = project.findProperty("iosSimulatorDevice") as? String ?: "iPhone 15" + + val xcrun = "/usr/bin/xcrun" + + tasks.register("bootIosSimulatorDevice", Exec::class.java) { + isIgnoreExitValue = true + commandLine(xcrun, "simctl", "boot", simulatorDeviceName) + + doLast { + val result = executionResult.get() + val code = result.exitValue + if (code != 148 && code != 149) { // ignore "simulator already running" errors + result.assertNormalExitValue() + } + } + } + + tasks.register("shutdownIosSimulatorDevice", Exec::class.java) { + isIgnoreExitValue = true + mustRunAfter(tasks.withType()) + commandLine(xcrun, "simctl", "shutdown", simulatorDeviceName) + + doLast { + val result = executionResult.get() + if (result.exitValue != 405) { // ignore "simulator already shutdown" errors + result.assertNormalExitValue() + } + } + } + + tasks.withType().configureEach { + if (!HostManager.hostIsMac) { + return@configureEach + } + + dependsOn("bootIosSimulatorDevice") + finalizedBy("shutdownIosSimulatorDevice") + + standalone.set(false) + device.set(simulatorDeviceName) + } +} diff --git a/build-plugins/kmp-conventions/src/main/kotlin/aws/sdk/kotlin/gradle/kmp/ConfigureTargets.kt b/build-plugins/kmp-conventions/src/main/kotlin/aws/sdk/kotlin/gradle/kmp/ConfigureTargets.kt index 97cc037..2ed42ed 100644 --- a/build-plugins/kmp-conventions/src/main/kotlin/aws/sdk/kotlin/gradle/kmp/ConfigureTargets.kt +++ b/build-plugins/kmp-conventions/src/main/kotlin/aws/sdk/kotlin/gradle/kmp/ConfigureTargets.kt @@ -246,7 +246,6 @@ private val KotlinNativeTarget.isApple: Boolean private val KotlinNativeTarget.isWindows: Boolean get() = konanTarget.family == Family.MINGW - internal fun Project.disable(knTarget: KotlinNativeTarget) { logger.warn("disabling Kotlin/Native target: ${knTarget.name}") knTarget.apply {