buildBazelPackage: optionally run bazel tests in checkPhase

Tests from the bazelTestTargets argument will be run before the build. The new  bazelTestFlags argument can be used to pass additional flags to this phase.
This commit is contained in:
Yves-Stan Le Cornec 2022-10-03 12:43:33 +02:00
parent f6dd0ea8b4
commit b739da0693

View file

@ -13,8 +13,10 @@ args@{
, bazel ? bazelPkg , bazel ? bazelPkg
, bazelFlags ? [] , bazelFlags ? []
, bazelBuildFlags ? [] , bazelBuildFlags ? []
, bazelTestFlags ? []
, bazelFetchFlags ? [] , bazelFetchFlags ? []
, bazelTarget , bazelTarget
, bazelTestTargets ? []
, buildAttrs , buildAttrs
, fetchAttrs , fetchAttrs
@ -50,13 +52,33 @@ let
fArgs = removeAttrs args [ "buildAttrs" "fetchAttrs" "removeRulesCC" ]; fArgs = removeAttrs args [ "buildAttrs" "fetchAttrs" "removeRulesCC" ];
fBuildAttrs = fArgs // buildAttrs; fBuildAttrs = fArgs // buildAttrs;
fFetchAttrs = fArgs // removeAttrs fetchAttrs [ "sha256" ]; fFetchAttrs = fArgs // removeAttrs fetchAttrs [ "sha256" ];
bazelCmd = { cmd, additionalFlags, targets }:
in stdenv.mkDerivation (fBuildAttrs // { lib.optionalString (targets != [ ]) ''
inherit name bazelFlags bazelBuildFlags bazelFetchFlags bazelTarget; # See footnote called [USER and BAZEL_USE_CPP_ONLY_TOOLCHAIN variables]
BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 \
USER=homeless-shelter \
bazel \
--batch \
--output_base="$bazelOut" \
--output_user_root="$bazelUserRoot" \
${cmd} \
--curses=no \
-j $NIX_BUILD_CORES \
"''${copts[@]}" \
"''${host_copts[@]}" \
"''${linkopts[@]}" \
"''${host_linkopts[@]}" \
$bazelFlags \
${lib.strings.concatStringsSep " " additionalFlags} \
${lib.strings.concatStringsSep " " targets}
'';
in
stdenv.mkDerivation (fBuildAttrs // {
inherit name bazelFlags bazelBuildFlags bazelTestFlags bazelFetchFlags bazelTarget bazelTestTargets;
deps = stdenv.mkDerivation (fFetchAttrs // { deps = stdenv.mkDerivation (fFetchAttrs // {
name = "${name}-deps.tar.gz"; name = "${name}-deps.tar.gz";
inherit bazelFlags bazelBuildFlags bazelFetchFlags bazelTarget; inherit bazelFlags bazelBuildFlags bazelTestFlags bazelFetchFlags bazelTarget bazelTestTargets;
impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ fFetchAttrs.impureEnvVars or []; impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ fFetchAttrs.impureEnvVars or [];
@ -77,14 +99,7 @@ in stdenv.mkDerivation (fBuildAttrs // {
buildPhase = fFetchAttrs.buildPhase or '' buildPhase = fFetchAttrs.buildPhase or ''
runHook preBuild runHook preBuild
# Bazel computes the default value of output_user_root before parsing the # See footnote called [USER and BAZEL_USE_CPP_ONLY_TOOLCHAIN variables].
# flag. The computation of the default value involves getting the $USER
# from the environment. I don't have that variable when building with
# sandbox enabled. Code here
# https://github.com/bazelbuild/bazel/blob/9323c57607d37f9c949b60e293b573584906da46/src/main/cpp/startup_options.cc#L123-L124
#
# On macOS Bazel will use the system installed Xcode or CLT toolchain instead of the one in the PATH unless we pass BAZEL_USE_CPP_ONLY_TOOLCHAIN
# We disable multithreading for the fetching phase since it can lead to timeouts with many dependencies/threads: # We disable multithreading for the fetching phase since it can lead to timeouts with many dependencies/threads:
# https://github.com/bazelbuild/bazel/issues/6502 # https://github.com/bazelbuild/bazel/issues/6502
BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 \ BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 \
@ -97,7 +112,8 @@ in stdenv.mkDerivation (fBuildAttrs // {
--loading_phase_threads=1 \ --loading_phase_threads=1 \
$bazelFlags \ $bazelFlags \
$bazelFetchFlags \ $bazelFetchFlags \
$bazelTarget ${bazelTarget} \
${lib.strings.concatStringsSep " " bazelTestTargets}
runHook postBuild runHook postBuild
''; '';
@ -189,7 +205,7 @@ in stdenv.mkDerivation (fBuildAttrs // {
# the wrappers are expecting will not be set. So instead of relying on the # the wrappers are expecting will not be set. So instead of relying on the
# wrappers picking them up, pass them in explicitly via `--copt`, `--linkopt` # wrappers picking them up, pass them in explicitly via `--copt`, `--linkopt`
# and related flags. # and related flags.
#
copts=() copts=()
host_copts=() host_copts=()
linkopts=() linkopts=()
@ -209,23 +225,29 @@ in stdenv.mkDerivation (fBuildAttrs // {
done done
fi fi
BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 \ ${
USER=homeless-shelter \ bazelCmd {
bazel \ cmd = "test";
--batch \ additionalFlags =
--output_base="$bazelOut" \ ["--test_output=errors"] ++ bazelTestFlags;
--output_user_root="$bazelUserRoot" \ targets = bazelTestTargets;
build \ }
--curses=no \ }
-j $NIX_BUILD_CORES \ ${
"''${copts[@]}" \ bazelCmd {
"''${host_copts[@]}" \ cmd = "build";
"''${linkopts[@]}" \ additionalFlags = bazelBuildFlags;
"''${host_linkopts[@]}" \ targets = [bazelTarget];
$bazelFlags \ }
$bazelBuildFlags \ }
$bazelTarget
runHook postBuild runHook postBuild
''; '';
}) })
# [USER and BAZEL_USE_CPP_ONLY_TOOLCHAIN variables]:
# Bazel computes the default value of output_user_root before parsing the
# flag. The computation of the default value involves getting the $USER
# from the environment. Code here :
# https://github.com/bazelbuild/bazel/blob/9323c57607d37f9c949b60e293b573584906da46/src/main/cpp/startup_options.cc#L123-L124
#
# On macOS Bazel will use the system installed Xcode or CLT toolchain instead of the one in the PATH unless we pass BAZEL_USE_CPP_ONLY_TOOLCHAIN.