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
27 TARGET_STRING="$LDK_TARGET"
28 if [ "$TARGET_STRING" = "" ]; then
29 # We assume clang-style $CC --version here, but worst-case we just get an empty suffix
30 TARGET_STRING="$($CC --version | grep Target | awk '{ print $2 }')"
34 [ "$($CC --version | grep apple-darwin)" != "" ] && IS_MAC=true
36 [ "$($CC --version | grep "Apple clang version")" != "" ] && IS_APPLE_CLANG=true
38 case "$TARGET_STRING" in
40 LDK_TARGET_SUFFIX="_Linux-amd64"
43 "x86_64-apple-darwin"*)
44 LDK_TARGET_SUFFIX="_MacOSX-x86_64"
48 "aarch64-apple-darwin"*)
49 LDK_TARGET_CPU="apple-a14"
50 LDK_TARGET_SUFFIX="_MacOSX-aarch64"
55 LDK_TARGET_SUFFIX="_${TARGET_STRING}"
57 if [ "$LDK_TARGET_CPU" = "" ]; then
58 LDK_TARGET_CPU="sandybridge"
61 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)="
62 [ "$IS_MAC" = "true" -a "$2" != "wasm" ] && COMMON_COMPILE="$COMMON_COMPILE --target=$TARGET_STRING -mcpu=$LDK_TARGET_CPU"
65 if [ "$3" = "leaks" ]; then
69 cp "$1/lightning-c-bindings/include/lightning.h" ./
71 sed -i "s/TransactionOutputs/C2Tuple_ThirtyTwoBytesCVec_C2Tuple_u32TxOutZZZ/g" ./lightning.h
73 # OSX sed is for some reason not compatible with GNU sed
74 sed -i '' "s/TransactionOutputs/C2Tuple_ThirtyTwoBytesCVec_C2Tuple_u32TxOutZZZ/g" ./lightning.h
76 echo "#define LDKCVec_C2Tuple_ThirtyTwoBytesCVec_C2Tuple_u32TxOutZZZZ LDKCVec_TransactionOutputsZ" > header.c
77 echo "#define CVec_C2Tuple_ThirtyTwoBytesCVec_C2Tuple_u32TxOutZZZZ_free CVec_TransactionOutputsZ_free" >> header.c
80 if [ "$LDK_GARBAGECOLLECTED_GIT_OVERRIDE" = "" ]; then
81 export LDK_GARBAGECOLLECTED_GIT_OVERRIDE=$(git describe --tag --dirty)
83 if [ "${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:0:1}" != "v" ]; then
84 echo "Version tag should start with a v" > /dev/stderr
89 if [ "$2" = "c_sharp" ]; then
90 echo "Creating C# bindings..."
91 mkdir -p c_sharp/src/org/ldk/{enums,structs,impl}
92 rm -f c_sharp/src/org/ldk/{enums,structs,impl}/*.cs
93 ./genbindings.py "./lightning.h" c_sharp/src/org/ldk/impl c_sharp/src/org/ldk c_sharp/ $DEBUG_ARG c_sharp $4 $TARGET_STRING
94 rm -f c_sharp/bindings.c
95 if [ "$3" = "true" ]; then
96 echo "#define LDK_DEBUG_BUILD" > c_sharp/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)" > c_sharp/bindings.c
102 cat header.c >> c_sharp/bindings.c
103 cat header.c >> c_sharp/bindings.c
104 cat c_sharp/bindings.c.body >> c_sharp/bindings.c
107 [ "$($CC --version | grep apple-darwin)" != "" ] && IS_MAC=true
109 [ "$($CC --version | grep "Apple clang version")" != "" ] && IS_APPLE_CLANG=true
111 # Compiling C# bindings with Mono
112 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"
113 if [ "$3" = "true" ]; then
114 mono-csc $MONO_COMPILE
116 mono-csc -o $MONO_COMPILE
119 echo "Building C# bindings..."
120 COMPILE="$COMMON_COMPILE -Isrc/main/jni -pthread -fPIC"
122 [ "$IS_MAC" = "false" ] && LINK="$LINK -Wl,--no-undefined"
123 [ "$IS_MAC" = "true" ] && COMPILE="$COMPILE -mmacosx-version-min=10.9"
124 [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && LINK="$LINK -fuse-ld=lld"
125 [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && echo "WARNING: Need at least upstream clang 13!"
126 [ "$IS_MAC" = "false" -a "$3" != "false" ] && LINK="$LINK -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,malloc -Wl,-wrap,free"
128 exit 0 # Sadly compilation doesn't currently work
129 if [ "$3" = "true" ]; then
130 $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
132 $COMPILE -o bindings.o -c -flto -O3 -I"$1"/lightning-c-bindings/include/ $2 c_sharp/bindings.c
133 $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
134 [ "$IS_APPLE_CLANG" != "true" ] && llvm-strip liblightningjni_release$LDK_TARGET_SUFFIX.so
136 elif [ "$2" = "python" ]; then
137 echo "Creating Python bindings..."
138 mkdir -p python/src/{enums,structs,impl}
139 rm -f python/src/{enums,structs,impl}/*.py
140 ./genbindings.py "./lightning.h" python/src/impl python/src python/ $DEBUG_ARG python $4 $TARGET_STRING
141 rm -f python/bindings.c
142 if [ "$3" = "true" ]; then
143 echo "#define LDK_DEBUG_BUILD" > python/bindings.c
144 elif [ "$3" = "leaks" ]; then
145 # For leak checking we use release libldk which doesn't expose
146 # __unmangle_inner_ptr, but the C code expects to be able to call it.
147 echo "#define __unmangle_inner_ptr(a) (a)" > python/bindings.c
149 echo "#define LDKCVec_C2Tuple_ThirtyTwoBytesCVec_C2Tuple_u32TxOutZZZZ LDKCVec_TransactionOutputsZ" >> python/bindings.c
150 echo "#define CVec_C2Tuple_ThirtyTwoBytesCVec_C2Tuple_u32TxOutZZZZ_free CVec_TransactionOutputsZ_free" >> python/bindings.c
151 cat python/bindings.c.body >> python/bindings.c
154 [ "$($CC --version | grep apple-darwin)" != "" ] && IS_MAC=true
156 [ "$($CC --version | grep "Apple clang version")" != "" ] && IS_APPLE_CLANG=true
158 echo "Building Python bindings..."
159 COMPILE="$COMMON_COMPILE -Isrc/main/jni -pthread -fPIC"
161 [ "$IS_MAC" = "false" ] && LINK="$LINK -Wl,--no-undefined"
162 [ "$IS_MAC" = "true" ] && COMPILE="$COMPILE -mmacosx-version-min=10.9"
163 [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && LINK="$LINK -fuse-ld=lld"
164 [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && echo "WARNING: Need at least upstream clang 13!"
165 [ "$IS_MAC" = "false" -a "$3" != "false" ] && LINK="$LINK -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,malloc -Wl,-wrap,free"
167 exit 0 # Sadly compilation doesn't currently work
168 if [ "$3" = "true" ]; then
169 $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
171 $COMPILE -o bindings.o -c -flto -O3 -I"$1"/lightning-c-bindings/include/ $2 c_sharp/bindings.c
172 $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
173 [ "$IS_APPLE_CLANG" != "true" ] && llvm-strip liblightningpython_release$LDK_TARGET_SUFFIX.so
175 elif [ "$2" = "wasm" ]; then
176 echo "Creating TS bindings..."
177 mkdir -p ts/{enums,structs}
178 rm -f ts/{enums,structs,}/*.{mjs,mts,mts.part}
179 if [ "$4" = "false" ]; then
180 ./genbindings.py "./lightning.h" ts ts ts $DEBUG_ARG typescript node wasm
182 ./genbindings.py "./lightning.h" ts ts ts $DEBUG_ARG typescript browser wasm
185 sed -i 's/^ "version": .*/ "version": "'${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}'",/g' ts/package.json
186 sed -i 's/^ "version": .*/ "version": "'${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}'",/g' node-net/package.json
187 sed -i 's/^ "lightningdevkit": .*/ "lightningdevkit": "'${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}'"/g' node-net/package.json
188 if [ "$3" = "true" ]; then
189 echo "#define LDK_DEBUG_BUILD" > ts/bindings.c
190 elif [ "$3" = "leaks" ]; then
191 # For leak checking we use release libldk which doesn't expose
192 # __unmangle_inner_ptr, but the C code expects to be able to call it.
193 echo "#define __unmangle_inner_ptr(a) (a)" > ts/bindings.c
195 cat header.c >> ts/bindings.c
196 cat ts/bindings.c.body >> ts/bindings.c
198 echo "Building TS bindings..."
199 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"
200 # We only need malloc and assert/abort, but for now just use WASI for those:
201 EXTRA_LINK=/usr/lib/wasm32-wasi/libc.a
202 [ "$3" != "false" ] && COMPILE="$COMPILE -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,reallocarray -Wl,-wrap,malloc -Wl,-wrap,aligned_alloc -Wl,-wrap,free"
203 if [ "$3" = "true" ]; then
204 WASM_FILE=liblightningjs_debug.wasm
205 $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
207 WASM_FILE=liblightningjs_release.wasm
208 $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
211 if [ -x "$(which tsc)" ]; then
213 for F in structs/*; do
214 cat imports.mts.part | grep -v " $(basename -s .mts $F)[ ,]" | cat - $F > $F.tmp
218 tsc --types node --typeRoots .
219 cp ../$WASM_FILE liblightningjs.wasm
220 cp ../README.md README.md
222 tsc --types node --typeRoots .
223 echo Ready to publish!
224 if [ -x "$(which node)" ]; then
225 NODE_V="$(node --version)"
226 if [ "${NODE_V:1:2}" -gt 14 ]; then
228 node --stack_trace_limit=200 --trace-uncaught test/node.mjs
230 node --stack_trace_limit=200 --trace-uncaught test/test.mjs
236 sed -i "s/^ <version>.*<\/version>/ <version>${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}<\/version>/g" pom.xml
238 # OSX sed is for some reason not compatible with GNU sed
239 sed -i '' "s/^ <version>.*<\/version>/ <version>${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}<\/version>/g" pom.xml
242 echo "Creating Java bindings..."
243 mkdir -p src/main/java/org/ldk/{enums,structs}
244 rm -f src/main/java/org/ldk/{enums,structs}/*.java
245 rm -f src/main/jni/*.h
246 if [ "$4" = "true" ]; then
247 ./genbindings.py "./lightning.h" src/main/java/org/ldk/impl src/main/java/org/ldk src/main/jni/ $DEBUG_ARG android $4 $TARGET_STRING
249 ./genbindings.py "./lightning.h" src/main/java/org/ldk/impl src/main/java/org/ldk src/main/jni/ $DEBUG_ARG java $4 $TARGET_STRING
251 rm -f src/main/jni/bindings.c
252 if [ "$3" = "true" ]; then
253 echo "#define LDK_DEBUG_BUILD" > src/main/jni/bindings.c
254 elif [ "$3" = "leaks" ]; then
255 # For leak checking we use release libldk which doesn't expose
256 # __unmangle_inner_ptr, but the C code expects to be able to call it.
257 echo "#define __unmangle_inner_ptr(a) (a)" > src/main/jni/bindings.c
259 cat header.c >> src/main/jni/bindings.c
260 cat header.c >> src/main/jni/bindings.c
261 cat src/main/jni/bindings.c.body >> src/main/jni/bindings.c
262 javac -h src/main/jni src/main/java/org/ldk/enums/*.java src/main/java/org/ldk/impl/*.java
263 rm src/main/java/org/ldk/enums/*.class src/main/java/org/ldk/impl/bindings*.class
265 echo "Building Java bindings..."
266 COMPILE="$COMMON_COMPILE -Isrc/main/jni -pthread -fPIC"
268 [ "$IS_MAC" = "false" ] && LINK="$LINK -Wl,--no-undefined"
269 [ "$IS_MAC" = "true" ] && COMPILE="$COMPILE -mmacosx-version-min=10.9"
270 [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && LINK="$LINK -fuse-ld=lld"
271 [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && echo "WARNING: Need at least upstream clang 13!"
272 [ "$IS_MAC" = "false" -a "$3" != "false" ] && LINK="$LINK -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,malloc -Wl,-wrap,free"
273 if [ "$3" = "true" ]; then
274 $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
276 [ "$IS_MAC" = "false" ] && LINK="$LINK -Wl,--no-undefined -flto -Wl,-O3 -Wl,--lto-O3"
277 [ "$IS_MAC" = "false" ] && COMPILE="$COMPILE -flto"
278 [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && LINK="$LINK -flto -Wl,-O3 -Wl,--lto-O3"
279 [ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && COMPILE="$COMPILE -flto"
280 LDK_LIB="$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
281 if [ "$IS_MAC" = "false" -a "$4" = "false" ]; then
282 LINK="$LINK -Wl,--version-script=libcode.version -fuse-ld=lld"
283 # __cxa_thread_atexit_impl is used to more effeciently cleanup per-thread local storage by rust libstd.
284 # However, it is not available on glibc versions 2.17 or earlier, and rust libstd has a null-check and
285 # fallback in case it is missing.
286 # Because it is weak-linked on the rust side, we should be able to simply define it
287 # explicitly, forcing rust to use the fallback. However, for some reason involving ancient
288 # dark magic and haunted code segments, overriding the weak symbol only impacts sites which
289 # *call* the symbol in question, not sites which *compare with* the symbol in question.
290 # This means that the NULL check in rust's libstd will always think the function is
291 # callable while the function which is called ends up being NULL (leading to a jmp to the
292 # zero page and a quick SEGFAULT).
293 # This issue persists not only with directly providing a symbol, but also ld.lld's -wrap
294 # and --defsym arguments.
295 # In smaller programs, it appears to be possible to work around this with -Bsymbolic and
296 # -nostdlib, however when applied the full-sized JNI library here it no longer works.
297 # After exhausting nearly every flag documented in lld, the only reliable method appears
298 # to be editing the LDK binary. Luckily, LLVM's tooling makes this rather easy as we can
299 # disassemble it into very readable code, edit it, and then reassemble it.
300 # Note that if we do so we don't have to bother overriding the actual call, LLVM should
301 # optimize it away, which also provides a good check that there isn't anything actually
302 # relying on it elsewhere.
303 [ ! -f "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a ] && exit 1
304 if [ "$(ar t "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a | grep -v "\.o$" || echo)" != "" ]; then
305 echo "Archive contained non-object files!"
308 if [ "$(ar t "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a | grep ldk.*-cgu.*.rcgu.o | wc -l)" != "1" ]; then
309 echo "Archive contained more than one LDK object file"
314 ar x --output=tmp "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
316 llvm-dis ldk*-cgu.*.rcgu.o
317 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
318 llvm-as ldk*-cgu.*.rcgu.o.ll -o ./libldk.bc
321 LDK_LIB="tmp/libldk.bc tmp/libldk.a"
323 $COMPILE -o bindings.o -c -O3 -I"$1"/lightning-c-bindings/include/ $2 src/main/jni/bindings.c
324 $COMPILE $LINK -o liblightningjni_release$LDK_TARGET_SUFFIX.so -O3 -I"$1"/lightning-c-bindings/include/ $2 bindings.o $LDK_LIB -lm
325 [ "$IS_APPLE_CLANG" != "true" ] && llvm-strip liblightningjni_release$LDK_TARGET_SUFFIX.so
326 if [ "$IS_MAC" = "false" -a "$4" = "false" ]; then
327 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)"
328 if [ "$GLIBC_SYMBS" != "" ]; then
329 echo "Unexpected glibc version dependency! Some users need glibc 2.17 support, symbols for newer glibcs cannot be included."
333 REALLOC_ARRAY_SYMBS="$(objdump -T liblightningjni_release$LDK_TARGET_SUFFIX.so | grep reallocarray || echo)"
334 if [ "$REALLOC_ARRAY_SYMBS" != "" ]; then
335 echo "Unexpected reallocarray dependency!"
339 if [ "$LDK_JAR_TARGET" = "true" ]; then
340 # Copy to JNI native directory for inclusion in JARs
341 mkdir -p src/main/resources/
342 cp liblightningjni_release$LDK_TARGET_SUFFIX.so src/main/resources/liblightningjni$LDK_TARGET_SUFFIX.nativelib