Merge pull request #36 from TheBlueMatt/main
[ldk-c-bindings] / genbindings.sh
index 094bfedc3a8d9e8af8430fce1fef418f892c6666..cd100f6a86f1fd00d81de86bc3a5df28435f910d 100755 (executable)
@@ -20,6 +20,55 @@ cd "$ORIG_PWD"
 
 # Generate (and reasonably test) C bindings
 
+# First we set various compiler flags...
+HOST_PLATFORM="$(rustc --version --verbose | grep "host:")"
+
+# Set path to include our rustc wrapper as well as cbindgen
+export LDK_RUSTC_PATH="$(which rustc)"
+export RUSTC="$(pwd)/deterministic-build-wrappers/rustc"
+PATH="$PATH:~/.cargo/bin"
+
+# 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
+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 &&
+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
+       export MACOSX_DEPLOYMENT_TARGET=10.9
+       LOCAL_CFLAGS="$LOCAL_CFLAGS -isysroot$(xcrun --show-sdk-path) -mmacosx-version-min=10.9"
+       BASE_CFLAGS="$BASE_CFLAGS -isysroot$(xcrun --show-sdk-path) -mmacosx-version-min=10.9"
+       # Targeting aarch64 appears to be supported only starting with Big Sur, so check it before use
+       clang -o /dev/null $BASE_CFLAGS --target=aarch64-apple-darwin -mcpu=apple-a14 genbindings_path_map_test_file.c &&
+       export CFLAGS_aarch64_apple_darwin="$BASE_CFLAGS --target=aarch64-apple-darwin -mcpu=apple-a14" ||
+       echo "WARNING: Can not build targeting aarch64-apple-darin. Upgrade to Big Sur or try upstream clang"
+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
+
 # First build the latest c-bindings-gen binary
 cd c-bindings-gen && cargo build --release && cd ..
 
@@ -77,8 +126,6 @@ OUT_F="$(pwd)/lightning-c-bindings/include/ldk_rust_types.h"
 OUT_CPP="$(pwd)/lightning-c-bindings/include/lightningpp.hpp"
 BIN="$(pwd)/c-bindings-gen/target/release/c-bindings-gen"
 
-HOST_PLATFORM="$(rustc --version --verbose | grep "host:")"
-
 function add_crate() {
        pushd "$LIGHTNING_PATH/$1"
        RUSTC_BOOTSTRAP=1 cargo rustc --profile=check $3 -- -Zunstable-options --pretty=expanded > /tmp/$1-crate-source.txt
@@ -133,53 +180,9 @@ echo -e 'pub extern "C" fn _ldk_c_bindings_get_compiled_version() -> crate::c_ty
 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
-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 &&
-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
-       export MACOSX_DEPLOYMENT_TARGET=10.9
-       LOCAL_CFLAGS="$LOCAL_CFLAGS -isysroot$(xcrun --show-sdk-path) -mmacosx-version-min=10.9"
-       BASE_CFLAGS="$BASE_CFLAGS -isysroot$(xcrun --show-sdk-path) -mmacosx-version-min=10.9"
-       # Targeting aarch64 appears to be supported only starting with Big Sur, so check it before use
-       clang -o /dev/null $BASE_CFLAGS --target=aarch64-apple-darwin -mcpu=apple-a14 genbindings_path_map_test_file.c &&
-       export CFLAGS_aarch64_apple_darwin="$BASE_CFLAGS --target=aarch64-apple-darwin -mcpu=apple-a14" ||
-       echo "WARNING: Can not build targeting aarch64-apple-darin. Upgrade to Big Sur or try upstream clang"
-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
@@ -240,7 +243,7 @@ if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" ]; then
                LLVM_V=$(rustc +nightly --version --verbose | grep "LLVM version" | awk '{ print substr($3, 0, 2); }')
                if [ -x "$(which clang-$LLVM_V)" ]; then
                        cargo +nightly clean
-                       cargo +nightly rustc -Zbuild-std --target x86_64-unknown-linux-gnu -v -- -Zsanitizer=memory -Zsanitizer-memory-track-origins -Cforce-frame-pointers=yes
+                       cargo +nightly rustc -Zbuild-std=std,panic_abort --target x86_64-unknown-linux-gnu -v -- -Zsanitizer=memory -Zsanitizer-memory-track-origins -Cforce-frame-pointers=yes
                        mv target/x86_64-unknown-linux-gnu/debug/libldk.* target/debug/
 
                        # Sadly, std doesn't seem to compile into something that is memsan-safe as of Aug 2020,
@@ -394,6 +397,21 @@ if [ "$2" = "false" -a "$(rustc --print target-list | grep wasm32-wasi)" != "" ]
        rm genbindings_wasm_test_file.c
 fi
 
+EXTRA_TARGETS=( $LDK_C_BINDINGS_EXTRA_TARGETS )
+EXTRA_CCS=( $LDK_C_BINDINGS_EXTRA_TARGET_CCS )
+
+if [ ${#EXTRA_TARGETS[@]} != ${#EXTRA_CCS[@]} ]; then
+       echo "LDK_C_BINDINGS_EXTRA_TARGETS and LDK_C_BINDINGS_EXTRA_TARGET_CCS didn't have the same number of elements!"
+       exit 1
+fi
+
+for IDX in ${!EXTRA_TARGETS[@]}; do
+       EXTRA_ENV_TARGET=$(echo "${EXTRA_TARGETS[$IDX]}" | sed 's/-/_/g')
+       export CFLAGS_$EXTRA_ENV_TARGET="$BASE_CFLAGS"
+       export CC_$EXTRA_ENV_TARGET=${EXTRA_CCS[$IDX]}
+       RUSTFLAGS="$BASE_RUSTFLAGS -C linker=${EXTRA_CCS[$IDX]}" CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release --target "${EXTRA_TARGETS[$IDX]}" -- -C lto
+done
+
 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