81
81
82
82
# Finally, sanity-check the generated C and C++ bindings with demo apps:
83
83
84
- CFLAGS =" -Wall -Wno-nullability-completeness -pthread"
84
+ LOCAL_CFLAGS =" -Wall -Wno-nullability-completeness -pthread"
85
85
86
86
# Naively run the C demo app:
87
- gcc $CFLAGS -Wall -g -pthread demo.c target/debug/libldk.a -ldl
87
+ gcc $LOCAL_CFLAGS -Wall -g -pthread demo.c target/debug/libldk.a -ldl
88
88
./a.out
89
89
90
90
# And run the C++ demo app in valgrind to test memory model correctness and lack of leaks.
91
- g++ $CFLAGS -std=c++11 -Wall -g -pthread demo.cpp -Ltarget/debug/ -lldk -ldl
91
+ g++ $LOCAL_CFLAGS -std=c++11 -Wall -g -pthread demo.cpp -Ltarget/debug/ -lldk -ldl
92
92
if [ -x " ` which valgrind` " ]; then
93
93
LD_LIBRARY_PATH=target/debug/ valgrind --error-exitcode=4 --memcheck:leak-check=full --show-leak-kinds=all ./a.out
94
94
echo
98
98
99
99
# Test a statically-linked C++ version, tracking the resulting binary size and runtime
100
100
# across debug, LTO, and cross-language LTO builds (using the same compiler each time).
101
- clang++ $CFLAGS -std=c++11 demo.cpp target/debug/libldk.a -ldl
101
+ clang++ $LOCAL_CFLAGS -std=c++11 demo.cpp target/debug/libldk.a -ldl
102
102
strip ./a.out
103
103
echo " C++ Bin size and runtime w/o optimization:"
104
104
ls -lha a.out
@@ -119,11 +119,11 @@ if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" ]; then
119
119
set +e
120
120
121
121
# First the C demo app...
122
- clang-$LLVM_V $CFLAGS -fsanitize=memory -fsanitize-memory-track-origins -g demo.c target/debug/libldk.a -ldl
122
+ clang-$LLVM_V $LOCAL_CFLAGS -fsanitize=memory -fsanitize-memory-track-origins -g demo.c target/debug/libldk.a -ldl
123
123
./a.out
124
124
125
125
# ...then the C++ demo app
126
- clang++-$LLVM_V $CFLAGS -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -g demo.cpp target/debug/libldk.a -ldl
126
+ clang++-$LLVM_V $LOCAL_CFLAGS -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -g demo.cpp target/debug/libldk.a -ldl
127
127
./a.out > /dev/null
128
128
129
129
# restore exit-on-failure
@@ -189,11 +189,11 @@ if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" -o "$HOST_PLATFORM" = "
189
189
mv Cargo.toml.bk Cargo.toml
190
190
191
191
# First the C demo app...
192
- $CLANG $CFLAGS -fsanitize=address -g demo.c target/debug/libldk.a -ldl
192
+ $CLANG $LOCAL_CFLAGS -fsanitize=address -g demo.c target/debug/libldk.a -ldl
193
193
ASAN_OPTIONS=' detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out
194
194
195
195
# ...then the C++ demo app
196
- $CLANGPP $CFLAGS -std=c++11 -fsanitize=address -g demo.cpp target/debug/libldk.a -ldl
196
+ $CLANGPP $LOCAL_CFLAGS -std=c++11 -fsanitize=address -g demo.cpp target/debug/libldk.a -ldl
197
197
ASAN_OPTIONS=' detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out > /dev/null
198
198
else
199
199
echo " WARNING: Please install clang-$RUSTC_LLVM_V and clang++-$RUSTC_LLVM_V to build with address sanitizer"
@@ -202,20 +202,31 @@ else
202
202
echo " WARNING: Can't use address sanitizer on non-Linux, non-OSX non-x86 platforms"
203
203
fi
204
204
205
+ # Now build with LTO on on both C++ and rust, but without cross-language LTO:
206
+ # Clear stale release build artifacts from previous runs
207
+ cargo clean --release
208
+ CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C lto
209
+ clang++ $LOCAL_CFLAGS -std=c++11 -flto -O2 demo.cpp target/release/libldk.a -ldl
210
+
211
+ if [ " $HOST_PLATFORM " != " host: x86_64-apple-darwin" -a " $CLANGPP " != " " ]; then
212
+ # If we can use cross-language LTO, use it for building C dependencies (i.e. libsecp256k1) as well
213
+ export CC=" $CLANG "
214
+ export CFLAGS_wasm32_wasi=" -target wasm32"
215
+ fi
216
+
205
217
if [ " $( rustc --print target-list | grep wasm32-wasi) " != " " ]; then
206
218
# Test to see if clang supports wasm32 as a target (which is needed to build rust-secp256k1)
207
219
echo " int main() {}" > genbindings_wasm_test_file.c
208
220
clang -nostdlib -o /dev/null --target=wasm32-wasi -Wl,--no-entry genbindings_wasm_test_file.c > /dev/null 2>&1 &&
209
221
# And if it does, build a WASM binary without capturing errors
210
222
cargo rustc -v --target=wasm32-wasi -- -C embed-bitcode=yes &&
223
+ # Now that we've done our last non-LTO build, turn on LTO in CFLAGS as well
224
+ export CFLAGS=" $CFLAGS -flto" &&
211
225
CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release --target=wasm32-wasi -- -C opt-level=s -C linker-plugin-lto -C lto ||
212
226
echo " Cannot build WASM lib as clang does not seem to support the wasm32-wasi target"
213
227
rm genbindings_wasm_test_file.c
214
228
fi
215
229
216
- # Now build with LTO on on both C++ and rust, but without cross-language LTO:
217
- CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C lto
218
- clang++ $CFLAGS -std=c++11 -flto -O2 demo.cpp target/release/libldk.a -ldl
219
230
strip ./a.out
220
231
echo " C++ Bin size and runtime with only RL (LTO) optimized:"
221
232
ls -lha a.out
@@ -227,8 +238,11 @@ if [ "$HOST_PLATFORM" != "host: x86_64-apple-darwin" -a "$CLANGPP" != "" ]; then
227
238
# or Ubuntu packages). This should work fine on Distros which do more involved
228
239
# packaging than simply shipping the rustup binaries (eg Debian should Just Work
229
240
# here).
241
+ export CFLAGS=" $CFLAGS -flto"
242
+ # Rust doesn't recognize CFLAGS changes, so we need to clean build artifacts
243
+ cargo clean --release
230
244
CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C linker-plugin-lto -C lto -C link-arg=-fuse-ld=lld
231
- $CLANGPP $CFLAGS -flto -fuse-ld=lld -O2 demo.cpp target/release/libldk.a -ldl
245
+ $CLANGPP $LOCAL_CFLAGS -flto -fuse-ld=lld -O2 demo.cpp target/release/libldk.a -ldl
232
246
strip ./a.out
233
247
echo " C++ Bin size and runtime with cross-language LTO:"
234
248
ls -lha a.out
0 commit comments