diff --git a/.github/workflows/kotlin.yml b/.github/workflows/kotlin.yml index dfefecf72..238ea50e8 100644 --- a/.github/workflows/kotlin.yml +++ b/.github/workflows/kotlin.yml @@ -25,9 +25,11 @@ jobs: distribution: temurin java-version: 21 - - uses: dtolnay/rust-toolchain@1.88.0 + - uses: dtolnay/rust-toolchain@1.91.0 with: - targets: x86_64-linux-android, aarch64-linux-android, armv7-linux-androideabi, i686-linux-android, aarch64-apple-ios, aarch64-apple-ios-sim, x86_64-apple-ios + targets: x86_64-linux-android, aarch64-linux-android, armv7-linux-androideabi, i686-linux-android, aarch64-apple-ios, aarch64-apple-ios-sim, x86_64-apple-ios, aarch64-apple-darwin, x86_64-apple-darwin, aarch64-unknown-linux-gnu, x86_64-unknown-linux-gnu, aarch64-pc-windows-msvc, x86_64-pc-windows-msvc + # cargo-xwin archives the Windows libraries with the llvm-ar of llvm-tools + components: llvm-tools - name: Install Protoc uses: arduino/setup-protoc@v3 @@ -42,6 +44,11 @@ jobs: run: | cargo install cargo-ndk --version 3.5.4 + # cargo-zigbuild cross builds the Linux libraries of the jvm jar with zig + - name: "Install zig" + run: | + brew install zig + - name: "Build Kotlin Multiplatform bindings" run: | /usr/local/bin/task build:kotlin @@ -51,6 +58,11 @@ jobs: run: | ./gradlew :lib:assemble --console=plain + - name: "Test JVM library" + working-directory: libs/gl-sdk-android + run: | + ./gradlew :lib:jvmTest --console=plain + - name: "Puiblish to Maven" working-directory: libs/gl-sdk-android run: | diff --git a/.gitignore b/.gitignore index 6fc724c0e..111c07c13 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ site target *.egg-info *.dylib +*.dll .mypy_cache # These files are auto generated and may contain sensible data that diff --git a/build/.tasks.yml b/build/.tasks.yml index 57efc6464..d6f4b1bed 100644 --- a/build/.tasks.yml +++ b/build/.tasks.yml @@ -1,5 +1,22 @@ version: "3" +vars: + # cargo-zigbuild and cargo-xwin cross build the Linux and Windows libraries bundled in the jvm jar + CARGO_ZIGBUILD_VERSION: 0.24.0 + CARGO_XWIN_VERSION: 0.23.1 + CROSS_TOOLS_ROOT: target/cross-tools + CROSS_TOOLS_BIN: "{{.ROOT_DIR}}/{{.CROSS_TOOLS_ROOT}}/bin" + # The Windows libraries link the C runtime statically, so they load without the Visual C++ Redistributable + WINDOWS_RUSTFLAGS: -C target-feature=+crt-static + +env: + # The MSVC CRT and Windows SDK cargo-xwin builds the Windows libraries with. An existing cargo-xwin cache + # is reused when it has the needed arches, whatever its versions, so each pair gets its own cache. + XWIN_CRT_VERSION: 14.44.17.14 + XWIN_SDK_VERSION: 10.0.26100 + XWIN_CACHE_DIR: + sh: echo "$HOME/Library/Caches/cargo-xwin/crt-$XWIN_CRT_VERSION-sdk-$XWIN_SDK_VERSION" + tasks: android: deps: @@ -15,14 +32,19 @@ tasks: - android - ios - ios-sim + - jvm vars: OS_NAME: sh: uname | tr '[:upper:]' '[:lower:]' # gobley 0.3.7 misreads u8/u16 return values on 32-bit ARM Android, which fails the UniFFI # checksum check once the app is AOT-compiled (https://github.com/gobley/gobley/issues/252). - # This revision carries the fix proposed upstream in https://github.com/gobley/gobley/pull/297. + # On the JVM it looks the library up by name in the system library paths, the working directory + # included on macOS and Windows, before the one bundled in the jar, so a library placed there + # would be loaded instead. This revision of the fork's main branch carries the fixes proposed + # upstream in https://github.com/gobley/gobley/pull/297 and, for the JVM library lookup, in + # https://github.com/gobley/gobley/pull/300. GOBLEY_GIT: https://github.com/angelix/gobley - GOBLEY_REV: 40cef27c59829ca97a71945928028f9ab4880305 + GOBLEY_REV: 2af41b487abc4546a5eebdc70f01c0c42855bc7c GOBLEY_ROOT: target/gobley cmds: - cargo install --locked --git {{.GOBLEY_GIT}} --rev {{.GOBLEY_REV}} --root {{.GOBLEY_ROOT}} --bin gobley-uniffi-bindgen gobley-uniffi-bindgen @@ -84,3 +106,95 @@ tasks: env: *sdk_env cmds: - IPHONEOS_DEPLOYMENT_TARGET=12.0 MACOSX_DEPLOYMENT_TARGET=11.0 cargo build --release --target {{.ARCH}}-apple-ios-sim -p gl-sdk + + # Builds the desktop libraries and puts each in the jvm resources directory named after its + # platform, for ex. darwin-aarch64, where JNA looks for it + jvm: + deps: + - jvm-darwin-aarch64 + - jvm-darwin-x86_64 + - jvm-linux-aarch64 + - jvm-linux-x86_64 + - jvm-windows + vars: + RESOURCES: libs/gl-sdk-android/lib/src/jvmMain/resources + cmds: + - mkdir -p {{.RESOURCES}}/darwin-aarch64 {{.RESOURCES}}/darwin-x86-64 {{.RESOURCES}}/linux-aarch64 {{.RESOURCES}}/linux-x86-64 {{.RESOURCES}}/win32-aarch64 {{.RESOURCES}}/win32-x86-64 + - cp -a target/aarch64-apple-darwin/release/libglsdk.dylib {{.RESOURCES}}/darwin-aarch64/ + - cp -a target/x86_64-apple-darwin/release/libglsdk.dylib {{.RESOURCES}}/darwin-x86-64/ + - cp -a target/aarch64-unknown-linux-gnu/release/libglsdk.so {{.RESOURCES}}/linux-aarch64/ + - cp -a target/x86_64-unknown-linux-gnu/release/libglsdk.so {{.RESOURCES}}/linux-x86-64/ + - cp -a target/aarch64-pc-windows-msvc/release/glsdk.dll {{.RESOURCES}}/win32-aarch64/ + - cp -a target/x86_64-pc-windows-msvc/release/glsdk.dll {{.RESOURCES}}/win32-x86-64/ + + # Installs the cargo subcommands that cross build the Linux and Windows libraries. cargo-zigbuild + # also requires zig (`brew install zig`), and cargo-xwin the llvm-ar of the llvm-tools component + # (`rustup component add llvm-tools`), which it links as llvm-lib. + cross-tools: + run: once + cmds: + - cargo install --locked --root {{.CROSS_TOOLS_ROOT}} cargo-zigbuild@{{.CARGO_ZIGBUILD_VERSION}} cargo-xwin@{{.CARGO_XWIN_VERSION}} + + # Builds a macOS dylib. cargo strips with rust-objcopy, which in rustc 1.91 does not pad the string + # table to 8 bytes after an indirect symbol table with an odd number of entries, and dyld on macOS 27 + # refuses to load the resulting dylib: Apple's strip is used instead, by its full path, as the strip + # first on PATH can be llvm-strip, with the same bug, or GNU strip, without Mach-O support. It removes + # the local symbols too, loading the library needs only the exported ones. + jvm-darwin-*: + vars: + ARCH: '{{ index .MATCH 0 }}' + env: *sdk_env + cmds: + - MACOSX_DEPLOYMENT_TARGET=11.0 CARGO_PROFILE_RELEASE_STRIP=false cargo build --release --target {{.ARCH}}-apple-darwin -p gl-sdk + - /usr/bin/strip -x target/{{.ARCH}}-apple-darwin/release/libglsdk.dylib + + # Cross builds a Linux library with zig, linking glibc 2.17 so it loads on older distributions too + jvm-linux-*: + vars: + ARCH: '{{ index .MATCH 0 }}' + deps: + - cross-tools + env: *sdk_env + cmds: + - PATH="{{.CROSS_TOOLS_BIN}}:$PATH" cargo zigbuild --release --target {{.ARCH}}-unknown-linux-gnu.2.17 -p gl-sdk + + # Builds the Windows libraries one after the other: every cargo-xwin run recreates the clang-cl + # symlink in its shared cache, and two runs at once fail with "Failed to setup clang-cl symlink" + jvm-windows: + cmds: + - task: jvm-windows-x86_64 + - task: jvm-windows-aarch64 + + # Cross builds the Windows x86_64 library, the MSVC CRT and Windows SDK are downloaded on first use. + # The static CRT flag is appended to RUSTFLAGS: cargo-xwin, like cargo, ignores the target rustflags + # when RUSTFLAGS is set, so a caller's RUSTFLAGS would drop it. + jvm-windows-x86_64: + deps: + - cross-tools + env: *sdk_env + cmds: + - PATH="{{.CROSS_TOOLS_BIN}}:$PATH" RUSTFLAGS="${RUSTFLAGS:-} {{.WINDOWS_RUSTFLAGS}}" cargo xwin build --release --target x86_64-pc-windows-msvc -p gl-sdk + + # Cross builds the Windows arm64 library, the MSVC CRT and Windows SDK are downloaded on first use. + # On this target ring compiles its C code with clang, which does not take the clang-cl /imsvc flags + # cargo-xwin gives the SDK headers with: they are given in INCLUDE instead, where clang and clang-cl + # both find them. + jvm-windows-aarch64: + deps: + - cross-tools + env: *sdk_env + cmds: + - | + set -eu -o pipefail + export PATH="{{.CROSS_TOOLS_BIN}}:$PATH" + # Appended to RUSTFLAGS as on x86_64: cargo xwin env folds RUSTFLAGS, the caller's included, + # into the target rustflags + export RUSTFLAGS="${RUSTFLAGS:-} {{.WINDOWS_RUSTFLAGS}}" + # Assigned first, so a failing cargo xwin env stops the script: eval would ignore its exit status + xwin_env=$(cargo xwin env --target aarch64-pc-windows-msvc) + eval "$xwin_env" + # cargo xwin env exports an empty RUSTFLAGS, which cargo would use instead of the target rustflags + unset RUSTFLAGS + export INCLUDE=$(echo "$CFLAGS_aarch64_pc_windows_msvc" | grep -o '/imsvc [^ ]*' | cut -d' ' -f2 | paste -sd ';' -) + export CFLAGS_aarch64_pc_windows_msvc=$(echo "$CFLAGS_aarch64_pc_windows_msvc" | sed -E 's#/imsvc [^ ]+##g') + cargo build --release --target aarch64-pc-windows-msvc -p gl-sdk diff --git a/gitlab/deploy-maven.yml b/gitlab/deploy-maven.yml index d6f35309a..f97141fa1 100644 --- a/gitlab/deploy-maven.yml +++ b/gitlab/deploy-maven.yml @@ -5,6 +5,13 @@ build_kotlin: rules: - if: "$CI_COMMIT_TAG" - if: '$CI_COMMIT_BRANCH == "main"' + before_script: + # The desktop libraries bundled in the jvm jar + - rustup target add aarch64-apple-darwin x86_64-apple-darwin aarch64-unknown-linux-gnu x86_64-unknown-linux-gnu aarch64-pc-windows-msvc x86_64-pc-windows-msvc + # cargo-xwin archives the Windows libraries with the llvm-ar of llvm-tools + - rustup component add llvm-tools + # cargo-zigbuild cross builds the Linux libraries with zig + - command -v zig >/dev/null || brew install zig script: - task build:kotlin artifacts: diff --git a/libs/gl-sdk-android/gradle/libs.versions.toml b/libs/gl-sdk-android/gradle/libs.versions.toml index ac8ebc77c..d635736f8 100644 --- a/libs/gl-sdk-android/gradle/libs.versions.toml +++ b/libs/gl-sdk-android/gradle/libs.versions.toml @@ -6,7 +6,7 @@ annotation = "1.9.1" kotlin = "2.2.21" kotlinx-serialization = "1.9.0" kotlinx-coroutines = "1.11.0" -android-gradle-plugin = "9.4.1" +android-gradle-plugin = "9.5.1" okio = "3.18.2" espresso = "3.7.0" jna = "5.19.1" diff --git a/libs/gl-sdk-android/gradle/wrapper/gradle-wrapper.jar b/libs/gl-sdk-android/gradle/wrapper/gradle-wrapper.jar index eddabd2ee..9fa7f2882 100644 Binary files a/libs/gl-sdk-android/gradle/wrapper/gradle-wrapper.jar and b/libs/gl-sdk-android/gradle/wrapper/gradle-wrapper.jar differ diff --git a/libs/gl-sdk-android/gradle/wrapper/gradle-wrapper.properties b/libs/gl-sdk-android/gradle/wrapper/gradle-wrapper.properties index ad7845be3..91c4fc64a 100644 --- a/libs/gl-sdk-android/gradle/wrapper/gradle-wrapper.properties +++ b/libs/gl-sdk-android/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-9.7.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.8.2-bin.zip networkTimeout=10000 retries=0 retryBackOffMs=500 diff --git a/libs/gl-sdk-android/gradlew.bat b/libs/gl-sdk-android/gradlew.bat index 8508ef684..03754c4c3 100644 --- a/libs/gl-sdk-android/gradlew.bat +++ b/libs/gl-sdk-android/gradlew.bat @@ -26,6 +26,33 @@ @rem Set local scope for the variables, and ensure extensions are enabled setlocal EnableExtensions +@rem Catch executions from older scripts and ensure they exit cleanly. +@rem This can be removed once we can be reasonably confident that few people +@rem will be migrating directly to this new wrapper. +goto afterSafetyNet +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +goto exitWithErrorLevel +:afterSafetyNet + set DIRNAME=%~dp0 if "%DIRNAME%"=="" set DIRNAME=. @rem This is normally unused @@ -45,13 +72,14 @@ set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 if %ERRORLEVEL% equ 0 goto execute -echo. 1>&2 -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 -echo. 1>&2 -echo Please set the JAVA_HOME variable in your environment to match the 1>&2 -echo location of your Java installation. 1>&2 +1>&2 echo. +1>&2 echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +1>&2 echo. +1>&2 echo Please set the JAVA_HOME variable in your environment to match the +1>&2 echo location of your Java installation. "%COMSPEC%" /c exit 1 +goto exitWithErrorLevel :findJavaFromJavaHome set JAVA_HOME=%JAVA_HOME:"=% @@ -59,13 +87,14 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe if exist "%JAVA_EXE%" goto execute -echo. 1>&2 -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 -echo. 1>&2 -echo Please set the JAVA_HOME variable in your environment to match the 1>&2 -echo location of your Java installation. 1>&2 +1>&2 echo. +1>&2 echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +1>&2 echo. +1>&2 echo Please set the JAVA_HOME variable in your environment to match the +1>&2 echo location of your Java installation. "%COMSPEC%" /c exit 1 +goto exitWithErrorLevel :execute @rem Setup the command line @@ -75,8 +104,9 @@ echo location of your Java installation. 1>&2 @rem Execute gradlew @rem endlocal doesn't take effect until after the line is parsed and variables are expanded @rem which allows us to clear the local environment before executing the java command -endlocal & "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* & call :exitWithErrorLevel +endlocal & "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* & call :exitWithErrorLevel & goto exitWithErrorLevel +@rem This label must not be changed. We rely on old scripts being able to jump to this point. :exitWithErrorLevel @rem Use "%COMSPEC%" /c exit to allow operators to work properly in scripts "%COMSPEC%" /c exit %ERRORLEVEL% diff --git a/libs/gl-sdk-android/lib/build.gradle.kts b/libs/gl-sdk-android/lib/build.gradle.kts index 2b7b49064..bb33d9ddb 100644 --- a/libs/gl-sdk-android/lib/build.gradle.kts +++ b/libs/gl-sdk-android/lib/build.gradle.kts @@ -31,7 +31,15 @@ kotlin { } } - jvm() + jvm { + compilerOptions { + jvmTarget.set(JvmTarget.JVM_17) + // Compile against the Java 17 API, not the one of the JDK running Gradle + freeCompilerArgs.add("-Xjdk-release=17") + } + // JNA loads the native libs with System::load, a restricted method that warns on Java 24 or later + testRuns["test"].executionTask.configure { jvmArgs("--enable-native-access=ALL-UNNAMED") } + } val xcf = XCFramework() @@ -93,7 +101,7 @@ kotlin { } } -val libraryVersion: String by project +val libraryVersion = project.property("libraryVersion") as String mavenPublishing { coordinates(groupId = "com.blockstream", artifactId = "glsdk", version = libraryVersion) diff --git a/libs/gl-sdk/CHANGELOG.md b/libs/gl-sdk/CHANGELOG.md index eb98cc3bb..54517f82e 100644 --- a/libs/gl-sdk/CHANGELOG.md +++ b/libs/gl-sdk/CHANGELOG.md @@ -10,8 +10,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - iOS (Kotlin and Swift): a Rust panic in the SDK reaches the app as an exception instead of aborting it, as it already does on Android. The Android and iOS libraries unwind on panic, unlike the workspace's release profile. +### Security + +- JVM (Kotlin): the native library bundled in the `glsdk-jvm` artifact is loaded by its absolute path. JNA looked the library up by name first, so a library with the same name in the working directory (macOS, Windows), on `PATH` (Windows) or in `LD_LIBRARY_PATH` was loaded instead and received every call into the SDK, the mnemonic included. + ### Fixed +- JVM (Kotlin): the `glsdk-jvm` artifact bundles the native library for macOS, Linux (glibc 2.17 or later) and Windows (no Visual C++ Redistributable needed), each on x86_64 and arm64. It shipped without one, so loading the SDK failed with `UnsatisfiedLinkError`. - Android: the native libraries are built in release mode. They were unoptimized debug builds with debug assertions, and the `arm64-v8a` `libglsdk.so` shrinks from 29.9 MB to 11.0 MB. ## [0.5.0] - 2026-10-12 diff --git a/libs/gl-sdk/README.md b/libs/gl-sdk/README.md index 1ede38580..56831d600 100644 --- a/libs/gl-sdk/README.md +++ b/libs/gl-sdk/README.md @@ -105,6 +105,31 @@ perl -pi -e 's/NodeBuilder::register\(/NodeBuilder::register_node\(/g; s/Schedul The `task sdk:bindings-cpp` command handles all of the above automatically, including platform detection. +## Kotlin Multiplatform + +Build the Kotlin Multiplatform package (Android, iOS, iOS simulator and JVM) in `libs/gl-sdk-android`: + +```bash +task build:kotlin +``` + +The JVM artifact bundles the native library for macOS, Linux and Windows, each on x86_64 and arm64. +They are built on macOS: the Linux libraries are cross compiled with [cargo-zigbuild](https://github.com/rust-cross/cargo-zigbuild), +which requires zig, and the Windows libraries with [cargo-xwin](https://github.com/rust-cross/cargo-xwin), +which archives with the `llvm-ar` of the `llvm-tools` component: + +```bash +brew install zig +rustup component add llvm-tools +``` + +On first use cargo-xwin downloads the MSVC CRT and the Windows SDK (about 1.1 GB, cached in `~/Library/Caches/cargo-xwin`). +Building the Windows libraries accepts their [license](https://go.microsoft.com/fwlink/?LinkId=2086102). + +### JVM + +JNA loads the native library with `System::load`, a restricted method: on Java 24 or later it prints a warning unless the application runs with `--enable-native-access=ALL-UNNAMED`. + ## Synchronous API Design The entire public API exposed by the SDK through UniFFI is