Skip to content

Commit 1d8e270

Browse files
authored
Use default target hierarchy to configure build targets (#272)
* remove `IR` in JS target, as it's default now * minor changes to use nicer APIs for JS testTasks and sourceSet dependencies * group native targets declaration by platform, to better understand which targets are enabled Fixes #147
1 parent b8e6424 commit 1d8e270

File tree

4 files changed

+72
-127
lines changed

4 files changed

+72
-127
lines changed

build-logic/src/main/kotlin/kotlinx/io/conventions/kotlinx-io-multiplatform.gradle.kts

Lines changed: 58 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
44
*/
55

6+
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
67
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
8+
import org.jetbrains.kotlin.gradle.plugin.KotlinHierarchyBuilder
9+
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
710
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
11+
import org.jetbrains.kotlin.gradle.targets.js.KotlinWasmTargetType
12+
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
13+
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget
814
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
915
import org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask
1016
import kotlin.jvm.optionals.getOrNull
@@ -30,31 +36,31 @@ kotlin {
3036
}
3137
}
3238

33-
js(IR) {
39+
js {
3440
browser {
3541
testTask(Action {
3642
filter.setExcludePatterns("*SmokeFileTest*")
3743
})
3844
}
3945
}
4046

41-
@OptIn(org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl::class)
47+
@OptIn(ExperimentalWasmDsl::class)
4248
wasmJs {
4349
nodejs()
4450
// Disabled because we can't exclude some tests: https://youtrack.jetbrains.com/issue/KT-58291
4551
// browser()
4652
}
4753

48-
@OptIn(org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl::class)
54+
@OptIn(ExperimentalWasmDsl::class)
4955
wasmWasi {
5056
nodejs()
5157
}
5258

59+
nativeTargets()
60+
5361
sourceSets {
54-
commonTest {
55-
dependencies {
56-
implementation(kotlin("test"))
57-
}
62+
commonTest.dependencies {
63+
implementation(kotlin("test"))
5864
}
5965
}
6066

@@ -63,42 +69,45 @@ kotlin {
6369
configureSourceSet()
6470
}
6571

66-
configureNativePlatforms()
67-
68-
val appleTargets = appleTargets()
69-
val mingwTargets = mingwTargets()
72+
@OptIn(ExperimentalKotlinGradlePluginApi::class)
73+
applyDefaultHierarchyTemplate {
74+
common {
75+
group("native") {
76+
group("nonApple") {
77+
group("mingw")
78+
group("unix") {
79+
group("linux")
80+
group("androidNative")
81+
}
82+
}
83+
}
84+
group("nodeFilesystemShared") {
85+
withJs()
86+
withWasmJs()
87+
}
88+
group("wasm") {
89+
withWasmJs()
90+
withWasmWasi()
91+
}
92+
}
93+
}
94+
}
7095

71-
/*
72-
Native source sets hierarchy:
73-
native
74-
|-> apple
75-
|-> nonApple
76-
|-> mingw
77-
|-> unix
78-
|-> linux
79-
|-> android
80-
*/
96+
// will be available in KGP 2.0
97+
@OptIn(ExperimentalKotlinGradlePluginApi::class)
98+
fun KotlinHierarchyBuilder.withWasmJs(): Unit = withCompilations {
99+
val target = it.target
100+
target.platformType == KotlinPlatformType.wasm &&
101+
target is KotlinJsIrTarget &&
102+
target.wasmTargetType == KotlinWasmTargetType.JS
103+
}
81104

82-
sourceSets {
83-
val nativeMain = createSourceSet("nativeMain", parent = commonMain.get())
84-
val nativeTest = createSourceSet("nativeTest", parent = commonTest.get())
85-
val nonAppleMain = createSourceSet("nonAppleMain", parent = nativeMain)
86-
val nonAppleTest = createSourceSet("nonAppleTest", parent = nativeTest)
87-
createSourceSet("appleMain", parent = nativeMain, children = appleTargets)
88-
createSourceSet("appleTest", parent = nativeTest, children = appleTargets)
89-
createSourceSet("mingwMain", parent = nonAppleMain, children = mingwTargets)
90-
createSourceSet("mingwTest", parent = nonAppleTest, children = mingwTargets)
91-
val unixMain = createSourceSet("unixMain", parent = nonAppleMain)
92-
val unixTest = createSourceSet("unixTest", parent = nonAppleTest)
93-
createSourceSet("linuxMain", parent = unixMain, children = linuxTargets())
94-
createSourceSet("linuxTest", parent = unixTest, children = linuxTargets())
95-
createSourceSet("androidMain", parent = unixMain, children = androidTargets())
96-
createSourceSet("androidTest", parent = unixTest, children = androidTargets())
97-
createSourceSet("nodeFilesystemSharedMain", parent = commonMain.get(), children = nodeTargets())
98-
createSourceSet("nodeFilesystemSharedTest", parent = commonTest.get(), children = nodeTargets())
99-
createSourceSet("wasmMain", parent = commonMain.get(), children = wasmTargets())
100-
createSourceSet("wasmTest", parent = commonTest.get(), children = wasmTargets())
101-
}
105+
@OptIn(ExperimentalKotlinGradlePluginApi::class)
106+
fun KotlinHierarchyBuilder.withWasmWasi(): Unit = withCompilations {
107+
val target = it.target
108+
target.platformType == KotlinPlatformType.wasm &&
109+
target is KotlinJsIrTarget &&
110+
target.wasmTargetType == KotlinWasmTargetType.WASI
102111
}
103112

104113
fun KotlinSourceSet.configureSourceSet() {
@@ -115,95 +124,35 @@ fun KotlinSourceSet.configureSourceSet() {
115124
}
116125
}
117126

118-
/**
119-
* Creates a source set for a directory that isn't already a built-in platform. Use this to create
120-
* custom shared directories like `nonJvmMain` or `unixMain`.
121-
*/
122-
fun NamedDomainObjectContainer<KotlinSourceSet>.createSourceSet(
123-
name: String,
124-
parent: KotlinSourceSet? = null,
125-
children: List<String> = listOf()
126-
): KotlinSourceSet {
127-
val result = create(name)
128-
129-
if (parent != null) {
130-
result.dependsOn(parent)
131-
}
132-
133-
val suffix = when {
134-
name.endsWith("Main") -> "Main"
135-
name.endsWith("Test") -> "Test"
136-
else -> error("unexpected source set name: ${name}")
137-
}
138-
139-
for (childTarget in children) {
140-
val childSourceSet = get("${childTarget}$suffix")
141-
childSourceSet.dependsOn(result)
142-
}
143-
144-
return result
145-
}
146-
147-
fun KotlinMultiplatformExtension.configureNativePlatforms() {
127+
private fun KotlinMultiplatformExtension.nativeTargets() {
148128
iosX64()
149129
iosArm64()
150130
iosSimulatorArm64()
131+
151132
tvosX64()
152133
tvosArm64()
153134
tvosSimulatorArm64()
135+
154136
watchosArm32()
155137
watchosArm64()
156138
watchosX64()
157139
watchosSimulatorArm64()
158140
watchosDeviceArm64()
159-
linuxArm64()
141+
160142
androidNativeArm32()
161143
androidNativeArm64()
162144
androidNativeX64()
163145
androidNativeX86()
164-
// Required to generate tests tasks: https://youtrack.jetbrains.com/issue/KT-26547
146+
165147
linuxX64()
148+
linuxArm64()
149+
166150
macosX64()
167151
macosArm64()
152+
168153
mingwX64()
169154
}
170155

171-
fun appleTargets() = listOf(
172-
"iosArm64",
173-
"iosX64",
174-
"iosSimulatorArm64",
175-
"macosX64",
176-
"macosArm64",
177-
"tvosArm64",
178-
"tvosX64",
179-
"tvosSimulatorArm64",
180-
"watchosArm32",
181-
"watchosArm64",
182-
"watchosX64",
183-
"watchosSimulatorArm64",
184-
"watchosDeviceArm64"
185-
)
186-
187-
fun mingwTargets() = listOf(
188-
"mingwX64"
189-
)
190-
191-
fun linuxTargets() = listOf(
192-
"linuxX64",
193-
"linuxArm64"
194-
)
195-
196-
fun androidTargets() = listOf(
197-
"androidNativeArm32",
198-
"androidNativeArm64",
199-
"androidNativeX64",
200-
"androidNativeX86"
201-
)
202-
203-
fun wasmTargets() = listOf("wasmJs", "wasmWasi")
204-
205-
fun nodeTargets() = listOf("js", "wasmJs")
206-
207156
rootProject.the<NodeJsRootExtension>().apply {
208157
nodeVersion = "21.0.0-v8-canary202310177990572111"
209158
nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"

bytestring/build.gradle.kts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ plugins {
99
}
1010

1111
kotlin {
12-
js(IR) {
12+
js {
1313
nodejs {
14-
testTask(Action {
14+
testTask {
1515
useMocha {
1616
timeout = "30s"
1717
}
18-
})
18+
}
1919
}
2020
browser {
21-
testTask(Action {
21+
testTask {
2222
useMocha {
2323
timeout = "30s"
2424
}
25-
})
25+
}
2626
}
2727
}
2828
}

core/build.gradle.kts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,29 @@ plugins {
1414
}
1515

1616
kotlin {
17-
js(IR) {
17+
js {
1818
nodejs {
19-
testTask(Action {
19+
testTask {
2020
useMocha {
2121
timeout = "300s"
2222
}
23-
})
23+
}
2424
}
2525
browser {
26-
testTask(Action {
26+
testTask {
2727
useMocha {
2828
timeout = "300s"
2929
}
30-
})
30+
}
3131
}
3232
}
3333

3434
sourceSets {
35-
commonMain {
36-
dependencies {
37-
api(project(":kotlinx-io-bytestring"))
38-
}
35+
commonMain.dependencies {
36+
api(project(":kotlinx-io-bytestring"))
3937
}
40-
appleTest {
41-
dependencies {
42-
implementation(libs.kotlinx.coroutines.core)
43-
}
38+
appleTest.dependencies {
39+
implementation(libs.kotlinx.coroutines.core)
4440
}
4541
}
4642
}

0 commit comments

Comments
 (0)