|
5 | 5 | import aws.sdk.kotlin.gradle.dsl.configureLinting |
6 | 6 | import aws.sdk.kotlin.gradle.dsl.configureNexus |
7 | 7 | import aws.sdk.kotlin.gradle.util.typedProp |
| 8 | +import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension |
| 9 | +import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget |
| 10 | +import org.jetbrains.kotlin.konan.target.Family |
| 11 | +import org.jetbrains.kotlin.konan.target.HostManager |
| 12 | +import kotlin.apply |
8 | 13 |
|
9 | 14 | buildscript { |
10 | 15 | // NOTE: buildscript classpath for the root project is the parent classloader for the subprojects, we |
@@ -154,3 +159,67 @@ apiValidation { |
154 | 159 | ), |
155 | 160 | ) |
156 | 161 | } |
| 162 | + |
| 163 | +val disableCrossCompile = typedProp<Boolean>("aws.kotlin.native.disableCrossCompile") == true |
| 164 | + |
| 165 | +if (disableCrossCompile) { |
| 166 | + logger.warn("aws.kotlin.native.disableCrossCompile=true: Cross compilation is disabled.") |
| 167 | + disableCrossCompileTargets() |
| 168 | +} |
| 169 | + |
| 170 | +private val KotlinNativeTarget.isLinux: Boolean |
| 171 | + get() = konanTarget.family == Family.LINUX |
| 172 | + |
| 173 | +private val KotlinNativeTarget.isApple: Boolean |
| 174 | + get() = konanTarget.family.isAppleFamily |
| 175 | + |
| 176 | +private val KotlinNativeTarget.isWindows: Boolean |
| 177 | + get() = konanTarget.family == Family.MINGW |
| 178 | + |
| 179 | +/** |
| 180 | + * Kotlin/Native Linux and Windows targets are generally enabled on all hosts since |
| 181 | + * the Kotlin toolchain and backend compilers support cross compilation. We |
| 182 | + * are using cinterop and have to compile CRT for those platforms which sometimes |
| 183 | + * requires using docker which isn't always available in CI or setup in users environment. |
| 184 | + * |
| 185 | + * See [KT-30498](https://youtrack.jetbrains.com/issue/KT-30498) |
| 186 | + */ |
| 187 | +fun Project.disableCrossCompileTargets() { |
| 188 | + plugins.withId("org.jetbrains.kotlin.multiplatform") { |
| 189 | + configure<KotlinMultiplatformExtension> { |
| 190 | + targets.withType<KotlinNativeTarget> { |
| 191 | + val knTarget = this |
| 192 | + when { |
| 193 | + HostManager.hostIsMac && (knTarget.isLinux || knTarget.isWindows) -> disable(knTarget) |
| 194 | + HostManager.hostIsLinux && knTarget.isApple -> disable(knTarget) |
| 195 | + HostManager.hostIsMingw && (knTarget.isLinux || knTarget.isApple) -> disable(knTarget) |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +internal fun Project.disable(knTarget: KotlinNativeTarget) { |
| 203 | + logger.warn("disabling Kotlin/Native target: ${knTarget.name}") |
| 204 | + knTarget.apply { |
| 205 | + compilations.all { |
| 206 | + cinterops.all { |
| 207 | + tasks.named(interopProcessingTaskName).configure { enabled = false } |
| 208 | + } |
| 209 | + compileTaskProvider.configure { enabled = false } |
| 210 | + } |
| 211 | + |
| 212 | + binaries.all { |
| 213 | + linkTaskProvider.configure { enabled = false } |
| 214 | + } |
| 215 | + |
| 216 | + mavenPublication { |
| 217 | + tasks.withType<AbstractPublishToMaven>().configureEach { |
| 218 | + onlyIf { publication != this@mavenPublication } |
| 219 | + } |
| 220 | + tasks.withType<GenerateModuleMetadata>().configureEach { |
| 221 | + onlyIf { publication != this@mavenPublication } |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments