Use lld to link on OSX with LLVM 13
[ldk-java] / genbindings.sh
1 #!/bin/bash
2 usage() {
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, false, or leaks"
6         echo "debug of leaks turns on leak tracking on an optimized release bianry"
7         echo "android should either be true or false"
8         exit 1
9 }
10 [ "$1" = "" ] && usage
11 [ "$3" != "true" -a "$3" != "false" -a "$3" != "leaks" ] && usage
12 [ "$4" != "true" -a "$4" != "false" ] && usage
13
14 set -x
15
16 if [ "$CC" != "" ]; then
17         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)="
18 else
19         CC=clang
20         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)="
21 fi
22
23 TARGET_STRING="$LDK_TARGET"
24 if [ "$TARGET_STRING" = "" ]; then
25         # We assume clang-style $CC --version here, but worst-case we just get an empty suffix
26         TARGET_STRING="$($CC --version | grep Target | awk '{ print $2 }')"
27 fi
28 case "$TARGET_STRING" in
29         "x86_64-pc-linux"*)
30                 LDK_TARGET_SUFFIX="_Linux-amd64"
31                 LDK_JAR_TARGET=true
32                 ;;
33         "x86_64-apple-darwin"*)
34                 LDK_TARGET_SUFFIX="_MacOSX-x86_64"
35                 LDK_JAR_TARGET=true
36                 ;;
37         "aarch64-apple-darwin"*)
38                 LDK_TARGET_SUFFIX="_MacOSX-aarch64"
39                 LDK_JAR_TARGET=true
40                 ;;
41         *)
42                 LDK_TARGET_SUFFIX="_${TARGET_STRING}"
43 esac
44 if [ "$LDK_TARGET_CPU" = "" ]; then
45         LDK_TARGET_CPU="sandybridge"
46 fi
47
48 set -e
49
50 if [ "$LDK_GARBAGECOLLECTED_GIT_OVERRIDE" = "" ]; then
51         export LDK_GARBAGECOLLECTED_GIT_OVERRIDE=$(git describe --tag --dirty)
52 fi
53 if [ "${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:0:1}" != "v" ]; then
54         echo "Version tag should start with a v" > /dev/stderr
55         exit 1
56 fi
57
58 cp "$1/lightning-c-bindings/include/lightning.h" ./
59 if [ "$(rustc --version --verbose | grep "host:")" = "host: x86_64-apple-darwin" ]; then
60         # OSX sed is for some reason not compatible with GNU sed
61         sed -i '' "s/TransactionOutputs/C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ/g" ./lightning.h
62         sed -i '' "s/^    <version>.*<\/version>/    <version>${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}<\/version>/g" pom.xml
63 else
64         sed -i "s/TransactionOutputs/C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ/g" ./lightning.h
65         sed -i "s/^    <version>.*<\/version>/    <version>${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}<\/version>/g" pom.xml
66 fi
67
68 echo "Creating Java bindings..."
69 mkdir -p src/main/java/org/ldk/{enums,structs}
70 rm -f src/main/java/org/ldk/{enums,structs}/*.java
71 rm -f src/main/jni/*.h
72 DEBUG_ARG="$3"
73 if [ "$3" = "leaks" ]; then
74         DEBUG_ARG="true"
75 fi
76 if [ "$4" = "true" ]; then
77         ./genbindings.py "./lightning.h" src/main/java/org/ldk/impl src/main/java/org/ldk src/main/jni/ $DEBUG_ARG android $4
78 else
79         ./genbindings.py "./lightning.h" src/main/java/org/ldk/impl src/main/java/org/ldk src/main/jni/ $DEBUG_ARG java $4
80 fi
81 echo "#define LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ LDKCVec_TransactionOutputsZ" > src/main/jni/bindings.c
82 echo "#define CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ_free CVec_TransactionOutputsZ_free" >> src/main/jni/bindings.c
83 cat src/main/jni/bindings.c.body >> src/main/jni/bindings.c
84 javac -h src/main/jni src/main/java/org/ldk/enums/*.java src/main/java/org/ldk/impl/*.java
85 rm src/main/java/org/ldk/enums/*.class src/main/java/org/ldk/impl/bindings*.class
86
87 IS_MAC=false
88 [ "$($CC --version | grep apple-darwin)" != "" ] && IS_MAC=true
89 IS_APPLE_CLANG=false
90 [ "$($CC --version | grep "Apple clang version")" != "" ] && IS_APPLE_CLANG=true
91
92 echo "Building Java bindings..."
93 COMPILE="$COMMON_COMPILE -mcpu=$LDK_TARGET_CPU -Isrc/main/jni -pthread -ldl -shared -fPIC"
94 [ "$IS_MAC" = "false" ] && COMPILE="$COMPILE -Wl,--no-undefined"
95 [ "$IS_MAC" = "true" ] && COMPILE="$COMPILE -mmacosx-version-min=10.9"
96 [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && COMPILE="$COMPILE -fuse-ld=lld"
97 [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && echo "WARNING: Need at least upstream clang 13!"
98 [ "$IS_MAC" = "false" -a "$3" != "false" ] && COMPILE="$COMPILE -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,malloc -Wl,-wrap,free"
99 if [ "$3" = "true" ]; then
100         $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
101 else
102         LDK_LIB="$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
103         if [ "$IS_MAC" = "false" -a "$4" = "false" ]; then
104                 COMPILE="$COMPILE -Wl,--version-script=libcode.version -fuse-ld=lld"
105                 # __cxa_thread_atexit_impl is used to more effeciently cleanup per-thread local storage by rust libstd.
106                 # However, it is not available on glibc versions 2.17 or earlier, and rust libstd has a null-check and
107                 # fallback in case it is missing.
108                 # Because it is weak-linked on the rust side, we should be able to simply define it
109                 # explicitly, forcing rust to use the fallback. However, for some reason involving ancient
110                 # dark magic and haunted code segments, overriding the weak symbol only impacts sites which
111                 # *call* the symbol in question, not sites which *compare with* the symbol in question.
112                 # This means that the NULL check in rust's libstd will always think the function is
113                 # callable while the function which is called ends up being NULL (leading to a jmp to the
114                 # zero page and a quick SEGFAULT).
115                 # This issue persists not only with directly providing a symbol, but also ld.lld's -wrap
116                 # and --defsym arguments.
117                 # In smaller programs, it appears to be possible to work around this with -Bsymbolic and
118                 # -nostdlib, however when applied the full-sized JNI library here it no longer works.
119                 # After exhausting nearly every flag documented in lld, the only reliable method appears
120                 # to be editing the LDK binary. Luckily, LLVM's tooling makes this rather easy as we can
121                 # disassemble it into very readable code, edit it, and then reassemble it.
122                 # Note that if we do so we don't have to bother overriding the actual call, LLVM should
123                 # optimize it away, which also provides a good check that there isn't anything actually
124                 # relying on it elsewhere.
125                 [ ! -f "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a ] && exit 1
126                 if [ "$(ar t "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a | grep -v "\.o$" || echo)" != "" ]; then
127                         echo "Archive contained non-object files!"
128                         exit 1
129                 fi
130                 if [ "$(ar t "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a | grep ldk.ldk.*-cgu.*.rcgu.o | wc -l)" != "1" ]; then
131                         echo "Archive contained more than one LDK object file"
132                         exit 1
133                 fi
134                 mkdir -p tmp
135                 rm -f tmp/*
136                 ar x --output=tmp "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
137                 pushd tmp
138                 llvm-dis ldk.ldk.*-cgu.*.rcgu.o
139                 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
140                 llvm-as ldk.ldk.*-cgu.*.rcgu.o.ll -o ./libldk.bc
141                 ar q libldk.a *.o
142                 popd
143                 LDK_LIB="tmp/libldk.bc tmp/libldk.a"
144         fi
145         $COMPILE -o liblightningjni_release$LDK_TARGET_SUFFIX.so -flto -O3 -I"$1"/lightning-c-bindings/include/ $2 src/main/jni/bindings.c $LDK_LIB
146         if [ "$IS_MAC" = "false" -a "$4" = "false" ]; then
147                 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)"
148                 if [ "$GLIBC_SYMBS" != "" ]; then
149                         echo "Unexpected glibc version dependency! Some users need glibc 2.17 support, symbols for newer glibcs cannot be included."
150                         echo "$GLIBC_SYMBS"
151                         exit 1
152                 fi
153                 REALLOC_ARRAY_SYMBS="$(objdump -T liblightningjni_release$LDK_TARGET_SUFFIX.so | grep reallocarray || echo)"
154                 if [ "$REALLOC_ARRAY_SYMBS" != "" ]; then
155                         echo "Unexpected reallocarray dependency!"
156                         exit 1
157                 fi
158         fi
159         if [ "$LDK_JAR_TARGET" = "true" ]; then
160                 # Copy to JNI native directory for inclusion in JARs
161                 mkdir -p src/main/resources/
162                 cp liblightningjni_release$LDK_TARGET_SUFFIX.so src/main/resources/liblightningjni$LDK_TARGET_SUFFIX.nativelib
163         fi
164 fi
165
166 echo "Creating TS bindings..."
167 mkdir -p ts/{enums,structs}
168 rm -f ts/{enums,structs}/*.ts
169 ./genbindings.py "./lightning.h" ts ts ts $DEBUG_ARG typescript
170 echo "#define LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ LDKCVec_TransactionOutputsZ" > ts/bindings.c
171 echo "#define CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ_free CVec_TransactionOutputsZ_free" >> ts/bindings.c
172 cat ts/bindings.c.body >> ts/bindings.c
173
174 echo "Building TS bindings..."
175 COMPILE="$COMMON_COMPILE -flto -Wl,--no-entry -Wl,--export-dynamic -Wl,-allow-undefined -nostdlib --target=wasm32-wasi"
176 # We only need malloc and assert/abort, but for now just use WASI for those:
177 #EXTRA_LINK=/usr/lib/wasm32-wasi/libc.a
178 EXTRA_LINK=
179 [ "$3" != "false" ] && COMPILE="$COMPILE -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,reallocarray -Wl,-wrap,malloc -Wl,-wrap,free"
180 if [ "$3" = "true" ]; then
181         $COMPILE -o liblightningjs_debug.wasm -g -I"$1"/lightning-c-bindings/include/ ts/bindings.c "$1"/lightning-c-bindings/target/wasm32-wasi/debug/libldk.a $EXTRA_LINK
182 else
183         $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
184 fi