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