From 4a5648b773b9bcf706eee5936338f727dc8b25bc Mon Sep 17 00:00:00 2001 From: igoriakovlev <54274820+igoriakovlev@users.noreply.github.com> Date: Tue, 21 Nov 2023 16:54:17 +0100 Subject: [PATCH] Add Wasm WASI target (#2510) This is implementation of Wasm WASI target support. Please notice, that due to lack of support this target in okio we are forced to escape building this formatter and all formatters unit tests for WASI. We hope that WasmJS target (i.e. backend and compiler plugin) are fully similar to the WasmWASI target so the library functionality is fully covered by WasmJS. --- build.gradle | 4 ++ formats/json/build.gradle | 3 ++ gradle/configure-source-sets.gradle | 49 ++++++++++++++++--- integration-test/build.gradle | 26 +++++++++- .../wasmJsMain/kotlin/sample/SampleWasm.kt | 4 +- .../kotlin/sample/SampleTestsWasm.kt | 2 +- .../wasmWasiMain/kotlin/sample/SampleWasm.kt | 21 ++++++++ .../kotlin/sample/SampleTestsWasm.kt | 11 +++++ 8 files changed, 108 insertions(+), 12 deletions(-) create mode 100644 integration-test/src/wasmWasiMain/kotlin/sample/SampleWasm.kt create mode 100644 integration-test/src/wasmWasiTest/kotlin/sample/SampleTestsWasm.kt diff --git a/build.gradle b/build.gradle index 207c115769..34765ef256 100644 --- a/build.gradle +++ b/build.gradle @@ -249,3 +249,7 @@ apply from: rootProject.file("gradle/benchmark-parsing.gradle") tasks.named("dokkaHtmlMultiModule") { pluginsMapConfiguration.set(["org.jetbrains.dokka.base.DokkaBase": """{ "templatesDir": "${projectDir.toString().replace('\\', '/')}/dokka-templates" }"""]) } + +tasks.withType(org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask).configureEach { + args.add("--ignore-engines") +} \ No newline at end of file diff --git a/formats/json/build.gradle b/formats/json/build.gradle index a71366f5b0..d35bcb5dc1 100644 --- a/formats/json/build.gradle +++ b/formats/json/build.gradle @@ -42,6 +42,9 @@ kotlin { wasmJsMain { dependsOn(sourceSets.jsWasmMain) } + wasmWasiMain { + dependsOn(sourceSets.jsWasmMain) + } } } diff --git a/gradle/configure-source-sets.gradle b/gradle/configure-source-sets.gradle index e763580a68..740a475a32 100644 --- a/gradle/configure-source-sets.gradle +++ b/gradle/configure-source-sets.gradle @@ -14,6 +14,11 @@ tasks.withType(JavaCompile).configureEach { options.release = 8 } +// 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. +// 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. +// 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. +Boolean isOkIoOrFormatTests = (project.name == 'kotlinx-serialization-json-okio' || project.name == 'kotlinx-serialization-json-tests') + kotlin { jvm { withJava() @@ -43,7 +48,13 @@ kotlin { } wasmJs { - d8() + nodejs() + } + + if (!isOkIoOrFormatTests) { + wasmWasi { + nodejs() + } } sourceSets.all { @@ -96,25 +107,43 @@ kotlin { } } + create("wasmMain") { + dependsOn(commonMain) + } + create("wasmTest") { + dependsOn(commonTest) + } wasmJsMain { - kotlin { - srcDir 'wasmMain/src' - } + dependsOn(wasmMain) dependencies { api 'org.jetbrains.kotlin:kotlin-stdlib-wasm-js' } } wasmJsTest { - kotlin { - srcDir 'wasmTest/src' - } + dependsOn(wasmTest) dependencies { api 'org.jetbrains.kotlin:kotlin-test-wasm-js' } } + if (!isOkIoOrFormatTests) { + wasmWasiMain { + dependsOn(wasmMain) + dependencies { + api 'org.jetbrains.kotlin:kotlin-stdlib-wasm-wasi' + } + } + + wasmWasiTest { + dependsOn(wasmTest) + dependencies { + api 'org.jetbrains.kotlin:kotlin-test-wasm-wasi' + } + } + } + nativeMain.dependencies { } } @@ -162,3 +191,9 @@ kotlin { } } } + +rootProject.extensions.findByType(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension.class).with { + // canary nodejs that supports recent Wasm GC changes + it.nodeVersion = "21.0.0-v8-canary202309167e82ab1fa2" + it.nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary" +} \ No newline at end of file diff --git a/integration-test/build.gradle b/integration-test/build.gradle index 225181b859..5edec74428 100644 --- a/integration-test/build.gradle +++ b/integration-test/build.gradle @@ -42,7 +42,10 @@ kotlin { } } wasmJs { - d8() + nodejs() + } + wasmWasi { + nodejs() } jvm { withJava() @@ -102,12 +105,21 @@ kotlin { api 'org.jetbrains.kotlin:kotlin-stdlib-wasm-js' } } - wasmJsTest { dependencies { api 'org.jetbrains.kotlin:kotlin-test-wasm-js' } } + wasmWasiMain { + dependencies { + api 'org.jetbrains.kotlin:kotlin-stdlib-wasm-wasi' + } + } + wasmWasiTest { + dependencies { + api 'org.jetbrains.kotlin:kotlin-test-wasm-wasi' + } + } } targets.all { @@ -129,3 +141,13 @@ dependencies { } task run dependsOn "check" + +rootProject.extensions.findByType(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension.class).with { + // canary nodejs that supports recent Wasm GC changes + it.nodeVersion = "21.0.0-v8-canary202309167e82ab1fa2" + it.nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary" +} + +tasks.withType(org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask).configureEach { + args.add("--ignore-engines") +} \ No newline at end of file diff --git a/integration-test/src/wasmJsMain/kotlin/sample/SampleWasm.kt b/integration-test/src/wasmJsMain/kotlin/sample/SampleWasm.kt index 63190362f6..4d01cf47e7 100644 --- a/integration-test/src/wasmJsMain/kotlin/sample/SampleWasm.kt +++ b/integration-test/src/wasmJsMain/kotlin/sample/SampleWasm.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019 JetBrains s.r.o. + * Copyright 2023 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,5 +17,5 @@ package sample actual object Platform { - actual val name: String = "Wasm" + actual val name: String = "WasmJs" } diff --git a/integration-test/src/wasmJsTest/kotlin/sample/SampleTestsWasm.kt b/integration-test/src/wasmJsTest/kotlin/sample/SampleTestsWasm.kt index 82dfb787b9..c5fd1a2f68 100644 --- a/integration-test/src/wasmJsTest/kotlin/sample/SampleTestsWasm.kt +++ b/integration-test/src/wasmJsTest/kotlin/sample/SampleTestsWasm.kt @@ -6,6 +6,6 @@ import kotlin.test.assertTrue class SampleTestsWasm { @Test fun testHello() { - assertTrue("Wasm" in hello()) + assertTrue("WasmJs" in hello()) } } diff --git a/integration-test/src/wasmWasiMain/kotlin/sample/SampleWasm.kt b/integration-test/src/wasmWasiMain/kotlin/sample/SampleWasm.kt new file mode 100644 index 0000000000..e461406bf5 --- /dev/null +++ b/integration-test/src/wasmWasiMain/kotlin/sample/SampleWasm.kt @@ -0,0 +1,21 @@ +/* + * Copyright 2023 JetBrains s.r.o. + * + * 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. + */ + +package sample + +actual object Platform { + actual val name: String = "WasmWasi" +} diff --git a/integration-test/src/wasmWasiTest/kotlin/sample/SampleTestsWasm.kt b/integration-test/src/wasmWasiTest/kotlin/sample/SampleTestsWasm.kt new file mode 100644 index 0000000000..0ba180b225 --- /dev/null +++ b/integration-test/src/wasmWasiTest/kotlin/sample/SampleTestsWasm.kt @@ -0,0 +1,11 @@ +package sample + +import kotlin.test.Test +import kotlin.test.assertTrue + +class SampleTestsWasm { + @Test + fun testHello() { + assertTrue("WasmWasi" in hello()) + } +}