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)"
12 [ "$1" = "" ] && usage
13 [ "$3" != "true" -a "$3" != "false" -a "$3" != "leaks" ] && usage
14 [ "$4" != "true" -a "$4" != "false" ] && usage
19 function is_gnu_sed(){
20 sed --version >/dev/null 2>&1
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)="
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)="
31 if [ "$3" = "leaks" ]; then
35 cp "$1/lightning-c-bindings/include/lightning.h" ./
37 sed -i "s/TransactionOutputs/C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ/g" ./lightning.h
39 # OSX sed is for some reason not compatible with GNU sed
40 sed -i '' "s/TransactionOutputs/C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ/g" ./lightning.h
43 if [ "$LDK_GARBAGECOLLECTED_GIT_OVERRIDE" = "" ]; then
44 export LDK_GARBAGECOLLECTED_GIT_OVERRIDE=$(git describe --tag --dirty)
46 if [ "${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:0:1}" != "v" ]; then
47 echo "Version tag should start with a v" > /dev/stderr
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 }')"
57 case "$TARGET_STRING" in
59 LDK_TARGET_SUFFIX="_Linux-amd64"
62 "x86_64-apple-darwin"*)
63 LDK_TARGET_SUFFIX="_MacOSX-x86_64"
66 "aarch64-apple-darwin"*)
67 LDK_TARGET_CPU="apple-a14"
68 LDK_TARGET_SUFFIX="_MacOSX-aarch64"
72 LDK_TARGET_SUFFIX="_${TARGET_STRING}"
74 if [ "$LDK_TARGET_CPU" = "" ]; then
75 LDK_TARGET_CPU="sandybridge"
79 sed -i "s/^ <version>.*<\/version>/ <version>${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}<\/version>/g" pom.xml
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
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
92 ./genbindings.py "./lightning.h" src/main/java/org/ldk/impl src/main/java/org/ldk src/main/jni/ $DEBUG_ARG java $4
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
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
109 [ "$($CC --version | grep apple-darwin)" != "" ] && IS_MAC=true
111 [ "$($CC --version | grep "Apple clang version")" != "" ] && IS_APPLE_CLANG=true
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
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!"
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"
157 ar x --output=tmp "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
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
164 LDK_LIB="tmp/libldk.bc tmp/libldk.a"
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."
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!"
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
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
193 ./genbindings.py "./lightning.h" ts ts ts $DEBUG_ARG typescript browser
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
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
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
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
223 if [ -x "$(which tsc)" ]; then
225 for F in structs/*; do
226 cat imports.mts.part | grep -v " $(basename -s .mts $F)[ ,]" | cat - $F > $F.tmp
230 tsc --types node --typeRoots .
231 cp ../$WASM_FILE liblightningjs.wasm
232 cp ../README.md README.md
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