Skip to content

Commit a40eeed

Browse files
committed
Include native libraries in jar by shifting folder structure
1 parent 0c72b95 commit a40eeed

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
- name: Run Java Tests against Debug Bindings
5353
run: |
5454
rm liblightningjni.so
55-
ln -s liblightningjni_debug.so liblightningjni.so
55+
ln -s liblightningjni_debug_Linux-amd64.so liblightningjni.so
5656
export LD_LIBRARY_PATH=.
5757
export LD_PRELOAD=/usr/lib/llvm-11/lib/clang/11.0.1/lib/linux/libclang_rt.asan-x86_64.so
5858
export ASAN_OPTIONS=detect_leaks=0
@@ -73,8 +73,6 @@ jobs:
7373
fi
7474
echo "Using $LDK_GARBAGECOLLECTED_GIT_OVERRIDE as git version"
7575
./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
76-
- name: Check latest headers are in git
76+
- name: Check latest headers and release lib are in git
7777
run: |
78-
# For some reason the debug library is not deterministic, this may be fixed in a future rustc
79-
git checkout liblightningjni_debug.so
8078
git diff --exit-code

genbindings.sh

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,21 @@ else
1919
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)="
2020
fi
2121

22-
if [ "$LDK_TARGET" != "" ]; then
23-
LDK_TARGET_SUFFIX="_$LDK_TARGET"
22+
TARGET_STRING="$LDK_TARGET"
23+
if [ "$TARGET_STRING" = "" ]; then
24+
# We assume clang-style $CC --version here, but worst-case we just get an empty suffix
25+
TARGET_STRING="$($CC --version | grep Target | awk '{ print $2 }')"
2426
fi
27+
case "$TARGET_STRING" in
28+
"x86_64-pc-linux"*)
29+
LDK_TARGET_SUFFIX="_Linux-amd64" ;;
30+
"x86_64-apple-darwin"*)
31+
LDK_TARGET_SUFFIX="_MacOSX-x86_64" ;;
32+
"aarch64-apple-darwin"*)
33+
LDK_TARGET_SUFFIX="_MacOSX-aarch64" ;;
34+
*)
35+
LDK_TARGET_SUFFIX=""
36+
esac
2537
if [ "$LDK_TARGET_CPU" = "" ]; then
2638
LDK_TARGET_CPU="sandybridge"
2739
fi
@@ -68,6 +80,10 @@ if [ "$3" = "true" ]; then
6880
else
6981
[ "$IS_MAC" = "false" ] && COMPILE="$COMPILE -Wl,--version-script=libcode.version -fuse-ld=lld"
7082
$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
83+
if [ "$LDK_TARGET_SUFFIX" != "" ]; then
84+
# Copy to JNI native directory for inclusion in JARs
85+
cp liblightningjni_release$LDK_TARGET_SUFFIX.so src/main/resources/liblightningjni$LDK_TARGET_SUFFIX.nativelib
86+
fi
7187
fi
7288

7389
echo "Creating TS bindings..."

java_strings.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ def __init__(self, DEBUG: bool, target: Target, **kwargs):
2424

2525
self.bindings_header = """package org.ldk.impl;
2626
import org.ldk.enums.*;
27+
import java.io.File;
28+
import java.io.InputStream;
29+
import java.io.IOException;
30+
import java.nio.file.Files;
31+
import java.nio.file.Path;
32+
import java.nio.file.StandardCopyOption;
2733
2834
public class bindings {
2935
public static class VecOrSliceDef {
@@ -35,7 +41,24 @@ def __init__(self, DEBUG: bool, target: Target, **kwargs):
3541
}
3642
}
3743
static {
38-
System.loadLibrary(\"lightningjni\");
44+
try {
45+
// Try to load natively first, this works on Android and in testing.
46+
System.loadLibrary(\"lightningjni\");
47+
} catch (UnsatisfiedLinkError _ignored) {
48+
// Otherwise try to load from the library jar.
49+
File tmpdir = new File(System.getProperty("java.io.tmpdir"), "ldk-java-nativelib");
50+
tmpdir.mkdir(); // If it fails to create, assume it was there already
51+
tmpdir.deleteOnExit();
52+
String libname = "liblightningjni_" + System.getProperty("os.name").replaceAll(" ", "") +
53+
"-" + System.getProperty("os.arch").replaceAll(" ", "") + ".nativelib";
54+
try (InputStream is = bindings.class.getResourceAsStream("/" + libname)) {
55+
Path libpath = new File(tmpdir.toPath().toString(), "liblightningjni.so").toPath();
56+
Files.copy(is, libpath, StandardCopyOption.REPLACE_EXISTING);
57+
Runtime.getRuntime().load(libpath.toString());
58+
} catch (IOException e) {
59+
throw new IllegalArgumentException(e);
60+
}
61+
}
3962
init(java.lang.Enum.class, VecOrSliceDef.class);
4063
init_class_cache();
4164
if (!get_lib_version_string().equals(get_ldk_java_bindings_version()))

0 commit comments

Comments
 (0)