From 8b0fce8cb1013837cd2108193653a1763cd68266 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Mon, 7 May 2018 13:15:34 -0400 Subject: {bintools,cc}-wrapper: Factor out role accumulation logic --- pkgs/build-support/bintools-wrapper/add-flags.sh | 11 +-- pkgs/build-support/bintools-wrapper/default.nix | 4 +- pkgs/build-support/bintools-wrapper/ld-wrapper.sh | 2 +- pkgs/build-support/cc-wrapper/add-flags.sh | 13 +--- pkgs/build-support/cc-wrapper/cc-wrapper.sh | 2 +- pkgs/build-support/cc-wrapper/default.nix | 4 +- pkgs/build-support/cc-wrapper/utils.sh | 80 ------------------- pkgs/build-support/wrapper-common/utils.bash | 95 +++++++++++++++++++++++ 8 files changed, 103 insertions(+), 108 deletions(-) delete mode 100644 pkgs/build-support/cc-wrapper/utils.sh create mode 100644 pkgs/build-support/wrapper-common/utils.bash diff --git a/pkgs/build-support/bintools-wrapper/add-flags.sh b/pkgs/build-support/bintools-wrapper/add-flags.sh index 7d118d20fc6..d02a25fe697 100644 --- a/pkgs/build-support/bintools-wrapper/add-flags.sh +++ b/pkgs/build-support/bintools-wrapper/add-flags.sh @@ -11,16 +11,7 @@ var_templates_bool=( NIX+DONT_SET_RPATH ) -declare -a role_infixes=() -if [ "${NIX_BINTOOLS_WRAPPER_@infixSalt@_TARGET_BUILD:-}" ]; then - role_infixes+=(_BUILD_) -fi -if [ "${NIX_BINTOOLS_WRAPPER_@infixSalt@_TARGET_HOST:-}" ]; then - role_infixes+=(_) -fi -if [ "${NIX_BINTOOLS_WRAPPER_@infixSalt@_TARGET_TARGET:-}" ]; then - role_infixes+=(_TARGET_) -fi +accumulateRoles for var in "${var_templates_list[@]}"; do mangleVarList "$var" "${role_infixes[@]}" diff --git a/pkgs/build-support/bintools-wrapper/default.nix b/pkgs/build-support/bintools-wrapper/default.nix index c0aea2d1131..bfcd3292729 100644 --- a/pkgs/build-support/bintools-wrapper/default.nix +++ b/pkgs/build-support/bintools-wrapper/default.nix @@ -294,7 +294,7 @@ stdenv.mkDerivation { set +u substituteAll ${./add-flags.sh} $out/nix-support/add-flags.sh substituteAll ${./add-hardening.sh} $out/nix-support/add-hardening.sh - substituteAll ${../cc-wrapper/utils.sh} $out/nix-support/utils.sh + substituteAll ${../wrapper-common/utils.bash} $out/nix-support/utils.bash ## ## Extra custom steps @@ -305,7 +305,7 @@ stdenv.mkDerivation { inherit dynamicLinker expand-response-params; - # for substitution in utils.sh + # for substitution in utils.bash expandResponseParams = "${expand-response-params}/bin/expand-response-params"; meta = diff --git a/pkgs/build-support/bintools-wrapper/ld-wrapper.sh b/pkgs/build-support/bintools-wrapper/ld-wrapper.sh index 991ed0fe263..2ad1fdcadc2 100644 --- a/pkgs/build-support/bintools-wrapper/ld-wrapper.sh +++ b/pkgs/build-support/bintools-wrapper/ld-wrapper.sh @@ -14,7 +14,7 @@ if [ -n "@coreutils_bin@" ]; then PATH="@coreutils_bin@/bin" fi -source @out@/nix-support/utils.sh +source @out@/nix-support/utils.bash if [ -z "${NIX_BINTOOLS_WRAPPER_@infixSalt@_FLAGS_SET:-}" ]; then source @out@/nix-support/add-flags.sh diff --git a/pkgs/build-support/cc-wrapper/add-flags.sh b/pkgs/build-support/cc-wrapper/add-flags.sh index 604aaf6b6cf..ee4cc1ec06d 100644 --- a/pkgs/build-support/cc-wrapper/add-flags.sh +++ b/pkgs/build-support/cc-wrapper/add-flags.sh @@ -14,18 +14,7 @@ var_templates_bool=( NIX+ENFORCE_NO_NATIVE ) -# Accumulate infixes for taking in the right input parameters. See setup-hook -# for details. -declare -a role_infixes=() -if [ "${NIX_CC_WRAPPER_@infixSalt@_TARGET_BUILD:-}" ]; then - role_infixes+=(_BUILD_) -fi -if [ "${NIX_CC_WRAPPER_@infixSalt@_TARGET_HOST:-}" ]; then - role_infixes+=(_) -fi -if [ "${NIX_CC_WRAPPER_@infixSalt@_TARGET_TARGET:-}" ]; then - role_infixes+=(_TARGET_) -fi +accumulateRoles # We need to mangle names for hygiene, but also take parameters/overrides # from the environment. diff --git a/pkgs/build-support/cc-wrapper/cc-wrapper.sh b/pkgs/build-support/cc-wrapper/cc-wrapper.sh index c2e6c140635..b2bacec5e9b 100644 --- a/pkgs/build-support/cc-wrapper/cc-wrapper.sh +++ b/pkgs/build-support/cc-wrapper/cc-wrapper.sh @@ -15,7 +15,7 @@ if [[ -n "@coreutils_bin@" && -n "@gnugrep_bin@" ]]; then PATH="@coreutils_bin@/bin:@gnugrep_bin@/bin" fi -source @out@/nix-support/utils.sh +source @out@/nix-support/utils.bash # Flirting with a layer violation here. if [ -z "${NIX_BINTOOLS_WRAPPER_@infixSalt@_FLAGS_SET:-}" ]; then diff --git a/pkgs/build-support/cc-wrapper/default.nix b/pkgs/build-support/cc-wrapper/default.nix index 0ea9dd0cb9f..1b1c82b9294 100644 --- a/pkgs/build-support/cc-wrapper/default.nix +++ b/pkgs/build-support/cc-wrapper/default.nix @@ -282,7 +282,7 @@ stdenv.mkDerivation { + '' substituteAll ${./add-flags.sh} $out/nix-support/add-flags.sh substituteAll ${./add-hardening.sh} $out/nix-support/add-hardening.sh - substituteAll ${./utils.sh} $out/nix-support/utils.sh + substituteAll ${../wrapper-common/utils.bash} $out/nix-support/utils.bash ## ## Extra custom steps @@ -293,7 +293,7 @@ stdenv.mkDerivation { inherit expand-response-params; - # for substitution in utils.sh + # for substitution in utils.bash expandResponseParams = "${expand-response-params}/bin/expand-response-params"; meta = diff --git a/pkgs/build-support/cc-wrapper/utils.sh b/pkgs/build-support/cc-wrapper/utils.sh deleted file mode 100644 index 9215fe2dc39..00000000000 --- a/pkgs/build-support/cc-wrapper/utils.sh +++ /dev/null @@ -1,80 +0,0 @@ -mangleVarList() { - local var="$1" - shift - local -a role_infixes=("$@") - - local outputVar="${var/+/_@infixSalt@_}" - declare -gx ${outputVar}+='' - # For each role we serve, we accumulate the input parameters into our own - # cc-wrapper-derivation-specific environment variables. - for infix in "${role_infixes[@]}"; do - local inputVar="${var/+/${infix}}" - if [ -v "$inputVar" ]; then - export ${outputVar}+="${!outputVar:+ }${!inputVar}" - fi - done -} - -mangleVarBool() { - local var="$1" - shift - local -a role_infixes=("$@") - - local outputVar="${var/+/_@infixSalt@_}" - declare -gxi ${outputVar}+=0 - for infix in "${role_infixes[@]}"; do - local inputVar="${var/+/${infix}}" - if [ -v "$inputVar" ]; then - # "1" in the end makes `let` return success error code when - # expression itself evaluates to zero. - # We don't use `|| true` because that would silence actual - # syntax errors from bad variable values. - let "${outputVar} |= ${!inputVar:-0}" "1" - fi - done -} - -skip () { - if (( "${NIX_DEBUG:-0}" >= 1 )); then - echo "skipping impure path $1" >&2 - fi -} - - -# Checks whether a path is impure. E.g., `/lib/foo.so' is impure, but -# `/nix/store/.../lib/foo.so' isn't. -badPath() { - local p=$1 - - # Relative paths are okay (since they're presumably relative to - # the temporary build directory). - if [ "${p:0:1}" != / ]; then return 1; fi - - # Otherwise, the path should refer to the store or some temporary - # directory (including the build directory). - test \ - "$p" != "/dev/null" -a \ - "${p:0:${#NIX_STORE}}" != "$NIX_STORE" -a \ - "${p:0:4}" != "/tmp" -a \ - "${p:0:${#NIX_BUILD_TOP}}" != "$NIX_BUILD_TOP" -} - -expandResponseParams() { - declare -ga params=("$@") - local arg - for arg in "$@"; do - if [[ "$arg" == @* ]]; then - # phase separation makes this look useless - # shellcheck disable=SC2157 - if [ -x "@expandResponseParams@" ]; then - # params is used by caller - #shellcheck disable=SC2034 - readarray -d '' params < <("@expandResponseParams@" "$@") - return 0 - else - echo "Response files aren't supported during bootstrapping" >&2 - return 1 - fi - fi - done -} diff --git a/pkgs/build-support/wrapper-common/utils.bash b/pkgs/build-support/wrapper-common/utils.bash new file mode 100644 index 00000000000..12b596a83e6 --- /dev/null +++ b/pkgs/build-support/wrapper-common/utils.bash @@ -0,0 +1,95 @@ +# Accumulate infixes for taking in the right input parameters with the `mangle*` +# functions below. See setup-hook for details. +accumulateRoles() { + declare -ga role_infixes=() + if [ "${NIX_@wrapperName@_@infixSalt@_TARGET_BUILD:-}" ]; then + role_infixes+=(_BUILD_) + fi + if [ "${NIX_@wrapperName@_@infixSalt@_TARGET_HOST:-}" ]; then + role_infixes+=(_) + fi + if [ "${NIX_@wrapperName@_@infixSalt@_TARGET_TARGET:-}" ]; then + role_infixes+=(_TARGET_) + fi +} + +mangleVarList() { + local var="$1" + shift + local -a role_infixes=("$@") + + local outputVar="${var/+/_@infixSalt@_}" + declare -gx ${outputVar}+='' + # For each role we serve, we accumulate the input parameters into our own + # cc-wrapper-derivation-specific environment variables. + for infix in "${role_infixes[@]}"; do + local inputVar="${var/+/${infix}}" + if [ -v "$inputVar" ]; then + export ${outputVar}+="${!outputVar:+ }${!inputVar}" + fi + done +} + +mangleVarBool() { + local var="$1" + shift + local -a role_infixes=("$@") + + local outputVar="${var/+/_@infixSalt@_}" + declare -gxi ${outputVar}+=0 + for infix in "${role_infixes[@]}"; do + local inputVar="${var/+/${infix}}" + if [ -v "$inputVar" ]; then + # "1" in the end makes `let` return success error code when + # expression itself evaluates to zero. + # We don't use `|| true` because that would silence actual + # syntax errors from bad variable values. + let "${outputVar} |= ${!inputVar:-0}" "1" + fi + done +} + +skip () { + if (( "${NIX_DEBUG:-0}" >= 1 )); then + echo "skipping impure path $1" >&2 + fi +} + + +# Checks whether a path is impure. E.g., `/lib/foo.so' is impure, but +# `/nix/store/.../lib/foo.so' isn't. +badPath() { + local p=$1 + + # Relative paths are okay (since they're presumably relative to + # the temporary build directory). + if [ "${p:0:1}" != / ]; then return 1; fi + + # Otherwise, the path should refer to the store or some temporary + # directory (including the build directory). + test \ + "$p" != "/dev/null" -a \ + "${p:0:${#NIX_STORE}}" != "$NIX_STORE" -a \ + "${p:0:4}" != "/tmp" -a \ + "${p:0:${#NIX_BUILD_TOP}}" != "$NIX_BUILD_TOP" +} + +expandResponseParams() { + declare -ga params=("$@") + local arg + for arg in "$@"; do + if [[ "$arg" == @* ]]; then + # phase separation makes this look useless + # shellcheck disable=SC2157 + if [ -x "@expandResponseParams@" ]; then + # params is used by caller + #shellcheck disable=SC2034 + readarray -d '' params < <("@expandResponseParams@" "$@") + return 0 + else + echo "Response files aren't supported during bootstrapping" >&2 + return 1 + fi + fi + done +} -- cgit 1.4.1