Update genbindings.sh and Cargo.toml for out-of-tree builds
[ldk-c-bindings] / genbindings.sh
1 #!/usr/bin/env bash
2
3 set -e
4 set -x
5
6 if [ ! -d "$1/lightning" ]; then
7         echo "USAGE: $0 path-to-rust-lightning"
8         exit 1
9 fi
10 LIGHTNING_PATH="$(realpath "$1/lightning")"
11
12 # Generate (and reasonably test) C bindings
13
14 # First build the latest c-bindings-gen binary
15 cd c-bindings-gen && cargo build --release && cd ..
16
17 # Then wipe all the existing C bindings (because we're being run in the right directory)
18 # note that we keep the few manually-generated files first:
19 mv lightning-c-bindings/src/c_types/mod.rs ./
20 mv lightning-c-bindings/src/bitcoin ./
21
22 rm -rf lightning-c-bindings/src
23
24 mkdir -p lightning-c-bindings/src/c_types/
25 mv ./mod.rs lightning-c-bindings/src/c_types/
26 mv ./bitcoin lightning-c-bindings/src/
27
28 # Finally, run the c-bindings-gen binary, building fresh bindings.
29 OUT="$(pwd)/lightning-c-bindings/src"
30 OUT_TEMPL="$(pwd)/lightning-c-bindings/src/c_types/derived.rs"
31 OUT_F="$(pwd)/lightning-c-bindings/include/rust_types.h"
32 OUT_CPP="$(pwd)/lightning-c-bindings/include/lightningpp.hpp"
33 BIN="$(pwd)/c-bindings-gen/target/release/c-bindings-gen"
34
35 pushd "$LIGHTNING_PATH"
36 RUSTC_BOOTSTRAP=1 cargo rustc --profile=check -- -Zunstable-options --pretty=expanded |
37         RUST_BACKTRACE=1 "$BIN" "$OUT/" lightning "$OUT_TEMPL" "$OUT_F" "$OUT_CPP"
38 popd
39
40 HOST_PLATFORM="$(rustc --version --verbose | grep "host:")"
41 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
42         # OSX sed is for some reason not compatible with GNU sed
43         sed -i '' 's|lightning = { .*|lightning = { path = "'"$LIGHTNING_PATH"'" }|' lightning-c-bindings/Cargo.toml
44 else
45         sed -i 's|lightning = { .*|lightning = { path = "'"$LIGHTNING_PATH"'" }|' lightning-c-bindings/Cargo.toml
46 fi
47
48 # Now cd to lightning-c-bindings, build the generated bindings, and call cbindgen to build a C header file
49 PATH="$PATH:~/.cargo/bin"
50 cd lightning-c-bindings
51 cargo build
52 cbindgen -v --config cbindgen.toml -o include/lightning.h >/dev/null 2>&1
53
54 # cbindgen is relatively braindead when exporting typedefs -
55 # it happily exports all our typedefs for private types, even with the
56 # generics we specified in C mode! So we drop all those types manually here.
57 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
58         # OSX sed is for some reason not compatible with GNU sed
59         sed -i '' 's/typedef LDKnative.*Import.*LDKnative.*;//g' include/lightning.h
60
61         # stdlib.h doesn't exist in clang's wasm sysroot, and cbindgen
62         # doesn't actually use it anyway, so drop the import.
63         sed -i '' 's/#include <stdlib.h>//g' include/lightning.h
64 else
65         sed -i 's/typedef LDKnative.*Import.*LDKnative.*;//g' include/lightning.h
66
67         # stdlib.h doesn't exist in clang's wasm sysroot, and cbindgen
68         # doesn't actually use it anyway, so drop the import.
69         sed -i 's/#include <stdlib.h>//g' include/lightning.h
70 fi
71
72 # Finally, sanity-check the generated C and C++ bindings with demo apps:
73
74 CFLAGS="-Wall -Wno-nullability-completeness -pthread"
75
76 # Naively run the C demo app:
77 gcc $CFLAGS -Wall -g -pthread demo.c target/debug/libldk.a -ldl
78 ./a.out
79
80 # And run the C++ demo app in valgrind to test memory model correctness and lack of leaks.
81 g++ $CFLAGS -std=c++11 -Wall -g -pthread demo.cpp -Ltarget/debug/ -lldk -ldl
82 if [ -x "`which valgrind`" ]; then
83         LD_LIBRARY_PATH=target/debug/ valgrind --error-exitcode=4 --memcheck:leak-check=full --show-leak-kinds=all ./a.out
84         echo
85 else
86         echo "WARNING: Please install valgrind for more testing"
87 fi
88
89 # Test a statically-linked C++ version, tracking the resulting binary size and runtime
90 # across debug, LTO, and cross-language LTO builds (using the same compiler each time).
91 clang++ $CFLAGS -std=c++11 demo.cpp target/debug/libldk.a -ldl
92 strip ./a.out
93 echo " C++ Bin size and runtime w/o optimization:"
94 ls -lha a.out
95 time ./a.out > /dev/null
96
97 # Then, check with memory sanitizer, if we're on Linux and have rustc nightly
98 if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" ]; then
99         if cargo +nightly --version >/dev/null 2>&1; then
100                 LLVM_V=$(rustc +nightly --version --verbose | grep "LLVM version" | awk '{ print substr($3, 0, 2); }')
101                 if [ -x "$(which clang-$LLVM_V)" ]; then
102                         cargo +nightly clean
103                         cargo +nightly rustc -Zbuild-std --target x86_64-unknown-linux-gnu -v -- -Zsanitizer=memory -Zsanitizer-memory-track-origins -Cforce-frame-pointers=yes
104                         mv target/x86_64-unknown-linux-gnu/debug/libldk.* target/debug/
105
106                         # Sadly, std doesn't seem to compile into something that is memsan-safe as of Aug 2020,
107                         # so we'll always fail, not to mention we may be linking against git rustc LLVM which
108                         # may differ from clang-llvm, so just allow everything here to fail.
109                         set +e
110
111                         # First the C demo app...
112                         clang-$LLVM_V $CFLAGS -fsanitize=memory -fsanitize-memory-track-origins -g demo.c target/debug/libldk.a -ldl
113                         ./a.out
114
115                         # ...then the C++ demo app
116                         clang++-$LLVM_V $CFLAGS -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -g demo.cpp target/debug/libldk.a -ldl
117                         ./a.out >/dev/null
118
119                         # restore exit-on-failure
120                         set -e
121                 else
122                         echo "WARNING: Can't use memory sanitizer without clang-$LLVM_V"
123                 fi
124         else
125                 echo "WARNING: Can't use memory sanitizer without rustc nightly"
126         fi
127 else
128         echo "WARNING: Can't use memory sanitizer on non-Linux, non-x86 platforms"
129 fi
130
131 RUSTC_LLVM_V=$(rustc --version --verbose | grep "LLVM version" | awk '{ print substr($3, 0, 2); }' | tr -d '.')
132
133 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
134         # Apple is special, as always, and decided that they must ensure that there is no way to identify
135         # the LLVM version used. Why? Just to make your life hard.
136         # This list is taken from https://en.wikipedia.org/wiki/Xcode
137         APPLE_CLANG_V=$(clang --version | head -n1 | awk '{ print $4 }')
138         if [ "$APPLE_CLANG_V" = "10.0.0" ]; then
139                 CLANG_LLVM_V="6"
140         elif [ "$APPLE_CLANG_V" = "10.0.1" ]; then
141                 CLANG_LLVM_V="7"
142         elif [ "$APPLE_CLANG_V" = "11.0.0" ]; then
143                 CLANG_LLVM_V="8"
144         elif [ "$APPLE_CLANG_V" = "11.0.3" ]; then
145                 CLANG_LLVM_V="9"
146         elif [ "$APPLE_CLANG_V" = "12.0.0" ]; then
147                 CLANG_LLVM_V="10"
148         else
149                 echo "WARNING: Unable to identify Apple clang LLVM version"
150                 CLANG_LLVM_V="0"
151         fi
152 else
153         CLANG_LLVM_V=$(clang --version | head -n1 | awk '{ print substr($4, 0, 2); }' | tr -d '.')
154 fi
155
156 if [ "$CLANG_LLVM_V" = "$RUSTC_LLVM_V" ]; then
157         CLANG=clang
158         CLANGPP=clang++
159 elif [ "$(which clang-$RUSTC_LLVM_V)" != "" ]; then
160         CLANG="$(which clang-$RUSTC_LLVM_V)"
161         CLANGPP="$(which clang++-$RUSTC_LLVM_V)"
162 fi
163
164 if [ "$CLANG" != "" -a "$CLANGPP" = "" ]; then
165         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"
166         echo "You should create a symlink called clang++-$RUSTC_LLVM_V pointing to $CLANG in $(dirname $CLANG)"
167 fi
168
169 # Finally, if we're on OSX or on Linux, build the final debug binary with address sanitizer (and leave it there)
170 if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" -o "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
171         if [ "$CLANGPP" != "" ]; then
172                 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
173                         # OSX sed is for some reason not compatible with GNU sed
174                         sed -i .bk 's/,"cdylib"]/]/g' Cargo.toml
175                 else
176                         sed -i.bk 's/,"cdylib"]/]/g' Cargo.toml
177                 fi
178                 RUSTC_BOOTSTRAP=1 cargo rustc -v -- -Zsanitizer=address -Cforce-frame-pointers=yes || ( mv Cargo.toml.bk Cargo.toml; exit 1)
179                 mv Cargo.toml.bk Cargo.toml
180
181                 # First the C demo app...
182                 $CLANG $CFLAGS -fsanitize=address -g demo.c target/debug/libldk.a -ldl
183                 ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out
184
185                 # ...then the C++ demo app
186                 $CLANGPP $CFLAGS -std=c++11 -fsanitize=address -g demo.cpp target/debug/libldk.a -ldl
187                 ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out >/dev/null
188         else
189                 echo "WARNING: Please install clang-$RUSTC_LLVM_V and clang++-$RUSTC_LLVM_V to build with address sanitizer"
190         fi
191 else
192         echo "WARNING: Can't use address sanitizer on non-Linux, non-OSX non-x86 platforms"
193 fi
194
195 if [ "$(rustc --print target-list | grep wasm32-wasi)" != "" ]; then
196         # Test to see if clang supports wasm32 as a target (which is needed to build rust-secp256k1)
197         echo "int main() {}" > genbindings_wasm_test_file.c
198         clang -nostdlib -o /dev/null --target=wasm32-wasi -Wl,--no-entry genbindings_wasm_test_file.c > /dev/null 2>&1 &&
199         # And if it does, build a WASM binary without capturing errors
200         cargo rustc -v --target=wasm32-wasi -- -C embed-bitcode=yes &&
201         CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release --target=wasm32-wasi -- -C opt-level=s -C linker-plugin-lto -C lto ||
202         echo "Cannot build WASM lib as clang does not seem to support the wasm32-wasi target"
203         rm genbindings_wasm_test_file.c
204 fi
205
206 # Now build with LTO on on both C++ and rust, but without cross-language LTO:
207 CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C lto
208 clang++ $CFLAGS -std=c++11 -flto -O2 demo.cpp target/release/libldk.a -ldl
209 strip ./a.out
210 echo "C++ Bin size and runtime with only RL (LTO) optimized:"
211 ls -lha a.out
212 time ./a.out > /dev/null
213
214 if [ "$HOST_PLATFORM" != "host: x86_64-apple-darwin" -a "$CLANGPP" != "" ]; then
215         # Finally, test cross-language LTO. Note that this will fail if rustc and clang++
216         # build against different versions of LLVM (eg when rustc is installed via rustup
217         # or Ubuntu packages). This should work fine on Distros which do more involved
218         # packaging than simply shipping the rustup binaries (eg Debian should Just Work
219         # here).
220         CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C linker-plugin-lto -C lto -C link-arg=-fuse-ld=lld
221         $CLANGPP $CFLAGS -flto -fuse-ld=lld -O2 demo.cpp target/release/libldk.a -ldl
222         strip ./a.out
223         echo "C++ Bin size and runtime with cross-language LTO:"
224         ls -lha a.out
225         time ./a.out > /dev/null
226 else
227         echo "WARNING: Building with cross-language LTO is not avilable on OSX or without clang-$RUSTC_LLVM_V"
228 fi