Skip to content

Commit 7ea7607

Browse files
committed
Disable cinterop tasks if not buildable
1 parent 84d3843 commit 7ea7607

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

aws-crt-kotlin/build.gradle.kts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import aws.sdk.kotlin.gradle.dsl.configurePublishing
99
import aws.sdk.kotlin.gradle.kmp.configureIosSimulatorTasks
1010
import aws.sdk.kotlin.gradle.kmp.configureKmpTargets
1111
import aws.sdk.kotlin.gradle.util.typedProp
12+
import org.jetbrains.kotlin.gradle.plugin.mpp.DefaultCInteropSettings
1213
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
14+
import org.jetbrains.kotlin.konan.target.Family
15+
import org.jetbrains.kotlin.konan.target.HostManager
1316

1417
plugins {
1518
alias(libs.plugins.kotlin.multiplatform)
@@ -70,8 +73,7 @@ kotlin {
7073
// see: https://github.com/JetBrains/kotlin-native/issues/2423#issuecomment-466300153
7174
targets.withType<KotlinNativeTarget> {
7275
val knTarget = this
73-
logger.info("configuring Kotlin/Native target $knTarget: ${knTarget.name}")
74-
val cmakeInstallTask = configureCrtCMakeBuild(knTarget, CMakeBuildType.Release)
76+
7577
val targetInstallDir = project.cmakeInstallDir(knTarget)
7678
val headerDir = targetInstallDir.resolve("include")
7779
val libDir = targetInstallDir.resolve("lib")
@@ -85,11 +87,20 @@ kotlin {
8587
compilerOpts("-L${libDir.absolutePath}")
8688
extraOpts("-libraryPath", libDir.absolutePath)
8789
}
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)
90+
val interopTaskName = interopSettings.interopProcessingTaskName
91+
92+
if (!knTarget.isBuildableOnHost) {
93+
logger.warn("Kotlin/Native target $knTarget is enabled but not buildable on host ${HostManager.host}, disabling cinterop")
94+
tasks.named(interopTaskName).configure {
95+
onlyIf { false }
96+
}
97+
} else {
98+
logger.info("Configuring Kotlin/Native target $knTarget: ${knTarget.name}")
99+
val cmakeInstallTask = configureCrtCMakeBuild(knTarget, CMakeBuildType.Release)
100+
// cinterop tasks processes header files which requires the corresponding CMake build/install to run
101+
tasks.named(interopTaskName).configure {
102+
dependsOn(cmakeInstallTask)
103+
}
93104
}
94105
}
95106
}
@@ -100,20 +111,6 @@ configureIosSimulatorTasks()
100111
// Publishing
101112
configurePublishing("aws-crt-kotlin")
102113

103-
val linuxTargets: List<String> = listOf(
104-
"linuxX64",
105-
"linuxArm64",
106-
)
107-
108-
// create a summary task that compiles all cross platform test binaries
109-
tasks.register("linuxTestBinaries") {
110-
linuxTargets.map {
111-
tasks.named("${it}TestBinaries")
112-
}.forEach { testTask ->
113-
dependsOn(testTask)
114-
}
115-
}
116-
117114
// run tests on specific JVM version
118115
val testJavaVersion = typedProp<String>("test.java.version")?.let {
119116
JavaLanguageVersion.of(it)
@@ -143,3 +140,18 @@ tasks.withType<AbstractTestTask> {
143140
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
144141
}
145142
}
143+
144+
// Returns whether this target can be built on the current host
145+
private val KotlinNativeTarget.isBuildableOnHost: Boolean
146+
get() = run {
147+
val family = konanTarget.family
148+
return if (HostManager.hostIsMac) {
149+
family in setOf(Family.OSX, Family.IOS, Family.TVOS, Family.WATCHOS)
150+
} else if (HostManager.hostIsLinux) {
151+
family == Family.LINUX
152+
} else if (HostManager.hostIsMingw) {
153+
family == Family.MINGW
154+
} else {
155+
throw Exception("Unsupported host: ${HostManager.host}")
156+
}
157+
}

0 commit comments

Comments
 (0)