|
| 1 | +import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension |
| 2 | +import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension |
| 3 | +import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension |
| 4 | +import org.jetbrains.kotlin.gradle.plugin.KotlinTarget |
| 5 | +import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget |
| 6 | +import org.jetbrains.kotlin.konan.target.Family |
| 7 | +import org.jetbrains.kotlin.konan.target.HostManager |
| 8 | +import util.buildHost |
| 9 | + |
| 10 | +plugins { |
| 11 | + id("convention.common") |
| 12 | +} |
| 13 | + |
| 14 | +pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") { |
| 15 | + extensions.getByType(KotlinMultiplatformExtension::class.java).targets.let(::control) |
| 16 | +} |
| 17 | +pluginManager.withPlugin("org.jetbrains.kotlin.jvm") { |
| 18 | + objects.namedDomainObjectList(KotlinTarget::class.java).apply { |
| 19 | + add(extensions.getByType(KotlinJvmProjectExtension::class.java).target) |
| 20 | + }.let(::control) |
| 21 | +} |
| 22 | +pluginManager.withPlugin("org.jetbrains.kotlin.js") { |
| 23 | + objects.namedDomainObjectList(KotlinTarget::class.java).apply { |
| 24 | + add(extensions.getByType(KotlinJsProjectExtension::class.java).js()) |
| 25 | + }.let(::control) |
| 26 | +} |
| 27 | + |
| 28 | +fun control(targets: NamedDomainObjectCollection<KotlinTarget>) { |
| 29 | + fun NamedDomainObjectCollection<out KotlinTarget>.onlyBuildIf(enabled: Spec<in Task>) { |
| 30 | + all { |
| 31 | + if (this is KotlinNativeTarget) { |
| 32 | + binaries.all { |
| 33 | + linkTask.onlyIf(enabled) |
| 34 | + } |
| 35 | + } |
| 36 | + compilations.all { |
| 37 | + compileTaskProvider { |
| 38 | + onlyIf(enabled) |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + val nativeTargets = targets.withType<KotlinNativeTarget>() |
| 45 | + val windowsHostTargets = nativeTargets.matching { it.konanTarget.buildHost == Family.MINGW } |
| 46 | + val linuxHostTargets = nativeTargets.matching { it.konanTarget.buildHost == Family.LINUX } |
| 47 | + val osxHostTargets = nativeTargets.matching { it.konanTarget.buildHost == Family.OSX } |
| 48 | + val mainHostTargets = targets.matching { it !in nativeTargets } |
| 49 | + linuxHostTargets.onlyBuildIf { |
| 50 | + val enabled = HostManager.hostIsLinux |
| 51 | + printlnCI("[${it.name}] ${!CI} || $SANDBOX || ${HostManager.hostIsLinux} = $enabled") |
| 52 | + enabled |
| 53 | + } |
| 54 | + osxHostTargets.onlyBuildIf { |
| 55 | + val enabled = HostManager.hostIsMac |
| 56 | + printlnCI("[${it.name}] ${!CI} || $SANDBOX || ${HostManager.hostIsMac} = $enabled") |
| 57 | + enabled |
| 58 | + } |
| 59 | + windowsHostTargets.onlyBuildIf { |
| 60 | + val enabled = HostManager.hostIsMingw |
| 61 | + printlnCI("[${it.name}] ${!CI} || $SANDBOX || ${HostManager.hostIsMingw} = $enabled") |
| 62 | + enabled |
| 63 | + } |
| 64 | + mainHostTargets.onlyBuildIf { |
| 65 | + val enabled = !CI || SANDBOX || isMainHost |
| 66 | + printlnCI("[${it.name}] ${!CI} || $SANDBOX || $isMainHost = $enabled") |
| 67 | + enabled |
| 68 | + } |
| 69 | +} |
0 commit comments