-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathbuild.gradle.kts
432 lines (382 loc) · 15.8 KB
/
build.gradle.kts
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import kotlinx.team.infra.mavenPublicationsPom
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.net.URL
import javax.xml.parsers.DocumentBuilderFactory
import java.io.ByteArrayOutputStream
import java.io.PrintWriter
import org.jetbrains.dokka.gradle.AbstractDokkaLeafTask
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
plugins {
kotlin("multiplatform")
kotlin("plugin.serialization")
id("org.jetbrains.dokka")
`maven-publish`
}
mavenPublicationsPom {
description.set("Kotlin Datetime Library")
}
base {
archivesName.set("kotlinx-datetime")
}
val mainJavaToolchainVersion: String by project
val modularJavaToolchainVersion: String by project
val serializationVersion: String by project
java {
toolchain { languageVersion.set(JavaLanguageVersion.of(mainJavaToolchainVersion)) }
with(javaToolchains.launcherFor(toolchain).get().metadata) {
logger.info("Using JDK $languageVersion toolchain installed in $installationPath")
}
}
kotlin {
explicitApi()
infra {
common("tzfile") {
// Tiers are in accordance with <https://kotlinlang.org/docs/native-target-support.html>
common("tzdbOnFilesystem") {
common("linux") {
// Tier 1
target("linuxX64")
// Tier 2
target("linuxArm64")
// Tier 4 (deprecated, but still in demand)
target("linuxArm32Hfp")
}
common("darwin") {
common("darwinDevices") {
// Tier 1
target("macosX64")
target("macosArm64")
// Tier 2
target("watchosX64")
target("watchosArm32")
target("watchosArm64")
target("tvosX64")
target("tvosArm64")
target("iosArm64")
// Tier 3
target("watchosDeviceArm64")
}
common("darwinSimulator") {
// Tier 1
target("iosSimulatorArm64")
target("iosX64")
// Tier 2
target("watchosSimulatorArm64")
target("tvosSimulatorArm64")
}
}
}
common("androidNative") {
target("androidNativeArm32")
target("androidNativeArm64")
target("androidNativeX86")
target("androidNativeX64")
}
}
// Tier 3
common("windows") {
target("mingwX64")
}
}
jvm {
attributes {
attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 8)
}
compilations.all {
// Set compilation options for JVM target here
}
}
js {
nodejs {
testTask {
useMocha {
timeout = "30s"
}
}
}
compilations.all {
kotlinOptions {
sourceMap = true
moduleKind = "umd"
metaInfo = true
}
}
// compilations["main"].apply {
// kotlinOptions {
// outputFile = "kotlinx-datetime-tmp.js"
// }
// }
}
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
nodejs {
testTask {
useMocha {
timeout = "30s"
}
}
}
}
@OptIn(ExperimentalKotlinGradlePluginApi::class)
compilerOptions {
freeCompilerArgs.add("-Xexpect-actual-classes")
}
sourceSets.all {
val suffixIndex = name.indexOfLast { it.isUpperCase() }
val targetName = name.substring(0, suffixIndex)
val suffix = name.substring(suffixIndex).lowercase().takeIf { it != "main" }
// println("SOURCE_SET: $name")
kotlin.srcDir("$targetName/${suffix ?: "src"}")
resources.srcDir("$targetName/${suffix?.let { it + "Resources" } ?: "resources"}")
languageSettings {
// progressiveMode = true
}
}
targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget> {
compilations["test"].kotlinOptions {
freeCompilerArgs += listOf("-trw")
}
when {
konanTarget.family == org.jetbrains.kotlin.konan.target.Family.MINGW -> {
compilations["main"].cinterops {
create("declarations") {
defFile("$projectDir/windows/cinterop/definitions.def")
headers("$projectDir/windows/cinterop/definitions.h")
}
}
}
konanTarget.family == org.jetbrains.kotlin.konan.target.Family.LINUX ||
konanTarget.family == org.jetbrains.kotlin.konan.target.Family.ANDROID ||
konanTarget.family.isAppleFamily ->
{
// do nothing special
}
else -> {
throw IllegalArgumentException("Unknown native target ${this@withType}")
}
}
}
sourceSets {
commonMain {
dependencies {
compileOnly("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
}
}
commonTest {
dependencies {
api("org.jetbrains.kotlin:kotlin-test")
}
}
val jvmMain by getting {
}
val jvmTest by getting {
}
val commonJsMain by creating {
dependsOn(commonMain.get())
dependencies {
api("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
implementation(npm("@js-joda/core", "3.2.0"))
}
}
val commonJsTest by creating {
dependsOn(commonTest.get())
dependencies {
implementation(npm("@js-joda/timezone", "2.3.0"))
}
}
val jsMain by getting {
dependsOn(commonJsMain)
}
val jsTest by getting {
dependsOn(commonJsTest)
}
val wasmJsMain by getting {
dependsOn(commonJsMain)
}
val wasmJsTest by getting {
dependsOn(commonJsTest)
}
val nativeMain by getting {
dependsOn(commonMain.get())
dependencies {
api("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
}
}
val nativeTest by getting {
}
val darwinMain by getting {
}
val darwinTest by getting {
}
}
}
tasks {
val jvmTest by existing(Test::class) {
// maxHeapSize = "1024m"
}
val compileJavaModuleInfo by registering(JavaCompile::class) {
val moduleName = "kotlinx.datetime" // this module's name
val compileKotlinJvm by getting(KotlinCompile::class)
val sourceDir = file("jvm/java9/")
val targetDir = compileKotlinJvm.destinationDirectory.map { it.dir("../java9/") }
// Use a Java 11 compiler for the module info.
javaCompiler.set(project.javaToolchains.compilerFor { languageVersion.set(JavaLanguageVersion.of(modularJavaToolchainVersion)) })
// Always compile kotlin classes before the module descriptor.
dependsOn(compileKotlinJvm)
// Add the module-info source file.
source(sourceDir)
// Also add the module-info.java source file to the Kotlin compile task.
// The Kotlin compiler will parse and check module dependencies,
// but it currently won't compile to a module-info.class file.
// Note that module checking only works on JDK 9+,
// because the JDK built-in base modules are not available in earlier versions.
val javaVersion = compileKotlinJvm.kotlinJavaToolchain.javaVersion.getOrNull()
if (javaVersion?.isJava9Compatible == true) {
logger.info("Module-info checking is enabled; $compileKotlinJvm is compiled using Java $javaVersion")
compileKotlinJvm.source(sourceDir)
} else {
logger.info("Module-info checking is disabled")
}
// Set the task outputs and destination dir
outputs.dir(targetDir)
destinationDirectory.set(targetDir)
// Configure JVM compatibility
sourceCompatibility = JavaVersion.VERSION_1_9.toString()
targetCompatibility = JavaVersion.VERSION_1_9.toString()
// Set the Java release version.
options.release.set(9)
// Ignore warnings about using 'requires transitive' on automatic modules.
// not needed when compiling with recent JDKs, e.g. 17
options.compilerArgs.add("-Xlint:-requires-transitive-automatic")
// Patch the compileKotlinJvm output classes into the compilation so exporting packages works correctly.
options.compilerArgs.addAll(listOf("--patch-module", "$moduleName=${compileKotlinJvm.destinationDirectory.get()}"))
// Use the classpath of the compileKotlinJvm task.
// Also ensure that the module path is used instead of classpath.
classpath = compileKotlinJvm.libraries
modularity.inferModulePath.set(true)
}
// Configure the JAR task so that it will include the compiled module-info class file.
val jvmJar by existing(Jar::class) {
manifest {
attributes("Multi-Release" to true)
}
from(compileJavaModuleInfo.map { it.destinationDirectory }) {
into("META-INF/versions/9/")
}
}
// Workaround for https://youtrack.jetbrains.com/issue/KT-58303:
// the `clean` task can't delete the expanded.lock file on Windows as it's still held by Gradle, failing the build
val clean by existing(Delete::class) {
setDelete(fileTree(layout.buildDirectory) {
exclude("tmp/.cache/expanded/expanded.lock")
})
}
// workaround from KT-61313
withType<Sign>().configureEach {
val pubName = name.removePrefix("sign").removeSuffix("Publication")
// These tasks only exist for native targets, hence findByName() to avoid trying to find them for other targets
// Task ':linkDebugTest<platform>' uses this output of task ':sign<platform>Publication' without declaring an explicit or implicit dependency
findByName("linkDebugTest$pubName")?.let {
mustRunAfter(it)
}
// Task ':compileTestKotlin<platform>' uses this output of task ':sign<platform>Publication' without declaring an explicit or implicit dependency
findByName("compileTestKotlin$pubName")?.let {
mustRunAfter(it)
}
}
}
val downloadWindowsZonesMapping by tasks.registering {
description = "Updates the mapping between Windows-specific and usual names for timezones"
val output = "$projectDir/windows/src/internal/WindowsZoneNames.kt"
outputs.file(output)
doLast {
val initialFileContents = try { File(output).readBytes() } catch(e: Throwable) { ByteArray(0) }
val documentBuilderFactory = DocumentBuilderFactory.newInstance()
// otherwise, parsing fails since it can't find the dtd
documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
val builder = documentBuilderFactory.newDocumentBuilder()
val url = URL("https://raw.githubusercontent.com/unicode-org/cldr/master/common/supplemental/windowsZones.xml")
val xmlDoc = with(url.openConnection() as java.net.HttpURLConnection) {
builder.parse(this.inputStream)
}
xmlDoc.documentElement.normalize()
val mapZones = xmlDoc.getElementsByTagName("mapZone")
val mapping = linkedMapOf<String, String>()
mapping["UTC"] = "UTC"
for (i in 0 until mapZones.length) {
val mapZone = mapZones.item(i)
val windowsName = mapZone.attributes.getNamedItem("other").nodeValue
val usualNames = mapZone.attributes.getNamedItem("type").nodeValue
for (usualName in usualNames.split(' ')) {
if (usualName == "") continue
val oldWindowsName = mapping[usualName] // don't do it in `put` to preserve the order in the map
if (oldWindowsName == null) {
mapping[usualName] = windowsName
} else if (oldWindowsName != windowsName) {
throw Error("Ambiguous mapping: '$usualName' to '$oldWindowsName' and '$windowsName'")
}
}
}
val sortedMapping = mapping.toSortedMap()
val bos = ByteArrayOutputStream()
PrintWriter(bos).use { out ->
out.println("""// generated with gradle task `$name`""")
out.println("""package kotlinx.datetime.internal""")
out.println("""internal val standardToWindows: Map<String, String> = mutableMapOf(""")
for ((usualName, windowsName) in sortedMapping) {
out.println(" \"$usualName\" to \"$windowsName\",")
}
out.println(")")
out.println("""internal val windowsToStandard: Map<String, String> = mutableMapOf(""")
val reverseMap = sortedMapOf<String, String>()
for ((usualName, windowsName) in mapping) {
if (reverseMap[windowsName] == null) {
reverseMap[windowsName] = usualName
}
}
for ((windowsName, usualName) in reverseMap) {
out.println(" \"$windowsName\" to \"$usualName\",")
}
out.println(")")
}
val newFileContents = bos.toByteArray()
if (!(initialFileContents contentEquals newFileContents)) {
File(output).writeBytes(newFileContents)
throw GradleException("The mappings between Windows and IANA timezone names changed. " +
"The new mappings were written to the filesystem.")
}
}
}
tasks.withType<AbstractDokkaLeafTask>().configureEach {
pluginsMapConfiguration.set(mapOf("org.jetbrains.dokka.base.DokkaBase" to """{ "templatesDir" : "${projectDir.toString().replace('\\', '/')}/dokka-templates" }"""))
dokkaSourceSets.configureEach {
// reportUndocumented.set(true) // much noisy output about `hashCode` and serializer encoders, decoders etc
skipDeprecated.set(true)
// Enum members and undocumented toString()
suppressInheritedMembers.set(true)
// hide the `internal` package, which, on JS, has public members generated by Dukat that would get mentioned
perPackageOption {
matchingRegex.set(".*\\.internal(\\..*)?")
suppress.set(true)
}
sourceLink {
localDirectory.set(rootDir)
remoteUrl.set(URL("https://github.com/kotlin/kotlinx-datetime/tree/latest-release"))
remoteLineSuffix.set("#L")
}
}
}
// Disable intermediate sourceSet compilation because we do not need js-wasmJs artifact
tasks.configureEach {
if (name == "compileCommonJsMainKotlinMetadata") {
enabled = false
}
}
// Drop this configuration when the Node.JS version in KGP will support wasm gc milestone 4
// check it here:
// https://github.com/JetBrains/kotlin/blob/master/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/nodejs/NodeJsRootExtension.kt
with(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin.apply(rootProject)) {
nodeVersion = "21.0.0-v8-canary202309167e82ab1fa2"
nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
}