Skip to content

Commit b2e9104

Browse files
authored
Add handling of the project property that allows to disable cross-compilation (#1096)
without removing targets from module file. This is needed to reduce amount of time wasted during release process (linux targets are built and uploaded three times, because they're available on all hosts)
1 parent 174be44 commit b2e9104

File tree

1 file changed

+71
-6
lines changed

1 file changed

+71
-6
lines changed

gradle/native-targets.gradle

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,78 @@
22
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
/**
6+
* Specifies what subset of Native targets to build
7+
*
8+
* ALL — all possible for compilation
9+
* HOST — host-specific ones, without cross compilation (e.g. do not build linuxX64 on macOS host)
10+
* SINGLE — only for current OS (to import into IDEA / quickly run tests)
11+
* DISABLED — disable all Native targets (useful with Kotlin compiler built from sources)
12+
*
13+
* For HOST mode, all targets are still listed in .module file, so HOST mode is useful for release process.
14+
*/
15+
enum NativeState { ALL, HOST, SINGLE, DISABLED }
16+
17+
def getNativeState(String description) {
18+
if (description == null) return NativeState.SINGLE
19+
switch(description.toLowerCase()) {
20+
case 'all':
21+
case 'true':
22+
return NativeState.ALL
23+
case 'host':
24+
return NativeState.HOST
25+
case 'disabled':
26+
return NativeState.DISABLED
27+
// 'single', 'false', etc
28+
default:
29+
return NativeState.SINGLE
30+
}
31+
}
32+
533
project.ext.ideaActive = System.getProperty('idea.active') == 'true'
6-
project.ext.deployMode = property('native.deploy') == 'true'
7-
project.ext.singleTargetMode = project.ext.ideaActive || !project.ext.deployMode
34+
project.ext.nativeState = getNativeState(property('native.deploy'))
35+
project.ext.singleTargetMode = project.ext.ideaActive || (project.ext.nativeState == NativeState.SINGLE)
836

937
project.ext.nativeMainSets = []
1038
project.ext.nativeTestSets = []
1139

40+
/**
41+
* Disables compilation but leaves the target in .module file
42+
*/
43+
def disableCompilation(targets) {
44+
configure(targets) {
45+
compilations.all {
46+
cinterops.all { project.tasks[interopProcessingTaskName].enabled = false }
47+
compileKotlinTask.enabled = false
48+
}
49+
binaries.all { linkTask.enabled = false }
50+
51+
mavenPublication { publicationToDisable ->
52+
tasks.withType(AbstractPublishToMaven).all {
53+
onlyIf { publication != publicationToDisable }
54+
}
55+
tasks.withType(GenerateModuleMetadata).all {
56+
onlyIf { publication.get() != publicationToDisable }
57+
}
58+
}
59+
}
60+
}
61+
62+
def getHostName() {
63+
def target = System.getProperty("os.name")
64+
if (target == 'Linux') return 'linux'
65+
if (target.startsWith('Windows')) return 'windows'
66+
if (target.startsWith('Mac')) return 'macos'
67+
return 'unknown'
68+
}
69+
1270
kotlin {
1371
targets {
1472
def manager = project.ext.hostManager
1573
def linuxEnabled = manager.isEnabled(presets.linuxX64.konanTarget)
1674
def macosEnabled = manager.isEnabled(presets.macosX64.konanTarget)
1775
def winEnabled = manager.isEnabled(presets.mingwX64.konanTarget)
1876

19-
project.ext.isLinuxHost = linuxEnabled
20-
project.ext.isMacosHost = macosEnabled
21-
project.ext.isWinHost = winEnabled
22-
2377
def ideaPreset = presets.linuxX64
2478
if (macosEnabled) ideaPreset = presets.macosX64
2579
if (winEnabled) ideaPreset = presets.mingwX64
@@ -33,6 +87,8 @@ kotlin {
3387
}
3488

3589
targets {
90+
if (project.ext.nativeState == NativeState.DISABLED) return
91+
3692
if (project.ext.singleTargetMode) {
3793
fromPreset(project.ext.ideaPreset, 'native')
3894
} else {
@@ -59,8 +115,17 @@ kotlin {
59115
addTarget(presets.mingwX64)
60116
addTarget(presets.mingwX86)
61117
}
118+
119+
if (project.ext.nativeState == NativeState.HOST) {
120+
// linux targets that can cross-compile on all three hosts
121+
def linuxCrossCompileTargets = [linuxX64, linuxArm32Hfp, linuxArm64]
122+
if (getHostName() != "linux") {
123+
disableCompilation(linuxCrossCompileTargets)
124+
}
125+
}
62126
}
63127

128+
64129
sourceSets {
65130
nativeMain { dependsOn commonMain }
66131
// Empty source set is required in order to have native tests task

0 commit comments

Comments
 (0)