1 /*
2 * Copyright (C) 2023 Square, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 import org.gradle.api.NamedDomainObjectContainer
17 import org.gradle.api.Project
18 import org.gradle.kotlin.dsl.get
19 import org.gradle.kotlin.dsl.withType
20 import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
21 import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
22 import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
23 import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
24 import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
25 import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
26
configureOrCreateOkioPlatformsnull27 fun KotlinMultiplatformExtension.configureOrCreateOkioPlatforms() {
28 jvm {
29 }
30 if (kmpJsEnabled) {
31 configureOrCreateJsPlatforms()
32 }
33 if (kmpNativeEnabled) {
34 configureOrCreateNativePlatforms()
35 }
36 if (kmpWasmEnabled) {
37 configureOrCreateWasmPlatform()
38 }
39 }
40
KotlinMultiplatformExtensionnull41 fun KotlinMultiplatformExtension.configureOrCreateNativePlatforms() {
42 iosX64()
43 iosArm64()
44 iosSimulatorArm64()
45 tvosX64()
46 tvosArm64()
47 tvosSimulatorArm64()
48 watchosArm32()
49 watchosArm64()
50 watchosDeviceArm64()
51 watchosX64()
52 watchosSimulatorArm64()
53 // Required to generate tests tasks: https://youtrack.jetbrains.com/issue/KT-26547
54 linuxX64()
55 linuxArm64()
56 macosX64()
57 macosArm64()
58 mingwX64()
59 }
60
61 val appleTargets = listOf(
62 "iosArm64",
63 "iosX64",
64 "iosSimulatorArm64",
65 "macosX64",
66 "macosArm64",
67 "tvosArm64",
68 "tvosX64",
69 "tvosSimulatorArm64",
70 "watchosArm32",
71 "watchosArm64",
72 "watchosDeviceArm64",
73 "watchosX64",
74 "watchosSimulatorArm64",
75 )
76
77 val mingwTargets = listOf(
78 "mingwX64",
79 )
80
81 val linuxTargets = listOf(
82 "linuxX64",
83 "linuxArm64",
84 )
85
86 val nativeTargets = appleTargets + linuxTargets + mingwTargets
87
88 val wasmTargets = listOf(
89 "wasmJs",
90 "wasmWasi",
91 )
92
93 /**
94 * Creates a source set for a directory that isn't already a built-in platform. Use this to create
95 * custom shared directories like `nonJvmMain` or `unixMain`.
96 */
NamedDomainObjectContainernull97 fun NamedDomainObjectContainer<KotlinSourceSet>.createSourceSet(
98 name: String,
99 parent: KotlinSourceSet? = null,
100 children: List<String> = listOf()
101 ): KotlinSourceSet {
102 val result = create(name)
103
104 if (parent != null) {
105 result.dependsOn(parent)
106 }
107
108 val suffix = when {
109 name.endsWith("Main") -> "Main"
110 name.endsWith("Test") -> "Test"
111 else -> error("unexpected source set name: ${name}")
112 }
113
114 for (childTarget in children) {
115 val childSourceSet = get("${childTarget}$suffix")
116 childSourceSet.dependsOn(result)
117 }
118
119 return result
120 }
121
KotlinMultiplatformExtensionnull122 fun KotlinMultiplatformExtension.configureOrCreateJsPlatforms() {
123 js {
124 compilations.all {
125 kotlinOptions {
126 moduleKind = "umd"
127 sourceMap = true
128 metaInfo = true
129 }
130 }
131 nodejs {
132 testTask {
133 useMocha {
134 timeout = "30s"
135 }
136 }
137 }
138 browser {
139 }
140 }
141 }
142
KotlinMultiplatformExtensionnull143 fun KotlinMultiplatformExtension.configureOrCreateWasmPlatform(
144 js: Boolean = true,
145 wasi: Boolean = true,
146 ) {
147 if (js) {
148 wasmJs {
149 nodejs()
150 }
151 }
152 if (wasi) {
153 wasmWasi {
154 nodejs()
155 }
156 }
157 }
158