From 3dd72e5832137f78c296bfc2bff9ede3655c5a9c Mon Sep 17 00:00:00 2001
From: Matt Corallo <git@bluematt.me>
Date: Fri, 8 Jan 2021 21:55:47 -0500
Subject: [PATCH] [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] https://github.com/rust-lang/rust/pull/79998
---
 genbindings.sh                           | 7 +++++++
 lightning-c-bindings/Cargo.toml          | 7 +++++++
 lightning-c-bindings/include/lightning.h | 2 +-
 3 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/genbindings.sh b/genbindings.sh
index 1ca2f6e8e88..75d0ab5e6c2 100755
--- a/genbindings.sh
+++ b/genbindings.sh
@@ -48,6 +48,10 @@ else
 	sed -i 's/typedef LDKnative.*Import.*LDKnative.*;//g' include/lightning.h
 fi
 
+# 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
+
 # Finally, sanity-check the generated C and C++ bindings with demo apps:
 
 CFLAGS="-Wall -Wno-nullability-completeness -pthread"
@@ -171,6 +175,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
diff --git a/lightning-c-bindings/Cargo.toml b/lightning-c-bindings/Cargo.toml
index 9f7210386b6..76047314ea1 100644
--- a/lightning-c-bindings/Cargo.toml
+++ b/lightning-c-bindings/Cargo.toml
@@ -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
diff --git a/lightning-c-bindings/include/lightning.h b/lightning-c-bindings/include/lightning.h
index 8f6679e3c6f..0f255adb3d2 100644
--- a/lightning-c-bindings/include/lightning.h
+++ b/lightning-c-bindings/include/lightning.h
@@ -7,7 +7,7 @@
 #include <stdarg.h>
 #include <stdbool.h>
 #include <stdint.h>
-#include <stdlib.h>
+
 
 /**
  * An error when accessing the chain via [`Access`].