X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-c-bindings;a=blobdiff_plain;f=genbindings.sh;h=e0ff6b3f060af4259a92c2545f6ced32d1a6b4d9;hp=0d03950f106dcff10f9f1d8ffcb889e0dd4d07c0;hb=1926a7a71ae0f37ebd6562996769334e0af0cf1b;hpb=b25e291110094bb9b59bc05f3f233c1b5d37b6e6 diff --git a/genbindings.sh b/genbindings.sh index 0d03950..e0ff6b3 100755 --- a/genbindings.sh +++ b/genbindings.sh @@ -9,14 +9,9 @@ if [ ! -d "$1/lightning" -o "$2" != "true" -a "$2" != "false" ]; then exit 1 fi -if [ "$2" = "true" ]; then - FEATURES_ARGS='--features=allow_wallclock_use' - FEATURES='"allow_wallclock_use"' -fi - # On reasonable systems, we can use realpath here, but OSX is a diva with 20-year-old software. ORIG_PWD="$(pwd)" -cd "$1/lightning" +cd "$1" LIGHTNING_PATH="$(pwd)" cd "$ORIG_PWD" @@ -32,7 +27,7 @@ mv lightning-c-bindings/src/bitcoin ./ rm -rf lightning-c-bindings/src -mkdir -p lightning-c-bindings/src/c_types/ +mkdir -p lightning-c-bindings/src/{c_types,lightning} mv ./mod.rs lightning-c-bindings/src/c_types/ mv ./bitcoin lightning-c-bindings/src/ @@ -43,26 +38,65 @@ OUT_F="$(pwd)/lightning-c-bindings/include/rust_types.h" OUT_CPP="$(pwd)/lightning-c-bindings/include/lightningpp.hpp" BIN="$(pwd)/c-bindings-gen/target/release/c-bindings-gen" -pushd "$LIGHTNING_PATH" -RUSTC_BOOTSTRAP=1 cargo rustc $FEATURES_ARGS --profile=check -- -Zunstable-options --pretty=expanded | - RUST_BACKTRACE=1 "$BIN" "$OUT/" lightning "$OUT_TEMPL" "$OUT_F" "$OUT_CPP" -popd - 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|lightning = { .*|lightning = { path = "'"$LIGHTNING_PATH"'", features = ['"$FEATURES"'] }|' lightning-c-bindings/Cargo.toml + +function add_crate() { + pushd "$LIGHTNING_PATH/$1" + RUSTC_BOOTSTRAP=1 cargo rustc --profile=check $3 -- -Zunstable-options --pretty=expanded > /tmp/$1-crate-source.txt + popd + if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then + sed -i".original" "1i\\ +pub mod $2 { +" /tmp/$1-crate-source.txt + else + sed -i "1ipub mod $2 {\n" /tmp/$1-crate-source.txt + fi + echo "}" >> /tmp/$1-crate-source.txt + cat /tmp/$1-crate-source.txt >> /tmp/crate-source.txt + rm /tmp/$1-crate-source.txt + if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then + # OSX sed is for some reason not compatible with GNU sed + sed -E -i '' 's|#?'$1' = \{ .*|'$1' = \{ path = "'"$LIGHTNING_PATH"'/'$1'" '"$4"' }|' lightning-c-bindings/Cargo.toml + else + sed -E -i 's|#?'$1' = \{ .*|'$1' = \{ path = "'"$LIGHTNING_PATH"'/'$1'" '"$4"' }|' lightning-c-bindings/Cargo.toml + fi +} + +function drop_crate() { + if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then + # OSX sed is for some reason not compatible with GNU sed + sed -E -i '' 's|'$1' = \{ (.*)|#'$1' = \{ \1|' lightning-c-bindings/Cargo.toml + else + sed -E -i 's|'$1' = \{ (.*)|#'$1' = \{ \1|' lightning-c-bindings/Cargo.toml + fi +} + +echo > /tmp/crate-source.txt +if [ "$2" = "true" ]; then + add_crate lightning lightning --features=allow_wallclock_use ', features = ["allow_wallclock_use"]' + add_crate "lightning-persister" "lightning_persister" else - sed -i 's|lightning = { .*|lightning = { path = "'"$LIGHTNING_PATH"'", features = ['"$FEATURES"'] }|' lightning-c-bindings/Cargo.toml + add_crate lightning lightning + drop_crate "lightning-persister" fi +add_crate "lightning-invoice" "lightning_invoice" + +cat /tmp/crate-source.txt | RUST_BACKTRACE=1 "$BIN" "$OUT/" "$OUT_TEMPL" "$OUT_F" "$OUT_CPP" # Set path to include our rustc wrapper as well as cbindgen PATH="$(pwd)/deterministic-build-wrappers:$PATH:~/.cargo/bin" # Now cd to lightning-c-bindings, build the generated bindings, and call cbindgen to build a C header file cd lightning-c-bindings + # Remap paths so that our builds are deterministic export RUSTFLAGS="--remap-path-prefix $LIGHTNING_PATH=rust-lightning --remap-path-prefix $(pwd)=ldk-c-bindings --remap-path-prefix $HOME/.cargo= -C target-cpu=generic" + +# If the C compiler supports it, also set -ffile-prefix-map +echo "int main() {}" > genbindings_path_map_test_file.c +clang -o /dev/null -ffile-prefix-map=$HOME/.cargo= genbindings_path_map_test_file.c > /dev/null 2>&1 && +# Now that we've done our last non-LTO build, turn on LTO in CFLAGS as well export CFLAGS="-ffile-prefix-map=$HOME/.cargo=" +rm genbindings_path_map_test_file.c cargo build cbindgen -v --config cbindgen.toml -o include/lightning.h >/dev/null 2>&1 @@ -220,7 +254,7 @@ if [ "$HOST_PLATFORM" != "host: x86_64-apple-darwin" -a "$CLANGPP" != "" ]; then export CFLAGS_wasm32_wasi="-target wasm32" fi -if [ "$(rustc --print target-list | grep wasm32-wasi)" != "" ]; then +if [ "$2" = "false" -a "$(rustc --print target-list | grep wasm32-wasi)" != "" ]; then # Test to see if clang supports wasm32 as a target (which is needed to build rust-secp256k1) echo "int main() {}" > genbindings_wasm_test_file.c clang -nostdlib -o /dev/null --target=wasm32-wasi -Wl,--no-entry genbindings_wasm_test_file.c > /dev/null 2>&1 && @@ -244,7 +278,11 @@ 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). - export CFLAGS="$CFLAGS -flto" + # The cc-rs crate tries to force -fdata-sections and -ffunction-sections on, which + # breaks -fembed-bitcode, so we turn off cc-rs' default flags and specify exactly + # what we want here. + export CFLAGS="$CFLAGS -O3 -fPIC -fembed-bitcode" + export CRATE_CC_NO_DEFAULTS=true # Rust doesn't recognize CFLAGS changes, so we need to clean build artifacts cargo clean --release CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C linker-plugin-lto -C lto -C link-arg=-fuse-ld=lld