6 # Generate (and reasonably test) C bindings
8 # First build the latest c-bindings-gen binary
9 cd c-bindings-gen && cargo build --release && cd ..
11 # Then wipe all the existing C bindings (because we're being run in the right directory)
12 # note that we keep the few manually-generated files first:
13 mv lightning-c-bindings/src/c_types/mod.rs ./
14 mv lightning-c-bindings/src/bitcoin ./
16 rm -rf lightning-c-bindings/src
18 mkdir -p lightning-c-bindings/src/c_types/
19 mv ./mod.rs lightning-c-bindings/src/c_types/
20 mv ./bitcoin lightning-c-bindings/src/
22 # Finally, run the c-bindings-gen binary, building fresh bindings.
23 OUT="$(pwd)/lightning-c-bindings/src"
24 OUT_TEMPL="$(pwd)/lightning-c-bindings/src/c_types/derived.rs"
25 OUT_F="$(pwd)/lightning-c-bindings/include/rust_types.h"
26 OUT_CPP="$(pwd)/lightning-c-bindings/include/lightningpp.hpp"
29 RUSTC_BOOTSTRAP=1 cargo rustc --profile=check -- -Zunstable-options --pretty=expanded |
30 RUST_BACKTRACE=1 ../c-bindings-gen/target/release/c-bindings-gen $OUT/ lightning $OUT_TEMPL $OUT_F $OUT_CPP
33 # Now cd to lightning-c-bindings, build the generated bindings, and call cbindgen to build a C header file
34 PATH="$PATH:~/.cargo/bin"
35 cd lightning-c-bindings
37 cbindgen -v --config cbindgen.toml -o include/lightning.h >/dev/null 2>&1
39 HOST_PLATFORM="$(rustc --version --verbose | grep "host:")"
41 # cbindgen is relatively braindead when exporting typedefs -
42 # it happily exports all our typedefs for private types, even with the
43 # generics we specified in C mode! So we drop all those types manually here.
44 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
45 # OSX sed is for some reason not compatible with GNU sed
46 sed -i '' 's/typedef LDKnative.*Import.*LDKnative.*;//g' include/lightning.h
48 # stdlib.h doesn't exist in clang's wasm sysroot, and cbindgen
49 # doesn't actually use it anyway, so drop the import.
50 sed -i '' 's/#include <stdlib.h>//g' include/lightning.h
52 sed -i 's/typedef LDKnative.*Import.*LDKnative.*;//g' include/lightning.h
54 # stdlib.h doesn't exist in clang's wasm sysroot, and cbindgen
55 # doesn't actually use it anyway, so drop the import.
56 sed -i 's/#include <stdlib.h>//g' include/lightning.h
59 # Finally, sanity-check the generated C and C++ bindings with demo apps:
61 CFLAGS="-Wall -Wno-nullability-completeness -pthread"
63 # Naively run the C demo app:
64 gcc $CFLAGS -Wall -g -pthread demo.c target/debug/libldk.a -ldl
67 # And run the C++ demo app in valgrind to test memory model correctness and lack of leaks.
68 g++ $CFLAGS -std=c++11 -Wall -g -pthread demo.cpp -Ltarget/debug/ -lldk -ldl
69 if [ -x "`which valgrind`" ]; then
70 LD_LIBRARY_PATH=target/debug/ valgrind --error-exitcode=4 --memcheck:leak-check=full --show-leak-kinds=all ./a.out
73 echo "WARNING: Please install valgrind for more testing"
76 # Test a statically-linked C++ version, tracking the resulting binary size and runtime
77 # across debug, LTO, and cross-language LTO builds (using the same compiler each time).
78 clang++ $CFLAGS -std=c++11 demo.cpp target/debug/libldk.a -ldl
80 echo " C++ Bin size and runtime w/o optimization:"
82 time ./a.out > /dev/null
84 # Then, check with memory sanitizer, if we're on Linux and have rustc nightly
85 if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" ]; then
86 if cargo +nightly --version >/dev/null 2>&1; then
87 LLVM_V=$(rustc +nightly --version --verbose | grep "LLVM version" | awk '{ print substr($3, 0, 2); }')
88 if [ -x "$(which clang-$LLVM_V)" ]; then
90 cargo +nightly rustc -Zbuild-std --target x86_64-unknown-linux-gnu -v -- -Zsanitizer=memory -Zsanitizer-memory-track-origins -Cforce-frame-pointers=yes
91 mv target/x86_64-unknown-linux-gnu/debug/libldk.* target/debug/
93 # Sadly, std doesn't seem to compile into something that is memsan-safe as of Aug 2020,
94 # so we'll always fail, not to mention we may be linking against git rustc LLVM which
95 # may differ from clang-llvm, so just allow everything here to fail.
98 # First the C demo app...
99 clang-$LLVM_V $CFLAGS -fsanitize=memory -fsanitize-memory-track-origins -g demo.c target/debug/libldk.a -ldl
102 # ...then the C++ demo app
103 clang++-$LLVM_V $CFLAGS -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -g demo.cpp target/debug/libldk.a -ldl
106 # restore exit-on-failure
109 echo "WARNING: Can't use memory sanitizer without clang-$LLVM_V"
112 echo "WARNING: Can't use memory sanitizer without rustc nightly"
115 echo "WARNING: Can't use memory sanitizer on non-Linux, non-x86 platforms"
118 RUSTC_LLVM_V=$(rustc --version --verbose | grep "LLVM version" | awk '{ print substr($3, 0, 2); }' | tr -d '.')
120 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
121 # Apple is special, as always, and decided that they must ensure that there is no way to identify
122 # the LLVM version used. Why? Just to make your life hard.
123 # This list is taken from https://en.wikipedia.org/wiki/Xcode
124 APPLE_CLANG_V=$(clang --version | head -n1 | awk '{ print $4 }')
125 if [ "$APPLE_CLANG_V" = "10.0.0" ]; then
127 elif [ "$APPLE_CLANG_V" = "10.0.1" ]; then
129 elif [ "$APPLE_CLANG_V" = "11.0.0" ]; then
131 elif [ "$APPLE_CLANG_V" = "11.0.3" ]; then
133 elif [ "$APPLE_CLANG_V" = "12.0.0" ]; then
136 echo "WARNING: Unable to identify Apple clang LLVM version"
140 CLANG_LLVM_V=$(clang --version | head -n1 | awk '{ print substr($4, 0, 2); }' | tr -d '.')
143 if [ "$CLANG_LLVM_V" = "$RUSTC_LLVM_V" ]; then
146 elif [ "$(which clang-$RUSTC_LLVM_V)" != "" ]; then
147 CLANG="$(which clang-$RUSTC_LLVM_V)"
148 CLANGPP="$(which clang++-$RUSTC_LLVM_V)"
151 if [ "$CLANG" != "" -a "$CLANGPP" = "" ]; then
152 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"
153 echo "You should create a symlink called clang++-$RUSTC_LLVM_V pointing to $CLANG in $(dirname $CLANG)"
156 # Finally, if we're on OSX or on Linux, build the final debug binary with address sanitizer (and leave it there)
157 if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" -o "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
158 if [ "$CLANGPP" != "" ]; then
159 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
160 # OSX sed is for some reason not compatible with GNU sed
161 sed -i .bk 's/,"cdylib"]/]/g' Cargo.toml
163 sed -i.bk 's/,"cdylib"]/]/g' Cargo.toml
165 RUSTC_BOOTSTRAP=1 cargo rustc -v -- -Zsanitizer=address -Cforce-frame-pointers=yes || ( mv Cargo.toml.bk Cargo.toml; exit 1)
166 mv Cargo.toml.bk Cargo.toml
168 # First the C demo app...
169 $CLANG $CFLAGS -fsanitize=address -g demo.c target/debug/libldk.a -ldl
170 ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out
172 # ...then the C++ demo app
173 $CLANGPP $CFLAGS -std=c++11 -fsanitize=address -g demo.cpp target/debug/libldk.a -ldl
174 ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out >/dev/null
176 echo "WARNING: Please install clang-$RUSTC_LLVM_V and clang++-$RUSTC_LLVM_V to build with address sanitizer"
179 echo "WARNING: Can't use address sanitizer on non-Linux, non-OSX non-x86 platforms"
182 cargo rustc -v --target=wasm32-wasi -- -C embed-bitcode=yes || echo "WARNING: Failed to generate WASM LLVM-bitcode-embedded library"
183 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"
185 # Now build with LTO on on both C++ and rust, but without cross-language LTO:
186 CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C lto
187 clang++ $CFLAGS -std=c++11 -flto -O2 demo.cpp target/release/libldk.a -ldl
189 echo "C++ Bin size and runtime with only RL (LTO) optimized:"
191 time ./a.out > /dev/null
193 if [ "$HOST_PLATFORM" != "host: x86_64-apple-darwin" -a "$CLANGPP" != "" ]; then
194 # Finally, test cross-language LTO. Note that this will fail if rustc and clang++
195 # build against different versions of LLVM (eg when rustc is installed via rustup
196 # or Ubuntu packages). This should work fine on Distros which do more involved
197 # packaging than simply shipping the rustup binaries (eg Debian should Just Work
199 CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C linker-plugin-lto -C lto -C link-arg=-fuse-ld=lld
200 $CLANGPP $CFLAGS -flto -fuse-ld=lld -O2 demo.cpp target/release/libldk.a -ldl
202 echo "C++ Bin size and runtime with cross-language LTO:"
204 time ./a.out > /dev/null
206 echo "WARNING: Building with cross-language LTO is not avilable on OSX or without clang-$RUSTC_LLVM_V"