xref: /aosp_15_r20/external/kotlinx.serialization/gradle/configure-source-sets.gradle (revision 57b5a4a64c534cf7f27ac9427ceab07f3d8ed3d8)
1/*
2 * Copyright 2017-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3 */
4
5import static KotlinVersion.*
6
7java {
8    toolchain {
9        languageVersion.set(JavaLanguageVersion.of(11))
10    }
11}
12
13tasks.withType(JavaCompile).configureEach {
14    options.release = 8
15}
16
17// Unfortunately there is no compatible version of okio for Wasm WASI target, so we need to skip to configure WASI for json-okio and json-tests.
18// json-tests uses okio with incorporate with other formatter tests so it is hard and not worth to separate it for two projects for WASI.
19// So we disable WASI target in it and we hope, that WASI version of compiler and serialization plugin are identical to the WasmJS target so WASI target is being covered.
20Boolean isOkIoOrFormatTests = (project.name == 'kotlinx-serialization-json-okio' || project.name == 'kotlinx-serialization-json-tests')
21
22kotlin {
23    jvm {
24        withJava()
25        compilations.configureEach {
26            kotlinOptions {
27                jvmTarget = '1.8'
28                freeCompilerArgs += '-Xjdk-release=1.8'
29            }
30        }
31    }
32
33    js {
34        nodejs {
35            testTask {
36                useMocha {
37		    timeout = "10s"
38                }
39            }
40        }
41        configure([compilations.main, compilations.test]) {
42            kotlinOptions {
43                sourceMap = true
44                moduleKind = "umd"
45            }
46        }
47    }
48
49    wasmJs {
50        nodejs()
51    }
52
53    if (!isOkIoOrFormatTests) {
54        wasmWasi {
55            nodejs()
56        }
57    }
58
59    sourceSets.all {
60        kotlin.srcDirs = ["$it.name/src"]
61        resources.srcDirs = ["$it.name/resources"]
62        languageSettings {
63            progressiveMode = true
64
65            optIn("kotlin.ExperimentalMultiplatform")
66            optIn("kotlin.ExperimentalStdlibApi")
67            optIn("kotlinx.serialization.InternalSerializationApi")
68        }
69    }
70
71    sourceSets {
72        commonMain {
73            dependencies {
74                api 'org.jetbrains.kotlin:kotlin-stdlib-common'
75            }
76        }
77
78        commonTest {
79            dependencies {
80                api 'org.jetbrains.kotlin:kotlin-test-common'
81                api 'org.jetbrains.kotlin:kotlin-test-annotations-common'
82            }
83        }
84
85        jvmMain {
86            dependencies {
87                api 'org.jetbrains.kotlin:kotlin-stdlib'
88            }
89        }
90
91        jvmTest {
92            dependencies {
93                api 'org.jetbrains.kotlin:kotlin-test-junit'
94            }
95        }
96
97        jsMain {
98            dependencies {
99                api 'org.jetbrains.kotlin:kotlin-stdlib-js'
100            }
101        }
102
103        jsTest {
104            dependencies {
105                api 'org.jetbrains.kotlin:kotlin-test-js'
106            }
107        }
108
109        create("wasmMain") {
110            dependsOn(commonMain)
111        }
112        create("wasmTest") {
113            dependsOn(commonTest)
114        }
115
116        wasmJsMain {
117            dependsOn(wasmMain)
118            dependencies {
119                api 'org.jetbrains.kotlin:kotlin-stdlib-wasm-js'
120            }
121        }
122
123        wasmJsTest {
124            dependsOn(wasmTest)
125            dependencies {
126                api 'org.jetbrains.kotlin:kotlin-test-wasm-js'
127            }
128        }
129
130        if (!isOkIoOrFormatTests) {
131            wasmWasiMain {
132                dependsOn(wasmMain)
133                dependencies {
134                    api 'org.jetbrains.kotlin:kotlin-stdlib-wasm-wasi'
135                }
136            }
137
138            wasmWasiTest {
139                dependsOn(wasmTest)
140                dependencies {
141                    api 'org.jetbrains.kotlin:kotlin-test-wasm-wasi'
142                }
143            }
144        }
145
146        nativeMain.dependencies {
147        }
148    }
149
150    sourceSets.findAll({ it.name.contains("Test") }).forEach { srcSet ->
151        srcSet.languageSettings {
152            it.optIn("kotlinx.serialization.InternalSerializationApi")
153            it.optIn("kotlinx.serialization.ExperimentalSerializationApi")
154        }
155    }
156
157    sourceSets.matching({ it.name.contains("Main") }).all { srcSet ->
158        project.ext.set("kotlin.mpp.freeCompilerArgsForSourceSet.${srcSet.name}", ["-Xexplicit-api=strict"])
159    }
160
161    targets.all {
162        compilations.all {
163            kotlinOptions {
164                if (rootProject.ext.kotlin_lv_override != null) {
165                    languageVersion = rootProject.ext.kotlin_lv_override
166                    freeCompilerArgs += "-Xsuppress-version-warnings"
167                }
168                freeCompilerArgs += "-Xexpect-actual-classes"
169            }
170        }
171        compilations.main {
172            kotlinOptions {
173                allWarningsAsErrors = true
174            }
175        }
176    }
177
178    def targetsWithoutTestRunners = ["linuxArm64", "linuxArm32Hfp"]
179    configure(targets) {
180        // Configure additional binaries to run tests in the background
181        if (["macos", "linux", "mingw"].any { name.startsWith(it) && !targetsWithoutTestRunners.contains(name) }) {
182            binaries {
183                test("background", [nativeDebugBuild]) {
184                    freeCompilerArgs += ["-trw"]
185                }
186            }
187            testRuns {
188                background { setExecutionSourceFrom(binaries.backgroundDebugTest) }
189            }
190        }
191    }
192}
193
194rootProject.extensions.findByType(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension.class).with {
195    // canary nodejs that supports recent Wasm GC changes
196    it.nodeVersion = "21.0.0-v8-canary202309167e82ab1fa2"
197    it.nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
198}