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
52 if [ "$2" = "c_sharp" ]; then
53 TARGET_STRING="$LDK_TARGET"
54 if [ "$TARGET_STRING" = "" ]; then
55 # We assume clang-style $CC --version here, but worst-case we just get an empty suffix
56 TARGET_STRING="$($CC --version | grep Target | awk '{ print $2 }')"
58 case "$TARGET_STRING" in
60 LDK_TARGET_SUFFIX="_Linux-amd64"
63 "x86_64-apple-darwin"*)
64 LDK_TARGET_SUFFIX="_MacOSX-x86_64"
67 "aarch64-apple-darwin"*)
68 LDK_TARGET_CPU="apple-a14"
69 LDK_TARGET_SUFFIX="_MacOSX-aarch64"
73 LDK_TARGET_SUFFIX="_${TARGET_STRING}"
75 if [ "$LDK_TARGET_CPU" = "" ]; then
76 LDK_TARGET_CPU="sandybridge"
79 echo "Creating C# bindings..."
80 mkdir -p c_sharp/src/org/ldk/{enums,structs,impl}
81 rm -f c_sharp/src/org/ldk/{enums,structs,impl}/*.cs
82 ./genbindings.py "./lightning.h" c_sharp/src/org/ldk/impl c_sharp/src/org/ldk c_sharp/ $DEBUG_ARG c_sharp $4
83 rm -f c_sharp/bindings.c
84 if [ "$3" = "true" ]; then
85 echo "#define LDK_DEBUG_BUILD" > c_sharp/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)" > c_sharp/bindings.c
91 echo "#define LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ LDKCVec_TransactionOutputsZ" >> c_sharp/bindings.c
92 echo "#define CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ_free CVec_TransactionOutputsZ_free" >> c_sharp/bindings.c
93 cat c_sharp/bindings.c.body >> c_sharp/bindings.c
96 [ "$($CC --version | grep apple-darwin)" != "" ] && IS_MAC=true
98 [ "$($CC --version | grep "Apple clang version")" != "" ] && IS_APPLE_CLANG=true
100 # Compiling C# bindings with Mono
101 MONO_COMPILE="-out:csharpldk.dll -langversion:3 -t:library c_sharp/src/org/ldk/enums/*.cs c_sharp/src/org/ldk/impl/*.cs c_sharp/src/org/ldk/util/*.cs c_sharp/src/org/ldk/structs/*.cs"
102 if [ "$3" = "true" ]; then
103 mono-csc $MONO_COMPILE
105 mono-csc -o $MONO_COMPILE
108 mv ./-out:csharpldk.dll csharpldk.dll # Mono is braindead, apparently
110 echo "Building C# bindings..."
111 COMPILE="$COMMON_COMPILE -mcpu=$LDK_TARGET_CPU -Isrc/main/jni -pthread -fPIC"
113 [ "$IS_MAC" = "false" ] && LINK="$LINK -Wl,--no-undefined"
114 [ "$IS_MAC" = "true" ] && COMPILE="$COMPILE -mmacosx-version-min=10.9"
115 [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && LINK="$LINK -fuse-ld=lld"
116 [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && echo "WARNING: Need at least upstream clang 13!"
117 [ "$IS_MAC" = "false" -a "$3" != "false" ] && LINK="$LINK -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,malloc -Wl,-wrap,free"
119 exit 0 # Sadly compilation doesn't currently work
120 if [ "$3" = "true" ]; then
121 $COMPILE $LINK -o liblightningjni_debug$LDK_TARGET_SUFFIX.so -g -fsanitize=address -shared-libasan -rdynamic -I"$1"/lightning-c-bindings/include/ $2 c_sharp/bindings.c "$1"/lightning-c-bindings/target/$LDK_TARGET/debug/libldk.a -lm
123 $COMPILE -o bindings.o -c -flto -O3 -I"$1"/lightning-c-bindings/include/ $2 c_sharp/bindings.c
124 $COMPILE $LINK -o liblightningjni_release$LDK_TARGET_SUFFIX.so -flto -O3 -Wl,--lto-O3 -Wl,-O3 -Wl,--version-script=c_sharp/libcode.version -I"$1"/lightning-c-bindings/include/ $2 bindings.o "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a -lm
125 llvm-strip liblightningjni_release$LDK_TARGET_SUFFIX.so
127 elif [ "$2" = "python" ]; then
128 TARGET_STRING="$LDK_TARGET"
129 if [ "$TARGET_STRING" = "" ]; then
130 # We assume clang-style $CC --version here, but worst-case we just get an empty suffix
131 TARGET_STRING="$($CC --version | grep Target | awk '{ print $2 }')"
133 case "$TARGET_STRING" in
135 LDK_TARGET_SUFFIX="_Linux-amd64"
138 "x86_64-apple-darwin"*)
139 LDK_TARGET_SUFFIX="_MacOSX-x86_64"
142 "aarch64-apple-darwin"*)
143 LDK_TARGET_CPU="apple-a14"
144 LDK_TARGET_SUFFIX="_MacOSX-aarch64"
148 LDK_TARGET_SUFFIX="_${TARGET_STRING}"
150 if [ "$LDK_TARGET_CPU" = "" ]; then
151 LDK_TARGET_CPU="sandybridge"
154 echo "Creating Python bindings..."
155 mkdir -p python/src/{enums,structs,impl}
156 rm -f python/src/{enums,structs,impl}/*.py
157 ./genbindings.py "./lightning.h" python/src/impl python/src python/ $DEBUG_ARG python $4
158 rm -f python/bindings.c
159 if [ "$3" = "true" ]; then
160 echo "#define LDK_DEBUG_BUILD" > python/bindings.c
161 elif [ "$3" = "leaks" ]; then
162 # For leak checking we use release libldk which doesn't expose
163 # __unmangle_inner_ptr, but the C code expects to be able to call it.
164 echo "#define __unmangle_inner_ptr(a) (a)" > python/bindings.c
166 echo "#define LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ LDKCVec_TransactionOutputsZ" >> python/bindings.c
167 echo "#define CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ_free CVec_TransactionOutputsZ_free" >> python/bindings.c
168 cat python/bindings.c.body >> python/bindings.c
171 [ "$($CC --version | grep apple-darwin)" != "" ] && IS_MAC=true
173 [ "$($CC --version | grep "Apple clang version")" != "" ] && IS_APPLE_CLANG=true
175 echo "Building Python bindings..."
176 COMPILE="$COMMON_COMPILE -mcpu=$LDK_TARGET_CPU -Isrc/main/jni -pthread -fPIC"
178 [ "$IS_MAC" = "false" ] && LINK="$LINK -Wl,--no-undefined"
179 [ "$IS_MAC" = "true" ] && COMPILE="$COMPILE -mmacosx-version-min=10.9"
180 [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && LINK="$LINK -fuse-ld=lld"
181 [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && echo "WARNING: Need at least upstream clang 13!"
182 [ "$IS_MAC" = "false" -a "$3" != "false" ] && LINK="$LINK -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,malloc -Wl,-wrap,free"
184 exit 0 # Sadly compilation doesn't currently work
185 if [ "$3" = "true" ]; then
186 $COMPILE $LINK -o liblightningpython_debug$LDK_TARGET_SUFFIX.so -g -fsanitize=address -shared-libasan -rdynamic -I"$1"/lightning-c-bindings/include/ $2 c_sharp/bindings.c "$1"/lightning-c-bindings/target/$LDK_TARGET/debug/libldk.a -lm
188 $COMPILE -o bindings.o -c -flto -O3 -I"$1"/lightning-c-bindings/include/ $2 c_sharp/bindings.c
189 $COMPILE $LINK -o liblightningpython_release$LDK_TARGET_SUFFIX.so -Wl,--version-script=python/libcode.version -flto -O3 -Wl,--lto-O3 -Wl,-O3 -I"$1"/lightning-c-bindings/include/ $2 bindings.o "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a -lm
190 llvm-strip liblightningpython_release$LDK_TARGET_SUFFIX.so
192 elif [ "$2" = "wasm" ]; then
193 echo "Creating TS bindings..."
194 mkdir -p ts/{enums,structs}
195 rm -f ts/{enums,structs,}/*.{mjs,mts,mts.part}
196 if [ "$4" = "false" ]; then
197 ./genbindings.py "./lightning.h" ts ts ts $DEBUG_ARG typescript node
199 ./genbindings.py "./lightning.h" ts ts ts $DEBUG_ARG typescript browser
202 sed -i 's/^ "version": .*/ "version": "'${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}'",/g' ts/package.json
203 sed -i 's/^ "version": .*/ "version": "'${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}'",/g' node-net/package.json
204 sed -i 's/^ "lightningdevkit": .*/ "lightningdevkit": "'${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}'"/g' node-net/package.json
205 if [ "$3" = "true" ]; then
206 echo "#define LDK_DEBUG_BUILD" > ts/bindings.c
207 elif [ "$3" = "leaks" ]; then
208 # For leak checking we use release libldk which doesn't expose
209 # __unmangle_inner_ptr, but the C code expects to be able to call it.
210 echo "#define __unmangle_inner_ptr(a) (a)" > ts/bindings.c
212 echo "#define LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ LDKCVec_TransactionOutputsZ" >> ts/bindings.c
213 echo "#define CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ_free CVec_TransactionOutputsZ_free" >> ts/bindings.c
214 cat ts/bindings.c.body >> ts/bindings.c
216 echo "Building TS bindings..."
217 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"
218 # We only need malloc and assert/abort, but for now just use WASI for those:
219 EXTRA_LINK=/usr/lib/wasm32-wasi/libc.a
220 [ "$3" != "false" ] && COMPILE="$COMPILE -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,reallocarray -Wl,-wrap,malloc -Wl,-wrap,aligned_alloc -Wl,-wrap,free"
221 if [ "$3" = "true" ]; then
222 WASM_FILE=liblightningjs_debug.wasm
223 $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
225 WASM_FILE=liblightningjs_release.wasm
226 $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
229 if [ -x "$(which tsc)" ]; then
231 for F in structs/*; do
232 cat imports.mts.part | grep -v " $(basename -s .mts $F)[ ,]" | cat - $F > $F.tmp
236 tsc --types node --typeRoots .
237 cp ../$WASM_FILE liblightningjs.wasm
238 cp ../README.md README.md
240 tsc --types node --typeRoots .
241 echo Ready to publish!
242 if [ -x "$(which node)" ]; then
243 NODE_V="$(node --version)"
244 if [ "${NODE_V:1:2}" -gt 14 ]; then
246 node --stack_trace_limit=200 --trace-uncaught test/node.mjs
248 node --stack_trace_limit=200 --trace-uncaught test/test.mjs
253 TARGET_STRING="$LDK_TARGET"
254 if [ "$TARGET_STRING" = "" ]; then
255 # We assume clang-style $CC --version here, but worst-case we just get an empty suffix
256 TARGET_STRING="$($CC --version | grep Target | awk '{ print $2 }')"
258 case "$TARGET_STRING" in
260 LDK_TARGET_SUFFIX="_Linux-amd64"
263 "x86_64-apple-darwin"*)
264 LDK_TARGET_SUFFIX="_MacOSX-x86_64"
267 "aarch64-apple-darwin"*)
268 LDK_TARGET_CPU="apple-a14"
269 LDK_TARGET_SUFFIX="_MacOSX-aarch64"
273 LDK_TARGET_SUFFIX="_${TARGET_STRING}"
275 if [ "$LDK_TARGET_CPU" = "" ]; then
276 LDK_TARGET_CPU="sandybridge"
280 sed -i "s/^ <version>.*<\/version>/ <version>${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}<\/version>/g" pom.xml
282 # OSX sed is for some reason not compatible with GNU sed
283 sed -i '' "s/^ <version>.*<\/version>/ <version>${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}<\/version>/g" pom.xml
286 echo "Creating Java bindings..."
287 mkdir -p src/main/java/org/ldk/{enums,structs}
288 rm -f src/main/java/org/ldk/{enums,structs}/*.java
289 rm -f src/main/jni/*.h
290 if [ "$4" = "true" ]; then
291 ./genbindings.py "./lightning.h" src/main/java/org/ldk/impl src/main/java/org/ldk src/main/jni/ $DEBUG_ARG android $4
293 ./genbindings.py "./lightning.h" src/main/java/org/ldk/impl src/main/java/org/ldk src/main/jni/ $DEBUG_ARG java $4
295 rm -f src/main/jni/bindings.c
296 if [ "$3" = "true" ]; then
297 echo "#define LDK_DEBUG_BUILD" > src/main/jni/bindings.c
298 elif [ "$3" = "leaks" ]; then
299 # For leak checking we use release libldk which doesn't expose
300 # __unmangle_inner_ptr, but the C code expects to be able to call it.
301 echo "#define __unmangle_inner_ptr(a) (a)" > src/main/jni/bindings.c
303 echo "#define LDKCVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ LDKCVec_TransactionOutputsZ" >> src/main/jni/bindings.c
304 echo "#define CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ_free CVec_TransactionOutputsZ_free" >> src/main/jni/bindings.c
305 cat src/main/jni/bindings.c.body >> src/main/jni/bindings.c
306 javac -h src/main/jni src/main/java/org/ldk/enums/*.java src/main/java/org/ldk/impl/*.java
307 rm src/main/java/org/ldk/enums/*.class src/main/java/org/ldk/impl/bindings*.class
310 [ "$($CC --version | grep apple-darwin)" != "" ] && IS_MAC=true
312 [ "$($CC --version | grep "Apple clang version")" != "" ] && IS_APPLE_CLANG=true
314 echo "Building Java bindings..."
315 COMPILE="$COMMON_COMPILE -mcpu=$LDK_TARGET_CPU -Isrc/main/jni -pthread -fPIC"
317 [ "$IS_MAC" = "false" ] && LINK="$LINK -Wl,--no-undefined"
318 [ "$IS_MAC" = "true" ] && COMPILE="$COMPILE -mmacosx-version-min=10.9"
319 [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && LINK="$LINK -fuse-ld=lld"
320 [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && echo "WARNING: Need at least upstream clang 13!"
321 [ "$IS_MAC" = "false" -a "$3" != "false" ] && LINK="$LINK -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,malloc -Wl,-wrap,free"
322 if [ "$3" = "true" ]; then
323 $COMPILE $LINK -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
325 LDK_LIB="$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
326 if [ "$IS_MAC" = "false" -a "$4" = "false" ]; then
327 LINK="$LINK -Wl,--version-script=libcode.version -fuse-ld=lld"
328 # __cxa_thread_atexit_impl is used to more effeciently cleanup per-thread local storage by rust libstd.
329 # However, it is not available on glibc versions 2.17 or earlier, and rust libstd has a null-check and
330 # fallback in case it is missing.
331 # Because it is weak-linked on the rust side, we should be able to simply define it
332 # explicitly, forcing rust to use the fallback. However, for some reason involving ancient
333 # dark magic and haunted code segments, overriding the weak symbol only impacts sites which
334 # *call* the symbol in question, not sites which *compare with* the symbol in question.
335 # This means that the NULL check in rust's libstd will always think the function is
336 # callable while the function which is called ends up being NULL (leading to a jmp to the
337 # zero page and a quick SEGFAULT).
338 # This issue persists not only with directly providing a symbol, but also ld.lld's -wrap
339 # and --defsym arguments.
340 # In smaller programs, it appears to be possible to work around this with -Bsymbolic and
341 # -nostdlib, however when applied the full-sized JNI library here it no longer works.
342 # After exhausting nearly every flag documented in lld, the only reliable method appears
343 # to be editing the LDK binary. Luckily, LLVM's tooling makes this rather easy as we can
344 # disassemble it into very readable code, edit it, and then reassemble it.
345 # Note that if we do so we don't have to bother overriding the actual call, LLVM should
346 # optimize it away, which also provides a good check that there isn't anything actually
347 # relying on it elsewhere.
348 [ ! -f "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a ] && exit 1
349 if [ "$(ar t "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a | grep -v "\.o$" || echo)" != "" ]; then
350 echo "Archive contained non-object files!"
353 if [ "$(ar t "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a | grep ldk.*-cgu.*.rcgu.o | wc -l)" != "1" ]; then
354 echo "Archive contained more than one LDK object file"
359 ar x --output=tmp "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
361 llvm-dis ldk*-cgu.*.rcgu.o
362 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
363 llvm-as ldk*-cgu.*.rcgu.o.ll -o ./libldk.bc
366 LDK_LIB="tmp/libldk.bc tmp/libldk.a"
368 $COMPILE -o bindings.o -c -flto -O3 -I"$1"/lightning-c-bindings/include/ $2 src/main/jni/bindings.c
369 $COMPILE $LINK -o liblightningjni_release$LDK_TARGET_SUFFIX.so -flto -Wl,--lto-O3 -Wl,-O3 -O3 -I"$1"/lightning-c-bindings/include/ $2 bindings.o $LDK_LIB -lm
370 llvm-strip liblightningjni_release$LDK_TARGET_SUFFIX.so
371 if [ "$IS_MAC" = "false" -a "$4" = "false" ]; then
372 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)"
373 if [ "$GLIBC_SYMBS" != "" ]; then
374 echo "Unexpected glibc version dependency! Some users need glibc 2.17 support, symbols for newer glibcs cannot be included."
378 REALLOC_ARRAY_SYMBS="$(objdump -T liblightningjni_release$LDK_TARGET_SUFFIX.so | grep reallocarray || echo)"
379 if [ "$REALLOC_ARRAY_SYMBS" != "" ]; then
380 echo "Unexpected reallocarray dependency!"
384 if [ "$LDK_JAR_TARGET" = "true" ]; then
385 # Copy to JNI native directory for inclusion in JARs
386 mkdir -p src/main/resources/
387 cp liblightningjni_release$LDK_TARGET_SUFFIX.so src/main/resources/liblightningjni$LDK_TARGET_SUFFIX.nativelib