Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/test-desktop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Kotlin/Native desktop

# The example's suites in Kotlin/Native executables on macOS, Linux and Windows, each on its own host (a native image
# builds on its own platform): with the façades in a GraalVM native image (the default) and in a JDK's JVM. Kotlin/Native
# doesn't run on Linux arm64 hosts: linuxArm64's test executable is linked on Linux x64, not run
on:
pull_request:
workflow_dispatch:

jobs:
test:
strategy:
fail-fast: false
matrix:
host:
- { os: macos-15, target: macosArm64 }
- { os: ubuntu-latest, target: linuxX64, also: linkDebugTestLinuxArm64 }
- { os: windows-latest, target: mingwX64 }
runtime: [GRAALVM, JVM]
runs-on: ${{ matrix.host.os }}
timeout-minutes: 90
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 25
- uses: gradle/actions/setup-gradle@v4
# native-image's C toolchain on Windows: Visual Studio's
- uses: ilammy/msvc-dev-cmd@v1
if: runner.os == 'Windows'
- name: Tests
shell: bash
env:
GITHUB_TOKEN: ${{ github.token }}
run: ./gradlew :examples:calculator:${{ matrix.host.target }}Test ${{ matrix.host.also && format(':examples:calculator:{0}', matrix.host.also) || '' }} -Pnja.runtime=${{ matrix.runtime }} --stacktrace
- uses: actions/upload-artifact@v4
if: always()
with:
name: desktop-${{ matrix.host.target }}-${{ matrix.runtime }}
path: examples/calculator/build/reports/tests/**
4 changes: 3 additions & 1 deletion .github/workflows/test-graalvm-android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ jobs:
(yes || true) | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" "ndk;27.2.12479018" > /dev/null || true
echo "ANDROID_NDK_HOME=$ANDROID_HOME/ndk/27.2.12479018" >> "$GITHUB_ENV"
# native-image's C queries for arm64 run on this x64 runner through qemu-user
sudo apt-get update -q && sudo apt-get install -yq qemu-user-static binfmt-support
# (qemu-user-static until Ubuntu 25.10; qemu-user-binfmt from 26.04, whose qemu-user is static)
sudo apt-get update -q
sudo apt-get install -yq qemu-user-static binfmt-support || sudo apt-get install -yq qemu-user-binfmt
- name: Test APKs
env:
GITHUB_TOKEN: ${{ github.token }}
Expand Down
353 changes: 204 additions & 149 deletions README.md

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion examples/calculator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ kotlin {
}
iosSimulatorArm64()
iosArm64()
// Kotlin/Native on the desktop: the same suites, the runtime next to the test executable
macosArm64()
macosX64()
linuxX64()
linuxArm64()
mingwX64()
// The desktop: the façades in this JVM, the same suites
jvm()

Expand Down Expand Up @@ -43,7 +49,7 @@ nucleusJvm {
// A second façade in the same JVM: NJA's own stress tests
facade(project(":examples:torture-facade"))
jvmOptions.addAll("-Xss2m", "-Xmx512m")
// The same suites on a GraalVM native image: -Pnja.runtime=GRAALVM
// The same suites on a JVM (jdk-mobile's on iOS and Android, a JDK's on the desktop): -Pnja.runtime=JVM
providers.gradleProperty("nja.runtime").orNull?.let { runtime.set(dev.nucleusframework.nja.plugin.NjaRuntimeType.valueOf(it)) }
// -Pnja.androidAbis=arm64-v8a,x86_64
providers.gradleProperty("nja.androidAbis").orNull?.let { androidAbis.set(it.split(',').toSet()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ internal actual fun appFaultStorm(count: Int): Int {
}
return caught + (if (missing == null) 0 else 0)
}

internal actual val isDesktopNative: Boolean = false
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import kotlin.time.TimeSource
class NjaTortureTest {
private val jvm = JvmTorture()

private val isAndroid = jvm.vm().name.contains("Server")
private val isAndroid = !isDesktopNative && jvm.vm().name.contains("Server")

@Test
fun `the JVM is the expected one`() {
Expand All @@ -42,6 +42,10 @@ class NjaTortureTest {
} else if (vm.name.contains("Substrate")) {
// A GraalVM native image (GraalVM CE 25)
assertTrue(vm.version.startsWith("25"), "$vm")
} else if (isDesktopNative) {
// A JDK's HotSpot (the façades' Java 25 or later)
assertTrue(vm.version.substringBefore('.').toInt() >= 25, "$vm")
assertTrue(vm.name.contains("Server"), "$vm")
} else if (!vm.name.contains("Dalvik")) {
// jdk-mobile's JVM (on ART, the façades run as they are)
assertTrue(vm.version.startsWith("27"), "$vm")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ internal expect fun platformGetenv(name: String): String?
/** On a JVM target, the version of the JVM the tests (and so the façades) run in; on the mobile ones, null (jdk-mobile). */
internal expect fun hostJvmVersion(): String?

/** Kotlin/Native on macOS, Linux or Windows: the JVM runtime is a JDK's HotSpot. */
internal expect val isDesktopNative: Boolean

object System {
private val origin = TimeSource.Monotonic.markNow()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ internal actual fun appFaultStorm(count: Int): Int {
}
return caught + (if (missing == null) 0 else 0)
}

internal actual val isDesktopNative: Boolean = false
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.StableRef
import kotlinx.cinterop.alloc
import kotlinx.cinterop.asStableRef
import kotlinx.cinterop.convert
import kotlinx.cinterop.memScoped
import kotlinx.cinterop.nativeHeap
import kotlinx.cinterop.ptr
Expand All @@ -25,6 +26,8 @@ import platform.posix.sched_yield
import platform.posix.timespec
import kotlin.concurrent.atomics.AtomicBoolean
import kotlin.experimental.ExperimentalNativeApi
import kotlin.native.OsFamily
import kotlin.native.Platform
import kotlin.native.runtime.GC
import kotlin.native.runtime.NativeRuntimeApi

Expand All @@ -43,7 +46,7 @@ private class Runner(val body: () -> Unit) {
}
}

/** A pthread with a JVM-like 8 MB stack (iOS' default for secondary threads is 512 KB). */
/** A pthread with a JVM-like 8 MB stack (iOS' and macOS' default for secondary threads is 512 KB). */
actual class Thread actual constructor(body: () -> Unit) {
private val runner = Runner(body)
private val handle = nativeHeap.alloc<pthread_tVar>()
Expand All @@ -56,7 +59,7 @@ actual class Thread actual constructor(body: () -> Unit) {
memScoped {
val attr = alloc<pthread_attr_t>()
pthread_attr_init(attr.ptr)
pthread_attr_setstacksize(attr.ptr, 8uL * 1024uL * 1024uL)
pthread_attr_setstacksize(attr.ptr, (8 * 1024 * 1024).convert())
val ref = StableRef.create(runner)
val entry = staticCFunction { arg: COpaquePointer? ->
val r = arg!!.asStableRef<Runner>()
Expand Down Expand Up @@ -90,8 +93,8 @@ actual class Thread actual constructor(body: () -> Unit) {
actual fun sleep(millis: Long, nanos: Int) {
memScoped {
val ts = alloc<timespec>()
ts.tv_sec = millis / 1000
ts.tv_nsec = (millis % 1000) * 1_000_000 + nanos
ts.tv_sec = (millis / 1000).convert()
ts.tv_nsec = ((millis % 1000) * 1_000_000 + nanos).convert()
nanosleep(ts.ptr, null)
}
}
Expand All @@ -113,6 +116,9 @@ internal actual fun platformGetenv(name: String): String? = getenv(name)?.toKStr

internal actual fun hostJvmVersion(): String? = null

internal actual val isDesktopNative: Boolean =
Platform.osFamily == OsFamily.MACOSX || Platform.osFamily == OsFamily.LINUX || Platform.osFamily == OsFamily.WINDOWS

internal actual fun appFaultStorm(count: Int): Int {
var caught = 0
val holder: Any? = if (count < 0) "never" else null
Expand Down
Loading
Loading