stdenv: generalize _accumFlagsArray to concatTo

Passing "flagsArray" as the first argument allows using this function
in a few more places.
This commit is contained in:
Wolfgang Walther
2024-06-09 12:24:21 +02:00
parent bfd97a691f
commit 6bdfef9d2d
4 changed files with 75 additions and 27 deletions
+1 -1
View File
@@ -488,7 +488,7 @@ let
''${enableParallelBuilding:+-j''${NIX_BUILD_CORES}}
SHELL=$SHELL
)
_accumFlagsArray makeFlags makeFlagsArray buildFlags buildFlagsArray
concatTo flagsArray makeFlags makeFlagsArray buildFlags buildFlagsArray
echoCmd 'build flags' "''${flagsArray[@]}"
make build "''${flagsArray[@]}"
unset flagsArray
+1 -1
View File
@@ -25,7 +25,7 @@ mesonConfigurePhase() {
"--buildtype=${mesonBuildType:-plain}"
)
_accumFlagsArray mesonFlags mesonFlagsArray
concatTo flagsArray mesonFlags mesonFlagsArray
echoCmd 'mesonConfigurePhase flags' "${flagsArray[@]}"
+15 -25
View File
@@ -334,23 +334,24 @@ appendToVar() {
fi
}
# Accumulate into `flagsArray` the flags from the named variables.
# Accumulate flags from the named variables $2+ into the indexed array $1.
#
# Arrays are simply concatenated, strings are split on whitespace.
_accumFlagsArray() {
concatTo() {
local -n targetref="$1"; shift
local name type
for name in "$@"; do
if type=$(declare -p "$name" 2> /dev/null); then
local -n nameref="$name"
case "${type#* }" in
-A*)
echo "_accumFlagsArray(): ERROR: trying to use _accumFlagsArray on an associative array." >&2
echo "concatTo(): ERROR: trying to use concatTo on an associative array." >&2
return 1 ;;
-a*)
flagsArray+=( "${nameref[@]}" ) ;;
targetref+=( "${nameref[@]}" ) ;;
*)
# shellcheck disable=SC2206
flagsArray+=( ${nameref-} ) ;;
targetref+=( ${nameref-} ) ;;
esac
fi
done
@@ -1167,12 +1168,7 @@ unpackPhase() {
fi
local -a srcsArray
if [ -n "$__structuredAttrs" ]; then
srcsArray=( "${srcs[@]}" )
else
# shellcheck disable=SC2206
srcsArray=( $srcs )
fi
concatTo srcsArray srcs
# To determine the source directory created by unpacking the
# source archives, we record the contents of the current
@@ -1237,13 +1233,7 @@ patchPhase() {
runHook prePatch
local -a patchesArray
if [ -n "$__structuredAttrs" ]; then
# shellcheck disable=SC2206
patchesArray=( ${patches:+"${patches[@]}"} )
else
# shellcheck disable=SC2206
patchesArray=( ${patches:-} )
fi
concatTo patchesArray patches
for i in "${patchesArray[@]}"; do
echo "applying patch $i"
@@ -1355,7 +1345,7 @@ configurePhase() {
if [ -n "$configureScript" ]; then
local -a flagsArray
_accumFlagsArray configureFlags configureFlagsArray
concatTo flagsArray configureFlags configureFlagsArray
echoCmd 'configure flags' "${flagsArray[@]}"
# shellcheck disable=SC2086
@@ -1382,7 +1372,7 @@ buildPhase() {
${enableParallelBuilding:+-j${NIX_BUILD_CORES}}
SHELL="$SHELL"
)
_accumFlagsArray makeFlags makeFlagsArray buildFlags buildFlagsArray
concatTo flagsArray makeFlags makeFlagsArray buildFlags buildFlagsArray
echoCmd 'build flags' "${flagsArray[@]}"
make ${makefile:+-f $makefile} "${flagsArray[@]}"
@@ -1421,14 +1411,14 @@ checkPhase() {
SHELL="$SHELL"
)
_accumFlagsArray makeFlags makeFlagsArray
concatTo flagsArray makeFlags makeFlagsArray
if [ -n "$__structuredAttrs" ]; then
flagsArray+=( "${checkFlags[@]:-VERBOSE=y}" )
else
# shellcheck disable=SC2206
flagsArray+=( ${checkFlags:-VERBOSE=y} )
fi
_accumFlagsArray checkFlagsArray
concatTo flagsArray checkFlagsArray
# shellcheck disable=SC2206
flagsArray+=( ${checkTarget} )
@@ -1463,7 +1453,7 @@ installPhase() {
${enableParallelInstalling:+-j${NIX_BUILD_CORES}}
SHELL="$SHELL"
)
_accumFlagsArray makeFlags makeFlagsArray installFlags installFlagsArray
concatTo flagsArray makeFlags makeFlagsArray installFlags installFlagsArray
if [ -n "$__structuredAttrs" ]; then
flagsArray+=( "${installTargets[@]:-install}" )
else
@@ -1552,7 +1542,7 @@ installCheckPhase() {
SHELL="$SHELL"
)
_accumFlagsArray makeFlags makeFlagsArray \
concatTo flagsArray makeFlags makeFlagsArray \
installCheckFlags installCheckFlagsArray
# shellcheck disable=SC2206
flagsArray+=( ${installCheckTarget:-installcheck} )
@@ -1570,7 +1560,7 @@ distPhase() {
runHook preDist
local flagsArray=()
_accumFlagsArray distFlags distFlagsArray
concatTo flagsArray distFlags distFlagsArray
# shellcheck disable=SC2206
flagsArray+=( ${distTarget:-dist} )
+58
View File
@@ -96,6 +96,41 @@ let
'';
} // extraAttrs);
testConcatTo = { name, stdenv', extraAttrs ? { } }:
stdenv'.mkDerivation
({
inherit name;
string = "a b";
list = ["c" "d"];
passAsFile = [ "buildCommand" ] ++ lib.optionals (extraAttrs ? extraTest) [ "extraTest" ];
buildCommand = ''
declare -A associativeArray=(["X"]="Y")
[[ $(concatTo nowhere associativeArray 2>&1) =~ "trying to use" ]] || (echo "concatTo did not throw concatenating associativeArray" && false)
declare -a flagsArray
concatTo flagsArray string list
declare -p flagsArray
[[ "''${flagsArray[0]}" == "a" ]] || (echo "'\$flagsArray[0]' was not 'a'" && false)
[[ "''${flagsArray[1]}" == "b" ]] || (echo "'\$flagsArray[1]' was not 'b'" && false)
[[ "''${flagsArray[2]}" == "c" ]] || (echo "'\$flagsArray[2]' was not 'c'" && false)
[[ "''${flagsArray[3]}" == "d" ]] || (echo "'\$flagsArray[3]' was not 'd'" && false)
# test concatenating to unset variable
concatTo nonExistant string list
declare -p nonExistant
[[ "''${nonExistant[0]}" == "a" ]] || (echo "'\$nonExistant[0]' was not 'a'" && false)
[[ "''${nonExistant[1]}" == "b" ]] || (echo "'\$nonExistant[1]' was not 'b'" && false)
[[ "''${nonExistant[2]}" == "c" ]] || (echo "'\$nonExistant[2]' was not 'c'" && false)
[[ "''${nonExistant[3]}" == "d" ]] || (echo "'\$nonExistant[3]' was not 'd'" && false)
eval "$extraTest"
touch $out
'';
} // extraAttrs);
in
{
@@ -196,6 +231,11 @@ in
stdenv' = bootStdenv;
};
test-concat-to = testConcatTo {
name = "test-concat-to";
stdenv' = bootStdenv;
};
test-structured-env-attrset = testEnvAttrset {
name = "test-structured-env-attrset";
stdenv' = bootStdenv;
@@ -255,6 +295,24 @@ in
};
};
test-concat-to = testConcatTo {
name = "test-concat-to-structuredAttrsByDefault";
stdenv' = bootStdenvStructuredAttrsByDefault;
extraAttrs = {
# test that whitespace is kept in the bash array for structuredAttrs
listWithSpaces = [ "c c" "d d" ];
extraTest = ''
declare -a flagsWithSpaces
concatTo flagsWithSpaces string listWithSpaces
declare -p flagsWithSpaces
[[ "''${flagsWithSpaces[0]}" == "a" ]] || (echo "'\$flagsWithSpaces[0]' was not 'a'" && false)
[[ "''${flagsWithSpaces[1]}" == "b" ]] || (echo "'\$flagsWithSpaces[1]' was not 'b'" && false)
[[ "''${flagsWithSpaces[2]}" == "c c" ]] || (echo "'\$flagsWithSpaces[2]' was not 'c c'" && false)
[[ "''${flagsWithSpaces[3]}" == "d d" ]] || (echo "'\$flagsWithSpaces[3]' was not 'd d'" && false)
'';
};
};
test-golden-example-structuredAttrs =
let
goldenSh = earlyPkgs.writeText "goldenSh" ''