Merge pull request #113 from TheBlueMatt/main
[ldk-java] / genbindings.sh
index 6c7f6501210dd816317a58b0150488c48680b791..491e1d425397e7f3e1d40340d249f4ca10e6c9b4 100755 (executable)
@@ -6,6 +6,7 @@ usage() {
        echo "debug should either be true, false, or leaks"
        echo "debug of leaks turns on leak tracking on an optimized release bianry"
        echo "android_web should either be true or false and indicates if we build for android (Java) or web (WASM)"
        echo "debug should either be true, false, or leaks"
        echo "debug of leaks turns on leak tracking on an optimized release bianry"
        echo "android_web should either be true or false and indicates if we build for android (Java) or web (WASM)"
+       echo "Note that web currently generates the same results as !web (ie Node.JS)"
        exit 1
 }
 [ "$1" = "" ] && usage
        exit 1
 }
 [ "$1" = "" ] && usage
@@ -15,6 +16,10 @@ usage() {
 set -e
 set -x
 
 set -e
 set -x
 
+function is_gnu_sed(){
+  sed --version >/dev/null 2>&1
+}
+
 if [ "$CC" != "" ]; then
        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)="
 else
 if [ "$CC" != "" ]; then
        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)="
 else
@@ -28,11 +33,19 @@ if [ "$3" = "leaks" ]; then
 fi
 
 cp "$1/lightning-c-bindings/include/lightning.h" ./
 fi
 
 cp "$1/lightning-c-bindings/include/lightning.h" ./
-if [ "$(rustc --version --verbose | grep "host:")" = "host: x86_64-apple-darwin" ]; then
-       # OSX sed is for some reason not compatible with GNU sed
-       sed -i '' "s/TransactionOutputs/C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ/g" ./lightning.h
-else
+if is_gnu_sed; then
        sed -i "s/TransactionOutputs/C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ/g" ./lightning.h
        sed -i "s/TransactionOutputs/C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ/g" ./lightning.h
+else
+  # OSX sed is for some reason not compatible with GNU sed
+       sed -i '' "s/TransactionOutputs/C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZ/g" ./lightning.h
+fi
+
+if [ "$LDK_GARBAGECOLLECTED_GIT_OVERRIDE" = "" ]; then
+       export LDK_GARBAGECOLLECTED_GIT_OVERRIDE=$(git describe --tag --dirty)
+fi
+if [ "${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:0:1}" != "v" ]; then
+       echo "Version tag should start with a v" > /dev/stderr
+       exit 1
 fi
 
 if [ "$2" != "wasm" ]; then
 fi
 
 if [ "$2" != "wasm" ]; then
@@ -51,6 +64,7 @@ if [ "$2" != "wasm" ]; then
                        LDK_JAR_TARGET=true
                        ;;
                "aarch64-apple-darwin"*)
                        LDK_JAR_TARGET=true
                        ;;
                "aarch64-apple-darwin"*)
+                       LDK_TARGET_CPU="apple-a14"
                        LDK_TARGET_SUFFIX="_MacOSX-aarch64"
                        LDK_JAR_TARGET=true
                        ;;
                        LDK_TARGET_SUFFIX="_MacOSX-aarch64"
                        LDK_JAR_TARGET=true
                        ;;
@@ -61,19 +75,11 @@ if [ "$2" != "wasm" ]; then
                LDK_TARGET_CPU="sandybridge"
        fi
 
                LDK_TARGET_CPU="sandybridge"
        fi
 
-       if [ "$LDK_GARBAGECOLLECTED_GIT_OVERRIDE" = "" ]; then
-               export LDK_GARBAGECOLLECTED_GIT_OVERRIDE=$(git describe --tag --dirty)
-       fi
-       if [ "${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:0:1}" != "v" ]; then
-               echo "Version tag should start with a v" > /dev/stderr
-               exit 1
-       fi
-
-       if [ "$(rustc --version --verbose | grep "host:")" = "host: x86_64-apple-darwin" ]; then
-               # OSX sed is for some reason not compatible with GNU sed
-               sed -i '' "s/^    <version>.*<\/version>/    <version>${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}<\/version>/g" pom.xml
-       else
+       if is_gnu_sed; then
                sed -i "s/^    <version>.*<\/version>/    <version>${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}<\/version>/g" pom.xml
                sed -i "s/^    <version>.*<\/version>/    <version>${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}<\/version>/g" pom.xml
+       else
+         # OSX sed is for some reason not compatible with GNU sed
+               sed -i '' "s/^    <version>.*<\/version>/    <version>${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}<\/version>/g" pom.xml
        fi
 
        echo "Creating Java bindings..."
        fi
 
        echo "Creating Java bindings..."
@@ -142,7 +148,7 @@ if [ "$2" != "wasm" ]; then
                                echo "Archive contained non-object files!"
                                exit 1
                        fi
                                echo "Archive contained non-object files!"
                                exit 1
                        fi
-                       if [ "$(ar t "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a | grep ldk.ldk.*-cgu.*.rcgu.o | wc -l)" != "1" ]; then
+                       if [ "$(ar t "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a | grep ldk.*-cgu.*.rcgu.o | wc -l)" != "1" ]; then
                                echo "Archive contained more than one LDK object file"
                                exit 1
                        fi
                                echo "Archive contained more than one LDK object file"
                                exit 1
                        fi
@@ -150,14 +156,14 @@ if [ "$2" != "wasm" ]; then
                        rm -f tmp/*
                        ar x --output=tmp "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
                        pushd tmp
                        rm -f tmp/*
                        ar x --output=tmp "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
                        pushd tmp
-                       llvm-dis ldk.ldk.*-cgu.*.rcgu.o
-                       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
-                       llvm-as ldk.ldk.*-cgu.*.rcgu.o.ll -o ./libldk.bc
+                       llvm-dis ldk*-cgu.*.rcgu.o
+                       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
+                       llvm-as ldk*-cgu.*.rcgu.o.ll -o ./libldk.bc
                        ar q libldk.a *.o
                        popd
                        LDK_LIB="tmp/libldk.bc tmp/libldk.a"
                fi
                        ar q libldk.a *.o
                        popd
                        LDK_LIB="tmp/libldk.bc tmp/libldk.a"
                fi
-               $COMPILE -o liblightningjni_release$LDK_TARGET_SUFFIX.so -flto -O3 -I"$1"/lightning-c-bindings/include/ $2 src/main/jni/bindings.c $LDK_LIB
+               $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
                if [ "$IS_MAC" = "false" -a "$4" = "false" ]; then
                        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)"
                        if [ "$GLIBC_SYMBS" != "" ]; then
                if [ "$IS_MAC" = "false" -a "$4" = "false" ]; then
                        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)"
                        if [ "$GLIBC_SYMBS" != "" ]; then
@@ -180,13 +186,16 @@ if [ "$2" != "wasm" ]; then
 else
        echo "Creating TS bindings..."
        mkdir -p ts/{enums,structs}
 else
        echo "Creating TS bindings..."
        mkdir -p ts/{enums,structs}
-       rm -f ts/{enums,structs,}/*.{mjs,mts}
+       rm -f ts/{enums,structs,}/*.{mjs,mts,mts.part}
        if [ "$4" = "false" ]; then
                ./genbindings.py "./lightning.h" ts ts ts $DEBUG_ARG typescript node
        else
                ./genbindings.py "./lightning.h" ts ts ts $DEBUG_ARG typescript browser
        fi
        rm -f ts/bindings.c
        if [ "$4" = "false" ]; then
                ./genbindings.py "./lightning.h" ts ts ts $DEBUG_ARG typescript node
        else
                ./genbindings.py "./lightning.h" ts ts ts $DEBUG_ARG typescript browser
        fi
        rm -f ts/bindings.c
+       sed -i 's/^  "version": .*/  "version": "'${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}'",/g' ts/package.json
+       sed -i 's/^  "version": .*/  "version": "'${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}'",/g' node-net/package.json
+       sed -i 's/^    "lightningdevkit": .*/    "lightningdevkit": "'${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}'"/g' node-net/package.json
        if [ "$3" = "true" ]; then
                echo "#define LDK_DEBUG_BUILD" > ts/bindings.c
        elif [ "$3" = "leaks" ]; then
        if [ "$3" = "true" ]; then
                echo "#define LDK_DEBUG_BUILD" > ts/bindings.c
        elif [ "$3" = "leaks" ]; then
@@ -199,34 +208,38 @@ else
        cat ts/bindings.c.body >> ts/bindings.c
 
        echo "Building TS bindings..."
        cat ts/bindings.c.body >> ts/bindings.c
 
        echo "Building TS bindings..."
-       COMPILE="$COMMON_COMPILE -flto -Wl,--no-entry -Wl,--export-dynamic -Wl,-allow-undefined -nostdlib --target=wasm32-wasi"
+       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"
        # We only need malloc and assert/abort, but for now just use WASI for those:
        # We only need malloc and assert/abort, but for now just use WASI for those:
-       #EXTRA_LINK=/usr/lib/wasm32-wasi/libc.a
-       EXTRA_LINK=
-       [ "$3" != "false" ] && COMPILE="$COMPILE -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,reallocarray -Wl,-wrap,malloc -Wl,-wrap,free"
+       EXTRA_LINK=/usr/lib/wasm32-wasi/libc.a
+       [ "$3" != "false" ] && COMPILE="$COMPILE -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,reallocarray -Wl,-wrap,malloc -Wl,-wrap,aligned_alloc -Wl,-wrap,free"
        if [ "$3" = "true" ]; then
                WASM_FILE=liblightningjs_debug.wasm
        if [ "$3" = "true" ]; then
                WASM_FILE=liblightningjs_debug.wasm
-               $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
+               $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
        else
                WASM_FILE=liblightningjs_release.wasm
        else
                WASM_FILE=liblightningjs_release.wasm
-               $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
+               $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
        fi
 
        if [ -x "$(which tsc)" ]; then
                cd ts
        fi
 
        if [ -x "$(which tsc)" ]; then
                cd ts
-       rm -r structs # TODO: Make the human-types compile
-               if [ "$4" = "true" ]; then
-                       tsc
-               else
-                       tsc --types node --typeRoots .
-                       cd ..
-                       if [ -x "$(which node)" ]; then
-                               NODE_V="$(node --version)"
-                               if [ "${NODE_V:1:2}" -gt 14 ]; then
-                                       rm -f liblightningjs.wasm
-                                       ln -s $WASM_FILE liblightningjs.wasm
-                                       node ts/test/node.mjs
-                               fi
+               for F in structs/*; do
+                       cat imports.mts.part | grep -v " $(basename -s .mts $F)[ ,]" | cat - $F > $F.tmp
+                       mv $F.tmp $F
+               done
+               rm imports.mts.part
+               tsc --types node --typeRoots .
+               cp ../$WASM_FILE liblightningjs.wasm
+               cp ../README.md README.md
+               cd ../node-net
+               tsc --types node --typeRoots .
+               echo Ready to publish!
+               if [ -x "$(which node)" ]; then
+                       NODE_V="$(node --version)"
+                       if [ "${NODE_V:1:2}" -gt 14 ]; then
+                               cd ../ts
+                               node --stack_trace_limit=200 --trace-uncaught test/node.mjs
+                               cd ../node-net
+                               node --stack_trace_limit=200 --trace-uncaught test/test.mjs
                        fi
                fi
        fi
                        fi
                fi
        fi