6 if [ ! -d "$1/lightning" -o "$2" != "true" -a "$2" != "false" ]; then
7 echo "USAGE: $0 path-to-rust-lightning allow-std"
8 echo "allow-std must be either 'true' or 'false' to indicate if we should be built relying on time and pthread support"
12 if [ "$2" = "true" ]; then
13 FEATURES_ARGS='--features=allow_wallclock_use'
14 FEATURES='"allow_wallclock_use"'
17 # On reasonable systems, we can use realpath here, but OSX is a diva with 20-year-old software.
20 LIGHTNING_PATH="$(pwd)"
23 # Generate (and reasonably test) C bindings
25 # First build the latest c-bindings-gen binary
26 cd c-bindings-gen && cargo build --release && cd ..
28 # Then wipe all the existing C bindings (because we're being run in the right directory)
29 # note that we keep the few manually-generated files first:
30 mv lightning-c-bindings/src/c_types/mod.rs ./
31 mv lightning-c-bindings/src/bitcoin ./
33 rm -rf lightning-c-bindings/src
35 mkdir -p lightning-c-bindings/src/{c_types,lightning}
36 mv ./mod.rs lightning-c-bindings/src/c_types/
37 mv ./bitcoin lightning-c-bindings/src/
39 # Finally, run the c-bindings-gen binary, building fresh bindings.
40 OUT="$(pwd)/lightning-c-bindings/src"
41 OUT_TEMPL="$(pwd)/lightning-c-bindings/src/c_types/derived.rs"
42 OUT_F="$(pwd)/lightning-c-bindings/include/rust_types.h"
43 OUT_CPP="$(pwd)/lightning-c-bindings/include/lightningpp.hpp"
44 BIN="$(pwd)/c-bindings-gen/target/release/c-bindings-gen"
46 pushd "$LIGHTNING_PATH/lightning"
47 RUSTC_BOOTSTRAP=1 cargo rustc $FEATURES_ARGS --profile=check -- -Zunstable-options --pretty=expanded > /tmp/lightning-crate-source.txt
50 HOST_PLATFORM="$(rustc --version --verbose | grep "host:")"
51 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
52 # OSX sed is for some reason not compatible with GNU sed
53 sed -E -i '' 's/#!\[crate_name = "(.*)"\]/pub mod \1 {/' /tmp/lightning-crate-source.txt
55 sed -E -i 's/#!\[crate_name = "(.*)"\]/pub mod \1 {/' /tmp/lightning-crate-source.txt
57 echo "}" >> /tmp/lightning-crate-source.txt
59 if [ "$2" = "true" ]; then
60 pushd "$LIGHTNING_PATH/lightning-persister"
61 RUSTC_BOOTSTRAP=1 cargo rustc --profile=check -- -Zunstable-options --pretty=expanded > /tmp/lightning-persist-crate-source.txt
63 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
64 sed -i".original" '1i\
65 pub mod lightning_persister {
66 ' /tmp/lightning-persist-crate-source.txt
68 sed -i '1ipub mod lightning_persister {\n' /tmp/lightning-persist-crate-source.txt
70 echo "}" >> /tmp/lightning-persist-crate-source.txt
71 cat /tmp/lightning-persist-crate-source.txt >> /tmp/lightning-crate-source.txt
72 rm /tmp/lightning-persist-crate-source.txt
75 cat /tmp/lightning-crate-source.txt | RUST_BACKTRACE=1 "$BIN" "$OUT/" "$OUT_TEMPL" "$OUT_F" "$OUT_CPP"
77 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
78 # OSX sed is for some reason not compatible with GNU sed
79 sed -i '' 's|lightning = { .*|lightning = { path = "'"$LIGHTNING_PATH/lightning"'", features = ['"$FEATURES"'] }|' lightning-c-bindings/Cargo.toml
80 sed -E -i '' 's|lightning-(.*) = \{ .*|lightning-\1 = \{ path = "'"$LIGHTNING_PATH"'/lightning-\1" }|' lightning-c-bindings/Cargo.toml
82 sed -i 's|lightning = { .*|lightning = { path = "'"$LIGHTNING_PATH/lightning"'", features = ['"$FEATURES"'] }|' lightning-c-bindings/Cargo.toml
83 sed -E -i 's|lightning-(.*) = \{ .*|lightning-\1 = \{ path = "'"$LIGHTNING_PATH"'/lightning-\1" }|' lightning-c-bindings/Cargo.toml
86 # Set path to include our rustc wrapper as well as cbindgen
87 PATH="$(pwd)/deterministic-build-wrappers:$PATH:~/.cargo/bin"
88 # Now cd to lightning-c-bindings, build the generated bindings, and call cbindgen to build a C header file
89 cd lightning-c-bindings
90 # Remap paths so that our builds are deterministic
91 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"
92 export CFLAGS="-ffile-prefix-map=$HOME/.cargo="
95 cbindgen -v --config cbindgen.toml -o include/lightning.h >/dev/null 2>&1
97 # cbindgen is relatively braindead when exporting typedefs -
98 # it happily exports all our typedefs for private types, even with the
99 # generics we specified in C mode! So we drop all those types manually here.
100 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
101 # OSX sed is for some reason not compatible with GNU sed
102 sed -i '' 's/typedef LDKnative.*Import.*LDKnative.*;//g' include/lightning.h
104 # stdlib.h doesn't exist in clang's wasm sysroot, and cbindgen
105 # doesn't actually use it anyway, so drop the import.
106 sed -i '' 's/#include <stdlib.h>//g' include/lightning.h
108 sed -i 's/typedef LDKnative.*Import.*LDKnative.*;//g' include/lightning.h
110 # stdlib.h doesn't exist in clang's wasm sysroot, and cbindgen
111 # doesn't actually use it anyway, so drop the import.
112 sed -i 's/#include <stdlib.h>//g' include/lightning.h
115 # Finally, sanity-check the generated C and C++ bindings with demo apps:
117 LOCAL_CFLAGS="-Wall -Wno-nullability-completeness -pthread"
119 # Naively run the C demo app:
120 gcc $LOCAL_CFLAGS -Wall -g -pthread demo.c target/debug/libldk.a -ldl
123 # And run the C++ demo app in valgrind to test memory model correctness and lack of leaks.
124 g++ $LOCAL_CFLAGS -std=c++11 -Wall -g -pthread demo.cpp -Ltarget/debug/ -lldk -ldl
125 if [ -x "`which valgrind`" ]; then
126 LD_LIBRARY_PATH=target/debug/ valgrind --error-exitcode=4 --memcheck:leak-check=full --show-leak-kinds=all ./a.out
129 echo "WARNING: Please install valgrind for more testing"
132 # Test a statically-linked C++ version, tracking the resulting binary size and runtime
133 # across debug, LTO, and cross-language LTO builds (using the same compiler each time).
134 clang++ $LOCAL_CFLAGS -std=c++11 demo.cpp target/debug/libldk.a -ldl
136 echo " C++ Bin size and runtime w/o optimization:"
138 time ./a.out > /dev/null
140 # Then, check with memory sanitizer, if we're on Linux and have rustc nightly
141 if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" ]; then
142 if cargo +nightly --version >/dev/null 2>&1; then
143 LLVM_V=$(rustc +nightly --version --verbose | grep "LLVM version" | awk '{ print substr($3, 0, 2); }')
144 if [ -x "$(which clang-$LLVM_V)" ]; then
146 cargo +nightly rustc -Zbuild-std --target x86_64-unknown-linux-gnu -v -- -Zsanitizer=memory -Zsanitizer-memory-track-origins -Cforce-frame-pointers=yes
147 mv target/x86_64-unknown-linux-gnu/debug/libldk.* target/debug/
149 # Sadly, std doesn't seem to compile into something that is memsan-safe as of Aug 2020,
150 # so we'll always fail, not to mention we may be linking against git rustc LLVM which
151 # may differ from clang-llvm, so just allow everything here to fail.
154 # First the C demo app...
155 clang-$LLVM_V $LOCAL_CFLAGS -fsanitize=memory -fsanitize-memory-track-origins -g demo.c target/debug/libldk.a -ldl
158 # ...then the C++ demo app
159 clang++-$LLVM_V $LOCAL_CFLAGS -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -g demo.cpp target/debug/libldk.a -ldl
162 # restore exit-on-failure
165 echo "WARNING: Can't use memory sanitizer without clang-$LLVM_V"
168 echo "WARNING: Can't use memory sanitizer without rustc nightly"
171 echo "WARNING: Can't use memory sanitizer on non-Linux, non-x86 platforms"
174 RUSTC_LLVM_V=$(rustc --version --verbose | grep "LLVM version" | awk '{ print substr($3, 0, 2); }' | tr -d '.')
176 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
177 # Apple is special, as always, and decided that they must ensure that there is no way to identify
178 # the LLVM version used. Why? Just to make your life hard.
179 # This list is taken from https://en.wikipedia.org/wiki/Xcode
180 APPLE_CLANG_V=$(clang --version | head -n1 | awk '{ print $4 }')
181 if [ "$APPLE_CLANG_V" = "10.0.0" ]; then
183 elif [ "$APPLE_CLANG_V" = "10.0.1" ]; then
185 elif [ "$APPLE_CLANG_V" = "11.0.0" ]; then
187 elif [ "$APPLE_CLANG_V" = "11.0.3" ]; then
189 elif [ "$APPLE_CLANG_V" = "12.0.0" ]; then
192 echo "WARNING: Unable to identify Apple clang LLVM version"
196 CLANG_LLVM_V=$(clang --version | head -n1 | awk '{ print substr($4, 0, 2); }' | tr -d '.')
199 if [ "$CLANG_LLVM_V" = "$RUSTC_LLVM_V" ]; then
202 elif [ "$(which clang-$RUSTC_LLVM_V)" != "" ]; then
203 CLANG="$(which clang-$RUSTC_LLVM_V)"
204 CLANGPP="$(which clang++-$RUSTC_LLVM_V)"
207 if [ "$CLANG" != "" -a "$CLANGPP" = "" ]; then
208 echo "WARNING: It appears you have a clang-$RUSTC_LLVM_V but not clang++-$RUSTC_LLVM_V. This is common, but leaves us unable to compile C++ with LLVM $RUSTC_LLVM_V"
209 echo "You should create a symlink called clang++-$RUSTC_LLVM_V pointing to $CLANG in $(dirname $CLANG)"
212 # Finally, if we're on OSX or on Linux, build the final debug binary with address sanitizer (and leave it there)
213 if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" -o "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
214 if [ "$CLANGPP" != "" ]; then
215 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
216 # OSX sed is for some reason not compatible with GNU sed
217 sed -i .bk 's/,"cdylib"]/]/g' Cargo.toml
219 sed -i.bk 's/,"cdylib"]/]/g' Cargo.toml
221 RUSTC_BOOTSTRAP=1 cargo rustc -v -- -Zsanitizer=address -Cforce-frame-pointers=yes || ( mv Cargo.toml.bk Cargo.toml; exit 1)
222 mv Cargo.toml.bk Cargo.toml
224 # First the C demo app...
225 $CLANG $LOCAL_CFLAGS -fsanitize=address -g demo.c target/debug/libldk.a -ldl
226 ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out
228 # ...then the C++ demo app
229 $CLANGPP $LOCAL_CFLAGS -std=c++11 -fsanitize=address -g demo.cpp target/debug/libldk.a -ldl
230 ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out >/dev/null
232 echo "WARNING: Please install clang-$RUSTC_LLVM_V and clang++-$RUSTC_LLVM_V to build with address sanitizer"
235 echo "WARNING: Can't use address sanitizer on non-Linux, non-OSX non-x86 platforms"
238 # Now build with LTO on on both C++ and rust, but without cross-language LTO:
239 # Clear stale release build artifacts from previous runs
240 cargo clean --release
241 CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C lto
242 clang++ $LOCAL_CFLAGS -std=c++11 -flto -O2 demo.cpp target/release/libldk.a -ldl
244 if [ "$HOST_PLATFORM" != "host: x86_64-apple-darwin" -a "$CLANGPP" != "" ]; then
245 # If we can use cross-language LTO, use it for building C dependencies (i.e. libsecp256k1) as well
247 export CFLAGS_wasm32_wasi="-target wasm32"
250 if [ "$2" = "false" -a "$(rustc --print target-list | grep wasm32-wasi)" != "" ]; then
251 # Test to see if clang supports wasm32 as a target (which is needed to build rust-secp256k1)
252 echo "int main() {}" > genbindings_wasm_test_file.c
253 clang -nostdlib -o /dev/null --target=wasm32-wasi -Wl,--no-entry genbindings_wasm_test_file.c > /dev/null 2>&1 &&
254 # And if it does, build a WASM binary without capturing errors
255 cargo rustc -v --target=wasm32-wasi -- -C embed-bitcode=yes &&
256 # Now that we've done our last non-LTO build, turn on LTO in CFLAGS as well
257 export CFLAGS="$CFLAGS -flto" &&
258 CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release --target=wasm32-wasi -- -C opt-level=s -C linker-plugin-lto -C lto ||
259 echo "Cannot build WASM lib as clang does not seem to support the wasm32-wasi target"
260 rm genbindings_wasm_test_file.c
264 echo "C++ Bin size and runtime with only RL (LTO) optimized:"
266 time ./a.out > /dev/null
268 if [ "$HOST_PLATFORM" != "host: x86_64-apple-darwin" -a "$CLANGPP" != "" ]; then
269 # Finally, test cross-language LTO. Note that this will fail if rustc and clang++
270 # build against different versions of LLVM (eg when rustc is installed via rustup
271 # or Ubuntu packages). This should work fine on Distros which do more involved
272 # packaging than simply shipping the rustup binaries (eg Debian should Just Work
274 # The cc-rs crate tries to force -fdata-sections and -ffunction-sections on, which
275 # breaks -fembed-bitcode, so we turn off cc-rs' default flags and specify exactly
277 export CFLAGS="$CFLAGS -O3 -fPIC -fembed-bitcode"
278 export CRATE_CC_NO_DEFAULTS=true
279 # Rust doesn't recognize CFLAGS changes, so we need to clean build artifacts
280 cargo clean --release
281 CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C linker-plugin-lto -C lto -C link-arg=-fuse-ld=lld
282 $CLANGPP $LOCAL_CFLAGS -flto -fuse-ld=lld -O2 demo.cpp target/release/libldk.a -ldl
284 echo "C++ Bin size and runtime with cross-language LTO:"
286 time ./a.out > /dev/null
288 echo "WARNING: Building with cross-language LTO is not avilable on OSX or without clang-$RUSTC_LLVM_V"