From 463463b3951422c734cf33b1456ba9264a586293 Mon Sep 17 00:00:00 2001 From: Albert Safin Date: Thu, 19 Sep 2019 09:41:35 +0000 Subject: setup.sh: avoid subshells: shopt -po nounset --- pkgs/stdenv/generic/setup.sh | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) (limited to 'pkgs/stdenv/generic') diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh index 311292169ecd..326a60676a26 100644 --- a/pkgs/stdenv/generic/setup.sh +++ b/pkgs/stdenv/generic/setup.sh @@ -17,7 +17,8 @@ fi # code). The hooks for are the shell function or variable # , and the values of the shell array ‘Hooks’. runHook() { - local oldOpts="$(shopt -po nounset)" + local oldOpts="-u" + shopt -qo nounset || oldOpts="+u" set -u # May be called from elsewhere, so do `set -u`. local hookName="$1" @@ -32,7 +33,7 @@ runHook() { set -u # To balance `_eval` done - eval "${oldOpts}" + set "$oldOpts" return 0 } @@ -40,7 +41,8 @@ runHook() { # Run all hooks with the specified name, until one succeeds (returns a # zero exit code). If none succeed, return a non-zero exit code. runOneHook() { - local oldOpts="$(shopt -po nounset)" + local oldOpts="-u" + shopt -qo nounset || oldOpts="+u" set -u # May be called from elsewhere, so do `set -u`. local hookName="$1" @@ -57,7 +59,7 @@ runOneHook() { set -u # To balance `_eval` done - eval "${oldOpts}" + set "$oldOpts" return "$ret" } @@ -500,10 +502,11 @@ activatePackage() { (( "$hostOffset" <= "$targetOffset" )) || exit -1 if [ -f "$pkg" ]; then - local oldOpts="$(shopt -po nounset)" + local oldOpts="-u" + shopt -qo nounset || oldOpts="+u" set +u source "$pkg" - eval "$oldOpts" + set "$oldOpts" fi # Only dependencies whose host platform is guaranteed to match the @@ -522,10 +525,11 @@ activatePackage() { fi if [[ -f "$pkg/nix-support/setup-hook" ]]; then - local oldOpts="$(shopt -po nounset)" + local oldOpts="-u" + shopt -qo nounset || oldOpts="+u" set +u source "$pkg/nix-support/setup-hook" - eval "$oldOpts" + set "$oldOpts" fi } @@ -1273,17 +1277,19 @@ showPhaseHeader() { genericBuild() { if [ -f "${buildCommandPath:-}" ]; then - local oldOpts="$(shopt -po nounset)" + local oldOpts="-u" + shopt -qo nounset || oldOpts="+u" set +u source "$buildCommandPath" - eval "$oldOpts" + set "$oldOpts" return fi if [ -n "${buildCommand:-}" ]; then - local oldOpts="$(shopt -po nounset)" + local oldOpts="-u" + shopt -qo nounset || oldOpts="+u" set +u eval "$buildCommand" - eval "$oldOpts" + set "$oldOpts" return fi @@ -1313,10 +1319,11 @@ genericBuild() { # Evaluate the variable named $curPhase if it exists, otherwise the # function named $curPhase. - local oldOpts="$(shopt -po nounset)" + local oldOpts="-u" + shopt -qo nounset || oldOpts="+u" set +u eval "${!curPhase:-$curPhase}" - eval "$oldOpts" + set "$oldOpts" if [ "$curPhase" = unpackPhase ]; then cd "${sourceRoot:-.}" -- cgit 1.4.1