Set a library suffix for all platforms, useful for android builds
[ldk-java] / genbindings.sh
index dd762ec9426086105c8ba8c46215eeede930145a..7ecbdeba7e59ed278dafbb2d9e50d16f838099c4 100755 (executable)
@@ -19,9 +19,27 @@ 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"
+               LDK_JAR_TARGET=true
+               ;;
+       "x86_64-apple-darwin"*)
+               LDK_TARGET_SUFFIX="_MacOSX-x86_64"
+               LDK_JAR_TARGET=true
+               ;;
+       "aarch64-apple-darwin"*)
+               LDK_TARGET_SUFFIX="_MacOSX-aarch64"
+               LDK_JAR_TARGET=true
+               ;;
+       *)
+               LDK_TARGET_SUFFIX="_${TARGET_STRING}"
+esac
 if [ "$LDK_TARGET_CPU" = "" ]; then
        LDK_TARGET_CPU="sandybridge"
 fi
@@ -66,8 +84,28 @@ if [ "$3" = "true" ]; then
        [ "$IS_MAC" = "false" ] && COMPILE="$COMPILE -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,reallocarray -Wl,-wrap,malloc -Wl,-wrap,free"
        $COMPILE -o liblightningjni_debug$LDK_TARGET_SUFFIX.so -g -fsanitize=address -shared-libasan -rdynamic -I"$1"/lightning-c-bindings/include/ $2 src/main/jni/bindings.c "$1"/lightning-c-bindings/target/$LDK_TARGET/debug/libldk.a -lm
 else
-       [ "$IS_MAC" = "false" ] && COMPILE="$COMPILE -Wl,--version-script=libcode.version -fuse-ld=lld"
+       if [ "$IS_MAC" = "false" ]; then
+               COMPILE="$COMPILE -Wl,--version-script=libcode.version -fuse-ld=lld"
+               echo "// __cxa_thread_atexit_impl is used to more effeciently cleanup per-thread local storage by rust libstd." >> src/main/jni/bindings.c
+               echo "// However, it is not available on glibc versions 2.17 or earlier, and rust libstd has a null-check and fallback in case it is missing." >> src/main/jni/bindings.c
+               echo "// Because it is weak-linked on the rust side, we can simply define it explicitly here, forcing rust to use the fallback." >> src/main/jni/bindings.c
+               echo "void *__cxa_thread_atexit_impl = NULL;" >> src/main/jni/bindings.c
+       fi
        $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 [ "$IS_MAC" = "false" ]; then
+               set +e # grep exits with 1 if no lines were left, which is our success condition
+               GLIBC_SYMBS="$(objdump -T liblightningjni_release$LDK_TARGET_SUFFIX.so | grep GLIBC_ | grep -v "GLIBC_2\.2\." | grep -v "GLIBC_2\.3\(\.\| \)" | grep -v "GLIBC_2.\(14\|17\) ")"
+               set -e
+               if [ "$GLIBC_SYMBS" != "" ]; then
+                       echo "Unexpected glibc version dependency! Some users need glibc 2.17 support, symbols for newer glibcs cannot be included."
+                       echo "$GLIBC_SYMBS"
+                       exit 1
+               fi
+       fi
+       if [ "$LDK_JAR_TARGET" = "true" ]; 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..."