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