1
+
2
+ import com.android.build.gradle.internal.tasks.factory.dependsOn
1
3
import org.gradle.api.Action
2
4
import org.gradle.api.Project
5
+ import org.gradle.api.tasks.testing.Test
3
6
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
7
+ import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
8
+ import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
4
9
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrLink
10
+ import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
5
11
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink
12
+ import org.jetbrains.kotlin.konan.target.Family
6
13
7
14
private val allAppleTargets = setOf (
8
15
" macosX64" ,
@@ -19,23 +26,23 @@ private val allAppleTargets = setOf(
19
26
)
20
27
21
28
// Try to guess the dev machine to make sure the tests are running smoothly
22
- val hostTarget: String
29
+ internal val hostTarget: String
23
30
get() = if (System .getProperty(" os.arch" ) == " aarch64" ) {
24
31
" macosArm64"
25
32
} else {
26
33
" macosX64"
27
34
}
28
35
29
- val enabledAppleTargets = allAppleTargets
30
- val enabledLinux = true
31
- val enabledJs = true
36
+ private val enableLinux = true
37
+ private val enableJs = true
32
38
33
39
fun Project.configureMppDefaults (withJs : Boolean , withLinux : Boolean , withAndroid : Boolean ) {
34
40
configureMpp(
35
41
withJvm = true ,
36
42
withJs = withJs,
43
+ browserTest = false ,
37
44
withLinux = withLinux,
38
- appleTargets = enabledAppleTargets ,
45
+ appleTargets = allAppleTargets ,
39
46
withAndroid = withAndroid,
40
47
)
41
48
}
@@ -67,7 +74,7 @@ fun Project.configureMpp(
67
74
withLinux : Boolean ,
68
75
withAndroid : Boolean ,
69
76
appleTargets : Collection <String >,
70
- browserTest : Boolean = false ,
77
+ browserTest : Boolean ,
71
78
) {
72
79
val kotlinExtension = extensions.findByName(" kotlin" ) as ? KotlinMultiplatformExtension
73
80
check(kotlinExtension != null ) {
@@ -78,7 +85,7 @@ fun Project.configureMpp(
78
85
jvm()
79
86
}
80
87
81
- if (enabledJs && withJs) {
88
+ if (enableJs && withJs) {
82
89
js(IR ) {
83
90
if (browserTest) {
84
91
browser {
@@ -101,7 +108,7 @@ fun Project.configureMpp(
101
108
}
102
109
}
103
110
104
- if (enabledLinux && withLinux) {
111
+ if (enableLinux && withLinux) {
105
112
linuxX64(" linux" )
106
113
}
107
114
@@ -111,8 +118,14 @@ fun Project.configureMpp(
111
118
}
112
119
}
113
120
114
- createAndConfigureAppleTargets(appleTargets.toSet().intersect(enabledAppleTargets))
121
+ appleTargets.toSet().intersect(allAppleTargets).forEach { presetName ->
122
+ targetFromPreset(
123
+ presets.getByName(presetName),
124
+ presetName,
125
+ )
126
+ }
115
127
128
+ configureSourceSetGraph()
116
129
addTestDependencies()
117
130
118
131
tasks.withType(KotlinJsIrLink ::class .java).configureEach {
@@ -124,31 +137,65 @@ fun Project.configureMpp(
124
137
}
125
138
}
126
139
127
- private fun KotlinMultiplatformExtension.createAndConfigureAppleTargets (presetNames : Collection <String >) {
128
- if (presetNames.isEmpty()) {
129
- return
140
+ /* *
141
+ * Current Graph is something like so
142
+ *
143
+ * graph TB
144
+ * commonMain --> jvmMain
145
+ * commonMain --> appleMain
146
+ * commonMain --> linuxMain
147
+ * commonMain --> jsMain
148
+ * appleMain --> macosX64
149
+ * appleMain --> macosArm64
150
+ * appleMain --> iosArm64
151
+ * appleMain --> iosX64
152
+ * appleMain --> iosSimulatorArm64
153
+ * appleMain --> watchosArm32
154
+ * appleMain --> watchosArm64
155
+ * appleMain --> watchosSimulatorArm64
156
+ * appleMain --> tvosArm64
157
+ * appleMain --> tvosX64
158
+ * appleMain --> tvosSimulatorArm64
159
+ *
160
+ *
161
+ * commonTest --> kotlinCodegenTest
162
+ * commonTest --> jvmJavaCodeGen
163
+ * kotlinCodegenTest --> macOsArm64Test
164
+ * kotlinCodegenTest --> jvmTest
165
+ * kotlinCodegenTest --> jsTest
166
+ *
167
+ *
168
+ *
169
+ * classDef kotlinPurple fill:#A97BFF,stroke:#333,stroke-width:2px,color:#333
170
+ * classDef javaOrange fill:#b07289,stroke:#333,stroke-width:2px,color:#333
171
+ * classDef gray fill:#AAA,stroke:#333,stroke-width:2px,color:#333
172
+ * class kotlinCodegenTest gray
173
+ * class jvmJavaCodeGen,macOsArm64Test,jvmTest,jsTest kotlinPurple
174
+ * class commonTest javaOrange
175
+ */
176
+ private fun KotlinMultiplatformExtension.configureSourceSetGraph () {
177
+ val hasAppleTarget = targets.any {
178
+ it is KotlinNativeTarget && it.konanTarget.family in setOf (Family .IOS , Family .OSX , Family .WATCHOS , Family .TVOS )
130
179
}
180
+ if (hasAppleTarget) {
181
+ val appleMain = sourceSets.create(" appleMain" )
182
+ val appleTest = sourceSets.create(" appleTest" )
131
183
132
- if (System .getProperty(" idea.sync.active" ) != null ) {
133
- // Early return. Inside intelliJ, only configure one target
134
- targetFromPreset(presets.getByName(hostTarget), " apple" )
135
- return
136
- }
184
+ appleMain.dependsOn(sourceSets.getByName(" commonMain" ))
185
+ appleTest.dependsOn(sourceSets.getByName(" commonTest" ))
137
186
138
- val appleMain = sourceSets.create(" appleMain" )
139
- val appleTest = sourceSets.create(" appleTest" )
187
+ allAppleTargets.forEach {
188
+ sourceSets.findByName(" ${it} Main" )?.dependsOn(appleMain)
189
+ sourceSets.findByName(" ${it} Test" )?.dependsOn(appleTest)
190
+ }
191
+ }
140
192
141
- appleMain.dependsOn(sourceSets.getByName(" commonMain" ))
142
- appleTest.dependsOn(sourceSets.getByName(" commonTest" ))
193
+ val kotlinCodegentTest = sourceSets.create(" kotlinCodegenTest" )
143
194
144
- presetNames.forEach { presetName ->
145
- targetFromPreset(
146
- presets.getByName(presetName),
147
- presetName,
148
- )
195
+ kotlinCodegentTest.dependsOn(sourceSets.getByName(" commonTest" ))
149
196
150
- sourceSets.getByName( " ${presetName} Main " ).dependsOn(appleMain)
151
- sourceSets.getByName (" ${presetName } Test" ).dependsOn(appleTest )
197
+ targets.forEach {
198
+ sourceSets.findByName (" ${it.name } Test" )? .dependsOn(kotlinCodegentTest )
152
199
}
153
200
}
154
201
@@ -159,3 +206,38 @@ private fun KotlinMultiplatformExtension.addTestDependencies() {
159
206
}
160
207
}
161
208
}
209
+
210
+ fun Project.registerJavaCodegenTestTask () {
211
+ val kotlin = kotlinExtension
212
+ check(kotlin is KotlinMultiplatformExtension ) {
213
+ " Only multiplatform projects can register a javaCodegenTest task"
214
+ }
215
+ val jvmTarget = kotlin.targets.getByName(" jvm" ) as KotlinJvmTarget
216
+ jvmTarget.withJava()
217
+
218
+ val javaCodegenCompilation = jvmTarget.compilations.create(" javaCodegen" )
219
+ javaCodegenCompilation.compileJavaTaskProvider?.configure {
220
+ classpath + = configurations.getByName(" jvmTestCompileClasspath" )
221
+ }
222
+ javaCodegenCompilation.configurations.compileDependencyConfiguration.extendsFrom(configurations.getByName(" jvmTestCompileClasspath" ))
223
+ javaCodegenCompilation.defaultSourceSet.dependsOn(kotlin.sourceSets.getByName(" commonTest" ))
224
+ javaCodegenCompilation.defaultSourceSet.kotlin.apply {
225
+ srcDir(" src/kotlinCodegenTest/kotlin" )
226
+ }
227
+
228
+ val task = tasks.register(" javaCodegenTest" , Test ::class .java) {
229
+ description = " Runs Java codegen tests."
230
+ group = " verification"
231
+
232
+ testClassesDirs = javaCodegenCompilation.output.classesDirs
233
+ classpath = configurations.getByName(" jvmTestRuntimeClasspath" ) + javaCodegenCompilation.output.classesDirs
234
+
235
+ useJUnit()
236
+
237
+ testLogging {
238
+ events(" passed" )
239
+ }
240
+ }
241
+
242
+ tasks.named(" build" ).dependsOn(task)
243
+ }
0 commit comments