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