stdenv-setup: Add quotes that don't do anything for consistency.

@vcunat and others rightly point out that it's easier to quote always,
than learn Bash's idiosyncrasies enough to know when it doesn't make a
difference.

This reverts commit 2743078f664ae07c4bed06a96182c6a86bd7fa32, which
removes quotes that don't do anything, and then goes further adding
even more quotes.
This commit is contained in:
John Ericson 2017-07-16 12:10:52 -04:00
parent 98cff3f446
commit 34c0ba498c

View File

@ -13,10 +13,10 @@ set -o pipefail
# code). The hooks for <hookName> are the shell function or variable # code). The hooks for <hookName> are the shell function or variable
# <hookName>, and the values of the shell array <hookName>Hooks. # <hookName>, and the values of the shell array <hookName>Hooks.
runHook() { runHook() {
local hookName=$1 local hookName="$1"
shift shift
local var="$hookName" local var="$hookName"
if [[ $hookName =~ Hook$ ]]; then var+=s; else var+=Hooks; fi if [[ "$hookName" =~ Hook$ ]]; then var+=s; else var+=Hooks; fi
local -n var local -n var
local hook local hook
for hook in "_callImplicitHook 0 $hookName" "${var[@]}"; do for hook in "_callImplicitHook 0 $hookName" "${var[@]}"; do
@ -29,10 +29,10 @@ runHook() {
# Run all hooks with the specified name, until one succeeds (returns a # Run all hooks with the specified name, until one succeeds (returns a
# zero exit code). If none succeed, return a non-zero exit code. # zero exit code). If none succeed, return a non-zero exit code.
runOneHook() { runOneHook() {
local hookName=$1 local hookName="$1"
shift shift
local var="$hookName" local var="$hookName"
if [[ $hookName =~ Hook$ ]]; then var+=s; else var+=Hooks; fi if [[ "$hookName" =~ Hook$ ]]; then var+=s; else var+=Hooks; fi
local -n var local -n var
local hook local hook
for hook in "_callImplicitHook 1 $hookName" "${var[@]}"; do for hook in "_callImplicitHook 1 $hookName" "${var[@]}"; do
@ -50,8 +50,8 @@ runOneHook() {
# environment variables) and from shell scripts (as functions). If you # environment variables) and from shell scripts (as functions). If you
# want to allow multiple hooks, use runHook instead. # want to allow multiple hooks, use runHook instead.
_callImplicitHook() { _callImplicitHook() {
local def=$1 local def="$1"
local hookName=$2 local hookName="$2"
case "$(type -t "$hookName")" in case "$(type -t "$hookName")" in
(function|alias|builtin) "$hookName";; (function|alias|builtin) "$hookName";;
(file) source "$hookName";; (file) source "$hookName";;
@ -64,7 +64,7 @@ _callImplicitHook() {
# A function wrapper around eval that ensures that return inside # A function wrapper around eval that ensures that return inside
# hooks exits the hook, not the caller. # hooks exits the hook, not the caller.
_eval() { _eval() {
local code=$1 local code="$1"
shift shift
if [ "$(type -t "$code")" = function ]; then if [ "$(type -t "$code")" = function ]; then
eval "$code \"\$@\"" eval "$code \"\$@\""
@ -98,7 +98,7 @@ echoCmd() {
# Error handling. # Error handling.
exitHandler() { exitHandler() {
exitCode=$? exitCode="$?"
set +e set +e
if [ -n "$showBuildStats" ]; then if [ -n "$showBuildStats" ]; then
@ -112,7 +112,7 @@ exitHandler() {
echo "build time elapsed: " "${times[@]}" echo "build time elapsed: " "${times[@]}"
fi fi
if [ $exitCode != 0 ]; then if [ "$exitCode" != 0 ]; then
runHook failureHook runHook failureHook
# If the builder had a non-zero exit code and # If the builder had a non-zero exit code and
@ -122,7 +122,7 @@ exitHandler() {
if [ -n "$succeedOnFailure" ]; then if [ -n "$succeedOnFailure" ]; then
echo "build failed with exit code $exitCode (ignored)" echo "build failed with exit code $exitCode (ignored)"
mkdir -p "$out/nix-support" mkdir -p "$out/nix-support"
printf "%s" $exitCode > "$out/nix-support/failed" printf "%s" "$exitCode" > "$out/nix-support/failed"
exit 0 exit 0
fi fi
@ -130,7 +130,7 @@ exitHandler() {
runHook exitHook runHook exitHook
fi fi
exit $exitCode exit "$exitCode"
} }
trap "exitHandler" EXIT trap "exitHandler" EXIT
@ -141,11 +141,11 @@ trap "exitHandler" EXIT
addToSearchPathWithCustomDelimiter() { addToSearchPathWithCustomDelimiter() {
local delimiter=$1 local delimiter="$1"
local varName=$2 local varName="$2"
local dir=$3 local dir="$3"
if [ -d "$dir" ]; then if [ -d "$dir" ]; then
eval export ${varName}=${!varName}${!varName:+$delimiter}${dir} export "${varName}=${!varName}${!varName:+$delimiter}${dir}"
fi fi
} }
@ -182,31 +182,31 @@ _addRpathPrefix() {
# Return success if the specified file is an ELF object. # Return success if the specified file is an ELF object.
isELF() { isELF() {
local fn=$1 local fn="$1"
local fd local fd
local magic local magic
exec {fd}< "$fn" exec {fd}< "$fn"
read -r -n 4 -u $fd magic read -r -n 4 -u "$fd" magic
exec {fd}<&- exec {fd}<&-
if [[ $magic =~ ELF ]]; then return 0; else return 1; fi if [[ "$magic" =~ ELF ]]; then return 0; else return 1; fi
} }
# Return success if the specified file is a script (i.e. starts with # Return success if the specified file is a script (i.e. starts with
# "#!"). # "#!").
isScript() { isScript() {
local fn=$1 local fn="$1"
local fd local fd
local magic local magic
if ! [ -x /bin/sh ]; then return 0; fi if ! [ -x /bin/sh ]; then return 0; fi
exec {fd}< "$fn" exec {fd}< "$fn"
read -r -n 2 -u $fd magic read -r -n 2 -u "$fd" magic
exec {fd}<&- exec {fd}<&-
if [[ $magic =~ \#! ]]; then return 0; else return 1; fi if [[ "$magic" =~ \#! ]]; then return 0; else return 1; fi
} }
# printf unfortunately will print a trailing newline regardless # printf unfortunately will print a trailing newline regardless
printLines() { printLines() {
[[ $# -gt 0 ]] || return 0 [[ "$#" -gt 0 ]] || return 0
printf '%s\n' "$@" printf '%s\n' "$@"
} }
@ -232,7 +232,7 @@ shopt -s nullglob
PATH= PATH=
for i in $initialPath; do for i in $initialPath; do
if [ "$i" = / ]; then i=; fi if [ "$i" = / ]; then i=; fi
addToSearchPath PATH $i/bin addToSearchPath PATH "$i/bin"
done done
if [ "$NIX_DEBUG" = 1 ]; then if [ "$NIX_DEBUG" = 1 ]; then
@ -242,8 +242,8 @@ fi
# Check that the pre-hook initialised SHELL. # Check that the pre-hook initialised SHELL.
if [ -z "$SHELL" ]; then echo "SHELL not set"; exit 1; fi if [ -z "$SHELL" ]; then echo "SHELL not set"; exit 1; fi
BASH=$SHELL BASH="$SHELL"
export CONFIG_SHELL=$SHELL export CONFIG_SHELL="$SHELL"
# Dummy implementation of the paxmark function. On Linux, this is # Dummy implementation of the paxmark function. On Linux, this is
@ -252,7 +252,7 @@ paxmark() { true; }
# Execute the pre-hook. # Execute the pre-hook.
if [ -z "$shell" ]; then export shell=$SHELL; fi if [ -z "$shell" ]; then export shell="$SHELL"; fi
runHook preHook runHook preHook
@ -264,13 +264,13 @@ runHook addInputsHook
# Recursively find all build inputs. # Recursively find all build inputs.
findInputs() { findInputs() {
local pkg=$1 local pkg="$1"
local var=$2 local var="$2"
local -n varDeref=$var local -n varDeref="$var"
local propagatedBuildInputsFile=$3 local propagatedBuildInputsFile="$3"
# Stop if we've already added this one # Stop if we've already added this one
[[ -z ${varDeref[$pkg]} ]] || return 0 [[ -z "${varDeref["$pkg"]}" ]] || return 0
varDeref["$pkg"]=1 varDeref["$pkg"]=1
if ! [ -e "$pkg" ]; then if ! [ -e "$pkg" ]; then
@ -294,7 +294,7 @@ findInputs() {
local fd pkgNext local fd pkgNext
exec {fd}<"$pkg/nix-support/$propagatedBuildInputsFile" exec {fd}<"$pkg/nix-support/$propagatedBuildInputsFile"
while IFS= read -r -u $fd pkgNext; do while IFS= read -r -u $fd pkgNext; do
findInputs "$pkgNext" $var $propagatedBuildInputsFile findInputs "$pkgNext" "$var" "$propagatedBuildInputsFile"
done done
exec {fd}<&- exec {fd}<&-
fi fi
@ -307,17 +307,17 @@ if [ -z "$crossConfig" ]; then
for i in $nativeBuildInputs $buildInputs \ for i in $nativeBuildInputs $buildInputs \
$defaultNativeBuildInputs $defaultBuildInputs \ $defaultNativeBuildInputs $defaultBuildInputs \
$propagatedNativeBuildInputs $propagatedBuildInputs; do $propagatedNativeBuildInputs $propagatedBuildInputs; do
findInputs $i nativePkgs propagated-native-build-inputs findInputs "$i" nativePkgs propagated-native-build-inputs
done done
else else
declare -gA crossPkgs declare -gA crossPkgs
for i in $buildInputs $defaultBuildInputs $propagatedBuildInputs; do for i in $buildInputs $defaultBuildInputs $propagatedBuildInputs; do
findInputs $i crossPkgs propagated-build-inputs findInputs "$i" crossPkgs propagated-build-inputs
done done
declare -gA nativePkgs declare -gA nativePkgs
for i in $nativeBuildInputs $defaultNativeBuildInputs $propagatedNativeBuildInputs; do for i in $nativeBuildInputs $defaultNativeBuildInputs $propagatedNativeBuildInputs; do
findInputs $i nativePkgs propagated-native-build-inputs findInputs "$i" nativePkgs propagated-native-build-inputs
done done
fi fi
@ -325,25 +325,25 @@ fi
# Set the relevant environment variables to point to the build inputs # Set the relevant environment variables to point to the build inputs
# found above. # found above.
_addToNativeEnv() { _addToNativeEnv() {
local pkg=$1 local pkg="$1"
# Run the package-specific hooks set by the setup-hook scripts. # Run the package-specific hooks set by the setup-hook scripts.
runHook envHook "$pkg" runHook envHook "$pkg"
} }
for i in "${!nativePkgs[@]}"; do for i in "${!nativePkgs[@]}"; do
_addToNativeEnv $i _addToNativeEnv "$i"
done done
_addToCrossEnv() { _addToCrossEnv() {
local pkg=$1 local pkg="$1"
# Run the package-specific hooks set by the setup-hook scripts. # Run the package-specific hooks set by the setup-hook scripts.
runHook crossEnvHook "$pkg" runHook crossEnvHook "$pkg"
} }
for i in "${!crossPkgs[@]}"; do for i in "${!crossPkgs[@]}"; do
_addToCrossEnv $i _addToCrossEnv "$i"
done done
@ -360,7 +360,7 @@ export TZ=UTC
# for instance if we just want to perform a test build/install to a # for instance if we just want to perform a test build/install to a
# temporary location and write a build report to $out. # temporary location and write a build report to $out.
if [ -z "$prefix" ]; then if [ -z "$prefix" ]; then
prefix=$out; prefix="$out";
fi fi
if [ "$useTempPrefix" = 1 ]; then if [ "$useTempPrefix" = 1 ]; then
@ -408,8 +408,8 @@ fi
substitute() { substitute() {
local input=$1 local input="$1"
local output=$2 local output="$2"
shift 2 shift 2
if [ ! -f "$input" ]; then if [ ! -f "$input" ]; then
@ -427,28 +427,28 @@ substitute() {
while (( "$#" )); do while (( "$#" )); do
case "$1" in case "$1" in
--replace) --replace)
pattern=$2 pattern="$2"
replacement=$3 replacement="$3"
shift 3 shift 3
;; ;;
--subst-var) --subst-var)
local varName=$2 local varName="$2"
shift 2 shift 2
# check if the used nix attribute name is a valid bash name # check if the used nix attribute name is a valid bash name
if ! [[ $varName =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then if ! [[ "$varName" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then
echo "${FUNCNAME[0]}(): WARNING: substitution variables should be valid bash names," >&2 echo "${FUNCNAME[0]}(): WARNING: substitution variables should be valid bash names," >&2
echo " \"$varName\" isn't and therefore was skipped; it might be caused" >&2 echo " \"$varName\" isn't and therefore was skipped; it might be caused" >&2
echo " by multi-line phases in variables - see #14907 for details." >&2 echo " by multi-line phases in variables - see #14907 for details." >&2
continue continue
fi fi
pattern=@$varName@ pattern="@$varName@"
replacement=${!varName} replacement="${!varName}"
;; ;;
--subst-var-by) --subst-var-by)
pattern=@$2@ pattern="@$2@"
replacement=$3 replacement="$3"
shift 3 shift 3
;; ;;
@ -467,7 +467,7 @@ substitute() {
substituteInPlace() { substituteInPlace() {
local fileName=$1 local fileName="$1"
shift shift
substitute "$fileName" "$fileName" "$@" substitute "$fileName" "$fileName" "$@"
} }
@ -477,8 +477,8 @@ substituteInPlace() {
# character or underscore. Note: other names that aren't bash-valid # character or underscore. Note: other names that aren't bash-valid
# will cause an error during `substitute --subst-var`. # will cause an error during `substitute --subst-var`.
substituteAll() { substituteAll() {
local input=$1 local input="$1"
local output=$2 local output="$2"
local -a args=() local -a args=()
# Select all environment variables that start with a lowercase character. # Select all environment variables that start with a lowercase character.
@ -494,7 +494,7 @@ substituteAll() {
substituteAllInPlace() { substituteAllInPlace() {
local fileName=$1 local fileName="$1"
shift shift
substituteAll "$fileName" "$fileName" "$@" substituteAll "$fileName" "$fileName" "$@"
} }
@ -521,7 +521,7 @@ dumpVars() {
stripHash() { stripHash() {
local strippedName local strippedName
# On separate line for `set -e` # On separate line for `set -e`
strippedName=$(basename "$1") strippedName="$(basename "$1")"
if echo "$strippedName" | grep -q '^[a-z0-9]\{32\}-'; then if echo "$strippedName" | grep -q '^[a-z0-9]\{32\}-'; then
echo "$strippedName" | cut -c34- echo "$strippedName" | cut -c34-
else else
@ -532,7 +532,7 @@ stripHash() {
unpackCmdHooks+=(_defaultUnpack) unpackCmdHooks+=(_defaultUnpack)
_defaultUnpack() { _defaultUnpack() {
local fn=$1 local fn="$1"
if [ -d "$fn" ]; then if [ -d "$fn" ]; then
@ -563,7 +563,7 @@ _defaultUnpack() {
unpackFile() { unpackFile() {
curSrc=$1 curSrc="$1"
header "unpacking source archive $curSrc" 3 header "unpacking source archive $curSrc" 3
if ! runOneHook unpackCmd "$curSrc"; then if ! runOneHook unpackCmd "$curSrc"; then
echo "do not know how to unpack source archive $curSrc" echo "do not know how to unpack source archive $curSrc"
@ -581,7 +581,7 @@ unpackPhase() {
echo 'variable $src or $srcs should point to the source' echo 'variable $src or $srcs should point to the source'
exit 1 exit 1
fi fi
srcs=$src srcs="$src"
fi fi
# To determine the source directory created by unpacking the # To determine the source directory created by unpacking the
@ -615,7 +615,7 @@ unpackPhase() {
echo "unpacker produced multiple directories" echo "unpacker produced multiple directories"
exit 1 exit 1
fi fi
sourceRoot=$i sourceRoot="$i"
;; ;;
esac esac
fi fi
@ -677,7 +677,7 @@ fixLibtool() {
configurePhase() { configurePhase() {
runHook preConfigure runHook preConfigure
if [[ -z $configureScript && -x ./configure ]]; then if [[ -z "$configureScript" && -x ./configure ]]; then
configureScript=./configure configureScript=./configure
fi fi
@ -689,7 +689,7 @@ configurePhase() {
done done
fi fi
if [[ -z $dontAddPrefix && -n $prefix ]]; then if [[ -z "$dontAddPrefix" && -n "$prefix" ]]; then
configureFlags="${prefixKey:---prefix=}$prefix $configureFlags" configureFlags="${prefixKey:---prefix=}$prefix $configureFlags"
fi fi
@ -725,7 +725,7 @@ configurePhase() {
buildPhase() { buildPhase() {
runHook preBuild runHook preBuild
if [[ -z $makeFlags && ! ( -n $makefile || -e Makefile || -e makefile || -e GNUmakefile[[ ) ]]; then if [[ -z "$makeFlags" && ! ( -n "$makefile" || -e Makefile || -e makefile || -e GNUmakefile[[ ) ]]; then
echo "no Makefile, doing nothing" echo "no Makefile, doing nothing"
else else
# See https://github.com/NixOS/nixpkgs/pull/1354#issuecomment-31260409 # See https://github.com/NixOS/nixpkgs/pull/1354#issuecomment-31260409
@ -770,7 +770,7 @@ installPhase() {
mkdir -p "$prefix" mkdir -p "$prefix"
fi fi
installTargets=${installTargets:-install} installTargets="${installTargets:-install}"
# shellcheck disable=SC2086 # shellcheck disable=SC2086
local flagsArray=( $installTargets \ local flagsArray=( $installTargets \
@ -799,7 +799,7 @@ fixupPhase() {
# Apply fixup to each output. # Apply fixup to each output.
local output local output
for output in $outputs; do for output in $outputs; do
prefix=${!output} runHook fixupOutput prefix="${!output}" runHook fixupOutput
done done
@ -879,7 +879,7 @@ distPhase() {
# Note: don't quote $tarballs, since we explicitly permit # Note: don't quote $tarballs, since we explicitly permit
# wildcards in there. # wildcards in there.
# shellcheck disable=SC2086 # shellcheck disable=SC2086
cp -pvd ${tarballs:-*.tar.gz} $out/tarballs cp -pvd ${tarballs:-*.tar.gz} "$out/tarballs"
fi fi
runHook postDist runHook postDist
@ -887,8 +887,8 @@ distPhase() {
showPhaseHeader() { showPhaseHeader() {
local phase=$1 local phase="$1"
case $phase in case "$phase" in
unpackPhase) header "unpacking sources";; unpackPhase) header "unpacking sources";;
patchPhase) header "patching sources";; patchPhase) header "patching sources";;
configurePhase) header "configuring";; configurePhase) header "configuring";;
@ -920,14 +920,14 @@ genericBuild() {
fi fi
for curPhase in $phases; do for curPhase in $phases; do
if [[ $curPhase = buildPhase && -n $dontBuild ]]; then continue; fi if [[ "$curPhase" = buildPhase && -n "$dontBuild" ]]; then continue; fi
if [[ $curPhase = checkPhase && -z $doCheck ]]; then continue; fi if [[ "$curPhase" = checkPhase && -z "$doCheck" ]]; then continue; fi
if [[ $curPhase = installPhase && -n $dontInstall ]]; then continue; fi if [[ "$curPhase" = installPhase && -n "$dontInstall" ]]; then continue; fi
if [[ $curPhase = fixupPhase && -n $dontFixup ]]; then continue; fi if [[ "$curPhase" = fixupPhase && -n "$dontFixup" ]]; then continue; fi
if [[ $curPhase = installCheckPhase && -z $doInstallCheck ]]; then continue; fi if [[ "$curPhase" = installCheckPhase && -z "$doInstallCheck" ]]; then continue; fi
if [[ $curPhase = distPhase && -z $doDist ]]; then continue; fi if [[ "$curPhase" = distPhase && -z "$doDist" ]]; then continue; fi
if [[ -n $tracePhases ]]; then if [[ -n "$tracePhases" ]]; then
echo echo
echo "@ phase-started $out $curPhase" echo "@ phase-started $out $curPhase"
fi fi