Merge pull request #17 from TheBlueMatt/2021-04-upstream-confirm
[ldk-c-bindings] / genbindings.sh
index a5c6bc18106bd36cd8140cff2f43e62aa6ac5728..e85f14af5ac35e798fd34ab689bf4d55676e5b34 100755 (executable)
@@ -87,9 +87,16 @@ fi
 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
+
 # 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"
+
+# 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="
+rm genbindings_path_map_test_file.c
 
 cargo build
 cbindgen -v --config cbindgen.toml -o include/lightning.h >/dev/null 2>&1
@@ -247,7 +254,7 @@ if [ "$HOST_PLATFORM" != "host: x86_64-apple-darwin" -a "$CLANGPP" != "" ]; then
        export CFLAGS_wasm32_wasi="-target wasm32"
 fi
 
-if [ "$(rustc --print target-list | grep wasm32-wasi)" != "" ]; then
+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 &&
@@ -271,7 +278,11 @@ 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).
-       export CFLAGS="$CFLAGS -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="$CFLAGS -O3 -fPIC -fembed-bitcode"
+       export CRATE_CC_NO_DEFAULTS=true
        # 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