diff --git a/.github/workflows/kotlin.yml b/.github/workflows/kotlin.yml new file mode 100644 index 000000000..754b8cf4b --- /dev/null +++ b/.github/workflows/kotlin.yml @@ -0,0 +1,64 @@ +on: + push: + branches: + - master + tags: + - 'gl-sdk-*' + pull_request: {} + +name: Kotlin library + +jobs: + + build: + runs-on: macos-26 + steps: + - name: "Show default version of NDK" + run: echo $ANDROID_NDK_ROOT + + - name: "Check out PR branch" + uses: actions/checkout@v4 + + - name: "Set up JDK" + uses: actions/setup-java@v5 + with: + distribution: temurin + java-version: 21 + + - uses: dtolnay/rust-toolchain@1.85.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 + + - name: Install Protoc + uses: arduino/setup-protoc@v3 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Setup Task + run: | + sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d -b /usr/local/bin + + - name: "Install NDK" + run: | + cargo install cargo-ndk --version 3.5.4 + + - name: "Build Kotlin Multiplatform bindings" + run: | + /usr/local/bin/task build:android + + - name: "Build Android library" + working-directory: libs/gl-sdk-android + run: | + ./gradlew :lib:assemble --console=plain + + - name: "Puiblish to Maven" + working-directory: libs/gl-sdk-android + run: | + ./gradlew :lib:publishToMavenLocal + + - name: Temporarily save artifact + uses: actions/upload-artifact@v4 + with: + name: glsdk-artifact + path: libs/gl-sdk-android/lib/build/outputs/ + retention-days: 14 diff --git a/Taskfile.yml b/Taskfile.yml index d6e3d8f82..d019c4c11 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -1,6 +1,10 @@ version: "3" includes: + build: + taskfile: "build/.tasks.yml" + dir: "." + clnvm: taskfile: "libs/cln-version-manager/.tasks.yml" dir: "libs/cln-version-manager/" @@ -52,10 +56,10 @@ tasks: -t gltesting \ -f docker/gl-testing/Dockerfile \ . - + docker-ci-check: deps: -# - docker-ci-build + - docker-ci-build cmds: - docker run --rm -ti -v $(pwd):/repo gltesting task ci-check diff --git a/build/.tasks.yml b/build/.tasks.yml new file mode 100644 index 000000000..2664a6fdc --- /dev/null +++ b/build/.tasks.yml @@ -0,0 +1,58 @@ +version: "3" + +tasks: + android: + deps: + - linux-android-aarch64 + - linux-androideabi-armv7 + - linux-android-i686 + - linux-android-x86_64 + cmds: + - cp -a target/release/android/jniLibs libs/gl-sdk-android/lib/src/androidMain + + kotlin: + deps: + - android + cmds: + - cargo install --bin gobley-uniffi-bindgen gobley-uniffi-bindgen@0.2.0 + - gobley-uniffi-bindgen --config ./libs/gl-sdk/uniffi.kotlin-multiplatform.toml --library ./target/aarch64-linux-android/debug/libglsdk.a --out-dir target/release/kotlin-multiplatform + - cp -a ./target/release/kotlin-multiplatform/* libs/gl-sdk-android/lib/src/ + + swift: + deps: + - ios + - ios-sim + cmds: + - cargo run --features bindings -- generate --library ./target/aarch64-apple-ios/release/libglsdk.a --language swift --out-dir ./target/swift + - mkdir -p ./target/swift/include + - mv target/swift/glsdkFFI.h target/swift/include + - mv target/swift/glsdkFFI.modulemap target/swift/include/module.modulemap + - xcodebuild -create-xcframework -library target/lipo-ios-sim/release/libglsdk.a -headers target/swift/include -library target/aarch64-apple-ios/release/libglsdk.a -headers target/swift/include -output target/glsdkFFI.xcframework + + linux-*-*: + vars: + ABI: '{{index .MATCH 0}}' + ARCH: '{{index .MATCH 1}}' + cmds: + - cargo ndk -t {{.ARCH}}-linux-{{.ABI}} -o target/release/android/jniLibs build -p gl-sdk + + ios: + deps: + - ios-apple-aarch64 + - ios-apple-x86_64 + + ios-sim: + deps: + - ios-sim-apple-aarch64 + + ios-apple-*: + vars: + ARCH: '{{ index .MATCH 0 }}' + cmds: + - IPHONEOS_DEPLOYMENT_TARGET=12.0 MACOSX_DEPLOYMENT_TARGET=11.0 cargo build --release --target {{.ARCH}}-apple-ios -p gl-sdk + + ios-sim-apple-*: + vars: + ARCH: '{{ index .MATCH 0 }}' + cmds: + - IPHONEOS_DEPLOYMENT_TARGET=12.0 MACOSX_DEPLOYMENT_TARGET=11.0 cargo build --release --target {{.ARCH}}-apple-ios-sim -p gl-sdl diff --git a/libs/gl-sdk-android/build.gradle.kts b/libs/gl-sdk-android/build.gradle.kts new file mode 100644 index 000000000..adfd2a530 --- /dev/null +++ b/libs/gl-sdk-android/build.gradle.kts @@ -0,0 +1,8 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +plugins { + alias(libs.plugins.androidLibrary) apply false + alias(libs.plugins.kotlinMultiplatform) apply false + alias(libs.plugins.kotlinSerialization) apply false + alias(libs.plugins.mavenPublish) apply false + alias(libs.plugins.atomicfu) apply false +} \ No newline at end of file diff --git a/libs/gl-sdk-android/gradle.properties b/libs/gl-sdk-android/gradle.properties new file mode 100644 index 000000000..ca4751c3b --- /dev/null +++ b/libs/gl-sdk-android/gradle.properties @@ -0,0 +1,30 @@ +# Project-wide Gradle settings. +# IDE (e.g. Android Studio) users: +# Gradle settings configured through the IDE *will override* +# any settings specified in this file. +# For more details on how to configure your build environment visit +# http://www.gradle.org/docs/current/userguide/build_environment.html +# Specifies the JVM arguments used for the daemon process. +# The setting is particularly useful for tweaking memory settings. +org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 +# When configured, Gradle will run in incubating parallel mode. +# This option should only be used with decoupled projects. More details, visit +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects +# org.gradle.parallel=true +# AndroidX package structure to make it clearer which packages are bundled with the +# Android operating system, and which are packaged with your app's APK +# https://developer.android.com/topic/libraries/support-library/androidx-rn +android.useAndroidX=true +# Kotlin code style for this project: "official" or "obsolete": +kotlin.code.style=official +# Enables namespacing of each library's R class so that its R class includes only the +# resources declared in the library itself and none from the library's dependencies, +# thereby reducing the size of the R class for that library +android.nonTransitiveRClass=true + +kotlinx.atomicfu.enableJvmIrTransformation=true +kotlinx.atomicfu.enableNativeIrTransformation=true + +kotlin.mpp.enableCInteropCommonization=true + +libraryVersion=0.12.4 diff --git a/libs/gl-sdk-android/gradle/libs.versions.toml b/libs/gl-sdk-android/gradle/libs.versions.toml new file mode 100644 index 000000000..4e501cd66 --- /dev/null +++ b/libs/gl-sdk-android/gradle/libs.versions.toml @@ -0,0 +1,31 @@ +[versions] +annotation = "1.9.1" +kotlin = "2.2.20" +kotlinx-serialization = "1.9.0" +kotlinx-coroutines = "1.10.2" +android-gradle-plugin = "8.13.0" +okio = "3.16.0" +espresso = "3.7.0" +jna = "5.18.1" +junit = "4.13.2" +junit-ext = "1.3.0" +maven-publish = "0.34.0" +atomicfu = "0.29.0" + +[libraries] +annotation = { module = "androidx.annotation:annotation", version.ref = "annotation" } +kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" } +kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" } +kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" } +okio = { module = "com.squareup.okio:okio", version.ref = "okio" } +jna = { module = "net.java.dev.jna:jna", version.ref = "jna" } +espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espresso" } +junit-ext = { module = "androidx.test.ext:junit", version.ref = "junit-ext" } +junit = { module = "junit:junit", version.ref = "junit" } + +[plugins] +androidLibrary = { id = "com.android.library", version.ref = "android-gradle-plugin" } +kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } +kotlinSerialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } +atomicfu = { id = "org.jetbrains.kotlinx.atomicfu", version.ref = "atomicfu" } +mavenPublish = { id = "com.vanniktech.maven.publish", version.ref = "maven-publish" } diff --git a/libs/gl-sdk-android/gradle/wrapper/gradle-wrapper.jar b/libs/gl-sdk-android/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 000000000..e708b1c02 Binary files /dev/null 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 new file mode 100644 index 000000000..d89ffb1a4 --- /dev/null +++ b/libs/gl-sdk-android/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Fri Jan 19 10:34:57 EET 2024 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-9.0.0-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/libs/gl-sdk-android/gradlew b/libs/gl-sdk-android/gradlew new file mode 100755 index 000000000..4f906e0c8 --- /dev/null +++ b/libs/gl-sdk-android/gradlew @@ -0,0 +1,185 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# 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 +# +# https://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. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git a/libs/gl-sdk-android/lib/build.gradle.kts b/libs/gl-sdk-android/lib/build.gradle.kts new file mode 100644 index 000000000..3549b683f --- /dev/null +++ b/libs/gl-sdk-android/lib/build.gradle.kts @@ -0,0 +1,142 @@ +import com.vanniktech.maven.publish.AndroidSingleVariantLibrary +import org.jetbrains.kotlin.gradle.dsl.JvmTarget +import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework + +plugins { + alias(libs.plugins.androidLibrary) + alias(libs.plugins.kotlinMultiplatform) + alias(libs.plugins.kotlinSerialization) + alias(libs.plugins.atomicfu) + alias(libs.plugins.mavenPublish) + signing +} + +kotlin { + androidTarget { + publishLibraryVariants("release") + compilerOptions { jvmTarget.set(JvmTarget.JVM_1_8) } + } + + jvm() + + val xcf = XCFramework() + /* + listOf( + iosArm64(), + iosSimulatorArm64() + ).forEach { + + it.binaries.framework { + baseName = "glsdk" + xcf.add(this) + } + + val platform = when (it.targetName) { + "iosSimulatorArm64" -> "ios_simulator_arm64" + "iosArm64" -> "ios_arm64" + else -> error("Unsupported target $name") + } + + + it.compilations["main"].cinterops { + create("glsdkCInterop") { + defFile(project.file("src/nativeInterop/cinterop/glsdk.def")) + includeDirs(project.file("src/nativeInterop/cinterop/headers/glsdk/"), project.file("src/libs/$platform")) + } + } + } +*/ + compilerOptions.freeCompilerArgs.add("-Xexpect-actual-classes") + + sourceSets { + commonMain.dependencies { + implementation(libs.kotlinx.coroutines.core) + implementation(libs.kotlinx.serialization.core) + implementation(libs.kotlinx.serialization.json) + implementation(libs.annotation) + implementation(libs.okio) + } + commonTest.dependencies { + implementation(kotlin("test")) + } + androidMain.dependencies { + implementation(libs.jna.get()) { + artifact { type = "aar" } + } + } + jvmMain.dependencies { + implementation(libs.jna) + } + androidUnitTest.dependencies { + implementation(libs.junit) + } + androidInstrumentedTest.dependencies { + implementation(libs.junit.ext) + implementation(libs.espresso.core) + } + } + +} + +android { + namespace = "com.blockstream.glsdk_bindings" + compileSdk = 36 + + defaultConfig { + minSdk = 24 + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + consumerProguardFiles("consumer-rules.pro") + } + + buildTypes { + release { + isMinifyEnabled = true + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + } +} + +val libraryVersion: String by project + +mavenPublishing { + coordinates(groupId = "com.blockstream", artifactId = "glsdk", version = libraryVersion) + + pom { + name = "glsdk" + description = "GreenlightSDK" + url = "https://blockstream.com" + licenses { + license { + name = "BSD-MIT" + url = "https://github.com/blockstream/glsdk/blob/main/LICENSE" + } + } + developers { + developer { + id = "cdecker" + name = "Christian Decker" + email = "decker@blockstream.com" + } + } + scm { + connection = "scm:git:github.com/blockstream/glsdk.git" + developerConnection = "scm:git:ssh://github.com/blockstream/glsdk.git" + url = "https://github.com/blockstream/glsdk" + } + } + + //publishToMavenCentral() + //signAllPublications() +} + +extensions.configure { + useGpgCmd() +} diff --git a/libs/gl-sdk-android/lib/src/androidMain/AndroidManifest.xml b/libs/gl-sdk-android/lib/src/androidMain/AndroidManifest.xml new file mode 100644 index 000000000..19d2638eb --- /dev/null +++ b/libs/gl-sdk-android/lib/src/androidMain/AndroidManifest.xml @@ -0,0 +1,4 @@ + + + + diff --git a/libs/gl-sdk-android/lib/src/nativeInterop/cinterop/glsdk.def b/libs/gl-sdk-android/lib/src/nativeInterop/cinterop/glsdk.def new file mode 100644 index 000000000..cc86d3287 --- /dev/null +++ b/libs/gl-sdk-android/lib/src/nativeInterop/cinterop/glsdk.def @@ -0,0 +1,7 @@ +headers = glsdk.h +package = glsdk.cinterop + +staticLibraries = libglsdk.a + +libraryPaths.ios_arm64 = src/libs/ios-arm64 +libraryPaths.ios_simulator_arm64 = src/libs/ios-simulator-arm64 diff --git a/libs/gl-sdk-android/settings.gradle.kts b/libs/gl-sdk-android/settings.gradle.kts new file mode 100644 index 000000000..cf3b87d55 --- /dev/null +++ b/libs/gl-sdk-android/settings.gradle.kts @@ -0,0 +1,17 @@ +pluginManagement { + repositories { + google() + mavenCentral() + gradlePluginPortal() + } +} +dependencyResolutionManagement { + repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) + repositories { + google() + mavenCentral() + } +} + +rootProject.name = "glsdk" +include(":lib") \ No newline at end of file diff --git a/libs/gl-sdk/Cargo.toml b/libs/gl-sdk/Cargo.toml index dd9b14079..ccd1f6004 100644 --- a/libs/gl-sdk/Cargo.toml +++ b/libs/gl-sdk/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2024" [lib] -crate-type = ["cdylib", "staticlib"] +crate-type = ["cdylib", "staticlib", "rlib"] name = "glsdk" [dependencies] diff --git a/libs/gl-sdk/uniffi.kotlin-multiplatform.toml b/libs/gl-sdk/uniffi.kotlin-multiplatform.toml new file mode 100644 index 000000000..0b7fd2e2a --- /dev/null +++ b/libs/gl-sdk/uniffi.kotlin-multiplatform.toml @@ -0,0 +1,8 @@ +# Kotlin Multiplatform https://gobley.dev/docs/bindgen#bindgen-configuration +package_name = "glsdk" +kotlin_multiplatform = true +kotlin_targets = ["jvm", "android", "native"] +kotlin_target_version = "2.2.20" +disable_java_cleaner = true +generate_serializable_types = true +cdylib_name="glsdk" \ No newline at end of file