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