buildRustPackage: factor out check phase to cargoCheckHook

API change:

`cargoParallelTestThreads` suggests that this attribute sets the
number of threads used during tests, while it is actually a boolean
option (use 1 thread or NIX_BUILD_CORES threads). In the hook, this
is replaced by a more canonical name `dontUseCargoParallelTests`.
This commit is contained in:
Daniël de Kok 2021-02-15 10:26:40 +01:00
parent 9757c7101a
commit 05e40e79a8
7 changed files with 66 additions and 21 deletions

View File

@ -196,7 +196,7 @@ sometimes it may be necessary to disable this so the tests run consecutively.
```nix
rustPlatform.buildRustPackage {
/* ... */
cargoParallelTestThreads = false;
dontUseCargoParallelTests = true;
}
```
@ -293,6 +293,10 @@ attributes can also be used:
variable `buildAndTestSubdir` can be used to build a crate in a
Cargo workspace. Additional maturin flags can be passed through
`maturinBuildFlags`.
* `cargoCheckHook`: run tests using Cargo. Additional flags can be
passed to Cargo using `checkFlags` and `checkFlagsArray`. By
default, tests are run in parallel. This can be disabled by setting
`dontUseCargoParallelTests`.
* `cargoInstallHook`: install binaries and static/shared libraries
that were built using `cargoBuildHook`.

View File

@ -39,7 +39,7 @@ rustPlatform.buildRustPackage rec {
postInstall = "make PREFIX=$out copy-data";
# Sometimes tests fail when run in parallel
cargoParallelTestThreads = false;
dontUseCargoParallelThreads = true;
meta = with lib; {
description = "A graphical client for plain-text protocols written in Rust with GTK. It currently supports the Gemini, Gopher and Finger protocols";

View File

@ -2,8 +2,8 @@
, lib
, buildPackages
, cacert
, cargo
, cargoBuildHook
, cargoCheckHook
, cargoInstallHook
, cargoSetupHook
, fetchCargoTarball
@ -42,7 +42,6 @@
, cargoVendorDir ? null
, checkType ? buildType
, depsExtraArgs ? {}
, cargoParallelTestThreads ? true
# Toggles whether a custom sysroot is created when the target is a .json file.
, __internal_dontAddSysroot ? false
@ -105,8 +104,15 @@ stdenv.mkDerivation ((removeAttrs args ["depsExtraArgs"]) // lib.optionalAttrs u
patchRegistryDeps = ./patch-registry-deps;
nativeBuildInputs = nativeBuildInputs ++
[ cacert git cargo cargoBuildHook cargoInstallHook cargoSetupHook rustc ];
nativeBuildInputs = nativeBuildInputs ++ [
cacert
git
cargoBuildHook
cargoCheckHook
cargoInstallHook
cargoSetupHook
rustc
];
buildInputs = buildInputs ++ lib.optional stdenv.hostPlatform.isMinGW windows.pthreads;
@ -126,18 +132,6 @@ stdenv.mkDerivation ((removeAttrs args ["depsExtraArgs"]) // lib.optionalAttrs u
runHook postConfigure
'';
checkPhase = args.checkPhase or (let
argstr = "${lib.optionalString (checkType == "release") "--release"} --target ${target} --frozen";
threads = if cargoParallelTestThreads then "$NIX_BUILD_CORES" else "1";
in ''
${lib.optionalString (buildAndTestSubdir != null) "pushd ${buildAndTestSubdir}"}
runHook preCheck
echo "Running cargo test ${argstr} -- ''${checkFlags} ''${checkFlagsArray+''${checkFlagsArray[@]}}"
cargo test -j $NIX_BUILD_CORES ${argstr} -- --test-threads=${threads} ''${checkFlags} ''${checkFlagsArray+"''${checkFlagsArray[@]}"}
runHook postCheck
${lib.optionalString (buildAndTestSubdir != null) "popd"}
'');
doCheck = args.doCheck or true;
strictDeps = true;

View File

@ -0,0 +1,37 @@
cargoCheckHook() {
echo "Executing cargoCheckHook"
runHook preCheck
if [[ -n "${buildAndTestSubdir-}" ]]; then
pushd "${buildAndTestSubdir}"
fi
if [[ -z ${dontUseCargoParallelTests-} ]]; then
threads=$NIX_BUILD_CORES
else
threads=1
fi
argstr="--${cargoBuildType} --target @rustTargetPlatformSpec@ --frozen";
(
set -x
cargo test \
-j $NIX_BUILD_CORES \
${argstr} -- \
--test-threads=${threads} \
${checkFlags} \
${checkFlagsArray+"${checkFlagsArray[@]}"}
)
if [[ -n "${buildAndTestSubdir-}" ]]; then
popd
fi
echo "Finished cargoCheckHook"
runHook postCheck
}
checkPhase=cargoCheckHook

View File

@ -36,6 +36,15 @@ in {
};
} ./cargo-build-hook.sh) {};
cargoCheckHook = callPackage ({ }:
makeSetupHook {
name = "cargo-check-hook.sh";
deps = [ cargo ];
substitutions = {
inherit rustTargetPlatformSpec;
};
} ./cargo-check-hook.sh) {};
cargoInstallHook = callPackage ({ }:
makeSetupHook {
name = "cargo-install-hook.sh";

View File

@ -12,7 +12,8 @@ rec {
};
buildRustPackage = callPackage ../../../build-support/rust {
inherit rustc cargo cargoBuildHook cargoInstallHook cargoSetupHook fetchCargoTarball;
inherit cargoBuildHook cargoCheckHook cargoInstallHook cargoSetupHook
fetchCargoTarball rustc;
};
rustcSrc = callPackage ./rust-src.nix {
@ -26,5 +27,5 @@ rec {
# Hooks
inherit (callPackage ../../../build-support/rust/hooks {
inherit cargo;
}) cargoBuildHook cargoInstallHook cargoSetupHook maturinBuildHook;
}) cargoBuildHook cargoCheckHook cargoInstallHook cargoSetupHook maturinBuildHook;
}

View File

@ -17,7 +17,7 @@ rustPlatform.buildRustPackage rec {
cargoSha256 = "sha256-jTZso61Lyt6jprBxBAhvchgOsgM9y1qBleTxUx1jCnE=";
checkFlagsArray = lib.optionals stdenv.isDarwin [ "--skip=copy" ];
cargoParallelTestThreads = false;
dontUseCargoParallelTests = true;
postInstall = ''
$out/bin/the-way config default tmp.toml