From f2e4f8a2293de3f9a51319d5b42eafc551c158b3 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 8 Jan 2021 21:55:47 -0500 Subject: [PATCH] [bindings] Build a wasm32-wasi library in genbindings.sh This will switch to use the clang/C WASM ABI instead of the wasm_bindgen WASM ABI as of rustc 1.51 (or nightly since [1]), allowing us to link C and Rust code in a single wasm binary. [1] https://github.com/rust-lang/rust/pull/79998 --- genbindings.sh | 11 +++++++++++ lightning-c-bindings/Cargo.toml | 7 +++++++ lightning-c-bindings/include/lightning.h | 2 +- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/genbindings.sh b/genbindings.sh index 1ca2f6e8..bfeda6bc 100755 --- a/genbindings.sh +++ b/genbindings.sh @@ -44,8 +44,16 @@ HOST_PLATFORM="$(rustc --version --verbose | grep "host:")" if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then # OSX sed is for some reason not compatible with GNU sed sed -i '' 's/typedef LDKnative.*Import.*LDKnative.*;//g' include/lightning.h + + # stdlib.h doesn't exist in clang's wasm sysroot, and cbindgen + # doesn't actually use it anyway, so drop the import. + sed -i '' 's/#include //g' include/lightning.h else sed -i 's/typedef LDKnative.*Import.*LDKnative.*;//g' include/lightning.h + + # stdlib.h doesn't exist in clang's wasm sysroot, and cbindgen + # doesn't actually use it anyway, so drop the import. + sed -i 's/#include //g' include/lightning.h fi # Finally, sanity-check the generated C and C++ bindings with demo apps: @@ -171,6 +179,9 @@ else echo "WARNING: Can't use address sanitizer on non-Linux, non-OSX non-x86 platforms" fi +cargo rustc -v --target=wasm32-wasi -- -C embed-bitcode=yes || echo "WARNING: Failed to generate WASM LLVM-bitcode-embedded library" +CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release --target=wasm32-wasi -- -C opt-level=s -C linker-plugin-lto -C lto || echo "WARNING: Failed to generate WASM LLVM-bitcode-embedded optimized library" + # Now build with LTO on on both C++ and rust, but without cross-language LTO: CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C lto clang++ $CFLAGS -std=c++11 -flto -O2 demo.cpp target/release/libldk.a -ldl diff --git a/lightning-c-bindings/Cargo.toml b/lightning-c-bindings/Cargo.toml index 9f721038..76047314 100644 --- a/lightning-c-bindings/Cargo.toml +++ b/lightning-c-bindings/Cargo.toml @@ -23,6 +23,13 @@ lightning = { version = "0.0.12", path = "../lightning" } # Rust-Secp256k1 PR 279. Should be dropped once merged. secp256k1 = { git = 'https://github.com/TheBlueMatt/rust-secp256k1', rev = '15a0d4195a20355f6b1e8f54c84eba56abc15cbd' } +# Always force panic=abort, further options are set in the genbindings.sh build script +[profile.dev] +panic = "abort" + +[profile.release] +panic = "abort" + # We eventually want to join the root workspace, but for now, the bindings generation is # a bit brittle and we don't want to hold up other developers from making changes just # because they break the bindings diff --git a/lightning-c-bindings/include/lightning.h b/lightning-c-bindings/include/lightning.h index fc5bf8b6..8076e251 100644 --- a/lightning-c-bindings/include/lightning.h +++ b/lightning-c-bindings/include/lightning.h @@ -7,7 +7,7 @@ #include #include #include -#include + /** * An error when accessing the chain via [`Access`]. -- 2.30.2