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