build-rust-crate: parallelize build_bin via Makefile jobserver

Collect binary targets into a bash associative array (name→path), then
generate a Makefile on the fly and pipe it to `make -j`. Make handles
parallel execution and the jobserver protocol natively, so rustc
invocations share the same token pool via MAKEFLAGS.

Also fix build_bin error propagation: add `|| return 1` to the rustc
invocation so failures are not silently swallowed when the crate name
has no hyphens and the mv rename is skipped.
This commit is contained in:
Luka Blaskovic
2023-04-21 04:19:32 +00:00
committed by Luka Blašković
parent 822bb2a0c5
commit 365abadbdd
2 changed files with 23 additions and 4 deletions
@@ -162,6 +162,8 @@ in
fi
''}
declare -A BINS
${lib.optionalString (lib.length crateBin > 0) (
lib.concatMapStringsSep "\n" (
bin:
@@ -188,7 +190,7 @@ in
BIN_PATH='${bin.path}'
''
}
${build_bin} "$BIN_NAME" "$BIN_PATH"
BINS["$BIN_NAME"]="$BIN_PATH"
''
else
''
@@ -222,13 +224,30 @@ in
${lib.optionalString (lib.length crateBin == 0 && !hasCrateBin) ''
if [[ -e src/main.rs ]]; then
mkdir -p target/bin
${build_bin} ${crateName} src/main.rs
BINS["${crateName}"]="src/main.rs"
fi
for i in src/bin/*.rs; do #*/
mkdir -p target/bin
${build_bin} "$(basename $i .rs)" "$i"
BINS["$(basename $i .rs)"]="$i"
done
''}
if [[ ''${#BINS[@]} -gt 0 ]]; then
export BIN_RUSTC_OPTS LINK EXTRA_LINK_ARGS EXTRA_LINK_ARGS_BINS EXTRA_LIB \
BUILD_OUT_DIR EXTRA_BUILD EXTRA_FEATURES EXTRA_RUSTC_FLAGS CAP_LINTS
export -f build_bin build_bin_test echo_build_heading noisily echo_colored echo_error
# Generate a Makefile and pipe it to make, which handles parallel execution
# and the jobserver protocol natively so rustc invocations share a token pool.
{
printf 'SHELL = %s\nall:' "$BASH"
for _n in "''${!BINS[@]}"; do printf ' %s' "$_n"; done
printf '\n'
for _n in "''${!BINS[@]}"; do
printf '%s:\n\t${build_bin} "%s" "%s"\n' "$_n" "$_n" "''${BINS[$_n]}"
done
} | make --no-print-directory -j"''${NIX_BUILD_CORES:-1}" -f -
fi
# Remove object files to avoid "wrong ELF type"
find target -type f -name "*.o" -print0 | xargs -0 rm -f
runHook postBuild
@@ -58,7 +58,7 @@ build_bin() {
$EXTRA_BUILD \
$EXTRA_FEATURES \
$EXTRA_RUSTC_FLAGS \
--color ${colors} \
--color ${colors} || return 1
if [ "$crate_name_" != "$crate_name" ]; then
if [ -f "$out_dir/$crate_name_.wasm" ]; then