Skip to content

Commit 1da97e3

Browse files
authored
misc: disable cinterop tasks if not buildable (#193)
1 parent b3e111a commit 1da97e3

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

aws-crt-kotlin/build.gradle.kts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import aws.sdk.kotlin.gradle.kmp.configureIosSimulatorTasks
1010
import aws.sdk.kotlin.gradle.kmp.configureKmpTargets
1111
import aws.sdk.kotlin.gradle.util.typedProp
1212
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
13+
import org.jetbrains.kotlin.konan.target.Family
14+
import org.jetbrains.kotlin.konan.target.HostManager
1315

1416
plugins {
1517
alias(libs.plugins.kotlin.multiplatform)
@@ -70,8 +72,7 @@ kotlin {
7072
// see: https://github.com/JetBrains/kotlin-native/issues/2423#issuecomment-466300153
7173
targets.withType<KotlinNativeTarget> {
7274
val knTarget = this
73-
logger.info("configuring Kotlin/Native target $knTarget: ${knTarget.name}")
74-
val cmakeInstallTask = configureCrtCMakeBuild(knTarget, CMakeBuildType.Release)
75+
7576
val targetInstallDir = project.cmakeInstallDir(knTarget)
7677
val headerDir = targetInstallDir.resolve("include")
7778
val libDir = targetInstallDir.resolve("lib")
@@ -85,11 +86,20 @@ kotlin {
8586
compilerOpts("-L${libDir.absolutePath}")
8687
extraOpts("-libraryPath", libDir.absolutePath)
8788
}
88-
89-
// cinterop tasks processes header files which requires the corresponding CMake build/install to run
90-
val cinteropTask = tasks.named(interopSettings.interopProcessingTaskName)
91-
cinteropTask.configure {
92-
dependsOn(cmakeInstallTask)
89+
val interopTaskName = interopSettings.interopProcessingTaskName
90+
91+
if (!knTarget.isBuildableOnHost) {
92+
logger.warn("Kotlin/Native target $knTarget is enabled but not buildable on host ${HostManager.host}, disabling cinterop")
93+
tasks.named(interopTaskName).configure {
94+
onlyIf { false }
95+
}
96+
} else {
97+
logger.info("Configuring Kotlin/Native target $knTarget: ${knTarget.name}")
98+
val cmakeInstallTask = configureCrtCMakeBuild(knTarget, CMakeBuildType.Release)
99+
// cinterop tasks processes header files which requires the corresponding CMake build/install to run
100+
tasks.named(interopTaskName).configure {
101+
dependsOn(cmakeInstallTask)
102+
}
93103
}
94104
}
95105
}
@@ -143,3 +153,18 @@ tasks.withType<AbstractTestTask> {
143153
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
144154
}
145155
}
156+
157+
// Returns whether this target can be built on the current host
158+
private val KotlinNativeTarget.isBuildableOnHost: Boolean
159+
get() = run {
160+
val family = konanTarget.family
161+
return if (HostManager.hostIsMac) {
162+
family in setOf(Family.OSX, Family.IOS, Family.TVOS, Family.WATCHOS)
163+
} else if (HostManager.hostIsLinux) {
164+
family == Family.LINUX
165+
} else if (HostManager.hostIsMingw) {
166+
family == Family.MINGW
167+
} else {
168+
throw Exception("Unsupported host: ${HostManager.host}")
169+
}
170+
}

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[versions]
22
kotlin-version = "2.2.0"
33

4-
aws-kotlin-repo-tools-version = "0.4.47"
4+
aws-kotlin-repo-tools-version = "0.4.48"
55

66
# libs
77
crt-java-version = "0.38.1"

0 commit comments

Comments
 (0)