buildBazelPackage: add support for bazel run targets

This commit is contained in:
Gaetan Lepage 2023-06-14 10:18:15 +02:00 committed by Nick Cao
parent 60eecf3cde
commit e754f2946b
No known key found for this signature in database

View File

@ -10,9 +10,12 @@ args@{
, bazelFlags ? [] , bazelFlags ? []
, bazelBuildFlags ? [] , bazelBuildFlags ? []
, bazelTestFlags ? [] , bazelTestFlags ? []
, bazelRunFlags ? []
, runTargetFlags ? []
, bazelFetchFlags ? [] , bazelFetchFlags ? []
, bazelTargets , bazelTargets ? []
, bazelTestTargets ? [] , bazelTestTargets ? []
, bazelRunTarget ? null
, buildAttrs , buildAttrs
, fetchAttrs , fetchAttrs
@ -46,17 +49,23 @@ args@{
let let
fArgs = removeAttrs args [ "buildAttrs" "fetchAttrs" "removeRulesCC" ] // { fArgs = removeAttrs args [ "buildAttrs" "fetchAttrs" "removeRulesCC" ] // {
name = name; inherit
bazelFlags = bazelFlags; name
bazelBuildFlags = bazelBuildFlags; bazelFlags
bazelTestFlags = bazelTestFlags; bazelBuildFlags
bazelFetchFlags = bazelFetchFlags; bazelTestFlags
bazelTestTargets = bazelTestTargets; bazelRunFlags
dontAddBazelOpts = dontAddBazelOpts; runTargetFlags
bazelFetchFlags
bazelTargets
bazelTestTargets
bazelRunTarget
dontAddBazelOpts
;
}; };
fBuildAttrs = fArgs // buildAttrs; fBuildAttrs = fArgs // buildAttrs;
fFetchAttrs = fArgs // removeAttrs fetchAttrs [ "sha256" ]; fFetchAttrs = fArgs // removeAttrs fetchAttrs [ "sha256" ];
bazelCmd = { cmd, additionalFlags, targets }: bazelCmd = { cmd, additionalFlags, targets, targetRunFlags ? [ ] }:
lib.optionalString (targets != [ ]) '' lib.optionalString (targets != [ ]) ''
# See footnote called [USER and BAZEL_USE_CPP_ONLY_TOOLCHAIN variables] # See footnote called [USER and BAZEL_USE_CPP_ONLY_TOOLCHAIN variables]
BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 \ BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 \
@ -73,7 +82,8 @@ let
"''${host_linkopts[@]}" \ "''${host_linkopts[@]}" \
$bazelFlags \ $bazelFlags \
${lib.strings.concatStringsSep " " additionalFlags} \ ${lib.strings.concatStringsSep " " additionalFlags} \
${lib.strings.concatStringsSep " " targets} ${lib.strings.concatStringsSep " " targets} \
${lib.optionalString (targetRunFlags != []) " -- " + lib.strings.concatStringsSep " " targetRunFlags}
''; '';
# we need this to chmod dangling symlinks on darwin, gnu coreutils refuses to do so: # we need this to chmod dangling symlinks on darwin, gnu coreutils refuses to do so:
# chmod: cannot operate on dangling symlink '$symlink' # chmod: cannot operate on dangling symlink '$symlink'
@ -262,6 +272,15 @@ stdenv.mkDerivation (fBuildAttrs // {
targets = fBuildAttrs.bazelTargets; targets = fBuildAttrs.bazelTargets;
} }
} }
${
bazelCmd {
cmd = "run";
additionalFlags = fBuildAttrs.bazelRunFlags ++ [ "--jobs" "$NIX_BUILD_CORES" ];
# Bazel run only accepts a single target, but `bazelCmd` expects `targets` to be a list.
targets = lib.optionals (fBuildAttrs.bazelRunTarget != null) [ fBuildAttrs.bazelRunTarget ];
targetRunFlags = fBuildAttrs.runTargetFlags;
}
}
runHook postBuild runHook postBuild
''; '';
}) })