Skip to content

Commit eaec5c5

Browse files
committed
Fix shellcheck warnings for genbindings.sh
1 parent 21e6ec3 commit eaec5c5

File tree

1 file changed

+79
-69
lines changed

1 file changed

+79
-69
lines changed

genbindings.sh

Lines changed: 79 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
set -e
44
set -x
55

6-
if [ ! -d "$1/lightning" -o "$2" != "true" -a "$2" != "false" ]; then
6+
if [ ! -d "$1/lightning" ] || [ "$2" != "true" ] && [ "$2" != "false" ]; then
77
echo "USAGE: $0 path-to-rust-lightning allow-std"
88
echo "allow-std must be either 'true' or 'false' to indicate if we should be built relying on time and pthread support"
99
exit 1
@@ -12,7 +12,7 @@ fi
1212
SKIP_TESTS_ARGUMENT=$3
1313
RUN_CPP_TESTS=true
1414

15-
if [ ! -z "$SKIP_TESTS_ARGUMENT" ]; then
15+
if [ -n "$SKIP_TESTS_ARGUMENT" ]; then
1616
if [ "$SKIP_TESTS_ARGUMENT" != "skip-tests" ]; then
1717
echo "To skip tests, usage must be: $0 path-to-rust-lightning allow-std skip-tests"
1818
exit 1
@@ -34,12 +34,14 @@ cd "$ORIG_PWD"
3434

3535
# First we set various compiler flags...
3636
HOST_PLATFORM="$(rustc --version --verbose | grep "host:" | awk '{ print $2 }')"
37-
ENV_TARGET=$(echo $HOST_PLATFORM | sed 's/-/_/g')
37+
ENV_TARGET=${HOST_PLATFORM//-/_}
3838

3939
# Set path to include our rustc wrapper as well as cbindgen
40-
export LDK_RUSTC_PATH="$(which rustc)"
41-
export RUSTC="$(pwd)/deterministic-build-wrappers/rustc"
42-
PATH="$PATH:~/.cargo/bin"
40+
LDK_RUSTC_PATH="$(which rustc)"
41+
export LDK_RUSTC_PATH
42+
RUSTC="$(pwd)/deterministic-build-wrappers/rustc"
43+
export RUSTC
44+
PATH="$PATH:$HOME/.cargo/bin"
4345

4446
# Set up CFLAGS and RUSTFLAGS vars appropriately for building libsecp256k1 and demo apps...
4547
BASE_CFLAGS="" # CFLAGS for libsecp256k1
@@ -50,7 +52,7 @@ BASE_RUSTFLAGS="--cfg=c_bindings --remap-path-prefix $LIGHTNING_PATH=rust-lightn
5052

5153
# If the C compiler supports it, also set -ffile-prefix-map
5254
echo "int main() {}" > genbindings_path_map_test_file.c
53-
clang -o /dev/null -ffile-prefix-map=$HOME/.cargo= genbindings_path_map_test_file.c > /dev/null 2>&1 &&
55+
clang -o /dev/null -ffile-prefix-map="$HOME"/.cargo= genbindings_path_map_test_file.c > /dev/null 2>&1 &&
5456
export BASE_CFLAGS="-ffile-prefix-map=$HOME/.cargo="
5557

5658
BASE_CFLAGS="$BASE_CFLAGS -frandom-seed=42"
@@ -65,7 +67,7 @@ fi
6567

6668
BASE_HOST_CFLAGS="$BASE_CFLAGS"
6769

68-
if [ "$MACOS_SDK" = "" -a "$HOST_OSX" = "true" ]; then
70+
if [ "$MACOS_SDK" = "" ] && [ "$HOST_OSX" = "true" ]; then
6971
MACOS_SDK="$(xcrun --show-sdk-path)"
7072
[ "$MACOS_SDK" = "" ] && exit 1
7173
fi
@@ -87,18 +89,18 @@ case "$ENV_TARGET" in
8789
"x86_64"*)
8890
export RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=sandybridge"
8991
export BASE_HOST_CFLAGS="$BASE_HOST_CFLAGS -march=sandybridge -mtune=sandybridge"
90-
export CFLAGS_$ENV_TARGET="$BASE_HOST_CFLAGS"
92+
export "CFLAGS_$ENV_TARGET"="$BASE_HOST_CFLAGS"
9193
;;
9294
"aarch64_apple_darwin")
9395
export RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14"
9496
export BASE_HOST_CFLAGS="$BASE_HOST_CFLAGS -mcpu=apple-a14"
95-
export CFLAGS_$ENV_TARGET="$BASE_HOST_CFLAGS"
97+
export "CFLAGS_$ENV_TARGET"="$BASE_HOST_CFLAGS"
9698
;;
9799
*)
98100
# Assume this isn't targeted at another host and build for the host's CPU.
99101
export RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=native"
100102
export BASE_HOST_CFLAGS="$BASE_HOST_CFLAGS -march=native -mtune=native"
101-
export CFLAGS_$ENV_TARGET="$BASE_HOST_CFLAGS"
103+
export "CFLAGS_$ENV_TARGET"="$BASE_HOST_CFLAGS"
102104
;;
103105
esac
104106

@@ -168,32 +170,32 @@ function is_gnu_sed(){
168170

169171
function add_crate() {
170172
pushd "$LIGHTNING_PATH/$1"
171-
RUSTC_BOOTSTRAP=1 cargo rustc --profile=check -Z avoid-dev-deps --no-default-features $3 -- --cfg=c_bindings -Zunpretty=expanded > /tmp/$1-crate-source.txt
173+
RUSTC_BOOTSTRAP=1 cargo rustc --profile=check -Z avoid-dev-deps --no-default-features "$3" -- --cfg=c_bindings -Zunpretty=expanded > "/tmp/$1-crate-source.txt"
172174
popd
173175
if [ "$HOST_OSX" = "true" ]; then
174176
sed -i".original" "1i\\
175177
pub mod $2 {
176-
" /tmp/$1-crate-source.txt
178+
" "/tmp/$1-crate-source.txt"
177179
else
178-
sed -i "1ipub mod $2 {\n" /tmp/$1-crate-source.txt
180+
sed -i "1ipub mod $2 {\n" "/tmp/$1-crate-source.txt"
179181
fi
180-
echo "}" >> /tmp/$1-crate-source.txt
181-
cat /tmp/$1-crate-source.txt >> /tmp/crate-source.txt
182-
rm /tmp/$1-crate-source.txt
182+
echo "}" >> "/tmp/$1-crate-source.txt"
183+
cat "/tmp/$1-crate-source.txt" >> /tmp/crate-source.txt
184+
rm "/tmp/$1-crate-source.txt"
183185
if is_gnu_sed; then
184-
sed -E -i 's|#*'$1' = \{ .*|'$1' = \{ path = "'"$LIGHTNING_PATH"'/'$1'", default-features = false }|' lightning-c-bindings/Cargo.toml
186+
sed -E -i 's|#*'"$1"' = \{ .*|'"$1"' = \{ path = "'"$LIGHTNING_PATH"'/'"$1"'", default-features = false }|' lightning-c-bindings/Cargo.toml
185187
else
186188
# OSX sed is for some reason not compatible with GNU sed
187-
sed -E -i '' 's|#*'$1' = \{ .*|'$1' = \{ path = "'"$LIGHTNING_PATH"'/'$1'", default-features = false }|' lightning-c-bindings/Cargo.toml
189+
sed -E -i '' 's|#*'"$1"' = \{ .*|'"$1"' = \{ path = "'"$LIGHTNING_PATH"'/'"$1"'", default-features = false }|' lightning-c-bindings/Cargo.toml
188190
fi
189191
}
190192

191193
function drop_crate() {
192194
if is_gnu_sed; then
193-
sed -E -i 's|'$1' = \{ (.*)|#'$1' = \{ \1|' lightning-c-bindings/Cargo.toml
195+
sed -E -i 's|'"$1"' = \{ (.*)|#'"$1"' = \{ \1|' lightning-c-bindings/Cargo.toml
194196
else
195197
# OSX sed is for some reason not compatible with GNU sed
196-
sed -E -i '' 's|'$1' = \{ (.*)|#'$1' = \{ \1|' lightning-c-bindings/Cargo.toml
198+
sed -E -i '' 's|'"$1"' = \{ (.*)|#'"$1"' = \{ \1|' lightning-c-bindings/Cargo.toml
197199
fi
198200
}
199201

@@ -232,7 +234,7 @@ EOF
232234
cd lightning-c-bindings
233235

234236
RUSTFLAGS="$RUSTFLAGS --cfg=test_mod_pointers" cargo build $CARGO_BUILD_ARGS
235-
if [ "$CFLAGS_aarch64_apple_darwin" != "" -a "$HOST_OSX" = "true" ]; then
237+
if [ "$CFLAGS_aarch64_apple_darwin" != "" ] && [ "$HOST_OSX" = "true" ]; then
236238
RUSTFLAGS="$BASE_RUSTFLAGS -C target-cpu=apple-a14" cargo build $CARGO_BUILD_ARGS --target aarch64-apple-darwin
237239
fi
238240
cbindgen -v --config cbindgen.toml -o include/lightning.h >/dev/null 2>&1
@@ -266,14 +268,16 @@ echo "Updating C++ header, this may take some time, especially on macOS"
266268
set +x # Echoing every command is very verbose here
267269
OLD_IFS="$IFS"
268270
export IFS=''
269-
echo '#include <string.h>' > include/lightningpp_new.hpp
270-
echo 'namespace LDK {' >> include/lightningpp_new.hpp
271-
echo '// Forward declarations' >> include/lightningpp_new.hpp
271+
{
272+
echo '#include <string.h>'
273+
echo 'namespace LDK {'
274+
echo '// Forward declarations'
275+
} > include/lightningpp_new.hpp
272276
cat include/lightningpp.hpp | sed -n 's/class \(.*\) {/class \1;/p' >> include/lightningpp_new.hpp
273277
echo '' >> include/lightningpp_new.hpp
274278

275279
DECLS=""
276-
while read LINE; do
280+
while read -r LINE; do
277281
case "$LINE" in
278282
"#include <string.h>")
279283
# We already printed this above.
@@ -311,7 +315,7 @@ while read LINE; do
311315

312316
IFS=','; for PARAM in $PARAMS; do
313317
DECLS="$DECLS, "
314-
DECLS="$DECLS$(echo $PARAM | sed 's/.* (*\**\([a-zA-Z0-9_]*\)\()[\[0-9\]*]\)*/\1/')"
318+
DECLS="$DECLS$(echo "$PARAM" | sed 's/.* (*\**\([a-zA-Z0-9_]*\)\()[\[0-9\]*]\)*/\1/')"
315319
done
316320
IFS=''
317321

@@ -333,21 +337,21 @@ mv include/lightningpp_new.hpp include/lightningpp.hpp
333337
if $RUN_CPP_TESTS; then
334338
# Finally, sanity-check the generated C and C++ bindings with demo apps:
335339
# Naively run the C demo app:
336-
gcc $LOCAL_CFLAGS -Wall -g -pthread demo.c target/debug/libldk.a -ldl -lm
340+
gcc "$LOCAL_CFLAGS" -Wall -g -pthread demo.c target/debug/libldk.a -ldl -lm
337341
./a.out
338342

339343
# And run the C++ demo app
340344
if [ "$2" = "true" ]; then
341-
g++ $LOCAL_CFLAGS -std=c++11 -Wall -g -pthread demo.cpp -Ltarget/debug/ -lldk -ldl
345+
g++ "$LOCAL_CFLAGS" -std=c++11 -Wall -g -pthread demo.cpp -Ltarget/debug/ -lldk -ldl
342346
LD_LIBRARY_PATH=target/debug/ ./a.out > /dev/null
343347
fi
344348

345349
# Finally, run the C++ demo app with our native networking library
346350
# in valgrind to test memory model correctness and lack of leaks.
347-
gcc $LOCAL_CFLAGS -fPIC -std=c99 -Wall -g -pthread -I../ldk-net ../ldk-net/ldk_net.c -c -o ldk_net.o
351+
gcc "$LOCAL_CFLAGS" -fPIC -std=c99 -Wall -g -pthread -I../ldk-net ../ldk-net/ldk_net.c -c -o ldk_net.o
348352
if [ "$2" = "true" ]; then
349-
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
350-
if [ -x "`which valgrind`" -a "$(uname -m)" != "ppc64le" ]; then
353+
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
354+
if [ -x "$(which valgrind)" ] && [ "$(uname -m)" != "ppc64le" ]; then
351355
valgrind --error-exitcode=4 --memcheck:leak-check=full --show-leak-kinds=all ./a.out
352356
echo
353357
else
@@ -360,7 +364,7 @@ if $RUN_CPP_TESTS; then
360364
# Test a statically-linked C++ version, tracking the resulting binary size and runtime
361365
# across debug, LTO, and cross-language LTO builds (using the same compiler each time).
362366
if [ "$2" = "true" ]; then
363-
clang++ $LOCAL_CFLAGS -std=c++11 demo.cpp target/debug/libldk.a -ldl
367+
clang++ "$LOCAL_CFLAGS" -std=c++11 demo.cpp target/debug/libldk.a -ldl
364368
strip ./a.out
365369
time ./a.out
366370
echo " C++ Bin size and runtime w/o optimization:"
@@ -385,8 +389,12 @@ function REALLY_PIN_CC {
385389
# registry and build --offline to avoid it using the latest version.
386390
NEW_CC_DEP="$CARGO_HOME"
387391
[ "$NEW_CC_DEP" = "" ] && NEW_CC_DEP="$HOME"
388-
[ -d "$NEW_CC_DEP/.cargo/registry/cache/"github.com-* ] && CARGO_REGISTRY_CACHE="$(echo "$NEW_CC_DEP/.cargo/registry/cache/"github.com-*)"
389-
[ -d "$NEW_CC_DEP/.cargo/registry/cache/"index.crates.io-* ] && CARGO_REGISTRY_CACHE="$(echo "$NEW_CC_DEP/.cargo/registry/cache/"index.crates.io-*)"
392+
for dir in "$NEW_CC_DEP/.cargo/registry/cache/"github.com-*; do
393+
CARGO_REGISTRY_CACHE="$dir"
394+
done
395+
for dir in "$NEW_CC_DEP/.cargo/registry/cache/"index.crates.io-*; do
396+
CARGO_REGISTRY_CACHE="$dir"
397+
done
390398
if [ -d "$CARGO_REGISTRY_CACHE" ]; then
391399
if [ -f "$CARGO_REGISTRY_CACHE/compiler_builtins-0.1.109.crate" ]; then
392400
mv "$CARGO_REGISTRY_CACHE/compiler_builtins-0.1.109.crate" ./
@@ -407,11 +415,11 @@ function REALLY_PIN_CC {
407415
if [ "$HOST_PLATFORM" = "x86_64-unknown-linux-gnu" ]; then
408416
if cargo +nightly --version >/dev/null 2>&1; then
409417
LLVM_V=$(rustc +nightly --version --verbose | grep "LLVM version" | awk '{ print substr($3, 0, 2); }')
410-
if [ -x "$(which clang-$LLVM_V)" ]; then
418+
if [ -x "$(which clang-"$LLVM_V")" ]; then
411419
cargo +nightly clean
412420

413421
REALLY_PIN_CC
414-
cargo +nightly rustc --offline $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
422+
cargo +nightly rustc --offline "$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
415423
mv target/x86_64-unknown-linux-gnu/debug/libldk.* target/debug/
416424

417425
# Sadly, std doesn't seem to compile into something that is memsan-safe as of Aug 2020,
@@ -420,17 +428,17 @@ if [ "$HOST_PLATFORM" = "x86_64-unknown-linux-gnu" ]; then
420428
set +e
421429

422430
# First the C demo app...
423-
clang-$LLVM_V $LOCAL_CFLAGS -fsanitize=memory -fsanitize-memory-track-origins -g demo.c target/debug/libldk.a -ldl
431+
"clang-$LLVM_V" "$LOCAL_CFLAGS" -fsanitize=memory -fsanitize-memory-track-origins -g demo.c target/debug/libldk.a -ldl
424432
./a.out
425433

426434
if [ "$2" = "true" ]; then
427435
# ...then the C++ demo app
428-
clang++-$LLVM_V $LOCAL_CFLAGS -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -g demo.cpp target/debug/libldk.a -ldl
436+
"clang++-$LLVM_V" "$LOCAL_CFLAGS" -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -g demo.cpp target/debug/libldk.a -ldl
429437
./a.out >/dev/null
430438

431439
# ...then the C++ demo app with the ldk_net network implementation
432-
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
433-
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
440+
"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
441+
"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
434442
./a.out >/dev/null
435443
fi
436444

@@ -481,22 +489,22 @@ if [ "$CLANG_LLVM_V" = "$RUSTC_LLVM_V" ]; then
481489
if [ "$LLD_LLVM_V" = "$CLANG_LLVM_V" ]; then
482490
LLD=lld
483491
fi
484-
elif [ -x "$(which clang-$RUSTC_LLVM_V)" ]; then
485-
CLANG="$(which clang-$RUSTC_LLVM_V)"
486-
CLANGPP="$(which clang++-$RUSTC_LLVM_V || echo clang++)"
492+
elif [ -x "$(which "clang-$RUSTC_LLVM_V")" ]; then
493+
CLANG="$(which "clang-$RUSTC_LLVM_V")"
494+
CLANGPP="$(which "clang++-$RUSTC_LLVM_V" || echo clang++)"
487495
if [ "$($CLANG --version)" != "$($CLANGPP --version)" ]; then
488496
echo "$CLANG and $CLANGPP are not the same version of clang!"
489497
unset CLANG
490498
unset CLANGPP
491499
fi
492500
if [ "$LLD_LLVM_V" != "$RUSTC_LLVM_V" ]; then
493501
LLD="lld"
494-
[ -x "$(which lld-$RUSTC_LLVM_V)" ] && LLD="lld-$RUSTC_LLVM_V"
495-
LLD_LLVM_V="$(ld.lld-$RUSTC_LLVM_V --version | awk '{ print $2; }')"
502+
[ -x "$(which "lld-$RUSTC_LLVM_V")" ] && LLD="lld-$RUSTC_LLVM_V"
503+
LLD_LLVM_V="$("ld.lld-$RUSTC_LLVM_V" --version | awk '{ print $2; }')"
496504
if [ "$LLD_LLVM_V" = "LLD" ]; then # eg if the output is "Debian LLD ..."
497-
LLD_LLVM_V="$(ld.lld-$RUSTC_LLVM_V --version | awk '{ print substr($3, 0, 2); }')"
505+
LLD_LLVM_V="$("ld.lld-$RUSTC_LLVM_V" --version | awk '{ print substr($3, 0, 2); }')"
498506
else
499-
LLD_LLVM_V="$(ld.lld-$RUSTC_LLVM_V --version | awk '{ print substr($2, 0, 2); }')"
507+
LLD_LLVM_V="$("ld.lld-$RUSTC_LLVM_V" --version | awk '{ print substr($2, 0, 2); }')"
500508
fi
501509
if [ "$LLD_LLVM_V" != "$RUSTC_LLVM_V" ]; then
502510
echo "Could not find a workable version of lld, not using cross-language LTO"
@@ -505,9 +513,9 @@ elif [ -x "$(which clang-$RUSTC_LLVM_V)" ]; then
505513
fi
506514
fi
507515

508-
if [ "$CLANG" != "" -a "$CLANGPP" = "" ]; then
516+
if [ "$CLANG" != "" ] && [ "$CLANGPP" = "" ]; then
509517
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"
510-
echo "You should create a symlink called clang++-$RUSTC_LLVM_V pointing to $CLANG in $(dirname $CLANG)"
518+
echo "You should create a symlink called clang++-$RUSTC_LLVM_V pointing to $CLANG in $(dirname "$CLANG")"
511519
fi
512520

513521
# Finally, if we're on Linux, build the final debug binary with address sanitizer (and leave it there)
@@ -550,9 +558,9 @@ fi
550558
# Now build with LTO on on both C++ and rust, but without cross-language LTO:
551559
# Clear stale release build artifacts from previous runs
552560
cargo clean --release
553-
CARGO_PROFILE_RELEASE_LTO=true RUSTFLAGS="$RUSTFLAGS -C embed-bitcode=yes -C lto" cargo build $CARGO_BUILD_ARGS -v --release
561+
CARGO_PROFILE_RELEASE_LTO=true RUSTFLAGS="$RUSTFLAGS -C embed-bitcode=yes -C lto" cargo build "$CARGO_BUILD_ARGS" -v --release
554562
if [ "$2" = "true" ]; then
555-
clang++ $LOCAL_CFLAGS -std=c++11 -O2 demo.cpp target/release/libldk.a -ldl
563+
clang++ "$LOCAL_CFLAGS" -std=c++11 -O2 demo.cpp target/release/libldk.a -ldl
556564
fi
557565

558566
strip ./a.out
@@ -566,17 +574,17 @@ if [ "$CLANGPP" != "" ]; then
566574
# The cc-rs crate tries to force -fdata-sections and -ffunction-sections on, which
567575
# breaks -fembed-bitcode, so we turn off cc-rs' default flags and specify exactly
568576
# what we want here.
569-
export CFLAGS_$ENV_TARGET="$BASE_HOST_CFLAGS -fPIC -fembed-bitcode"
577+
export "CFLAGS_$ENV_TARGET"="$BASE_HOST_CFLAGS -fPIC -fembed-bitcode"
570578
export CRATE_CC_NO_DEFAULTS=true
571579
fi
572580

573-
if [ "$2" = "false" -a "$(rustc --print target-list | grep wasm32-wasi)" != "" ]; then
581+
if [ "$2" = "false" ] && [ "$(rustc --print target-list | grep wasm32-wasi)" != "" ]; then
574582
# Test to see if clang supports wasm32 as a target (which is needed to build rust-secp256k1)
575583
echo "int main() {}" > genbindings_wasm_test_file.c
576584
if clang -nostdlib -o /dev/null --target=wasm32-wasi -Wl,--no-entry genbindings_wasm_test_file.c > /dev/null 2>&1; then
577585
# And if it does, build a WASM binary without capturing errors
578586
export CFLAGS_wasm32_wasi="$BASE_CFLAGS -target wasm32-wasi -O1"
579-
RUSTFLAGS="$BASE_RUSTFLAGS -C opt-level=1 --cfg=test_mod_pointers" cargo build $CARGO_BUILD_ARGS -v --target=wasm32-wasi
587+
RUSTFLAGS="$BASE_RUSTFLAGS -C opt-level=1 --cfg=test_mod_pointers" cargo build "$CARGO_BUILD_ARGS" -v --target=wasm32-wasi
580588
export CFLAGS_wasm32_wasi="$BASE_CFLAGS -fembed-bitcode -target wasm32-wasi -Oz"
581589
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
582590
else
@@ -585,41 +593,43 @@ if [ "$2" = "false" -a "$(rustc --print target-list | grep wasm32-wasi)" != "" ]
585593
rm genbindings_wasm_test_file.c
586594
fi
587595

588-
EXTRA_TARGETS=( $LDK_C_BINDINGS_EXTRA_TARGETS )
589-
EXTRA_CCS=( $LDK_C_BINDINGS_EXTRA_TARGET_CCS )
590-
EXTRA_LINK_LTO=( $LDK_C_BINDINGS_EXTRA_TARGET_LINK_LTO )
596+
EXTRA_TARGETS=( "$LDK_C_BINDINGS_EXTRA_TARGETS" )
597+
EXTRA_CCS=( "$LDK_C_BINDINGS_EXTRA_TARGET_CCS" )
598+
EXTRA_LINK_LTO=( "$LDK_C_BINDINGS_EXTRA_TARGET_LINK_LTO" )
591599

592600
if [ ${#EXTRA_TARGETS[@]} != ${#EXTRA_CCS[@]} ]; then
593601
echo "LDK_C_BINDINGS_EXTRA_TARGETS and LDK_C_BINDINGS_EXTRA_TARGET_CCS didn't have the same number of elements!"
594602
exit 1
595603
fi
596604

597-
for IDX in ${!EXTRA_TARGETS[@]}; do
598-
EXTRA_ENV_TARGET=$(echo "${EXTRA_TARGETS[$IDX]}" | sed 's/-/_/g')
599-
export CFLAGS_$EXTRA_ENV_TARGET="$BASE_CFLAGS"
600-
export CC_$EXTRA_ENV_TARGET=${EXTRA_CCS[$IDX]}
605+
for IDX in "${!EXTRA_TARGETS[@]}"; do
606+
EXTRA_ENV_TARGET="${EXTRA_TARGETS[$IDX]//-/_}"
607+
export "CFLAGS_$EXTRA_ENV_TARGET"="$BASE_CFLAGS"
608+
export "CC_$EXTRA_ENV_TARGET"="${EXTRA_CCS[$IDX]}"
601609
EXTRA_RUSTFLAGS=""
602610
case "$EXTRA_ENV_TARGET" in
603611
"x86_64"*)
604-
export CFLAGS_$EXTRA_ENV_TARGET="$BASE_CFLAGS -march=sandybridge -mtune=sandybridge"
612+
export "CFLAGS_$EXTRA_ENV_TARGET"="$BASE_CFLAGS -march=sandybridge -mtune=sandybridge"
605613
EXTRA_RUSTFLAGS="-C target-cpu=sandybridge"
606614
;;
607615
esac
608616
[ "${EXTRA_LINK_LTO[$IDX]}" != "" ] && EXTRA_RUSTFLAGS="-C linker-plugin-lto"
609617
RUSTFLAGS="$BASE_RUSTFLAGS -C embed-bitcode=yes -C lto -C linker=${EXTRA_CCS[$IDX]} $EXTRA_RUSTFLAGS" CARGO_PROFILE_RELEASE_LTO=true cargo build $CARGO_BUILD_ARGS -v --release --target "${EXTRA_TARGETS[$IDX]}"
610618
done
611619

612-
if [ "$CLANGPP" != "" -a "$LLD" != "" ]; then
620+
if [ "$CLANGPP" != "" ] && [ "$LLD" != "" ]; then
613621
# Finally, test cross-language LTO. Note that this will fail if rustc and clang++
614622
# build against different versions of LLVM (eg when rustc is installed via rustup
615623
# or Ubuntu packages). This should work fine on Distros which do more involved
616624
# packaging than simply shipping the rustup binaries (eg Debian should Just Work
617625
# here).
618626
LINK_ARG_FLAGS="-C link-arg=-fuse-ld=$LLD"
619-
export LDK_CLANG_PATH=$(which $CLANG)
627+
LDK_CLANG_PATH=$(which "$CLANG")
628+
export LDK_CLANG_PATH
620629
if [ "$MACOS_SDK" != "" ]; then
621630
REALLY_PIN_CC
622-
export CLANG="$(pwd)/../deterministic-build-wrappers/clang-lto-link-osx"
631+
CLANG="$(pwd)/../deterministic-build-wrappers/clang-lto-link-osx"
632+
export CLANG
623633
for ARG in $CFLAGS_aarch64_apple_darwin; do
624634
MANUAL_LINK_CFLAGS="$MANUAL_LINK_CFLAGS -C link-arg=$ARG"
625635
done
@@ -638,14 +648,14 @@ if [ "$CLANGPP" != "" -a "$LLD" != "" ]; then
638648
# If we're on an M1 don't bother building X86 binaries
639649
if [ "$HOST_PLATFORM" != "aarch64-apple-darwin" ]; then
640650
[ "$HOST_OSX" != "true" ] && export CLANG="$LDK_CLANG_PATH"
641-
export CFLAGS_$ENV_TARGET="$BASE_HOST_CFLAGS -O3 -fPIC -fembed-bitcode"
651+
export "CFLAGS_$ENV_TARGET"="$BASE_HOST_CFLAGS -O3 -fPIC -fembed-bitcode"
642652
# Rust doesn't recognize CFLAGS changes, so we need to clean build artifacts
643653
cargo clean --release
644654
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=-mtune=sandybridge" cargo build $CARGO_BUILD_ARGS -v --release
645655

646656
if [ "$2" = "true" ]; then
647-
$CLANGPP $LOCAL_CFLAGS -flto -fuse-ld=$LLD -O2 -c demo.cpp -o demo.o
648-
$CLANGPP $LOCAL_CFLAGS -flto -fuse-ld=$LLD -Wl,--lto-O2 -Wl,-O2 -O2 demo.o target/release/libldk.a -ldl
657+
"$CLANGPP" "$LOCAL_CFLAGS" -flto -fuse-ld="$LLD" -O2 -c demo.cpp -o demo.o
658+
"$CLANGPP" "$LOCAL_CFLAGS" -flto -fuse-ld="$LLD" -Wl,--lto-O2 -Wl,-O2 -O2 demo.o target/release/libldk.a -ldl
649659
strip ./a.out
650660
time ./a.out
651661
echo "C++ Bin size and runtime with cross-language LTO:"

0 commit comments

Comments
 (0)