Skip to content

Commit e81fab9

Browse files
committed
[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] rust-lang/rust#79998
1 parent 2ab424f commit e81fab9

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

genbindings.sh

+7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ else
4848
sed -i 's/typedef LDKnative.*Import.*LDKnative.*;//g' include/lightning.h
4949
fi
5050

51+
# stdlib.h doesn't exist in clang's wasm sysroot, and cbindgen
52+
# doesn't actually use it anyway, so drop the import.
53+
sed -i 's/#include <stdlib.h>//g' include/lightning.h
54+
5155
# Finally, sanity-check the generated C and C++ bindings with demo apps:
5256

5357
CFLAGS="-Wall -Wno-nullability-completeness -pthread"
@@ -171,6 +175,9 @@ else
171175
echo "WARNING: Can't use address sanitizer on non-Linux, non-OSX non-x86 platforms"
172176
fi
173177

178+
cargo rustc -v --target=wasm32-wasi -- -C embed-bitcode=yes || echo "WARNING: Failed to generate WASM LLVM-bitcode-embedded library"
179+
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"
180+
174181
# Now build with LTO on on both C++ and rust, but without cross-language LTO:
175182
CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C lto
176183
clang++ $CFLAGS -std=c++11 -flto -O2 demo.cpp target/release/libldk.a -ldl

lightning-c-bindings/Cargo.toml

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ lightning = { version = "0.0.12", path = "../lightning" }
2323
# Rust-Secp256k1 PR 279. Should be dropped once merged.
2424
secp256k1 = { git = 'https://github.com/TheBlueMatt/rust-secp256k1', rev = '15a0d4195a20355f6b1e8f54c84eba56abc15cbd' }
2525

26+
# Always force panic=abort, further options are set in the genbindings.sh build script
27+
[profile.dev]
28+
panic = "abort"
29+
30+
[profile.release]
31+
panic = "abort"
32+
2633
# We eventually want to join the root workspace, but for now, the bindings generation is
2734
# a bit brittle and we don't want to hold up other developers from making changes just
2835
# because they break the bindings

lightning-c-bindings/include/lightning.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <stdarg.h>
88
#include <stdbool.h>
99
#include <stdint.h>
10-
#include <stdlib.h>
10+
1111

1212
/**
1313
* An error when accessing the chain via [`Access`].

0 commit comments

Comments
 (0)