From 05e40e79a8559cdbe323ab05e003d56033000ee8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20de=20Kok?= Date: Mon, 15 Feb 2021 10:26:40 +0100 Subject: [PATCH] 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`. --- doc/languages-frameworks/rust.section.md | 6 ++- .../networking/browsers/castor/default.nix | 2 +- pkgs/build-support/rust/default.nix | 26 +++++-------- .../rust/hooks/cargo-check-hook.sh | 37 +++++++++++++++++++ pkgs/build-support/rust/hooks/default.nix | 9 +++++ .../compilers/rust/make-rust-platform.nix | 5 ++- pkgs/development/tools/the-way/default.nix | 2 +- 7 files changed, 66 insertions(+), 21 deletions(-) create mode 100644 pkgs/build-support/rust/hooks/cargo-check-hook.sh diff --git a/doc/languages-frameworks/rust.section.md b/doc/languages-frameworks/rust.section.md index 8451c126410d..2290d92f7ade 100644 --- a/doc/languages-frameworks/rust.section.md +++ b/doc/languages-frameworks/rust.section.md @@ -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`. diff --git a/pkgs/applications/networking/browsers/castor/default.nix b/pkgs/applications/networking/browsers/castor/default.nix index be3d8295f991..daead82e4854 100644 --- a/pkgs/applications/networking/browsers/castor/default.nix +++ b/pkgs/applications/networking/browsers/castor/default.nix @@ -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"; diff --git a/pkgs/build-support/rust/default.nix b/pkgs/build-support/rust/default.nix index b7d6cb522bc5..760f7c920da0 100644 --- a/pkgs/build-support/rust/default.nix +++ b/pkgs/build-support/rust/default.nix @@ -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; diff --git a/pkgs/build-support/rust/hooks/cargo-check-hook.sh b/pkgs/build-support/rust/hooks/cargo-check-hook.sh new file mode 100644 index 000000000000..a1df2babffbc --- /dev/null +++ b/pkgs/build-support/rust/hooks/cargo-check-hook.sh @@ -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 diff --git a/pkgs/build-support/rust/hooks/default.nix b/pkgs/build-support/rust/hooks/default.nix index b43f83acda0b..e8927e2b542f 100644 --- a/pkgs/build-support/rust/hooks/default.nix +++ b/pkgs/build-support/rust/hooks/default.nix @@ -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"; diff --git a/pkgs/development/compilers/rust/make-rust-platform.nix b/pkgs/development/compilers/rust/make-rust-platform.nix index e963c4631357..584b1fdbe438 100644 --- a/pkgs/development/compilers/rust/make-rust-platform.nix +++ b/pkgs/development/compilers/rust/make-rust-platform.nix @@ -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; } diff --git a/pkgs/development/tools/the-way/default.nix b/pkgs/development/tools/the-way/default.nix index e8f52fa83345..6d7fbef2f19b 100644 --- a/pkgs/development/tools/the-way/default.nix +++ b/pkgs/development/tools/the-way/default.nix @@ -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