summary refs log tree commit diff
path: root/pkgs/build-support/cc-wrapper
diff options
context:
space:
mode:
authorJohn Ericson <Ericson2314@yahoo.com>2018-05-07 15:32:10 -0400
committerGitHub <noreply@github.com>2018-05-07 15:32:10 -0400
commit4f7cdd35d58e3d32ba576e218316b3c835bc508c (patch)
treeb88a4fc1cb1f397c83dbf32d598c07c6361fb2cf /pkgs/build-support/cc-wrapper
parent1c87d77efea75e29061d13e11af68efa6f50aee2 (diff)
parent8b0fce8cb1013837cd2108193653a1763cd68266 (diff)
downloadnixlib-4f7cdd35d58e3d32ba576e218316b3c835bc508c.tar
nixlib-4f7cdd35d58e3d32ba576e218316b3c835bc508c.tar.gz
nixlib-4f7cdd35d58e3d32ba576e218316b3c835bc508c.tar.bz2
nixlib-4f7cdd35d58e3d32ba576e218316b3c835bc508c.tar.lz
nixlib-4f7cdd35d58e3d32ba576e218316b3c835bc508c.tar.xz
nixlib-4f7cdd35d58e3d32ba576e218316b3c835bc508c.tar.zst
nixlib-4f7cdd35d58e3d32ba576e218316b3c835bc508c.zip
Merge pull request #40139 from obsidiansystems/modular-setup-hooks
treewide: Modular setup hooks
Diffstat (limited to 'pkgs/build-support/cc-wrapper')
-rw-r--r--pkgs/build-support/cc-wrapper/add-flags.sh13
-rw-r--r--pkgs/build-support/cc-wrapper/cc-wrapper.sh2
-rw-r--r--pkgs/build-support/cc-wrapper/default.nix11
-rw-r--r--pkgs/build-support/cc-wrapper/setup-hook.sh49
-rw-r--r--pkgs/build-support/cc-wrapper/utils.sh80
5 files changed, 18 insertions, 137 deletions
diff --git a/pkgs/build-support/cc-wrapper/add-flags.sh b/pkgs/build-support/cc-wrapper/add-flags.sh
index e384d30ecbb6..9762894607ac 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 8a3cfb694b4f..1b43d7cc211a 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 700e7547b7ae..365aa192db29 100644
--- a/pkgs/build-support/cc-wrapper/default.nix
+++ b/pkgs/build-support/cc-wrapper/default.nix
@@ -191,7 +191,12 @@ stdenv.mkDerivation {
   propagatedBuildInputs = [ bintools ];
   depsTargetTargetPropagated = extraPackages;
 
-  setupHook = ./setup-hook.sh;
+  wrapperName = "CC_WRAPPER";
+
+  setupHooks = [
+    ../setup-hooks/role.bash
+    ./setup-hook.sh
+  ];
 
   postFixup =
     ''
@@ -277,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
@@ -288,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/setup-hook.sh b/pkgs/build-support/cc-wrapper/setup-hook.sh
index 15b84dca2794..94b5cc6231ba 100644
--- a/pkgs/build-support/cc-wrapper/setup-hook.sh
+++ b/pkgs/build-support/cc-wrapper/setup-hook.sh
@@ -66,55 +66,22 @@ set -u
 # over no state, and there's no @-substitutions within, so any redefined
 # function is guaranteed to be exactly the same.
 ccWrapper_addCVars () {
-    # The `depHostOffset` describes how the host platform of the dependencies
-    # are slid relative to the depending package. It is brought into scope of
-    # the environment hook defined as the role of the dependency being applied.
-    case $depHostOffset in
-        -1) local role='BUILD_' ;;
-        0)  local role='' ;;
-        1)  local role='TARGET_' ;;
-        *)  echo "cc-wrapper: Error: Cannot be used with $depHostOffset-offset deps" >2;
-            return 1 ;;
-    esac
+    # See ../setup-hooks/role.bash
+    local role_post role_pre
+    getTargetRoleEnvHook
 
     if [[ -d "$1/include" ]]; then
-        export NIX_${role}CFLAGS_COMPILE+=" ${ccIncludeFlag:--isystem} $1/include"
+        export NIX_${role_pre}CFLAGS_COMPILE+=" ${ccIncludeFlag:--isystem} $1/include"
     fi
 
     if [[ -d "$1/Library/Frameworks" ]]; then
-        export NIX_${role}CFLAGS_COMPILE+=" -F$1/Library/Frameworks"
+        export NIX_${role_pre}CFLAGS_COMPILE+=" -F$1/Library/Frameworks"
     fi
 }
 
-# Since the same cc-wrapper derivation can be depend on in multiple ways, we
-# need to accumulate *each* role (i.e. target platform relative the depending
-# derivation) in which the cc-wrapper derivation is used.
-# `NIX_CC_WRAPPER_@infixSalt@_TARGET_*` tracks this (needs to be an exported env
-# var so can't use fancier data structures).
-#
-# We also need to worry about what role is being added on *this* invocation of
-# setup-hook, which `role` tracks.
-case $targetOffset in
-    -1)
-        export NIX_CC_WRAPPER_@infixSalt@_TARGET_BUILD=1
-        role_pre='BUILD_'
-        role_post='_FOR_BUILD'
-        ;;
-    0)
-        export NIX_CC_WRAPPER_@infixSalt@_TARGET_HOST=1
-        role_pre=''
-        role_post=''
-        ;;
-    1)
-        export NIX_CC_WRAPPER_@infixSalt@_TARGET_TARGET=1
-        role_pre='TARGET_'
-        role_post='_FOR_TARGET'
-        ;;
-    *)
-        echo "cc-wrapper: used as improper sort of dependency" >2;
-        return 1
-        ;;
-esac
+# See ../setup-hooks/role.bash
+getTargetRole
+getTargetRoleWrapper
 
 # We use the `targetOffset` to choose the right env hook to accumulate the right
 # sort of deps (those with that offset).
diff --git a/pkgs/build-support/cc-wrapper/utils.sh b/pkgs/build-support/cc-wrapper/utils.sh
deleted file mode 100644
index 9215fe2dc397..000000000000
--- 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
-}