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