Skip to content

Commit c41808a

Browse files
committed
Use cross-language LTO when building deps
1 parent bcbf891 commit c41808a

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

genbindings.sh

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ fi
8181

8282
# Finally, sanity-check the generated C and C++ bindings with demo apps:
8383

84-
CFLAGS="-Wall -Wno-nullability-completeness -pthread"
84+
LOCAL_CFLAGS="-Wall -Wno-nullability-completeness -pthread"
8585

8686
# 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
8888
./a.out
8989

9090
# 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
9292
if [ -x "`which valgrind`" ]; then
9393
LD_LIBRARY_PATH=target/debug/ valgrind --error-exitcode=4 --memcheck:leak-check=full --show-leak-kinds=all ./a.out
9494
echo
@@ -98,7 +98,7 @@ fi
9898

9999
# Test a statically-linked C++ version, tracking the resulting binary size and runtime
100100
# 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
102102
strip ./a.out
103103
echo " C++ Bin size and runtime w/o optimization:"
104104
ls -lha a.out
@@ -119,11 +119,11 @@ if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" ]; then
119119
set +e
120120

121121
# 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
123123
./a.out
124124

125125
# ...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
127127
./a.out >/dev/null
128128

129129
# restore exit-on-failure
@@ -189,11 +189,11 @@ if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" -o "$HOST_PLATFORM" = "
189189
mv Cargo.toml.bk Cargo.toml
190190

191191
# 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
193193
ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out
194194

195195
# ...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
197197
ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out >/dev/null
198198
else
199199
echo "WARNING: Please install clang-$RUSTC_LLVM_V and clang++-$RUSTC_LLVM_V to build with address sanitizer"
@@ -202,20 +202,31 @@ else
202202
echo "WARNING: Can't use address sanitizer on non-Linux, non-OSX non-x86 platforms"
203203
fi
204204

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+
205217
if [ "$(rustc --print target-list | grep wasm32-wasi)" != "" ]; then
206218
# Test to see if clang supports wasm32 as a target (which is needed to build rust-secp256k1)
207219
echo "int main() {}" > genbindings_wasm_test_file.c
208220
clang -nostdlib -o /dev/null --target=wasm32-wasi -Wl,--no-entry genbindings_wasm_test_file.c > /dev/null 2>&1 &&
209221
# And if it does, build a WASM binary without capturing errors
210222
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" &&
211225
CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release --target=wasm32-wasi -- -C opt-level=s -C linker-plugin-lto -C lto ||
212226
echo "Cannot build WASM lib as clang does not seem to support the wasm32-wasi target"
213227
rm genbindings_wasm_test_file.c
214228
fi
215229

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
219230
strip ./a.out
220231
echo "C++ Bin size and runtime with only RL (LTO) optimized:"
221232
ls -lha a.out
@@ -227,8 +238,11 @@ if [ "$HOST_PLATFORM" != "host: x86_64-apple-darwin" -a "$CLANGPP" != "" ]; then
227238
# or Ubuntu packages). This should work fine on Distros which do more involved
228239
# packaging than simply shipping the rustup binaries (eg Debian should Just Work
229240
# here).
241+
export CFLAGS="$CFLAGS -flto"
242+
# Rust doesn't recognize CFLAGS changes, so we need to clean build artifacts
243+
cargo clean --release
230244
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
232246
strip ./a.out
233247
echo "C++ Bin size and runtime with cross-language LTO:"
234248
ls -lha a.out

0 commit comments

Comments
 (0)