Skip to content

Commit

Permalink
[bindings] Build a wasm32-wasi library in genbindings.sh
Browse files Browse the repository at this point in the history
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] rust-lang/rust#79998
  • Loading branch information
TheBlueMatt committed Feb 26, 2021
1 parent c6c15fa commit ea0d421
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
11 changes: 11 additions & 0 deletions genbindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions lightning-c-bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lightning-c-bindings/include/lightning.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>


/**
* An error when accessing the chain via [`Access`].
Expand Down

0 comments on commit ea0d421

Please sign in to comment.