Skip to content

Commit 633f6c1

Browse files
committed
buildRustCrate: parallize build_bin with gnu parallel
1 parent 66c7daa commit 633f6c1

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

pkgs/build-support/rust/build-rust-crate/build-crate.nix

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{ lib, stdenv
22
, mkRustcDepArgs, mkRustcFeatureArgs, needUnstableCLI, rustc
3+
, parallel
34
}:
45

56
{ crateName,
@@ -71,7 +72,8 @@
7172
${lib.optionalString buildTests "build_lib_test src/lib.rs"}
7273
fi
7374
74-
75+
BIN_NAMES=()
76+
BIN_PATHS=()
7577
7678
${lib.optionalString (lib.length crateBin > 0) (lib.concatMapStringsSep "\n" (bin:
7779
let
@@ -90,7 +92,8 @@
9092
'' else ''
9193
BIN_PATH='${bin.path}'
9294
''}
93-
${build_bin} "$BIN_NAME" "$BIN_PATH"
95+
BIN_NAMES+=("$BIN_NAME")
96+
BIN_PATHS+=("$BIN_PATH")
9497
'' else ''
9598
echo Binary ${bin.name or crateName} not compiled due to not having all of the required features -- ${lib.escapeShellArg (builtins.toJSON bin.requiredFeatures)} -- enabled.
9699
'') crateBin)}
@@ -120,13 +123,39 @@
120123
${lib.optionalString (lib.length crateBin == 0 && !hasCrateBin) ''
121124
if [[ -e src/main.rs ]]; then
122125
mkdir -p target/bin
123-
${build_bin} ${crateName} src/main.rs
126+
BIN_NAMES+=("${crateName}")
127+
BIN_PATHS+=("src/main.rs")
124128
fi
125129
for i in src/bin/*.rs; do #*/
126130
mkdir -p target/bin
127-
${build_bin} "$(basename $i .rs)" "$i"
131+
BIN_NAMES+=("$(basename $i .rs)")
132+
BIN_PATHS+=("$i")
128133
done
134+
129135
''}
136+
137+
export BIN_RUSTC_OPTS
138+
export LINK
139+
export EXTRA_LINK_ARGS
140+
export EXTRA_LINK_ARGS_BINS
141+
export EXTRA_LIB
142+
export BUILD_OUT_DIR
143+
export EXTRA_BUILD
144+
export EXTRA_FEATURES
145+
export EXTRA_RUSTC_FLAGS
146+
export -f build_bin
147+
export -f build_bin_test
148+
export -f echo_build_heading
149+
export -f noisily
150+
export -f echo_colored
151+
152+
${parallel}/bin/parallel \
153+
--no-notice \
154+
--link \
155+
--halt-on-error 2 \
156+
-j$NIX_BUILD_CORES \
157+
${build_bin} ::: ''${BIN_NAMES[@]} ::: ''${BIN_PATHS[@]}
158+
130159
# Remove object files to avoid "wrong ELF type"
131160
find target -type f -name "*.o" -print0 | xargs -0 rm -f
132161
runHook postBuild

pkgs/build-support/rust/build-rust-crate/default.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
, cargo
1414
, jq
1515
, libiconv
16+
, parallel
1617
}:
1718

1819
let
@@ -253,7 +254,7 @@ crate_: lib.makeOverridable
253254
hasCrateBin = crate ? crateBin;
254255

255256
buildCrate = import ./build-crate.nix {
256-
inherit lib stdenv mkRustcDepArgs mkRustcFeatureArgs needUnstableCLI;
257+
inherit lib stdenv mkRustcDepArgs mkRustcFeatureArgs needUnstableCLI parallel;
257258
rustc = rust;
258259
};
259260
in

pkgs/build-support/rust/build-rust-crate/lib.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ build_bin() {
5959
$EXTRA_RUSTC_FLAGS \
6060
--color ${colors} \
6161

62+
if [[ $? -ne 0 ]]; then
63+
exit 1
64+
fi
65+
6266
if [ "$crate_name_" != "$crate_name" ]; then
6367
mv target/bin/$crate_name_ target/bin/$crate_name
6468
fi

0 commit comments

Comments
 (0)