aboutsummaryrefslogtreecommitdiff
path: root/build-support/src/main/kotlin/platforms.kt
blob: 4cfb5ae59bebe8f2b3add174bb7757596c8d15cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright (C) 2023 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.withType
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

fun KotlinMultiplatformExtension.configureOrCreateOkioPlatforms() {
  jvm {
  }
  if (kmpJsEnabled) {
    configureOrCreateJsPlatforms()
  }
  if (kmpNativeEnabled) {
    configureOrCreateNativePlatforms()
  }
  if (kmpWasmEnabled) {
    configureOrCreateWasmPlatform()
  }
}

fun KotlinMultiplatformExtension.configureOrCreateNativePlatforms() {
  iosX64()
  iosArm64()
  iosSimulatorArm64()
  tvosX64()
  tvosArm64()
  tvosSimulatorArm64()
  watchosArm32()
  watchosArm64()
  watchosDeviceArm64()
  watchosX64()
  watchosSimulatorArm64()
  // Required to generate tests tasks: https://youtrack.jetbrains.com/issue/KT-26547
  linuxX64()
  linuxArm64()
  macosX64()
  macosArm64()
  mingwX64()
}

val appleTargets = listOf(
  "iosArm64",
  "iosX64",
  "iosSimulatorArm64",
  "macosX64",
  "macosArm64",
  "tvosArm64",
  "tvosX64",
  "tvosSimulatorArm64",
  "watchosArm32",
  "watchosArm64",
  "watchosDeviceArm64",
  "watchosX64",
  "watchosSimulatorArm64",
)

val mingwTargets = listOf(
  "mingwX64",
)

val linuxTargets = listOf(
  "linuxX64",
  "linuxArm64",
)

val nativeTargets = appleTargets + linuxTargets + mingwTargets

val wasmTargets = listOf(
  "wasmJs",
  "wasmWasi",
)

/**
 * Creates a source set for a directory that isn't already a built-in platform. Use this to create
 * custom shared directories like `nonJvmMain` or `unixMain`.
 */
fun NamedDomainObjectContainer<KotlinSourceSet>.createSourceSet(
  name: String,
  parent: KotlinSourceSet? = null,
  children: List<String> = listOf()
): KotlinSourceSet {
  val result = create(name)

  if (parent != null) {
    result.dependsOn(parent)
  }

  val suffix = when {
    name.endsWith("Main") -> "Main"
    name.endsWith("Test") -> "Test"
    else -> error("unexpected source set name: ${name}")
  }

  for (childTarget in children) {
    val childSourceSet = get("${childTarget}$suffix")
    childSourceSet.dependsOn(result)
  }

  return result
}

fun KotlinMultiplatformExtension.configureOrCreateJsPlatforms() {
  js {
    compilations.all {
      kotlinOptions {
        moduleKind = "umd"
        sourceMap = true
        metaInfo = true
      }
    }
    nodejs {
      testTask {
        useMocha {
          timeout = "30s"
        }
      }
    }
    browser {
    }
  }
}

fun KotlinMultiplatformExtension.configureOrCreateWasmPlatform(
  js: Boolean = true,
  wasi: Boolean = true,
) {
  if (js) {
    wasmJs {
      nodejs()
    }
  }
  if (wasi) {
    wasmWasi {
      nodejs()
    }
  }
}