Skip to content

Commit

Permalink
Add support for Wasm!
Browse files Browse the repository at this point in the history
Also, clean up our build script approach while we're at it. Instead of
forcing myself to stay on an old version of the multiplatform plugin
(and its associated older compiler), now we use the latest version of
the multiplatform plugin but:

1) set the language and api versions to control output compatibility
2) add an explicit dependency on an older version of the stdlib (otherwise,
   even if our generated bytecode is for an older version, we might include
   stdlib bytecode built with a newer compiler)

Fixes #10
  • Loading branch information
bitspittle committed Jan 19, 2025
1 parent 0b139f1 commit 907d2c7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/gradle-test-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,5 @@ jobs:
path: ~/.konan
key: kotlin-native-compiler-${{ runner.OS }}

# This is necessary because older versions of Kotlin have issues that
# fail to run tests on Mac targets.
- name: Override Kotlin Version for testing
run: echo "KOTLIN_VERSION_OVERRIDE=2.0.0" >> $GITHUB_ENV

- name: Test with Gradle
run: ./gradlew allTests
2 changes: 2 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ jobs:
(matrix.os == 'ubuntu-latest' && inputs.target-js)
run: |
./gradlew publishJsPublicationToGCloudMavenRepository
./gradlew publishWasmJsPublicationToCloudMavenRepository
- name: Publish Linux (GCloud)
if: |
Expand Down Expand Up @@ -204,6 +205,7 @@ jobs:
(matrix.os == 'ubuntu-latest' && inputs.target-js)
run: |
./gradlew publishJsPublicationToSonatypeMavenRepository
./gradlew publishWasmJsPublicationToSonatypeMavenRepository
- name: Publish Linux (Sonatype)
if: |
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
![coverage badge](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/bitspittle/01b6bfe88483946d9f5438f5616d9b9f/raw/truthish-coverage-badge.json)
<br>
![kotlin version](https://img.shields.io/badge/kotlin_compatibility-1.6+-lightgray?logo=kotlin)
![targets](https://img.shields.io/badge/targets-JVM,_JS,_Win,_Linux,_Mac,_Android,_iOS-white.svg)
![targets](https://img.shields.io/badge/targets-JVM,_JS,_Win,_Linux,_Mac,_Android,_iOS,_Wasm-white.svg)
<br>
<a href="https://discord.gg/bCdxPr7aTV">
<img alt="Varabyte Discord" src="https://img.shields.io/discord/886036660767305799.svg?label=&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2" />
Expand Down Expand Up @@ -115,6 +115,11 @@ kotlin {
browser()
nodeJs()
}
wasmJs {
browser()
nodeJs()
d8()
}

linuxX64()
macosArm64() // Mac M1+
Expand Down
61 changes: 48 additions & 13 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
@file:OptIn(ExperimentalWasmDsl::class)

import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.plugin.LanguageSettingsBuilder
import javax.xml.parsers.DocumentBuilderFactory

plugins {
// NOTE: Intentionally older version to maximize compatibility with projects in the wild
// Going much older and this buildscript won't compile.
// NOTE2: We allow the CI to override the Kotlin version that we use, because otherwise this or that test fails due
// to missing features in the Kotlin multiplatform plugin.
kotlin("multiplatform") version (System.getenv("KOTLIN_VERSION_OVERRIDE") ?: "1.7.21")
kotlin("multiplatform") version "2.1.0"
id("org.jetbrains.dokka") version "1.9.20"
id("org.jetbrains.kotlinx.kover") version "0.8.3"
`maven-publish`
Expand Down Expand Up @@ -57,20 +58,30 @@ tasks.register("printLineCoverage") {
}
}

fun LanguageSettingsBuilder.setToVersion(version: String) {
check(version.count { it == '.' } == 2)
val withoutPatch = version.substringBeforeLast('.')
languageVersion = withoutPatch
apiVersion = withoutPatch
}

kotlin {
jvmToolchain(11) // Used by Android to set JDK version used by kotlin compilation

jvm {
val main by compilations.getting {
kotlinOptions {
jvmTarget = "11"
}
compilerOptions {
jvmTarget.set(JvmTarget.JVM_11)
}
}
js(IR) {
js {
browser()
nodejs()
}
wasmJs {
browser()
nodejs()
d8()
}

linuxX64()
macosArm64() // Mac M1+
Expand All @@ -79,16 +90,40 @@ kotlin {
iosX64() // iOS Intel
iosArm64() // iOS M1+
iosSimulatorArm64()
android {
androidTarget {
publishLibraryVariants("release")
}

sourceSets {
val commonTest by getting {
// Anything lower and: "API version 1.x is no longer supported; please, use version 1.6 or greater."
// We also use "buildMap" from 1.6
val targetKotlinVersion = "1.6.21"

all {
languageSettings {
setToVersion(targetKotlinVersion)
}
}

commonMain.dependencies {
implementation(kotlin("stdlib", targetKotlinVersion))
}

wasmJsMain {
// Wasm needs to use 1.9+ or else compilation dies.
// This exception is not a big deal since Wasm didn't really exist until the end of 1.8 anyway
val wasmKotlinVersion = "1.9.25"
languageSettings {
setToVersion(wasmKotlinVersion)
}

dependencies {
implementation(kotlin("test"))
implementation(kotlin("stdlib", wasmKotlinVersion))
}
}
commonTest.dependencies {
implementation(kotlin("test"))
}
}
}

Expand Down

0 comments on commit 907d2c7

Please sign in to comment.