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