]> git.bitcoin.ninja Git - rust-lightning/commitdiff
[bindings] Build a wasm32-wasi library in genbindings.sh
authorMatt Corallo <git@bluematt.me>
Sat, 9 Jan 2021 02:55:47 +0000 (21:55 -0500)
committerMatt Corallo <git@bluematt.me>
Mon, 1 Mar 2021 23:07:29 +0000 (18:07 -0500)
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
lightning-c-bindings/Cargo.toml
lightning-c-bindings/include/lightning.h

index 1ca2f6e8e886d1463d24bc9da55ae487ab04fde3..bfeda6bcdaebac000f32e4d1968355395c314821 100755 (executable)
@@ -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 <stdlib.h>//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 <stdlib.h>//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
index 9f7210386b629ad4c10be009129a8673180a1bff..76047314ea148ad27d667ec63155a2f91a43bde8 100644 (file)
@@ -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
index fc5bf8b69ac3ff508ae7012948124e1c1e3ff23d..8076e251b85ed9039859d20a1df09320007a610e 100644 (file)
@@ -7,7 +7,7 @@
 #include <stdarg.h>
 #include <stdbool.h>
 #include <stdint.h>
-#include <stdlib.h>
+
 
 /**
  * An error when accessing the chain via [`Access`].