3
3
*/
4
4
5
5
apply plugin : ' kotlin-multiplatform'
6
- apply from : rootProject. file(" gradle/targets.gradle" )
7
6
apply from : rootProject. file(" gradle/compile-jvm-multiplatform.gradle" )
8
7
apply from : rootProject. file(" gradle/compile-common.gradle" )
9
8
apply from : rootProject. file(" gradle/compile-js-multiplatform.gradle" )
10
9
apply from : rootProject. file(" gradle/compile-native-multiplatform.gradle" )
11
10
apply from : rootProject. file(' gradle/publish-npm-js.gradle' )
12
11
12
+ project. ext. ideaActive = System . getProperty(' idea.active' ) == ' true'
13
+ project. ext. sourceSetSuffixes = [" Main" , " Test" ]
14
+
15
+ void defineSourceSet (newName , dependsOn , includedInPred ) {
16
+ for (suffix in project. ext. sourceSetSuffixes) {
17
+ def newSS = kotlin. sourceSets. maybeCreate(newName + suffix)
18
+ for (dep in dependsOn) {
19
+ newSS. dependsOn(kotlin. sourceSets[dep + suffix])
20
+ }
21
+ for (curSS in kotlin. sourceSets) {
22
+ def curName = curSS. name
23
+ if (curName. endsWith(suffix)) {
24
+ def prefix = curName. substring(0 , curName. length() - suffix. length())
25
+ if (includedInPred(prefix)) curSS. dependsOn(newSS)
26
+ }
27
+ }
28
+ }
29
+ }
30
+
31
+ boolean isNativeDarwin (String name ) { return [" ios" , " macos" , " tvos" , " watchos" ]. any { name. startsWith(it) } }
32
+ boolean isNativeOther (String name ) { return [" linux" , " mingw" ]. any { name. startsWith(it) } }
33
+
34
+ /* TARGETS:
35
+ js -----------------------------------------------------+
36
+ |
37
+ V
38
+ jvm -------------------------------> concurrent ---> common
39
+ ^
40
+ ios \ |
41
+ macos | ---> nativeDarwin ---> native --+
42
+ tvos | ^
43
+ watchos / |
44
+ |
45
+ linux \ ---> nativeOther -------+
46
+ mingw /
47
+ */
48
+
49
+ defineSourceSet(" concurrent" , [" common" ]) { it in [" jvm" , " native" ] }
50
+ defineSourceSet(" nativeDarwin" , [" native" ]) { isNativeDarwin(it) }
51
+ defineSourceSet(" nativeOther" , [" native" ]) { isNativeOther(it) }
52
+
13
53
/*
14
54
* All platform plugins and configuration magic happens here instead of build.gradle
15
55
* because JMV-only projects depend on core, thus core should always be initialized before configuration.
16
56
*/
17
57
kotlin {
58
+ sourceSets {
59
+ commonMain {
60
+ dependencies {
61
+ // todo: we should not need it
62
+ // todo: it must be compileOnly or JVM is spoiled !!!
63
+ implementation " org.jetbrains.kotlinx:atomicfu-common:$atomicfu_version "
64
+ }
65
+ }
66
+ }
67
+
18
68
configure(sourceSets) {
19
69
def srcDir = name. endsWith(' Main' ) ? ' src' : ' test'
20
70
def platform = name[0 .. -5 ]
21
- kotlin. srcDir " $platform /$srcDir "
71
+ kotlin. srcDirs = [ " $platform /$srcDir " ]
22
72
if (name == " jvmMain" ) {
23
73
resources. srcDirs = [" $platform /resources" ]
24
74
} else if (name == " jvmTest" ) {
@@ -30,6 +80,23 @@ kotlin {
30
80
}
31
81
}
32
82
83
+ configure(targets) {
84
+ // "Main" native targets -- one for each OS
85
+ if ([" macos" , " linux" , " mingw" ]. any { name. startsWith(it) }) {
86
+ binaries {
87
+ // Test for memory leaks using a special entry point that does not exit but returns from main
88
+ binaries. getTest(" DEBUG" ). freeCompilerArgs + = [" -e" , " kotlinx.coroutines.mainNoExit" ]
89
+ // Configure a separate test where code runs in background
90
+ test(" background" , [org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType . DEBUG ]) {
91
+ freeCompilerArgs + = [" -e" , " kotlinx.coroutines.mainBackground" ]
92
+ }
93
+ }
94
+ testRuns {
95
+ background { setExecutionSourceFrom(binaries. backgroundDebugTest) }
96
+ }
97
+ }
98
+ }
99
+
33
100
configure(targets) {
34
101
def targetName = it. name
35
102
compilations. all { compilation ->
@@ -54,6 +121,36 @@ compileKotlinMetadata {
54
121
}
55
122
}
56
123
124
+ def configureNativeSourceSetPreset (name , preset ) {
125
+ def hostMainCompilation = project. kotlin. targetFromPreset(preset). compilations. main
126
+ def compileConfiguration = configurations[hostMainCompilation. compileDependencyConfigurationName]
127
+ def hostNativePlatformLibs = files(provider {
128
+ compileConfiguration. findAll {
129
+ it. path. endsWith(" .klib" ) || it. absolutePath. contains(" klib${ File.separator} platform" )
130
+ }
131
+ })
132
+ for (suffix in project. ext. sourceSetSuffixes) {
133
+ configure(kotlin. sourceSets[name + suffix]) {
134
+ dependencies. add(implementationMetadataConfigurationName, hostNativePlatformLibs)
135
+ }
136
+ }
137
+ }
138
+
139
+ // Configure platform libraries for native source sets when working in IDEA
140
+ if (project. ext. ideaActive) {
141
+ def manager = project. ext. hostManager
142
+ def linuxPreset = kotlin. presets. linuxX64
143
+ def macosPreset = kotlin. presets. macosX64
144
+ // linux should be always available (cross-compilation capable) -- use it as default
145
+ assert manager. isEnabled(linuxPreset. konanTarget)
146
+ // use macOS libs for nativeDarwin if available
147
+ def macosAvailable = manager. isEnabled(macosPreset. konanTarget)
148
+ // configure source sets
149
+ configureNativeSourceSetPreset(" native" , linuxPreset)
150
+ configureNativeSourceSetPreset(" nativeOther" , linuxPreset)
151
+ configureNativeSourceSetPreset(" nativeDarwin" , macosAvailable ? macosPreset : linuxPreset)
152
+ }
153
+
57
154
kotlin. sourceSets {
58
155
jvmMain. dependencies {
59
156
compileOnly " com.google.android:annotations:4.1.1.4"
0 commit comments