From: Matt Corallo Date: Tue, 15 Jun 2021 21:07:25 +0000 (+0000) Subject: Include native libraries in jar by shifting folder structure X-Git-Tag: v0.0.98.2~3 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=a40eeed94e15dde00b73fa70c459e630ea679bf1;p=ldk-java Include native libraries in jar by shifting folder structure --- diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3145675a..66c49dea 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -52,7 +52,7 @@ jobs: - name: Run Java Tests against Debug Bindings run: | rm liblightningjni.so - ln -s liblightningjni_debug.so liblightningjni.so + ln -s liblightningjni_debug_Linux-amd64.so liblightningjni.so export LD_LIBRARY_PATH=. export LD_PRELOAD=/usr/lib/llvm-11/lib/clang/11.0.1/lib/linux/libclang_rt.asan-x86_64.so export ASAN_OPTIONS=detect_leaks=0 @@ -73,8 +73,6 @@ jobs: fi echo "Using $LDK_GARBAGECOLLECTED_GIT_OVERRIDE as git version" ./genbindings.sh ./ldk-c-bindings/ "-I/usr/lib/jvm/java-11-openjdk-amd64/include/ -I/usr/lib/jvm/java-11-openjdk-amd64/include/linux/" false false - - name: Check latest headers are in git + - name: Check latest headers and release lib are in git run: | - # For some reason the debug library is not deterministic, this may be fixed in a future rustc - git checkout liblightningjni_debug.so git diff --exit-code diff --git a/genbindings.sh b/genbindings.sh index dd762ec9..6a4109c8 100755 --- a/genbindings.sh +++ b/genbindings.sh @@ -19,9 +19,21 @@ else COMMON_COMPILE="clang -std=c11 -Wall -Wextra -Wno-unused-parameter -Wno-ignored-qualifiers -Wno-unused-function -Wno-nullability-completeness -Wno-pointer-sign -Wdate-time -ffile-prefix-map=$(pwd)=" fi -if [ "$LDK_TARGET" != "" ]; then - LDK_TARGET_SUFFIX="_$LDK_TARGET" +TARGET_STRING="$LDK_TARGET" +if [ "$TARGET_STRING" = "" ]; then + # We assume clang-style $CC --version here, but worst-case we just get an empty suffix + TARGET_STRING="$($CC --version | grep Target | awk '{ print $2 }')" fi +case "$TARGET_STRING" in + "x86_64-pc-linux"*) + LDK_TARGET_SUFFIX="_Linux-amd64" ;; + "x86_64-apple-darwin"*) + LDK_TARGET_SUFFIX="_MacOSX-x86_64" ;; + "aarch64-apple-darwin"*) + LDK_TARGET_SUFFIX="_MacOSX-aarch64" ;; + *) + LDK_TARGET_SUFFIX="" +esac if [ "$LDK_TARGET_CPU" = "" ]; then LDK_TARGET_CPU="sandybridge" fi @@ -68,6 +80,10 @@ if [ "$3" = "true" ]; then else [ "$IS_MAC" = "false" ] && COMPILE="$COMPILE -Wl,--version-script=libcode.version -fuse-ld=lld" $COMPILE -o liblightningjni_release$LDK_TARGET_SUFFIX.so -flto -O3 -I"$1"/lightning-c-bindings/include/ $2 src/main/jni/bindings.c "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a + if [ "$LDK_TARGET_SUFFIX" != "" ]; then + # Copy to JNI native directory for inclusion in JARs + cp liblightningjni_release$LDK_TARGET_SUFFIX.so src/main/resources/liblightningjni$LDK_TARGET_SUFFIX.nativelib + fi fi echo "Creating TS bindings..." diff --git a/java_strings.py b/java_strings.py index 6bfd571b..7f82567e 100644 --- a/java_strings.py +++ b/java_strings.py @@ -24,6 +24,12 @@ class Consts: self.bindings_header = """package org.ldk.impl; import org.ldk.enums.*; +import java.io.File; +import java.io.InputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; public class bindings { public static class VecOrSliceDef { @@ -35,7 +41,24 @@ public class bindings { } } static { - System.loadLibrary(\"lightningjni\"); + try { + // Try to load natively first, this works on Android and in testing. + System.loadLibrary(\"lightningjni\"); + } catch (UnsatisfiedLinkError _ignored) { + // Otherwise try to load from the library jar. + File tmpdir = new File(System.getProperty("java.io.tmpdir"), "ldk-java-nativelib"); + tmpdir.mkdir(); // If it fails to create, assume it was there already + tmpdir.deleteOnExit(); + String libname = "liblightningjni_" + System.getProperty("os.name").replaceAll(" ", "") + + "-" + System.getProperty("os.arch").replaceAll(" ", "") + ".nativelib"; + try (InputStream is = bindings.class.getResourceAsStream("/" + libname)) { + Path libpath = new File(tmpdir.toPath().toString(), "liblightningjni.so").toPath(); + Files.copy(is, libpath, StandardCopyOption.REPLACE_EXISTING); + Runtime.getRuntime().load(libpath.toString()); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } init(java.lang.Enum.class, VecOrSliceDef.class); init_class_cache(); if (!get_lib_version_string().equals(get_ldk_java_bindings_version()))