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