2
2
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
3
*/
4
4
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
+
5
33
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 )
8
36
9
37
project. ext. nativeMainSets = []
10
38
project. ext. nativeTestSets = []
11
39
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
+
12
70
kotlin {
13
71
targets {
14
72
def manager = project. ext. hostManager
15
73
def linuxEnabled = manager. isEnabled(presets. linuxX64. konanTarget)
16
74
def macosEnabled = manager. isEnabled(presets. macosX64. konanTarget)
17
75
def winEnabled = manager. isEnabled(presets. mingwX64. konanTarget)
18
76
19
- project. ext. isLinuxHost = linuxEnabled
20
- project. ext. isMacosHost = macosEnabled
21
- project. ext. isWinHost = winEnabled
22
-
23
77
def ideaPreset = presets. linuxX64
24
78
if (macosEnabled) ideaPreset = presets. macosX64
25
79
if (winEnabled) ideaPreset = presets. mingwX64
@@ -33,6 +87,8 @@ kotlin {
33
87
}
34
88
35
89
targets {
90
+ if (project. ext. nativeState == NativeState . DISABLED ) return
91
+
36
92
if (project. ext. singleTargetMode) {
37
93
fromPreset(project. ext. ideaPreset, ' native' )
38
94
} else {
@@ -59,8 +115,17 @@ kotlin {
59
115
addTarget(presets. mingwX64)
60
116
addTarget(presets. mingwX86)
61
117
}
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
+ }
62
126
}
63
127
128
+
64
129
sourceSets {
65
130
nativeMain { dependsOn commonMain }
66
131
// Empty source set is required in order to have native tests task
0 commit comments