]> git.bitcoin.ninja Git - ldk-java/commitdiff
Include native libraries in jar by shifting folder structure
authorMatt Corallo <git@bluematt.me>
Tue, 15 Jun 2021 21:07:25 +0000 (21:07 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 16 Jun 2021 17:55:44 +0000 (17:55 +0000)
.github/workflows/build.yml
genbindings.sh
java_strings.py

index 3145675ac66eaacd43cdbd5a6bdb1d3c38532220..66c49dea14076b8e1e5cf5097b493ab2e1b810f1 100644 (file)
@@ -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
index dd762ec9426086105c8ba8c46215eeede930145a..6a4109c8f447b0a909e3ce0e2562e058231db700 100755 (executable)
@@ -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..."
index 6bfd571bbf991caa8b5be640b409ce0ab33ac1d3..7f82567ef408adbb16ab25c58bb0ac857d026cc7 100644 (file)
@@ -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()))