Merge pull request #30 from TheBlueMatt/main
[ldk-c-bindings] / genbindings.sh
index 2a5e985e99355b71010484d3d3f11c246f8824da..812819dcbd3f98f98ab7195bfa8f9fcb85ad4647 100755 (executable)
@@ -9,11 +9,13 @@ if [ ! -d "$1/lightning" -o "$2" != "true" -a "$2" != "false" ]; then
        exit 1
 fi
 
+export LC_ALL=C
+
 # On reasonable systems, we can use realpath here, but OSX is a diva with 20-year-old software.
 ORIG_PWD="$(pwd)"
 cd "$1"
 LIGHTNING_PATH="$(pwd)"
-LIGHTNING_GIT="$(git describe --tag --dirty)"
+LIGHTNING_GIT="$(git describe --tag --dirty --abbrev=16)"
 cd "$ORIG_PWD"
 
 # Generate (and reasonably test) C bindings
@@ -31,7 +33,7 @@ mv lightning-c-bindings/src/bitcoin ./
 
 git checkout lightning-c-bindings/src
 git checkout lightning-c-bindings/include
-BINDINGS_GIT="$(git describe --tag --dirty)"
+BINDINGS_GIT="$(git describe --tag --dirty --abbrev=16)"
 echo -e "#ifndef _LDK_HEADER_VER" > lightning-c-bindings/include/ldk_ver.h
 echo -e "static inline int _ldk_strncmp(const char *s1, const char *s2, uint64_t n) {" >> lightning-c-bindings/include/ldk_ver.h
 echo -e "\tif (n && *s1 != *s2) return 1;" >> lightning-c-bindings/include/ldk_ver.h
@@ -112,9 +114,11 @@ echo > /tmp/crate-source.txt
 if [ "$2" = "true" ]; then
        add_crate lightning lightning --features=allow_wallclock_use ', features = ["allow_wallclock_use"]'
        add_crate "lightning-persister" "lightning_persister"
+       add_crate "lightning-background-processor" "lightning_background_processor"
 else
        add_crate lightning lightning
        drop_crate "lightning-persister"
+       drop_crate "lightning-background-processor"
 fi
 add_crate "lightning-invoice" "lightning_invoice"
 
@@ -130,21 +134,54 @@ echo -e '\t"'"$BINDINGS_GIT"'".into()' >> lightning-c-bindings/src/version.rs
 echo -e '}' >> lightning-c-bindings/src/version.rs
 
 # Set path to include our rustc wrapper as well as cbindgen
+export LDK_RUSTC_PATH="$(which rustc)"
 PATH="$(pwd)/deterministic-build-wrappers:$PATH:~/.cargo/bin"
 # Now cd to lightning-c-bindings, build the generated bindings, and call cbindgen to build a C header file
 cd lightning-c-bindings
 
+# Set up CFLAGS and RUSTFLAGS vars appropriately for building libsecp256k1 and demo apps...
+BASE_CFLAGS="" # CFLAGS for libsecp256k1
+LOCAL_CFLAGS="" # CFLAGS for demo apps
+BASE_RUSTFLAGS="" # RUSTFLAGS
+
 # Remap paths so that our builds are deterministic
-export RUSTFLAGS="--remap-path-prefix $LIGHTNING_PATH=rust-lightning --remap-path-prefix $(pwd)=ldk-c-bindings --remap-path-prefix $HOME/.cargo= -C target-cpu=generic"
+BASE_RUSTFLAGS="--remap-path-prefix $LIGHTNING_PATH=rust-lightning --remap-path-prefix $(pwd)=ldk-c-bindings --remap-path-prefix $HOME/.cargo="
 
 # If the C compiler supports it, also set -ffile-prefix-map
 echo "int main() {}" > genbindings_path_map_test_file.c
 clang -o /dev/null -ffile-prefix-map=$HOME/.cargo= genbindings_path_map_test_file.c > /dev/null 2>&1 &&
-# Now that we've done our last non-LTO build, turn on LTO in CFLAGS as well
-export CFLAGS="-ffile-prefix-map=$HOME/.cargo="
+export BASE_CFLAGS="-ffile-prefix-map=$HOME/.cargo="
+
+BASE_CFLAGS="$BASE_CFLAGS -frandom-seed=42"
+LOCAL_CFLAGS="-Wall -Wno-nullability-completeness -pthread -Iinclude/"
+
+if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
+       LOCAL_CFLAGS="$LOCAL_CFLAGS -isysroot$(xcrun --show-sdk-path)"
+       BASE_CFLAGS="$BASE_CFLAGS -isysroot$(xcrun --show-sdk-path)"
+       # Targeting aarch64 appears to be supported only starting with Big Sur, so check it before use
+       clang -o /dev/null -target=aarch64-apple-darwin -mcpu=apple-a14 genbindings_path_map_test_file.c > /dev/null 2>&1 &&
+       export CFLAGS_aarch64_apple_darwin="$BASE_CFLAGS -target=aarch64-apple-darwin -mcpu=apple-a14"
+fi
+
 rm genbindings_path_map_test_file.c
 
+ENV_TARGET=$(rustc --version --verbose | grep host | awk '{ print $2 }' | sed 's/-/_/g')
+case "$ENV_TARGET" in
+       "x86_64"*)
+               export RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=sandybridge"
+               export CFLAGS_$ENV_TARGET="$BASE_CFLAGS -march=sandybridge -mcpu=sandybridge -mtune=sandybridge"
+               ;;
+       *)
+               # Assume this isn't targeted at another host and build for the host's CPU.
+               export RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=native"
+               export CFLAGS_$ENV_TARGET="$BASE_CFLAGS -mcpu=native"
+               ;;
+esac
+
 cargo build
+if [ "$CFLAGS_aarch64_apple_darwin" != "" ]; then
+       RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14" cargo build --target aarch64-apple-darwin
+fi
 cbindgen -v --config cbindgen.toml -o include/lightning.h >/dev/null 2>&1
 
 # cbindgen is relatively braindead when exporting typedefs -
@@ -166,9 +203,6 @@ else
 fi
 
 # Finally, sanity-check the generated C and C++ bindings with demo apps:
-
-LOCAL_CFLAGS="-Wall -Wno-nullability-completeness -pthread -Iinclude/"
-
 # Naively run the C demo app:
 gcc $LOCAL_CFLAGS -Wall -g -pthread demo.c target/debug/libldk.a -ldl
 ./a.out
@@ -227,23 +261,13 @@ fi
 RUSTC_LLVM_V=$(rustc --version --verbose | grep "LLVM version" | awk '{ print substr($3, 0, 2); }' | tr -d '.')
 
 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
-       # Apple is special, as always, and decided that they must ensure that there is no way to identify
-       # the LLVM version used. Why? Just to make your life hard.
-       # This list is taken from https://en.wikipedia.org/wiki/Xcode
-       APPLE_CLANG_V=$(clang --version | head -n1 | awk '{ print $4 }')
-       if [ "$APPLE_CLANG_V" = "10.0.0" ]; then
-               CLANG_LLVM_V="6"
-       elif [ "$APPLE_CLANG_V" = "10.0.1" ]; then
-               CLANG_LLVM_V="7"
-       elif [ "$APPLE_CLANG_V" = "11.0.0" ]; then
-               CLANG_LLVM_V="8"
-       elif [ "$APPLE_CLANG_V" = "11.0.3" ]; then
-               CLANG_LLVM_V="9"
-       elif [ "$APPLE_CLANG_V" = "12.0.0" ]; then
-               CLANG_LLVM_V="10"
-       else
-               echo "WARNING: Unable to identify Apple clang LLVM version"
+       # Apple is special, as always, and their versions of clang aren't
+       # compatible with upstream LLVM.
+       if [ "$(clang --version | grep 'Apple clang')" != "" ]; then
+               echo "Apple clang isn't compatible with upstream clang, install upstream clang"
                CLANG_LLVM_V="0"
+       else
+               CLANG_LLVM_V=$(clang --version | head -n1 | awk '{ print substr($4, 0, 2); }' | tr -d '.')
        fi
 else
        CLANG_LLVM_V=$(clang --version | head -n1 | awk '{ print substr($4, 0, 2); }' | tr -d '.')
@@ -254,7 +278,12 @@ if [ "$CLANG_LLVM_V" = "$RUSTC_LLVM_V" ]; then
        CLANGPP=clang++
 elif [ "$(which clang-$RUSTC_LLVM_V)" != "" ]; then
        CLANG="$(which clang-$RUSTC_LLVM_V)"
-       CLANGPP="$(which clang++-$RUSTC_LLVM_V)"
+       CLANGPP="$(which clang++-$RUSTC_LLVM_V || echo clang++)"
+       if [ "$($CLANG --version)" != "$($CLANGPP --version)" ]; then
+               echo "$CLANG and $CLANGPP are not the same version of clang!"
+               unset CLANG
+               unset CLANGPP
+       fi
 fi
 
 if [ "$CLANG" != "" -a "$CLANGPP" = "" ]; then
@@ -271,6 +300,9 @@ if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" -o "$HOST_PLATFORM" = "
                else
                        sed -i.bk 's/,"cdylib"]/]/g' Cargo.toml
                fi
+               if [ "$CFLAGS_aarch64_apple_darwin" != "" ]; then
+                       RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14" RUSTC_BOOTSTRAP=1 cargo rustc --target aarch64-apple-darwin -v -- -Zsanitizer=address -Cforce-frame-pointers=yes || ( mv Cargo.toml.bk Cargo.toml; exit 1)
+               fi
                RUSTC_BOOTSTRAP=1 cargo rustc -v -- -Zsanitizer=address -Cforce-frame-pointers=yes || ( mv Cargo.toml.bk Cargo.toml; exit 1)
                mv Cargo.toml.bk Cargo.toml
 
@@ -294,27 +326,39 @@ cargo clean --release
 CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C lto
 clang++ $LOCAL_CFLAGS -std=c++11 -flto -O2 demo.cpp target/release/libldk.a -ldl
 
+strip ./a.out
+echo "C++ Bin size and runtime with only RL (LTO) optimized:"
+ls -lha a.out
+time ./a.out > /dev/null
+
 if [ "$HOST_PLATFORM" != "host: x86_64-apple-darwin" -a "$CLANGPP" != "" ]; then
        # If we can use cross-language LTO, use it for building C dependencies (i.e. libsecp256k1) as well
        export CC="$CLANG"
-       export CFLAGS_wasm32_wasi="-target wasm32 -flto"
+       # The cc-rs crate tries to force -fdata-sections and -ffunction-sections on, which
+       # breaks -fembed-bitcode, so we turn off cc-rs' default flags and specify exactly
+       # what we want here.
+       export CFLAGS_$ENV_TARGET="$BASE_CFLAGS -fPIC -fembed-bitcode -march=sandybridge"
+       export CRATE_CC_NO_DEFAULTS=true
 fi
 
 if [ "$2" = "false" -a "$(rustc --print target-list | grep wasm32-wasi)" != "" ]; then
        # Test to see if clang supports wasm32 as a target (which is needed to build rust-secp256k1)
        echo "int main() {}" > genbindings_wasm_test_file.c
-       clang -nostdlib -o /dev/null --target=wasm32-wasi -Wl,--no-entry genbindings_wasm_test_file.c > /dev/null 2>&1 &&
-       # And if it does, build a WASM binary without capturing errors
-       cargo rustc -v --target=wasm32-wasi -- -C embed-bitcode=yes &&
-       CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release --target=wasm32-wasi -- -C opt-level=s -C linker-plugin-lto -C lto ||
-       echo "Cannot build WASM lib as clang does not seem to support the wasm32-wasi target"
+       if clang -nostdlib -o /dev/null --target=wasm32-wasi -Wl,--no-entry genbindings_wasm_test_file.c > /dev/null 2>&1; then
+               # And if it does, build a WASM binary without capturing errors
+               export CFLAGS_wasm32_wasi="$BASE_CFLAGS -target wasm32"
+               cargo rustc -v --target=wasm32-wasi
+               export CFLAGS_wasm32_wasi="$BASE_CFLAGS -target wasm32 -Os"
+               CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release --target=wasm32-wasi -- -C embed-bitcode=yes -C opt-level=s -C linker-plugin-lto -C lto
+       else
+               echo "Cannot build WASM lib as clang does not seem to support the wasm32-wasi target"
+       fi
        rm genbindings_wasm_test_file.c
 fi
 
-strip ./a.out
-echo "C++ Bin size and runtime with only RL (LTO) optimized:"
-ls -lha a.out
-time ./a.out > /dev/null
+if [ "$CFLAGS_aarch64_apple_darwin" != "" ]; then
+       RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14" CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release --target aarch64-apple-darwin -- -C lto
+fi
 
 if [ "$HOST_PLATFORM" != "host: x86_64-apple-darwin" -a "$CLANGPP" != "" ]; then
        # Finally, test cross-language LTO. Note that this will fail if rustc and clang++
@@ -322,11 +366,7 @@ if [ "$HOST_PLATFORM" != "host: x86_64-apple-darwin" -a "$CLANGPP" != "" ]; then
        # or Ubuntu packages). This should work fine on Distros which do more involved
        # packaging than simply shipping the rustup binaries (eg Debian should Just Work
        # here).
-       # The cc-rs crate tries to force -fdata-sections and -ffunction-sections on, which
-       # breaks -fembed-bitcode, so we turn off cc-rs' default flags and specify exactly
-       # what we want here.
-       export CFLAGS="$CFLAGS -O3 -fPIC -fembed-bitcode"
-       export CRATE_CC_NO_DEFAULTS=true
+       export CFLAGS_$ENV_TARGET="$BASE_CFLAGS -O3 -fPIC -fembed-bitcode -march=sandybridge"
        # Rust doesn't recognize CFLAGS changes, so we need to clean build artifacts
        cargo clean --release
        CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C linker-plugin-lto -C lto -C link-arg=-fuse-ld=lld