X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=genbindings.sh;h=bfeda6bcdaebac000f32e4d1968355395c314821;hb=d924c7a8ae4344abfa934d95a0d3dc5f1d3437d3;hp=2d3d9ab6ab290b8bb666ff540cf97d8620dfed9a;hpb=dbf4f589876264819b2731e8a78304a40d3de27f;p=rust-lightning diff --git a/genbindings.sh b/genbindings.sh index 2d3d9ab6..bfeda6bc 100755 --- a/genbindings.sh +++ b/genbindings.sh @@ -6,7 +6,7 @@ set -x # Generate (and reasonably test) C bindings # First build the latest c-bindings-gen binary -cd c-bindings-gen && cargo build && cd .. +cd c-bindings-gen && cargo build --release && cd .. # Then wipe all the existing C bindings (because we're being run in the right directory) # note that we keep the few manually-generated files first: @@ -20,12 +20,15 @@ mv ./mod.rs lightning-c-bindings/src/c_types/ mv ./bitcoin lightning-c-bindings/src/ # Finally, run the c-bindings-gen binary, building fresh bindings. -SRC="$(pwd)/lightning/src" OUT="$(pwd)/lightning-c-bindings/src" OUT_TEMPL="$(pwd)/lightning-c-bindings/src/c_types/derived.rs" OUT_F="$(pwd)/lightning-c-bindings/include/rust_types.h" OUT_CPP="$(pwd)/lightning-c-bindings/include/lightningpp.hpp" -RUST_BACKTRACE=1 ./c-bindings-gen/target/debug/c-bindings-gen $SRC/ $OUT/ lightning $OUT_TEMPL $OUT_F $OUT_CPP + +cd lightning +RUSTC_BOOTSTRAP=1 cargo rustc --profile=check -- -Zunstable-options --pretty=expanded | + RUST_BACKTRACE=1 ../c-bindings-gen/target/release/c-bindings-gen $OUT/ lightning $OUT_TEMPL $OUT_F $OUT_CPP +cd .. # Now cd to lightning-c-bindings, build the generated bindings, and call cbindgen to build a C header file PATH="$PATH:~/.cargo/bin" @@ -41,18 +44,28 @@ 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: +CFLAGS="-Wall -Wno-nullability-completeness -pthread" + # Naively run the C demo app: -gcc -Wall -g -pthread demo.c target/debug/liblightning.a -ldl +gcc $CFLAGS -Wall -g -pthread demo.c target/debug/libldk.a -ldl ./a.out # And run the C++ demo app in valgrind to test memory model correctness and lack of leaks. -g++ -std=c++11 -Wall -g -pthread demo.cpp -Ltarget/debug/ -llightning -ldl +g++ $CFLAGS -std=c++11 -Wall -g -pthread demo.cpp -Ltarget/debug/ -lldk -ldl if [ -x "`which valgrind`" ]; then LD_LIBRARY_PATH=target/debug/ valgrind --error-exitcode=4 --memcheck:leak-check=full --show-leak-kinds=all ./a.out echo @@ -62,8 +75,8 @@ fi # Test a statically-linked C++ version, tracking the resulting binary size and runtime # across debug, LTO, and cross-language LTO builds (using the same compiler each time). -clang++ -std=c++11 -Wall -pthread demo.cpp target/debug/liblightning.a -ldl -./a.out >/dev/null +clang++ $CFLAGS -std=c++11 demo.cpp target/debug/libldk.a -ldl +strip ./a.out echo " C++ Bin size and runtime w/o optimization:" ls -lha a.out time ./a.out > /dev/null @@ -75,7 +88,7 @@ if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" ]; then if [ -x "$(which clang-$LLVM_V)" ]; then cargo +nightly clean cargo +nightly rustc -Zbuild-std --target x86_64-unknown-linux-gnu -v -- -Zsanitizer=memory -Zsanitizer-memory-track-origins -Cforce-frame-pointers=yes - mv target/x86_64-unknown-linux-gnu/debug/liblightning.* target/debug/ + mv target/x86_64-unknown-linux-gnu/debug/libldk.* target/debug/ # Sadly, std doesn't seem to compile into something that is memsan-safe as of Aug 2020, # so we'll always fail, not to mention we may be linking against git rustc LLVM which @@ -83,11 +96,11 @@ if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" ]; then set +e # First the C demo app... - clang-$LLVM_V -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -Wall -g -pthread demo.c target/debug/liblightning.a -ldl + clang-$LLVM_V $CFLAGS -fsanitize=memory -fsanitize-memory-track-origins -g demo.c target/debug/libldk.a -ldl ./a.out # ...then the C++ demo app - clang++-$LLVM_V -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -Wall -g -pthread demo.cpp target/debug/liblightning.a -ldl + clang++-$LLVM_V $CFLAGS -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -g demo.cpp target/debug/libldk.a -ldl ./a.out >/dev/null # restore exit-on-failure @@ -153,11 +166,11 @@ if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" -o "$HOST_PLATFORM" = " mv Cargo.toml.bk Cargo.toml # First the C demo app... - $CLANG -fsanitize=address -Wall -g -pthread demo.c target/debug/liblightning.a -ldl + $CLANG $CFLAGS -fsanitize=address -g demo.c target/debug/libldk.a -ldl ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out # ...then the C++ demo app - $CLANGPP -std=c++11 -fsanitize=address -Wall -g -pthread demo.cpp target/debug/liblightning.a -ldl + $CLANGPP $CFLAGS -std=c++11 -fsanitize=address -g demo.cpp target/debug/libldk.a -ldl ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out >/dev/null else echo "WARNING: Please install clang-$RUSTC_LLVM_V and clang++-$RUSTC_LLVM_V to build with address sanitizer" @@ -166,9 +179,13 @@ 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 rustc -v --release -- -C lto -clang++ -std=c++11 -Wall -flto -O2 -pthread demo.cpp target/release/liblightning.a -ldl +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 +strip ./a.out echo "C++ Bin size and runtime with only RL (LTO) optimized:" ls -lha a.out time ./a.out > /dev/null @@ -179,8 +196,9 @@ 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). - cargo rustc -v --release -- -C linker-plugin-lto -C lto -C link-arg=-fuse-ld=lld - $CLANGPP -Wall -std=c++11 -flto -fuse-ld=lld -O2 -pthread demo.cpp target/release/liblightning.a -ldl + CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C linker-plugin-lto -C lto -C link-arg=-fuse-ld=lld + $CLANGPP $CFLAGS -flto -fuse-ld=lld -O2 demo.cpp target/release/libldk.a -ldl + strip ./a.out echo "C++ Bin size and runtime with cross-language LTO:" ls -lha a.out time ./a.out > /dev/null