Move version information out of git into new files
[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 echo "#define LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ LDKCVec_TransactionOutputsZ" > src/main/jni/bindings.c
82 echo "#define CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ_free CVec_TransactionOutputsZ_free" >> src/main/jni/bindings.c
83 cat src/main/jni/bindings.c.body >> src/main/jni/bindings.c
84 javac -h src/main/jni src/main/java/org/ldk/enums/*.java src/main/java/org/ldk/impl/*.java
85 rm src/main/java/org/ldk/enums/*.class src/main/java/org/ldk/impl/bindings*.class
86
87 IS_MAC=false
88 [ "$($CC --version | grep apple-darwin)" != "" ] && IS_MAC=true
89
90 echo "Building Java bindings..."
91 COMPILE="$COMMON_COMPILE -mcpu=$LDK_TARGET_CPU -Isrc/main/jni -pthread -ldl -shared -fPIC"
92 [ "$IS_MAC" = "false" ] && COMPILE="$COMPILE -Wl,--no-undefined"
93 [ "$IS_MAC" = "true" ] && COMPILE="$COMPILE -mmacosx-version-min=10.9"
94 [ "$IS_MAC" = "false" -a "$3" != "false" ] && COMPILE="$COMPILE -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,malloc -Wl,-wrap,free"
95 if [ "$3" = "true" ]; then
96         $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
97 else
98         LDK_LIB="$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
99         if [ "$IS_MAC" = "false" -a "$4" = "false" ]; then
100                 COMPILE="$COMPILE -Wl,--version-script=libcode.version -fuse-ld=lld"
101                 # __cxa_thread_atexit_impl is used to more effeciently cleanup per-thread local storage by rust libstd.
102                 # However, it is not available on glibc versions 2.17 or earlier, and rust libstd has a null-check and
103                 # fallback in case it is missing.
104                 # Because it is weak-linked on the rust side, we should be able to simply define it
105                 # explicitly, forcing rust to use the fallback. However, for some reason involving ancient
106                 # dark magic and haunted code segments, overriding the weak symbol only impacts sites which
107                 # *call* the symbol in question, not sites which *compare with* the symbol in question.
108                 # This means that the NULL check in rust's libstd will always think the function is
109                 # callable while the function which is called ends up being NULL (leading to a jmp to the
110                 # zero page and a quick SEGFAULT).
111                 # This issue persists not only with directly providing a symbol, but also ld.lld's -wrap
112                 # and --defsym arguments.
113                 # In smaller programs, it appears to be possible to work around this with -Bsymbolic and
114                 # -nostdlib, however when applied the full-sized JNI library here it no longer works.
115                 # After exhausting nearly every flag documented in lld, the only reliable method appears
116                 # to be editing the LDK binary. Luckily, LLVM's tooling makes this rather easy as we can
117                 # disassemble it into very readable code, edit it, and then reassemble it.
118                 # Note that if we do so we don't have to bother overriding the actual call, LLVM should
119                 # optimize it away, which also provides a good check that there isn't anything actually
120                 # relying on it elsewhere.
121                 [ ! -f "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a ] && exit 1
122                 if [ "$(ar t "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a | grep -v "\.o$" || echo)" != "" ]; then
123                         echo "Archive contained non-object files!"
124                         exit 1
125                 fi
126                 if [ "$(ar t "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a | grep ldk.ldk.*-cgu.*.rcgu.o | wc -l)" != "1" ]; then
127                         echo "Archive contained more than one LDK object file"
128                         exit 1
129                 fi
130                 mkdir -p tmp
131                 rm -f tmp/*
132                 ar x --output=tmp "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
133                 pushd tmp
134                 llvm-dis ldk.ldk.*-cgu.*.rcgu.o
135                 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
136                 llvm-as ldk.ldk.*-cgu.*.rcgu.o.ll -o ./libldk.bc
137                 ar q libldk.a *.o
138                 popd
139                 LDK_LIB="tmp/libldk.bc tmp/libldk.a"
140         fi
141         $COMPILE -o liblightningjni_release$LDK_TARGET_SUFFIX.so -flto -O3 -I"$1"/lightning-c-bindings/include/ $2 src/main/jni/bindings.c $LDK_LIB
142         if [ "$IS_MAC" = "false" -a "$4" = "false" ]; then
143                 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)"
144                 if [ "$GLIBC_SYMBS" != "" ]; then
145                         echo "Unexpected glibc version dependency! Some users need glibc 2.17 support, symbols for newer glibcs cannot be included."
146                         echo "$GLIBC_SYMBS"
147                         exit 1
148                 fi
149                 REALLOC_ARRAY_SYMBS="$(objdump -T liblightningjni_release$LDK_TARGET_SUFFIX.so | grep reallocarray || echo)"
150                 if [ "$REALLOC_ARRAY_SYMBS" != "" ]; then
151                         echo "Unexpected reallocarray dependency!"
152                         exit 1
153                 fi
154         fi
155         if [ "$LDK_JAR_TARGET" = "true" ]; then
156                 # Copy to JNI native directory for inclusion in JARs
157                 mkdir -p src/main/resources/
158                 cp liblightningjni_release$LDK_TARGET_SUFFIX.so src/main/resources/liblightningjni$LDK_TARGET_SUFFIX.nativelib
159         fi
160 fi
161
162 echo "Creating TS bindings..."
163 mkdir -p ts/{enums,structs}
164 rm -f ts/{enums,structs}/*.ts
165 ./genbindings.py "./lightning.h" ts ts ts $DEBUG_ARG typescript
166 echo "#define LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ LDKCVec_TransactionOutputsZ" > ts/bindings.c
167 echo "#define CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ_free CVec_TransactionOutputsZ_free" >> ts/bindings.c
168 cat ts/bindings.c.body >> ts/bindings.c
169
170 echo "Building TS bindings..."
171 COMPILE="$COMMON_COMPILE -flto -Wl,--no-entry -Wl,--export-dynamic -Wl,-allow-undefined -nostdlib --target=wasm32-wasi"
172 # We only need malloc and assert/abort, but for now just use WASI for those:
173 #EXTRA_LINK=/usr/lib/wasm32-wasi/libc.a
174 EXTRA_LINK=
175 [ "$3" != "false" ] && COMPILE="$COMPILE -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,reallocarray -Wl,-wrap,malloc -Wl,-wrap,free"
176 if [ "$3" = "true" ]; then
177         $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
178 else
179         $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
180 fi