Skip to content

Commit

Permalink
stdenv: support default values in concatTo
Browse files Browse the repository at this point in the history
The previously used pattern was introduced in NixOS#318614, but technically
leaked the default flags into the global scope. While this would
probably not make much of a practical difference, making concatTo
support default values is a much cleaner approach.
  • Loading branch information
wolfgangwalther committed Aug 24, 2024
1 parent a383657 commit 1efcffa
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
7 changes: 5 additions & 2 deletions pkgs/build-support/setup-hooks/autoreconf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ autoreconfPhase() {
runHook preAutoreconf

local flagsArray=()
: "${autoreconfFlags:=--install --force --verbose}"
concatTo flagsArray autoreconfFlags
if [[ -v autoreconfFlags ]]; then
concatTo flagsArray autoreconfFlags
else
flagsArray+=(--install --force --verbose)
fi

autoreconf "${flagsArray[@]}"
runHook postAutoreconf
Expand Down
3 changes: 1 addition & 2 deletions pkgs/by-name/ni/ninja/setup-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ ninjaInstallPhase() {
local flagsArray=(
"-j$buildCores"
)
: "${installTargets:=install}"
concatTo flagsArray ninjaFlags ninjaFlagsArray installTargets
concatTo flagsArray ninjaFlags ninjaFlagsArray installTargets=install

echoCmd 'install flags' "${flagsArray[@]}"
TERM=dumb ninja "${flagsArray[@]}"
Expand Down
27 changes: 13 additions & 14 deletions pkgs/stdenv/generic/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,16 @@ appendToVar() {
# Accumulate flags from the named variables $2+ into the indexed array $1.
#
# Arrays are simply concatenated, strings are split on whitespace.
# Default values can be passed via name=default.
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"
local arg default name type
for arg in "$@"; do
IFS="=" read -r name default <<< "$arg"
local -n nameref="$name"
if [[ ! -n "${nameref[@]}" && -n "$default" ]]; then
targetref+=( "$default" )
elif type=$(declare -p "$name" 2> /dev/null); then
case "${type#* }" in
-A*)
echo "concatTo(): ERROR: trying to use concatTo on an associative array." >&2
Expand Down Expand Up @@ -1340,8 +1344,7 @@ patchPhase() {
esac

local -a flagsArray
: "${patchFlags:=-p1}"
concatTo flagsArray patchFlags
concatTo flagsArray patchFlags=-p1
# "2>&1" is a hack to make patch fail if the decompressor fails (nonexistent patch, etc.)
# shellcheck disable=SC2086
$uncompress < "$i" 2>&1 | patch "${flagsArray[@]}"
Expand Down Expand Up @@ -1493,8 +1496,7 @@ checkPhase() {
SHELL="$SHELL"
)

: "${checkFlags:=VERBOSE=y}"
concatTo flagsArray makeFlags makeFlagsArray checkFlags checkFlagsArray checkTarget
concatTo flagsArray makeFlags makeFlagsArray checkFlags=VERBOSE=y checkFlagsArray checkTarget

echoCmd 'check flags' "${flagsArray[@]}"
make ${makefile:+-f $makefile} "${flagsArray[@]}"
Expand Down Expand Up @@ -1528,8 +1530,7 @@ installPhase() {
SHELL="$SHELL"
)

: "${installTargets:=install}"
concatTo flagsArray makeFlags makeFlagsArray installFlags installFlagsArray installTargets
concatTo flagsArray makeFlags makeFlagsArray installFlags installFlagsArray installTargets=install

echoCmd 'install flags' "${flagsArray[@]}"
make ${makefile:+-f $makefile} "${flagsArray[@]}"
Expand Down Expand Up @@ -1612,9 +1613,8 @@ installCheckPhase() {
SHELL="$SHELL"
)

: "${installCheckTarget:=installcheck}"
concatTo flagsArray makeFlags makeFlagsArray \
installCheckFlags installCheckFlagsArray installCheckTarget
installCheckFlags installCheckFlagsArray installCheckTarget=installcheck

echoCmd 'installcheck flags' "${flagsArray[@]}"
make ${makefile:+-f $makefile} "${flagsArray[@]}"
Expand All @@ -1629,8 +1629,7 @@ distPhase() {
runHook preDist

local flagsArray=()
: "${distTarget:=dist}"
concatTo flagsArray distFlags distFlagsArray distTarget
concatTo flagsArray distFlags distFlagsArray distTarget=dist

echo 'dist flags: %q' "${flagsArray[@]}"
make ${makefile:+-f $makefile} "${flagsArray[@]}"
Expand Down
13 changes: 11 additions & 2 deletions pkgs/test/stdenv/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,30 @@ let
declare -A associativeArray=(["X"]="Y")
[[ $(concatTo nowhere associativeArray 2>&1) =~ "trying to use" ]] || (echo "concatTo did not throw concatenating associativeArray" && false)
empty_array=()
empty_string=""
declare -a flagsArray
concatTo flagsArray string list
concatTo flagsArray string list notset=e=f empty_array=g empty_string=h
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)
[[ "''${flagsArray[4]}" == "e=f" ]] || (echo "'\$flagsArray[4]' was not 'e=f'" && false)
[[ "''${flagsArray[5]}" == "g" ]] || (echo "'\$flagsArray[5]' was not 'g'" && false)
[[ "''${flagsArray[6]}" == "h" ]] || (echo "'\$flagsArray[6]' was not 'h'" && false)
# test concatenating to unset variable
concatTo nonExistant string list
concatTo nonExistant string list notset=e=f empty_array=g empty_string=h
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)
[[ "''${nonExistant[4]}" == "e=f" ]] || (echo "'\$nonExistant[4]' was not 'e=f'" && false)
[[ "''${nonExistant[5]}" == "g" ]] || (echo "'\$nonExistant[5]' was not 'g'" && false)
[[ "''${nonExistant[6]}" == "h" ]] || (echo "'\$nonExistant[6]' was not 'h'" && false)
eval "$extraTest"
Expand Down

0 comments on commit 1efcffa

Please sign in to comment.