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