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
16 changes: 14 additions & 2 deletions .github/workflows/kotlin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ site
target
*.egg-info
*.dylib
*.dll
.mypy_cache

# These files are auto generated and may contain sensible data that
Expand Down
118 changes: 116 additions & 2 deletions build/.tasks.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions gitlab/deploy-maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion libs/gl-sdk-android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Binary file modified libs/gl-sdk-android/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
52 changes: 41 additions & 11 deletions libs/gl-sdk-android/gradlew.bat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions libs/gl-sdk-android/lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions libs/gl-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions libs/gl-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading