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"
14 # On reasonable systems, we can use realpath here, but OSX is a diva with 20-year-old software.
17 LIGHTNING_PATH="$(pwd)"
18 LIGHTNING_GIT="$(git describe --tag --dirty --abbrev=16)"
21 # Generate (and reasonably test) C bindings
23 # First we set various compiler flags...
24 HOST_PLATFORM="$(rustc --version --verbose | grep "host:" | awk '{ print $2 }')"
25 ENV_TARGET=$(echo $HOST_PLATFORM | sed 's/-/_/g')
27 # Set path to include our rustc wrapper as well as cbindgen
28 export LDK_RUSTC_PATH="$(which rustc)"
29 export RUSTC="$(pwd)/deterministic-build-wrappers/rustc"
30 PATH="$PATH:~/.cargo/bin"
32 # Set up CFLAGS and RUSTFLAGS vars appropriately for building libsecp256k1 and demo apps...
33 BASE_CFLAGS="" # CFLAGS for libsecp256k1
34 LOCAL_CFLAGS="" # CFLAGS for demo apps
36 # Remap paths so that our builds are deterministic
37 BASE_RUSTFLAGS="--cfg=c_bindings --remap-path-prefix $LIGHTNING_PATH=rust-lightning --remap-path-prefix $(pwd)=ldk-c-bindings --remap-path-prefix $HOME/.cargo="
39 # If the C compiler supports it, also set -ffile-prefix-map
40 echo "int main() {}" > genbindings_path_map_test_file.c
41 clang -o /dev/null -ffile-prefix-map=$HOME/.cargo= genbindings_path_map_test_file.c > /dev/null 2>&1 &&
42 export BASE_CFLAGS="-ffile-prefix-map=$HOME/.cargo="
44 BASE_CFLAGS="$BASE_CFLAGS -frandom-seed=42"
45 LOCAL_CFLAGS="-Wall -Wno-nullability-completeness -pthread -Iinclude/"
48 if [ "$HOST_PLATFORM" = "x86_64-apple-darwin" ]; then
50 elif [ "$HOST_PLATFORM" = "aarch64-apple-darwin" ]; then
54 BASE_HOST_CFLAGS="$BASE_CFLAGS"
56 if [ "$HOST_OSX" = "true" ]; then
57 export MACOSX_DEPLOYMENT_TARGET=10.9
58 LOCAL_CFLAGS="$LOCAL_CFLAGS --target=$HOST_PLATFORM -isysroot$(xcrun --show-sdk-path) -mmacosx-version-min=10.9"
59 BASE_HOST_CFLAGS="$BASE_HOST_CFLAGS --target=$HOST_PLATFORM -isysroot$(xcrun --show-sdk-path) -mmacosx-version-min=10.9"
60 # Targeting aarch64 appears to be supported only starting with Big Sur, so check it before use
61 clang -o /dev/null $BASE_HOST_CFLAGS --target=aarch64-apple-darwin -mcpu=apple-a14 genbindings_path_map_test_file.c &&
62 export CFLAGS_aarch64_apple_darwin="$BASE_HOST_CFLAGS --target=aarch64-apple-darwin -mcpu=apple-a14" ||
63 echo "WARNING: Can not build targeting aarch64-apple-darin. Upgrade to Big Sur or try upstream clang"
66 rm genbindings_path_map_test_file.c
70 export RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=sandybridge"
71 export CFLAGS_$ENV_TARGET="$BASE_HOST_CFLAGS -march=sandybridge -mcpu=sandybridge -mtune=sandybridge"
73 "aarch64_apple_darwin")
74 export RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14"
75 export CFLAGS_$ENV_TARGET="$BASE_HOST_CFLAGS -mcpu=apple-a14"
78 # Assume this isn't targeted at another host and build for the host's CPU.
79 export RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=native"
80 export CFLAGS_$ENV_TARGET="$BASE_HOST_CFLAGS -mcpu=native"
84 # First build the latest c-bindings-gen binary
85 cd c-bindings-gen && cargo build --release && cd ..
87 # Then wipe all the existing C bindings (because we're being run in the right directory)
88 # note that we keep the few manually-generated files first:
89 mv lightning-c-bindings/src/c_types/mod.rs ./
90 mv lightning-c-bindings/src/bitcoin ./
92 # Before we try to sed the Cargo.toml, generate version define tags
93 # (ignoring any files that we're about to generate)
95 git checkout lightning-c-bindings/src
96 git checkout lightning-c-bindings/include
97 BINDINGS_GIT="$(git describe --tag --dirty --abbrev=16)"
99 #ifndef _LDK_HEADER_VER
100 static inline int _ldk_strncmp(const char *s1, const char *s2, uint64_t n) {
101 if (n && *s1 != *s2) return 1;
102 while (n && *s1 != 0 && *s2 != 0) {
104 if (n && *s1 != *s2) return 1;
109 #define _LDK_HEADER_VER "${LIGHTNING_GIT}"
110 #define _LDK_C_BINDINGS_HEADER_VER "${BINDINGS_GIT}"
111 static inline const char* check_get_ldk_version() {
112 LDKStr bin_ver = _ldk_get_compiled_version();
113 if (_ldk_strncmp(_LDK_HEADER_VER, (const char*)bin_ver.chars, bin_ver.len) != 0) {
114 // Version mismatch, we don't know what we're running!
117 return _LDK_HEADER_VER;
119 static inline const char* check_get_ldk_bindings_version() {
120 LDKStr bin_ver = _ldk_c_bindings_get_compiled_version();
121 if (_ldk_strncmp(_LDK_C_BINDINGS_HEADER_VER, (const char*)bin_ver.chars, bin_ver.len) != 0) {
122 // Version mismatch, we don't know what we're running!
125 return _LDK_C_BINDINGS_HEADER_VER;
127 #endif /* _LDK_HEADER_VER */
129 )" > lightning-c-bindings/include/ldk_ver.h
131 rm -rf lightning-c-bindings/src
133 mkdir -p lightning-c-bindings/src/{c_types,lightning}
134 mv ./mod.rs lightning-c-bindings/src/c_types/
135 mv ./bitcoin lightning-c-bindings/src/
137 # Finally, run the c-bindings-gen binary, building fresh bindings.
138 OUT="$(pwd)/lightning-c-bindings/src"
139 OUT_TEMPL="$(pwd)/lightning-c-bindings/src/c_types/derived.rs"
140 OUT_F="$(pwd)/lightning-c-bindings/include/ldk_rust_types.h"
141 OUT_CPP="$(pwd)/lightning-c-bindings/include/lightningpp.hpp"
142 BIN="$(pwd)/c-bindings-gen/target/release/c-bindings-gen"
144 function add_crate() {
145 pushd "$LIGHTNING_PATH/$1"
146 RUSTC_BOOTSTRAP=1 cargo rustc --profile=check --no-default-features $3 -- --cfg=c_bindings -Zunpretty=expanded > /tmp/$1-crate-source.txt
148 if [ "$HOST_OSX" = "true" ]; then
149 sed -i".original" "1i\\
151 " /tmp/$1-crate-source.txt
153 sed -i "1ipub mod $2 {\n" /tmp/$1-crate-source.txt
155 echo "}" >> /tmp/$1-crate-source.txt
156 cat /tmp/$1-crate-source.txt >> /tmp/crate-source.txt
157 rm /tmp/$1-crate-source.txt
158 if [ "$HOST_OSX" = "true" ]; then
159 # OSX sed is for some reason not compatible with GNU sed
160 sed -E -i '' 's|#*'$1' = \{ .*|'$1' = \{ path = "'"$LIGHTNING_PATH"'/'$1'", default-features = false }|' lightning-c-bindings/Cargo.toml
162 sed -E -i 's|#*'$1' = \{ .*|'$1' = \{ path = "'"$LIGHTNING_PATH"'/'$1'", default-features = false }|' lightning-c-bindings/Cargo.toml
166 function drop_crate() {
167 if [ "$HOST_OSX" = "true" ]; then
168 # OSX sed is for some reason not compatible with GNU sed
169 sed -E -i '' 's|'$1' = \{ (.*)|#'$1' = \{ \1|' lightning-c-bindings/Cargo.toml
171 sed -E -i 's|'$1' = \{ (.*)|#'$1' = \{ \1|' lightning-c-bindings/Cargo.toml
175 echo > /tmp/crate-source.txt
176 if [ "$2" = "true" ]; then
177 add_crate lightning lightning --features=std
178 add_crate "lightning-persister" "lightning_persister"
179 add_crate "lightning-background-processor" "lightning_background_processor"
180 add_crate "lightning-invoice" "lightning_invoice" --features=std
181 CARGO_BUILD_ARGS="--features=std"
183 add_crate lightning lightning --features=no-std
184 drop_crate "lightning-persister"
185 drop_crate "lightning-background-processor"
186 add_crate "lightning-invoice" "lightning_invoice" --features=no-std
187 CARGO_BUILD_ARGS="--features=no-std"
190 cat /tmp/crate-source.txt | RUST_BACKTRACE=1 "$BIN" "$OUT/" "$OUT_TEMPL" "$OUT_F" "$OUT_CPP"
194 pub extern "C" fn _ldk_get_compiled_version() -> crate::c_types::Str {
195 "${LIGHTNING_GIT}".into()
198 pub extern "C" fn _ldk_c_bindings_get_compiled_version() -> crate::c_types::Str {
199 "${BINDINGS_GIT}".into()
202 )" >> lightning-c-bindings/src/version.rs
204 # Now cd to lightning-c-bindings, build the generated bindings, and call cbindgen to build a C header file
205 cd lightning-c-bindings
207 RUSTFLAGS="$RUSTFLAGS --cfg=test_mod_pointers" cargo build $CARGO_BUILD_ARGS
208 if [ "$CFLAGS_aarch64_apple_darwin" != "" ]; then
209 RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14" cargo build $CARGO_BUILD_ARGS --target aarch64-apple-darwin
211 cbindgen -v --config cbindgen.toml -o include/lightning.h >/dev/null 2>&1
213 # cbindgen is relatively braindead when exporting typedefs -
214 # it happily exports all our typedefs for private types, even with the
215 # generics we specified in C mode! So we drop all those types manually here.
216 if [ "$HOST_OSX" = "true" ]; then
217 # OSX sed is for some reason not compatible with GNU sed
218 sed -i '' 's/typedef LDKnative.*Import.*LDKnative.*;//g' include/lightning.h
220 # stdlib.h doesn't exist in clang's wasm sysroot, and cbindgen
221 # doesn't actually use it anyway, so drop the import.
222 sed -i '' 's/#include <stdlib.h>/#include "ldk_rust_types.h"/g' include/lightning.h
224 sed -i 's/typedef LDKnative.*Import.*LDKnative.*;//g' include/lightning.h
226 # stdlib.h doesn't exist in clang's wasm sysroot, and cbindgen
227 # doesn't actually use it anyway, so drop the import.
228 sed -i 's/#include <stdlib.h>/#include "ldk_rust_types.h"/g' include/lightning.h
231 # Build C++ class methods which call trait methods
232 echo "Updating C++ header, this may take some time, especially on macOS"
233 set +x # Echoing every command is very verbose here
236 echo '#include <string.h>' > include/lightningpp_new.hpp
237 echo 'namespace LDK {' >> include/lightningpp_new.hpp
238 echo '// Forward declarations' >> include/lightningpp_new.hpp
239 cat include/lightningpp.hpp | sed -n 's/class \(.*\) {/class \1;/p' >> include/lightningpp_new.hpp
240 echo '' >> include/lightningpp_new.hpp
245 "#include <string.h>")
246 # We already printed this above.
249 # We already printed this above.
252 # We'll print this at the end
255 NEW_STRUCT_NAME="$(echo "$LINE" | awk '{ print $2 }')"
256 if [ "$NEW_STRUCT_NAME" != "$STRUCT_NAME" ]; then
257 STRUCT_CONTENTS="$(cat include/lightning.h | sed -n -e "/struct LDK$NEW_STRUCT_NAME/{:s" -e "/\} LDK$NEW_STRUCT_NAME;/!{N" -e "b s" -e "}" -e p -e "}")"
259 STRUCT_NAME="$NEW_STRUCT_NAME"
260 METHOD_NAME="$(echo "$LINE" | awk '{ print $3 }')"
261 METHOD="$(echo "$STRUCT_CONTENTS" | grep "(\*$METHOD_NAME)")"
262 if [ "$METHOD" = "" ]; then
263 echo "Unable to find method declaration for $LINE"
266 RETVAL="$(echo "$METHOD" | sed 's/[ ]*\([A-Za-z0-9 _]*\)(\*\(.*\)).*/\1/' | sed 's/^struct LDK/LDK::/g' | tr -d ' ')"
267 [ "$RETVAL" = "LDK::SecretKey" ] && RETVAL="LDKSecretKey"
268 [ "$RETVAL" = "LDK::PublicKey" ] && RETVAL="LDKPublicKey"
269 [ "$RETVAL" = "LDK::ThirtyTwoBytes" ] && RETVAL="LDKThirtyTwoBytes"
270 PARAMS="$(echo "$METHOD" | sed 's/.*(\*.*)(\(const \)*void \*this_arg\(, \)*\(.*\));/\3/')"
272 echo -e "\tinline $RETVAL $METHOD_NAME($PARAMS);" >> include/lightningpp_new.hpp
273 DECLS="$DECLS"$'\n'"inline $RETVAL $STRUCT_NAME::$METHOD_NAME($PARAMS) {"
275 DECLS="$DECLS"$'\n'$'\t'
276 [ "$RETVAL" != "void" ] && DECLS="$DECLS$RETVAL ret = "
277 DECLS="$DECLS(self.$METHOD_NAME)(self.this_arg"
279 IFS=','; for PARAM in $PARAMS; do
281 DECLS="$DECLS$(echo $PARAM | sed 's/.* (*\**\([a-zA-Z0-9_]*\)\()[\[0-9\]*]\)*/\1/')"
286 [ "$RETVAL" != "void" ] && DECLS="$DECLS"$'\n'$'\t'"return ret;"
287 DECLS="$DECLS"$'\n'"}"
290 echo "$LINE" >> include/lightningpp_new.hpp
292 done < include/lightningpp.hpp
293 echo "$DECLS" >> include/lightningpp_new.hpp
294 echo "}" >> include/lightningpp_new.hpp
295 export IFS="$OLD_IFS"
297 mv include/lightningpp_new.hpp include/lightningpp.hpp
299 # Finally, sanity-check the generated C and C++ bindings with demo apps:
300 # Naively run the C demo app:
301 gcc $LOCAL_CFLAGS -Wall -g -pthread demo.c target/debug/libldk.a -ldl -lm
304 # And run the C++ demo app
305 if [ "$2" = "true" ]; then
306 g++ $LOCAL_CFLAGS -std=c++11 -Wall -g -pthread demo.cpp -Ltarget/debug/ -lldk -ldl
307 LD_LIBRARY_PATH=target/debug/ ./a.out > /dev/null
310 # Finally, run the C++ demo app with our native networking library
311 # in valgrind to test memory model correctness and lack of leaks.
312 gcc $LOCAL_CFLAGS -fPIC -std=c99 -Wall -g -pthread -I../ldk-net ../ldk-net/ldk_net.c -c -o ldk_net.o
313 if [ "$2" = "true" ]; then
314 g++ $LOCAL_CFLAGS -std=c++11 -Wall -g -pthread -DREAL_NET -I../ldk-net ldk_net.o demo.cpp target/debug/libldk.a -ldl -lm
315 if [ -x "`which valgrind`" -a "$(uname -m)" != "ppc64le" ]; then
316 valgrind --error-exitcode=4 --memcheck:leak-check=full --show-leak-kinds=all ./a.out
319 echo "WARNING: Please install valgrind for more testing"
325 # Test a statically-linked C++ version, tracking the resulting binary size and runtime
326 # across debug, LTO, and cross-language LTO builds (using the same compiler each time).
327 if [ "$2" = "true" ]; then
328 clang++ $LOCAL_CFLAGS -std=c++11 demo.cpp target/debug/libldk.a -ldl
330 echo " C++ Bin size and runtime w/o optimization:"
332 time ./a.out > /dev/null
335 # Then, check with memory sanitizer, if we're on Linux and have rustc nightly
336 if [ "$HOST_PLATFORM" = "x86_64-unknown-linux-gnu" ]; then
337 if cargo +nightly --version >/dev/null 2>&1; then
338 LLVM_V=$(rustc +nightly --version --verbose | grep "LLVM version" | awk '{ print substr($3, 0, 2); }')
339 if [ -x "$(which clang-$LLVM_V)" ]; then
341 cargo +nightly rustc $CARGO_BUILD_ARGS -Zbuild-std=std,panic_abort --target x86_64-unknown-linux-gnu -v -- -Zsanitizer=memory -Zsanitizer-memory-track-origins -Cforce-frame-pointers=yes
342 mv target/x86_64-unknown-linux-gnu/debug/libldk.* target/debug/
344 # Sadly, std doesn't seem to compile into something that is memsan-safe as of Aug 2020,
345 # so we'll always fail, not to mention we may be linking against git rustc LLVM which
346 # may differ from clang-llvm, so just allow everything here to fail.
349 # First the C demo app...
350 clang-$LLVM_V $LOCAL_CFLAGS -fsanitize=memory -fsanitize-memory-track-origins -g demo.c target/debug/libldk.a -ldl
353 if [ "$2" = "true" ]; then
354 # ...then the C++ demo app
355 clang++-$LLVM_V $LOCAL_CFLAGS -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -g demo.cpp target/debug/libldk.a -ldl
358 # ...then the C++ demo app with the ldk_net network implementation
359 clang-$LLVM_V $LOCAL_CFLAGS -std=c99 -fsanitize=memory -fsanitize-memory-track-origins -g -I../ldk-net ../ldk-net/ldk_net.c -c -o ldk_net.o
360 clang++-$LLVM_V $LOCAL_CFLAGS -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -g -DREAL_NET -I../ldk-net ldk_net.o demo.cpp target/debug/libldk.a -ldl
364 # restore exit-on-failure
367 echo "WARNING: Can't use memory sanitizer without clang-$LLVM_V"
370 echo "WARNING: Can't use memory sanitizer without rustc nightly"
373 echo "WARNING: Can't use memory sanitizer on non-Linux, non-x86 platforms"
376 RUSTC_LLVM_V=$(rustc --version --verbose | grep "LLVM version" | awk '{ print substr($3, 0, 2); }')
378 if [ "$HOST_OSX" = "true" ]; then
379 # Apple is special, as always, and their versions of clang aren't
380 # compatible with upstream LLVM.
381 if [ "$(clang --version | grep 'Apple clang')" != "" ]; then
382 echo "Apple clang isn't compatible with upstream clang, install upstream clang"
385 CLANG_LLVM_V=$(clang --version | head -n1 | awk '{ print substr($3, 0, 2); }')
386 if [ -x "$(which ld64.lld)" ]; then
387 LLD_LLVM_V="$(ld64.lld --version | awk '{ print substr($2, 0, 2); }')"
391 CLANG_LLVM_V=$(clang --version | head -n1 | awk '{ print substr($4, 0, 2); }')
392 if [ -x "$(which ld.lld)" ]; then
393 LLD_LLVM_V="$(ld.lld --version | awk '{ print $2; }')"
394 if [ "$LLD_LLVM_V" = "LLD" ]; then # eg if the output is "Debian LLD ..."
395 LLD_LLVM_V="$(ld.lld --version | awk '{ print substr($3, 0, 2); }')"
397 LLD_LLVM_V="$(ld.lld --version | awk '{ print substr($2, 0, 2); }')"
403 if [ "$CLANG_LLVM_V" = "$RUSTC_LLVM_V" ]; then
406 if [ "$LLD_LLVM_V" = "$CLANG_LLVM_V" ]; then
409 elif [ -x "$(which clang-$RUSTC_LLVM_V)" ]; then
410 CLANG="$(which clang-$RUSTC_LLVM_V)"
411 CLANGPP="$(which clang++-$RUSTC_LLVM_V || echo clang++)"
412 if [ "$($CLANG --version)" != "$($CLANGPP --version)" ]; then
413 echo "$CLANG and $CLANGPP are not the same version of clang!"
417 if [ "$LLD_LLVM_V" != "$RUSTC_LLVM_V" ]; then
419 [ -x "$(which lld-$RUSTC_LLVM_V)" ] && LLD="lld-$RUSTC_LLVM_V"
420 LLD_LLVM_V="$(ld.lld-$RUSTC_LLVM_V --version | awk '{ print $2; }')"
421 if [ "$LLD_LLVM_V" = "LLD" ]; then # eg if the output is "Debian LLD ..."
422 LLD_LLVM_V="$(ld.lld-$RUSTC_LLVM_V --version | awk '{ print substr($3, 0, 2); }')"
424 LLD_LLVM_V="$(ld.lld-$RUSTC_LLVM_V --version | awk '{ print substr($2, 0, 2); }')"
426 if [ "$LLD_LLVM_V" != "$RUSTC_LLVM_V" ]; then
427 echo "Could not find a workable version of lld, not using cross-language LTO"
433 if [ "$CLANG" != "" -a "$CLANGPP" = "" ]; then
434 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"
435 echo "You should create a symlink called clang++-$RUSTC_LLVM_V pointing to $CLANG in $(dirname $CLANG)"
438 # Finally, if we're on OSX or on Linux, build the final debug binary with address sanitizer (and leave it there)
439 if [ "$HOST_PLATFORM" = "x86_64-unknown-linux-gnu" -o "$HOST_PLATFORM" = "x86_64-apple-darwin" ]; then
440 if [ "$CLANGPP" != "" ]; then
441 if [ "$HOST_OSX" = "true" ]; then
442 # OSX sed is for some reason not compatible with GNU sed
443 sed -i .bk 's/,"cdylib"]/]/g' Cargo.toml
445 sed -i.bk 's/,"cdylib"]/]/g' Cargo.toml
447 if [ "$CFLAGS_aarch64_apple_darwin" != "" ]; then
448 RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14" RUSTC_BOOTSTRAP=1 cargo rustc $CARGO_BUILD_ARGS --target aarch64-apple-darwin -v -- -Zsanitizer=address -Cforce-frame-pointers=yes || ( mv Cargo.toml.bk Cargo.toml; exit 1)
450 RUSTFLAGS="$RUSTFLAGS --cfg=test_mod_pointers" RUSTC_BOOTSTRAP=1 cargo rustc $CARGO_BUILD_ARGS -v -- -Zsanitizer=address -Cforce-frame-pointers=yes || ( mv Cargo.toml.bk Cargo.toml; exit 1)
451 mv Cargo.toml.bk Cargo.toml
453 # First the C demo app...
454 $CLANG $LOCAL_CFLAGS -fsanitize=address -g demo.c target/debug/libldk.a -ldl
455 ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out
457 if [ "$2" = "true" ]; then
458 # ...then the C++ demo app
459 $CLANGPP $LOCAL_CFLAGS -std=c++11 -fsanitize=address -g demo.cpp target/debug/libldk.a -ldl
460 ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out >/dev/null
462 # ...then the C++ demo app with the ldk_net network implementation
463 $CLANG $LOCAL_CFLAGS -fPIC -fsanitize=address -g -I../ldk-net ../ldk-net/ldk_net.c -c -o ldk_net.o
464 $CLANGPP $LOCAL_CFLAGS -std=c++11 -fsanitize=address -g -DREAL_NET -I../ldk-net ldk_net.o demo.cpp target/debug/libldk.a -ldl
465 ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out >/dev/null
468 echo "WARNING: Please install clang-$RUSTC_LLVM_V and clang++-$RUSTC_LLVM_V to build with address sanitizer"
471 echo "WARNING: Can't use address sanitizer on non-Linux, non-OSX non-x86 platforms"
474 # Now build with LTO on on both C++ and rust, but without cross-language LTO:
475 # Clear stale release build artifacts from previous runs
476 cargo clean --release
477 CARGO_PROFILE_RELEASE_LTO=true RUSTFLAGS="$RUSTFLAGS -C embed-bitcode=yes -C lto" cargo build $CARGO_BUILD_ARGS -v --release
478 if [ "$2" = "true" ]; then
479 clang++ $LOCAL_CFLAGS -std=c++11 -O2 demo.cpp target/release/libldk.a -ldl
483 echo "C++ Bin size and runtime with only RL (LTO) optimized:"
485 time ./a.out > /dev/null
487 if [ "$CLANGPP" != "" ]; then
488 # If we can use cross-language LTO, use it for building C dependencies (i.e. libsecp256k1) as well
490 # The cc-rs crate tries to force -fdata-sections and -ffunction-sections on, which
491 # breaks -fembed-bitcode, so we turn off cc-rs' default flags and specify exactly
493 export CFLAGS_$ENV_TARGET="$BASE_HOST_CFLAGS -fPIC -fembed-bitcode -march=sandybridge -mcpu=sandybridge -mtune=sandybridge"
494 export CRATE_CC_NO_DEFAULTS=true
497 if [ "$2" = "false" -a "$(rustc --print target-list | grep wasm32-wasi)" != "" ]; then
498 # Test to see if clang supports wasm32 as a target (which is needed to build rust-secp256k1)
499 echo "int main() {}" > genbindings_wasm_test_file.c
500 if clang -nostdlib -o /dev/null --target=wasm32-wasi -Wl,--no-entry genbindings_wasm_test_file.c > /dev/null 2>&1; then
501 # And if it does, build a WASM binary without capturing errors
502 export CFLAGS_wasm32_wasi="$BASE_CFLAGS -target wasm32 -O1"
503 RUSTFLAGS="$BASE_RUSTFLAGS -C opt-level=1 --cfg=test_mod_pointers" cargo build $CARGO_BUILD_ARGS -v --target=wasm32-wasi
504 export CFLAGS_wasm32_wasi="$BASE_CFLAGS -fembed-bitcode -target wasm32 -Oz"
505 RUSTFLAGS="$BASE_RUSTFLAGS -C embed-bitcode=yes -C opt-level=z -C linker-plugin-lto -C lto" CARGO_PROFILE_RELEASE_LTO=true cargo build $CARGO_BUILD_ARGS -v --release --target=wasm32-wasi
507 echo "Cannot build WASM lib as clang does not seem to support the wasm32-wasi target"
509 rm genbindings_wasm_test_file.c
512 EXTRA_TARGETS=( $LDK_C_BINDINGS_EXTRA_TARGETS )
513 EXTRA_CCS=( $LDK_C_BINDINGS_EXTRA_TARGET_CCS )
515 if [ ${#EXTRA_TARGETS[@]} != ${#EXTRA_CCS[@]} ]; then
516 echo "LDK_C_BINDINGS_EXTRA_TARGETS and LDK_C_BINDINGS_EXTRA_TARGET_CCS didn't have the same number of elements!"
520 for IDX in ${!EXTRA_TARGETS[@]}; do
521 EXTRA_ENV_TARGET=$(echo "${EXTRA_TARGETS[$IDX]}" | sed 's/-/_/g')
522 export CFLAGS_$EXTRA_ENV_TARGET="$BASE_CFLAGS"
523 export CC_$EXTRA_ENV_TARGET=${EXTRA_CCS[$IDX]}
524 RUSTFLAGS="$BASE_RUSTFLAGS -C embed-bitcode=yes -C lto -C linker=${EXTRA_CCS[$IDX]}" CARGO_PROFILE_RELEASE_LTO=true cargo build $CARGO_BUILD_ARGS -v --release --target "${EXTRA_TARGETS[$IDX]}"
527 if [ "$CLANGPP" != "" -a "$LLD" != "" ]; then
528 # Finally, test cross-language LTO. Note that this will fail if rustc and clang++
529 # build against different versions of LLVM (eg when rustc is installed via rustup
530 # or Ubuntu packages). This should work fine on Distros which do more involved
531 # packaging than simply shipping the rustup binaries (eg Debian should Just Work
533 LINK_ARG_FLAGS="-C link-arg=-fuse-ld=$LLD"
534 if [ "$HOST_OSX" = "true" ]; then
535 export LDK_CLANG_PATH=$(which $CLANG)
536 export CLANG="$(pwd)/../deterministic-build-wrappers/clang-lto-link-osx"
537 for ARG in "CFLAGS_aarch64_apple_darwin"; do
538 MANUAL_LINK_CFLAGS="$MANUAL_LINK_CFLAGS -C link-arg=$ARG"
540 export CFLAGS_aarch64_apple_darwin="$CFLAGS_aarch64_apple_darwin -O3 -fPIC -fembed-bitcode"
541 LINK_ARG_FLAGS="$LINK_ARG_FLAGS -C link-arg="-isysroot$(xcrun --show-sdk-path)" -C link-arg=-mmacosx-version-min=10.9"
542 RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14 -C embed-bitcode=yes -C linker-plugin-lto -C lto -C linker=$CLANG $LINK_ARG_FLAGS -C link-arg=-mcpu=apple-a14" CARGO_PROFILE_RELEASE_LTO=true cargo build $CARGO_BUILD_ARGS -v --release --target aarch64-apple-darwin
544 # If we're on an M1 don't bother building X86 binaries
545 if [ "$HOST_PLATFORM" != "aarch64-apple-darwin" ]; then
546 export CFLAGS_$ENV_TARGET="$BASE_HOST_CFLAGS -O3 -fPIC -fembed-bitcode -march=sandybridge -mcpu=sandybridge -mtune=sandybridge"
547 # Rust doesn't recognize CFLAGS changes, so we need to clean build artifacts
548 cargo clean --release
549 CARGO_PROFILE_RELEASE_LTO=true RUSTFLAGS="$RUSTFLAGS -C embed-bitcode=yes -C linker-plugin-lto -C lto -C linker=$CLANG $LINK_ARG_FLAGS -C link-arg=-march=sandybridge -C link-arg=-mcpu=sandybridge -C link-arg=-mtune=sandybridge" cargo build $CARGO_BUILD_ARGS -v --release
551 if [ "$2" = "true" ]; then
552 $CLANGPP $LOCAL_CFLAGS -flto -fuse-ld=$LLD -O2 demo.cpp target/release/libldk.a -ldl
554 echo "C++ Bin size and runtime with cross-language LTO:"
556 time ./a.out > /dev/null
560 if [ "$CFLAGS_aarch64_apple_darwin" != "" ]; then
561 RUSTFLAGS="$BASE_RUSTFLAGS -C embed-bitcode=yes -C lto -C target-cpu=apple-a14" CARGO_PROFILE_RELEASE_LTO=true cargo build $CARGO_BUILD_ARGS -v --release --target aarch64-apple-darwin
563 echo "WARNING: Building with cross-language LTO is not avilable without clang-$RUSTC_LLVM_V"