3 echo "USAGE: path/to/ldk-c-bindings \"JNI_CFLAGS\" debug android"
4 echo "For JNI_CFLAGS you probably want -I/usr/lib/jvm/java-11-openjdk-amd64/include/ -I/usr/lib/jvm/java-11-openjdk-amd64/include/linux/"
5 echo "debug should either be true or false"
6 echo "android should either be true or false"
10 [ "$3" != "true" -a "$3" != "false" ] && usage
11 [ "$4" != "true" -a "$4" != "false" ] && usage
15 if [ "$CC" != "" ]; then
16 COMMON_COMPILE="$CC -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)="
19 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)="
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 }')"
27 case "$TARGET_STRING" in
29 LDK_TARGET_SUFFIX="_Linux-amd64"
32 "x86_64-apple-darwin"*)
33 LDK_TARGET_SUFFIX="_MacOSX-x86_64"
36 "aarch64-apple-darwin"*)
37 LDK_TARGET_SUFFIX="_MacOSX-aarch64"
41 LDK_TARGET_SUFFIX="_${TARGET_STRING}"
43 if [ "$LDK_TARGET_CPU" = "" ]; then
44 LDK_TARGET_CPU="sandybridge"
49 if [ "$LDK_GARBAGECOLLECTED_GIT_OVERRIDE" = "" ]; then
50 export LDK_GARBAGECOLLECTED_GIT_OVERRIDE=$(git describe --tag --dirty)
52 if [ "${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:0:1}" != "v" ]; then
53 echo "Version tag should start with a v" > /dev/stderr
57 cp "$1/lightning-c-bindings/include/lightning.h" ./
58 if [ "$(rustc --version --verbose | grep "host:")" = "host: x86_64-apple-darwin" ]; then
59 # OSX sed is for some reason not compatible with GNU sed
60 sed -i '' "s/TransactionOutputs/C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ/g" ./lightning.h
61 sed -i '' "s/^ <version>.*<\/version>/ <version>${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}<\/version>/g" pom.xml
63 sed -i "s/TransactionOutputs/C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ/g" ./lightning.h
64 sed -i "s/^ <version>.*<\/version>/ <version>${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}<\/version>/g" pom.xml
67 echo "Creating Java bindings..."
68 mkdir -p src/main/java/org/ldk/{enums,structs}
69 rm -f src/main/java/org/ldk/{enums,structs}/*.java
70 rm -f src/main/jni/*.h
71 if [ "$4" = "true" ]; then
72 ./genbindings.py "./lightning.h" src/main/java/org/ldk/impl/bindings.java src/main/java/org/ldk src/main/jni/bindings.c.body $3 android $4
74 ./genbindings.py "./lightning.h" src/main/java/org/ldk/impl/bindings.java src/main/java/org/ldk src/main/jni/bindings.c.body $3 java $4
76 echo "#define LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ LDKCVec_TransactionOutputsZ" > src/main/jni/bindings.c
77 echo "#define CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ_free CVec_TransactionOutputsZ_free" >> src/main/jni/bindings.c
78 cat src/main/jni/bindings.c.body >> src/main/jni/bindings.c
79 javac -h src/main/jni src/main/java/org/ldk/enums/*.java src/main/java/org/ldk/impl/bindings.java
80 rm src/main/java/org/ldk/enums/*.class src/main/java/org/ldk/impl/bindings*.class
83 [ "$($CC --version | grep apple-darwin)" != "" ] && IS_MAC=true
85 echo "Building Java bindings..."
86 COMPILE="$COMMON_COMPILE -mcpu=$LDK_TARGET_CPU -Isrc/main/jni -pthread -ldl -shared -fPIC"
87 [ "$IS_MAC" = "false" ] && COMPILE="$COMPILE -Wl,--no-undefined"
88 [ "$IS_MAC" = "true" ] && COMPILE="$COMPILE -mmacosx-version-min=10.9"
89 if [ "$3" = "true" ]; then
90 [ "$IS_MAC" = "false" ] && COMPILE="$COMPILE -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,reallocarray -Wl,-wrap,malloc -Wl,-wrap,free"
91 $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
93 LDK_LIB="$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
94 if [ "$IS_MAC" = "false" -a "$4" = "false" ]; then
95 COMPILE="$COMPILE -Wl,--version-script=libcode.version -fuse-ld=lld"
96 # __cxa_thread_atexit_impl is used to more effeciently cleanup per-thread local storage by rust libstd.
97 # However, it is not available on glibc versions 2.17 or earlier, and rust libstd has a null-check and
98 # fallback in case it is missing.
99 # Because it is weak-linked on the rust side, we should be able to simply define it
100 # explicitly, forcing rust to use the fallback. However, for some reason involving ancient
101 # dark magic and haunted code segments, overriding the weak symbol only impacts sites which
102 # *call* the symbol in question, not sites which *compare with* the symbol in question.
103 # This means that the NULL check in rust's libstd will always think the function is
104 # callable while the function which is called ends up being NULL (leading to a jmp to the
105 # zero page and a quick SEGFAULT).
106 # This issue persists not only with directly providing a symbol, but also ld.lld's -wrap
107 # and --defsym arguments.
108 # In smaller programs, it appears to be possible to work around this with -Bsymbolic and
109 # -nostdlib, however when applied the full-sized JNI library here it no longer works.
110 # After exhausting nearly every flag documented in lld, the only reliable method appears
111 # to be editing the LDK binary. Luckily, LLVM's tooling makes this rather easy as we can
112 # disassemble it into very readable code, edit it, and then reassemble it.
113 # Note that if we do so we don't have to bother overriding the actual call, LLVM should
114 # optimize it away, which also provides a good check that there isn't anything actually
115 # relying on it elsewhere.
116 [ ! -f "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a ] && exit 1
117 if [ "$(ar t "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a | grep -v "\.o$" || echo)" != "" ]; then
118 echo "Archive contained non-object files!"
121 if [ "$(ar t "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a | grep ldk.ldk.*-cgu.*.rcgu.o | wc -l)" != "1" ]; then
122 echo "Archive contained more than one LDK object file"
127 ar x --output=tmp "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
129 llvm-dis ldk.ldk.*-cgu.*.rcgu.o
130 sed -i 's/br i1 icmp eq (i8\* @__cxa_thread_atexit_impl, i8\* null)/br i1 icmp eq (i8* null, i8* null)/g' ldk.ldk.*-cgu.*.rcgu.o.ll
131 llvm-as ldk.ldk.*-cgu.*.rcgu.o.ll -o ./libldk.bc
134 LDK_LIB="tmp/libldk.bc tmp/libldk.a"
136 $COMPILE -o liblightningjni_release$LDK_TARGET_SUFFIX.so -flto -O3 -I"$1"/lightning-c-bindings/include/ $2 src/main/jni/bindings.c $LDK_LIB
137 if [ "$IS_MAC" = "false" -a "$4" = "false" ]; then
138 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\) " || echo)"
139 if [ "$GLIBC_SYMBS" != "" ]; then
140 echo "Unexpected glibc version dependency! Some users need glibc 2.17 support, symbols for newer glibcs cannot be included."
145 if [ "$LDK_JAR_TARGET" = "true" ]; then
146 # Copy to JNI native directory for inclusion in JARs
147 mkdir -p src/main/resources/
148 cp liblightningjni_release$LDK_TARGET_SUFFIX.so src/main/resources/liblightningjni$LDK_TARGET_SUFFIX.nativelib
152 echo "Creating TS bindings..."
153 mkdir -p ts/{enums,structs}
154 rm -f ts/{enums,structs}/*.ts
155 ./genbindings.py "./lightning.h" ts/bindings.ts ts ts/bindings.c.body $3 typescript
156 echo "#define LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ LDKCVec_TransactionOutputsZ" > ts/bindings.c
157 echo "#define CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ_free CVec_TransactionOutputsZ_free" >> ts/bindings.c
158 cat ts/bindings.c.body >> ts/bindings.c
160 echo "Building TS bindings..."
161 COMPILE="$COMMON_COMPILE -flto -Wl,--no-entry -Wl,--export-dynamic -Wl,-allow-undefined -nostdlib --target=wasm32-wasi"
162 # We only need malloc and assert/abort, but for now just use WASI for those:
163 #EXTRA_LINK=/usr/lib/wasm32-wasi/libc.a
165 if [ "$3" = "true" ]; then
166 $COMPILE -o liblightningjs_debug.wasm -g -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,reallocarray -Wl,-wrap,malloc -Wl,-wrap,free -I"$1"/lightning-c-bindings/include/ ts/bindings.c "$1"/lightning-c-bindings/target/wasm32-wasi/debug/libldk.a $EXTRA_LINK
168 $COMPILE -o liblightningjs_release.wasm -s -Os -I"$1"/lightning-c-bindings/include/ ts/bindings.c "$1"/lightning-c-bindings/target/wasm32-wasi/release/libldk.a $EXTRA_LINK