Merge pull request #19 from TheBlueMatt/2021-04-invoice-incl
[ldk-c-bindings] / genbindings.sh
index 67489a669ec49c7411788da9ce96fd2699b9ab3d..e0ff6b3f060af4259a92c2545f6ced32d1a6b4d9 100755 (executable)
@@ -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"
 
@@ -43,35 +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 > /tmp/lightning-crate-source.txt
-
 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 -E -i '' 's/#!\[crate_name = "(.*)"\]/pub mod \1 {/' /tmp/lightning-crate-source.txt
-else
-       sed -E -i 's/#!\[crate_name = "(.*)"\]/pub mod \1 {/' /tmp/lightning-crate-source.txt
-fi
-echo "}" >> /tmp/lightning-crate-source.txt
 
-cat /tmp/lightning-crate-source.txt | RUST_BACKTRACE=1 "$BIN" "$OUT/" lightning "$OUT_TEMPL" "$OUT_F" "$OUT_CPP"
-popd
+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
+}
 
-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 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
@@ -229,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 &&
@@ -253,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