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