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:")"
26 # Set path to include our rustc wrapper as well as cbindgen
27 export LDK_RUSTC_PATH="$(which rustc)"
28 export RUSTC="$(pwd)/deterministic-build-wrappers/rustc"
29 PATH="$PATH:~/.cargo/bin"
31 # Set up CFLAGS and RUSTFLAGS vars appropriately for building libsecp256k1 and demo apps...
32 BASE_CFLAGS="" # CFLAGS for libsecp256k1
33 LOCAL_CFLAGS="" # CFLAGS for demo apps
35 # Remap paths so that our builds are deterministic
36 BASE_RUSTFLAGS="--cfg=c_bindings --remap-path-prefix $LIGHTNING_PATH=rust-lightning --remap-path-prefix $(pwd)=ldk-c-bindings --remap-path-prefix $HOME/.cargo="
38 # If the C compiler supports it, also set -ffile-prefix-map
39 echo "int main() {}" > genbindings_path_map_test_file.c
40 clang -o /dev/null -ffile-prefix-map=$HOME/.cargo= genbindings_path_map_test_file.c > /dev/null 2>&1 &&
41 export BASE_CFLAGS="-ffile-prefix-map=$HOME/.cargo="
43 BASE_CFLAGS="$BASE_CFLAGS -frandom-seed=42"
44 LOCAL_CFLAGS="-Wall -Wno-nullability-completeness -pthread -Iinclude/"
46 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
47 export MACOSX_DEPLOYMENT_TARGET=10.9
48 LOCAL_CFLAGS="$LOCAL_CFLAGS -isysroot$(xcrun --show-sdk-path) -mmacosx-version-min=10.9"
49 BASE_CFLAGS="$BASE_CFLAGS -isysroot$(xcrun --show-sdk-path) -mmacosx-version-min=10.9"
50 # Targeting aarch64 appears to be supported only starting with Big Sur, so check it before use
51 clang -o /dev/null $BASE_CFLAGS --target=aarch64-apple-darwin -mcpu=apple-a14 genbindings_path_map_test_file.c &&
52 export CFLAGS_aarch64_apple_darwin="$BASE_CFLAGS --target=aarch64-apple-darwin -mcpu=apple-a14" ||
53 echo "WARNING: Can not build targeting aarch64-apple-darin. Upgrade to Big Sur or try upstream clang"
56 rm genbindings_path_map_test_file.c
58 ENV_TARGET=$(rustc --version --verbose | grep host | awk '{ print $2 }' | sed 's/-/_/g')
61 export RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=sandybridge"
62 export CFLAGS_$ENV_TARGET="$BASE_CFLAGS -march=sandybridge -mcpu=sandybridge -mtune=sandybridge"
65 # Assume this isn't targeted at another host and build for the host's CPU.
66 export RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=native"
67 export CFLAGS_$ENV_TARGET="$BASE_CFLAGS -mcpu=native"
71 # First build the latest c-bindings-gen binary
72 cd c-bindings-gen && cargo build --release && cd ..
74 # Then wipe all the existing C bindings (because we're being run in the right directory)
75 # note that we keep the few manually-generated files first:
76 mv lightning-c-bindings/src/c_types/mod.rs ./
77 mv lightning-c-bindings/src/bitcoin ./
79 # Before we try to sed the Cargo.toml, generate version define tags
80 # (ignoring any files that we're about to generate)
82 git checkout lightning-c-bindings/src
83 git checkout lightning-c-bindings/include
84 BINDINGS_GIT="$(git describe --tag --dirty --abbrev=16)"
85 echo -e "#ifndef _LDK_HEADER_VER" > lightning-c-bindings/include/ldk_ver.h
86 echo -e "static inline int _ldk_strncmp(const char *s1, const char *s2, uint64_t n) {" >> lightning-c-bindings/include/ldk_ver.h
87 echo -e "\tif (n && *s1 != *s2) return 1;" >> lightning-c-bindings/include/ldk_ver.h
88 echo -e "\twhile (n && *s1 != 0 && *s2 != 0) {" >> lightning-c-bindings/include/ldk_ver.h
89 echo -e "\t\ts1++; s2++; n--;" >> lightning-c-bindings/include/ldk_ver.h
90 echo -e "\t\tif (n && *s1 != *s2) return 1;" >> lightning-c-bindings/include/ldk_ver.h
91 echo -e "\t}" >> lightning-c-bindings/include/ldk_ver.h
92 echo -e "\treturn 0;" >> lightning-c-bindings/include/ldk_ver.h
93 echo -e "}" >> lightning-c-bindings/include/ldk_ver.h
94 echo -e "" >> lightning-c-bindings/include/ldk_ver.h
95 echo -e "#define _LDK_HEADER_VER \"$LIGHTNING_GIT\"" >> lightning-c-bindings/include/ldk_ver.h
96 echo -e "#define _LDK_C_BINDINGS_HEADER_VER \"$BINDINGS_GIT\"" >> lightning-c-bindings/include/ldk_ver.h
97 echo -e "static inline const char* check_get_ldk_version() {" >> lightning-c-bindings/include/ldk_ver.h
98 echo -e "\tLDKStr bin_ver = _ldk_get_compiled_version();" >> lightning-c-bindings/include/ldk_ver.h
99 echo -e "\tif (_ldk_strncmp(_LDK_HEADER_VER, (const char*)bin_ver.chars, bin_ver.len) != 0) {" >> lightning-c-bindings/include/ldk_ver.h
100 echo -e "\t// Version mismatch, we don't know what we're running!" >> lightning-c-bindings/include/ldk_ver.h
101 echo -e "\t\treturn 0;" >> lightning-c-bindings/include/ldk_ver.h
102 echo -e "\t}" >> lightning-c-bindings/include/ldk_ver.h
103 echo -e "\treturn _LDK_HEADER_VER;" >> lightning-c-bindings/include/ldk_ver.h
104 echo -e "}" >> lightning-c-bindings/include/ldk_ver.h
105 echo -e "static inline const char* check_get_ldk_bindings_version() {" >> lightning-c-bindings/include/ldk_ver.h
106 echo -e "\tLDKStr bin_ver = _ldk_c_bindings_get_compiled_version();" >> lightning-c-bindings/include/ldk_ver.h
107 echo -e "\tif (_ldk_strncmp(_LDK_C_BINDINGS_HEADER_VER, (const char*)bin_ver.chars, bin_ver.len) != 0) {" >> lightning-c-bindings/include/ldk_ver.h
108 echo -e "\t// Version mismatch, we don't know what we're running!" >> lightning-c-bindings/include/ldk_ver.h
109 echo -e "\t\treturn 0;" >> lightning-c-bindings/include/ldk_ver.h
110 echo -e "\t}" >> lightning-c-bindings/include/ldk_ver.h
111 echo -e "\treturn _LDK_C_BINDINGS_HEADER_VER;" >> lightning-c-bindings/include/ldk_ver.h
112 echo -e "}" >> lightning-c-bindings/include/ldk_ver.h
113 echo -e "#endif /* _LDK_HEADER_VER */" >> lightning-c-bindings/include/ldk_ver.h
115 rm -rf lightning-c-bindings/src
117 mkdir -p lightning-c-bindings/src/{c_types,lightning}
118 mv ./mod.rs lightning-c-bindings/src/c_types/
119 mv ./bitcoin lightning-c-bindings/src/
121 # Finally, run the c-bindings-gen binary, building fresh bindings.
122 OUT="$(pwd)/lightning-c-bindings/src"
123 OUT_TEMPL="$(pwd)/lightning-c-bindings/src/c_types/derived.rs"
124 OUT_F="$(pwd)/lightning-c-bindings/include/ldk_rust_types.h"
125 OUT_CPP="$(pwd)/lightning-c-bindings/include/lightningpp.hpp"
126 BIN="$(pwd)/c-bindings-gen/target/release/c-bindings-gen"
128 function add_crate() {
129 pushd "$LIGHTNING_PATH/$1"
130 RUSTC_BOOTSTRAP=1 cargo rustc --profile=check --no-default-features $3 -- --cfg=c_bindings -Zunpretty=expanded > /tmp/$1-crate-source.txt
132 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
133 sed -i".original" "1i\\
135 " /tmp/$1-crate-source.txt
137 sed -i "1ipub mod $2 {\n" /tmp/$1-crate-source.txt
139 echo "}" >> /tmp/$1-crate-source.txt
140 cat /tmp/$1-crate-source.txt >> /tmp/crate-source.txt
141 rm /tmp/$1-crate-source.txt
142 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
143 # OSX sed is for some reason not compatible with GNU sed
144 sed -E -i '' 's|#*'$1' = \{ .*|'$1' = \{ path = "'"$LIGHTNING_PATH"'/'$1'", default-features = false }|' lightning-c-bindings/Cargo.toml
146 sed -E -i 's|#*'$1' = \{ .*|'$1' = \{ path = "'"$LIGHTNING_PATH"'/'$1'", default-features = false }|' lightning-c-bindings/Cargo.toml
150 function drop_crate() {
151 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
152 # OSX sed is for some reason not compatible with GNU sed
153 sed -E -i '' 's|'$1' = \{ (.*)|#'$1' = \{ \1|' lightning-c-bindings/Cargo.toml
155 sed -E -i 's|'$1' = \{ (.*)|#'$1' = \{ \1|' lightning-c-bindings/Cargo.toml
159 echo > /tmp/crate-source.txt
160 if [ "$2" = "true" ]; then
161 add_crate lightning lightning --features=std
162 add_crate "lightning-persister" "lightning_persister"
163 add_crate "lightning-background-processor" "lightning_background_processor"
164 add_crate "lightning-invoice" "lightning_invoice"
165 CARGO_BUILD_ARGS="--features=std"
167 add_crate lightning lightning --features=no-std
168 drop_crate "lightning-persister"
169 drop_crate "lightning-background-processor"
170 drop_crate "lightning-invoice"
171 CARGO_BUILD_ARGS="--features=no-std"
174 cat /tmp/crate-source.txt | RUST_BACKTRACE=1 "$BIN" "$OUT/" "$OUT_TEMPL" "$OUT_F" "$OUT_CPP"
176 echo -e '#[no_mangle]' >> lightning-c-bindings/src/version.rs
177 echo -e 'pub extern "C" fn _ldk_get_compiled_version() -> crate::c_types::Str {' >> lightning-c-bindings/src/version.rs
178 echo -e '\t"'"$LIGHTNING_GIT"'".into()' >> lightning-c-bindings/src/version.rs
179 echo -e '}' >> lightning-c-bindings/src/version.rs
180 echo -e '#[no_mangle]' >> lightning-c-bindings/src/version.rs
181 echo -e 'pub extern "C" fn _ldk_c_bindings_get_compiled_version() -> crate::c_types::Str {' >> lightning-c-bindings/src/version.rs
182 echo -e '\t"'"$BINDINGS_GIT"'".into()' >> lightning-c-bindings/src/version.rs
183 echo -e '}' >> lightning-c-bindings/src/version.rs
185 # Now cd to lightning-c-bindings, build the generated bindings, and call cbindgen to build a C header file
186 cd lightning-c-bindings
188 RUSTFLAGS="$RUSTFLAGS --cfg=test_mod_pointers" cargo build $CARGO_BUILD_ARGS
189 if [ "$CFLAGS_aarch64_apple_darwin" != "" ]; then
190 RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14" cargo build $CARGO_BUILD_ARGS --target aarch64-apple-darwin
192 cbindgen -v --config cbindgen.toml -o include/lightning.h >/dev/null 2>&1
194 # cbindgen is relatively braindead when exporting typedefs -
195 # it happily exports all our typedefs for private types, even with the
196 # generics we specified in C mode! So we drop all those types manually here.
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 '' 's/typedef LDKnative.*Import.*LDKnative.*;//g' include/lightning.h
201 # stdlib.h doesn't exist in clang's wasm sysroot, and cbindgen
202 # doesn't actually use it anyway, so drop the import.
203 sed -i '' 's/#include <stdlib.h>/#include "ldk_rust_types.h"/g' include/lightning.h
205 sed -i 's/typedef LDKnative.*Import.*LDKnative.*;//g' include/lightning.h
207 # stdlib.h doesn't exist in clang's wasm sysroot, and cbindgen
208 # doesn't actually use it anyway, so drop the import.
209 sed -i 's/#include <stdlib.h>/#include "ldk_rust_types.h"/g' include/lightning.h
212 # Build C++ class methods which call trait methods
213 set +x # Echoing every command is very verbose here
216 echo '#include <string.h>' > include/lightningpp_new.hpp
217 echo 'namespace LDK {' >> include/lightningpp_new.hpp
218 echo '// Forward declarations' >> include/lightningpp_new.hpp
219 cat include/lightningpp.hpp | sed -n 's/class \(.*\) {/class \1;/p' >> include/lightningpp_new.hpp
220 echo '' >> include/lightningpp_new.hpp
225 "#include <string.h>")
226 # We already printed this above.
229 # We already printed this above.
232 # We'll print this at the end
235 NEW_STRUCT_NAME="$(echo "$LINE" | awk '{ print $2 }')"
236 if [ "$NEW_STRUCT_NAME" != "$STRUCT_NAME" ]; then
237 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 "}")"
239 STRUCT_NAME="$NEW_STRUCT_NAME"
240 METHOD_NAME="$(echo "$LINE" | awk '{ print $3 }')"
241 METHOD="$(echo "$STRUCT_CONTENTS" | grep "(\*$METHOD_NAME)")"
242 if [ "$METHOD" = "" ]; then
243 echo "Unable to find method declaration for $LINE"
246 RETVAL="$(echo "$METHOD" | sed 's/[ ]*\([A-Za-z0-9 _]*\)(\*\(.*\)).*/\1/' | sed 's/^struct LDK/LDK::/g' | tr -d ' ')"
247 [ "$RETVAL" = "LDK::SecretKey" ] && RETVAL="LDKSecretKey"
248 [ "$RETVAL" = "LDK::PublicKey" ] && RETVAL="LDKPublicKey"
249 [ "$RETVAL" = "LDK::ThirtyTwoBytes" ] && RETVAL="LDKThirtyTwoBytes"
250 PARAMS="$(echo "$METHOD" | sed 's/.*(\*.*)(\(const \)*void \*this_arg\(, \)*\(.*\));/\3/')"
252 echo -e "\tinline $RETVAL $METHOD_NAME($PARAMS);" >> include/lightningpp_new.hpp
253 DECLS="$DECLS"$'\n'"inline $RETVAL $STRUCT_NAME::$METHOD_NAME($PARAMS) {"
255 DECLS="$DECLS"$'\n'$'\t'
256 [ "$RETVAL" != "void" ] && DECLS="$DECLS$RETVAL ret = "
257 DECLS="$DECLS(self.$METHOD_NAME)(self.this_arg"
259 IFS=','; for PARAM in $PARAMS; do
261 DECLS="$DECLS$(echo $PARAM | sed 's/.* (*\**\([a-zA-Z0-9_]*\)\()[\[0-9\]*]\)*/\1/')"
266 [ "$RETVAL" != "void" ] && DECLS="$DECLS"$'\n'$'\t'"return ret;"
267 DECLS="$DECLS"$'\n'"}"
270 echo "$LINE" >> include/lightningpp_new.hpp
272 done < include/lightningpp.hpp
273 echo "$DECLS" >> include/lightningpp_new.hpp
274 echo "}" >> include/lightningpp_new.hpp
275 export IFS="$OLD_IFS"
277 mv include/lightningpp_new.hpp include/lightningpp.hpp
279 # Finally, sanity-check the generated C and C++ bindings with demo apps:
280 # Naively run the C demo app:
281 gcc $LOCAL_CFLAGS -Wall -g -pthread demo.c target/debug/libldk.a -ldl -lm
284 # And run the C++ demo app
285 if [ "$2" = "true" ]; then
286 g++ $LOCAL_CFLAGS -std=c++11 -Wall -g -pthread demo.cpp -Ltarget/debug/ -lldk -ldl
287 LD_LIBRARY_PATH=target/debug/ ./a.out > /dev/null
290 # Finally, run the C++ demo app with our native networking library
291 # in valgrind to test memory model correctness and lack of leaks.
292 gcc $LOCAL_CFLAGS -fPIC -std=c99 -Wall -g -pthread -I../ldk-net ../ldk-net/ldk_net.c -c -o ldk_net.o
293 if [ "$2" = "true" ]; then
294 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
295 if [ -x "`which valgrind`" ]; then
296 valgrind --error-exitcode=4 --memcheck:leak-check=full --show-leak-kinds=all ./a.out
299 echo "WARNING: Please install valgrind for more testing"
305 # Test a statically-linked C++ version, tracking the resulting binary size and runtime
306 # across debug, LTO, and cross-language LTO builds (using the same compiler each time).
307 if [ "$2" = "true" ]; then
308 clang++ $LOCAL_CFLAGS -std=c++11 demo.cpp target/debug/libldk.a -ldl
310 echo " C++ Bin size and runtime w/o optimization:"
312 time ./a.out > /dev/null
315 # Then, check with memory sanitizer, if we're on Linux and have rustc nightly
316 if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" ]; then
317 if cargo +nightly --version >/dev/null 2>&1; then
318 LLVM_V=$(rustc +nightly --version --verbose | grep "LLVM version" | awk '{ print substr($3, 0, 2); }')
319 if [ -x "$(which clang-$LLVM_V)" ]; then
321 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
322 mv target/x86_64-unknown-linux-gnu/debug/libldk.* target/debug/
324 # Sadly, std doesn't seem to compile into something that is memsan-safe as of Aug 2020,
325 # so we'll always fail, not to mention we may be linking against git rustc LLVM which
326 # may differ from clang-llvm, so just allow everything here to fail.
329 # First the C demo app...
330 clang-$LLVM_V $LOCAL_CFLAGS -fsanitize=memory -fsanitize-memory-track-origins -g demo.c target/debug/libldk.a -ldl
333 if [ "$2" = "true" ]; then
334 # ...then the C++ demo app
335 clang++-$LLVM_V $LOCAL_CFLAGS -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -g demo.cpp target/debug/libldk.a -ldl
338 # ...then the C++ demo app with the ldk_net network implementation
339 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
340 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
344 # restore exit-on-failure
347 echo "WARNING: Can't use memory sanitizer without clang-$LLVM_V"
350 echo "WARNING: Can't use memory sanitizer without rustc nightly"
353 echo "WARNING: Can't use memory sanitizer on non-Linux, non-x86 platforms"
356 RUSTC_LLVM_V=$(rustc --version --verbose | grep "LLVM version" | awk '{ print substr($3, 0, 2); }')
358 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
359 # Apple is special, as always, and their versions of clang aren't
360 # compatible with upstream LLVM.
361 if [ "$(clang --version | grep 'Apple clang')" != "" ]; then
362 echo "Apple clang isn't compatible with upstream clang, install upstream clang"
365 CLANG_LLVM_V=$(clang --version | head -n1 | awk '{ print substr($3, 0, 2); }')
366 if [ -x "$(which ld64.lld)" ]; then
367 LLD_LLVM_V="$(ld64.lld --version | awk '{ print substr($2, 0, 2); }')"
371 CLANG_LLVM_V=$(clang --version | head -n1 | awk '{ print substr($4, 0, 2); }')
372 if [ -x "$(which ld.lld)" ]; then
373 LLD_LLVM_V="$(ld.lld --version | awk '{ print $2; }')"
374 if [ "$LLD_LLVM_V" = "LLD" ]; then # eg if the output is "Debian LLD ..."
375 LLD_LLVM_V="$(ld.lld --version | awk '{ print substr($3, 0, 2); }')"
377 LLD_LLVM_V="$(ld.lld --version | awk '{ print substr($2, 0, 2); }')"
383 if [ "$CLANG_LLVM_V" = "$RUSTC_LLVM_V" ]; then
386 if [ "$LLD_LLVM_V" = "$CLANG_LLVM_V" ]; then
389 elif [ -x "$(which clang-$RUSTC_LLVM_V)" ]; then
390 CLANG="$(which clang-$RUSTC_LLVM_V)"
391 CLANGPP="$(which clang++-$RUSTC_LLVM_V || echo clang++)"
392 if [ "$($CLANG --version)" != "$($CLANGPP --version)" ]; then
393 echo "$CLANG and $CLANGPP are not the same version of clang!"
397 if [ "$LLD_LLVM_V" != "$RUSTC_LLVM_V" ]; then
399 [ -x "$(which lld-$RUSTC_LLVM_V)" ] && LLD="lld-$RUSTC_LLVM_V"
400 LLD_LLVM_V="$(ld.lld-$RUSTC_LLVM_V --version | awk '{ print $2; }')"
401 if [ "$LLD_LLVM_V" = "LLD" ]; then # eg if the output is "Debian LLD ..."
402 LLD_LLVM_V="$(ld.lld-$RUSTC_LLVM_V --version | awk '{ print substr($3, 0, 2); }')"
404 LLD_LLVM_V="$(ld.lld-$RUSTC_LLVM_V --version | awk '{ print substr($2, 0, 2); }')"
406 if [ "$LLD_LLVM_V" != "$RUSTC_LLVM_V" ]; then
407 echo "Could not find a workable version of lld, not using cross-language LTO"
413 if [ "$CLANG" != "" -a "$CLANGPP" = "" ]; then
414 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"
415 echo "You should create a symlink called clang++-$RUSTC_LLVM_V pointing to $CLANG in $(dirname $CLANG)"
418 # Finally, if we're on OSX or on Linux, build the final debug binary with address sanitizer (and leave it there)
419 if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" -o "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
420 if [ "$CLANGPP" != "" ]; then
421 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
422 # OSX sed is for some reason not compatible with GNU sed
423 sed -i .bk 's/,"cdylib"]/]/g' Cargo.toml
425 sed -i.bk 's/,"cdylib"]/]/g' Cargo.toml
427 if [ "$CFLAGS_aarch64_apple_darwin" != "" ]; then
428 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)
430 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)
431 mv Cargo.toml.bk Cargo.toml
433 # First the C demo app...
434 $CLANG $LOCAL_CFLAGS -fsanitize=address -g demo.c target/debug/libldk.a -ldl
435 ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out
437 if [ "$2" = "true" ]; then
438 # ...then the C++ demo app
439 $CLANGPP $LOCAL_CFLAGS -std=c++11 -fsanitize=address -g demo.cpp target/debug/libldk.a -ldl
440 ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out >/dev/null
442 # ...then the C++ demo app with the ldk_net network implementation
443 $CLANG $LOCAL_CFLAGS -fPIC -fsanitize=address -g -I../ldk-net ../ldk-net/ldk_net.c -c -o ldk_net.o
444 $CLANGPP $LOCAL_CFLAGS -std=c++11 -fsanitize=address -g -DREAL_NET -I../ldk-net ldk_net.o demo.cpp target/debug/libldk.a -ldl
445 ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out >/dev/null
448 echo "WARNING: Please install clang-$RUSTC_LLVM_V and clang++-$RUSTC_LLVM_V to build with address sanitizer"
451 echo "WARNING: Can't use address sanitizer on non-Linux, non-OSX non-x86 platforms"
454 # Now build with LTO on on both C++ and rust, but without cross-language LTO:
455 # Clear stale release build artifacts from previous runs
456 cargo clean --release
457 CARGO_PROFILE_RELEASE_LTO=true cargo rustc $CARGO_BUILD_ARGS -v --release -- -C lto
458 if [ "$2" = "true" ]; then
459 clang++ $LOCAL_CFLAGS -std=c++11 -O2 demo.cpp target/release/libldk.a -ldl
463 echo "C++ Bin size and runtime with only RL (LTO) optimized:"
465 time ./a.out > /dev/null
467 if [ "$CLANGPP" != "" ]; then
468 # If we can use cross-language LTO, use it for building C dependencies (i.e. libsecp256k1) as well
470 # The cc-rs crate tries to force -fdata-sections and -ffunction-sections on, which
471 # breaks -fembed-bitcode, so we turn off cc-rs' default flags and specify exactly
473 export CFLAGS_$ENV_TARGET="$BASE_CFLAGS -fPIC -fembed-bitcode -march=sandybridge -mcpu=sandybridge -mtune=sandybridge"
474 export CRATE_CC_NO_DEFAULTS=true
477 if [ "$2" = "false" -a "$(rustc --print target-list | grep wasm32-wasi)" != "" ]; then
478 # Test to see if clang supports wasm32 as a target (which is needed to build rust-secp256k1)
479 echo "int main() {}" > genbindings_wasm_test_file.c
480 if clang -nostdlib -o /dev/null --target=wasm32-wasi -Wl,--no-entry genbindings_wasm_test_file.c > /dev/null 2>&1; then
481 # And if it does, build a WASM binary without capturing errors
482 export CFLAGS_wasm32_wasi="$BASE_CFLAGS -target wasm32"
483 RUSTFLAGS="$RUSTFLAGS --cfg=test_mod_pointers" cargo rustc $CARGO_BUILD_ARGS -v --target=wasm32-wasi
484 export CFLAGS_wasm32_wasi="$BASE_CFLAGS -target wasm32 -Os"
485 CARGO_PROFILE_RELEASE_LTO=true cargo rustc $CARGO_BUILD_ARGS -v --release --target=wasm32-wasi -- -C embed-bitcode=yes -C opt-level=s -C linker-plugin-lto -C lto
487 echo "Cannot build WASM lib as clang does not seem to support the wasm32-wasi target"
489 rm genbindings_wasm_test_file.c
492 EXTRA_TARGETS=( $LDK_C_BINDINGS_EXTRA_TARGETS )
493 EXTRA_CCS=( $LDK_C_BINDINGS_EXTRA_TARGET_CCS )
495 if [ ${#EXTRA_TARGETS[@]} != ${#EXTRA_CCS[@]} ]; then
496 echo "LDK_C_BINDINGS_EXTRA_TARGETS and LDK_C_BINDINGS_EXTRA_TARGET_CCS didn't have the same number of elements!"
500 for IDX in ${!EXTRA_TARGETS[@]}; do
501 EXTRA_ENV_TARGET=$(echo "${EXTRA_TARGETS[$IDX]}" | sed 's/-/_/g')
502 export CFLAGS_$EXTRA_ENV_TARGET="$BASE_CFLAGS"
503 export CC_$EXTRA_ENV_TARGET=${EXTRA_CCS[$IDX]}
504 RUSTFLAGS="$BASE_RUSTFLAGS -C linker=${EXTRA_CCS[$IDX]}" CARGO_PROFILE_RELEASE_LTO=true cargo rustc $CARGO_BUILD_ARGS -v --release --target "${EXTRA_TARGETS[$IDX]}" -- -C lto
507 if [ "$CLANGPP" != "" -a "$LLD" != "" ]; then
508 # Finally, test cross-language LTO. Note that this will fail if rustc and clang++
509 # build against different versions of LLVM (eg when rustc is installed via rustup
510 # or Ubuntu packages). This should work fine on Distros which do more involved
511 # packaging than simply shipping the rustup binaries (eg Debian should Just Work
513 LINK_ARG_FLAGS="-C link-arg=-fuse-ld=$LLD"
514 if [ "$HOST_PLATFORM" = "host: x86_64-apple-darwin" ]; then
515 export LDK_CLANG_PATH=$(which $CLANG)
516 export CLANG="$(pwd)/../deterministic-build-wrappers/clang-lto-link-osx"
517 for ARG in "CFLAGS_aarch64_apple_darwin"; do
518 MANUAL_LINK_CFLAGS="$MANUAL_LINK_CFLAGS -C link-arg=$ARG"
520 export CFLAGS_aarch64_apple_darwin="$CFLAGS_aarch64_apple_darwin -O3 -fPIC -fembed-bitcode"
521 LINK_ARG_FLAGS="$LINK_ARG_FLAGS -C link-arg="-isysroot$(xcrun --show-sdk-path)" -C link-arg=-mmacosx-version-min=10.9"
522 RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14" CARGO_PROFILE_RELEASE_LTO=true cargo rustc $CARGO_BUILD_ARGS -v --release --target aarch64-apple-darwin -- -C linker-plugin-lto -C lto -C linker=$CLANG $LINK_ARG_FLAGS -C link-arg=-mcpu=apple-a14
524 export CFLAGS_$ENV_TARGET="$BASE_CFLAGS -O3 -fPIC -fembed-bitcode -march=sandybridge -mcpu=sandybridge -mtune=sandybridge"
525 # Rust doesn't recognize CFLAGS changes, so we need to clean build artifacts
526 cargo clean --release
527 CARGO_PROFILE_RELEASE_LTO=true cargo rustc $CARGO_BUILD_ARGS -v --release -- -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
529 if [ "$2" = "true" ]; then
530 $CLANGPP $LOCAL_CFLAGS -flto -fuse-ld=$LLD -O2 demo.cpp target/release/libldk.a -ldl
532 echo "C++ Bin size and runtime with cross-language LTO:"
534 time ./a.out > /dev/null
537 if [ "$CFLAGS_aarch64_apple_darwin" != "" ]; then
538 RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14" CARGO_PROFILE_RELEASE_LTO=true cargo rustc $CARGO_BUILD_ARGS -v --release --target aarch64-apple-darwin -- -C lto
540 echo "WARNING: Building with cross-language LTO is not avilable without clang-$RUSTC_LLVM_V"