1# Accumulate suffixes for taking in the right input parameters with the `mangle*`
2# functions below. See setup-hook for details.
3accumulateRoles() {
4 declare -ga role_suffixes=()
5 if [ "${NIX_@wrapperName@_TARGET_BUILD_@suffixSalt@:-}" ]; then
6 role_suffixes+=('_FOR_BUILD')
7 fi
8 if [ "${NIX_@wrapperName@_TARGET_HOST_@suffixSalt@:-}" ]; then
9 role_suffixes+=('')
10 fi
11 if [ "${NIX_@wrapperName@_TARGET_TARGET_@suffixSalt@:-}" ]; then
12 role_suffixes+=('_FOR_TARGET')
13 fi
14}
15
16mangleVarListGeneric() {
17 local sep="$1"
18 shift
19 local var="$1"
20 shift
21 local -a role_suffixes=("$@")
22
23 local outputVar="${var}_@suffixSalt@"
24 declare -gx ${outputVar}+=''
25 # For each role we serve, we accumulate the input parameters into our own
26 # cc-wrapper-derivation-specific environment variables.
27 for suffix in "${role_suffixes[@]}"; do
28 local inputVar="${var}${suffix}"
29 if [ -v "$inputVar" ]; then
30 export ${outputVar}+="${!outputVar:+$sep}${!inputVar}"
31 fi
32 done
33}
34
35mangleVarList() {
36 mangleVarListGeneric " " "$@"
37}
38
39mangleVarBool() {
40 local var="$1"
41 shift
42 local -a role_suffixes=("$@")
43
44 local outputVar="${var}_@suffixSalt@"
45 declare -gxi ${outputVar}+=0
46 for suffix in "${role_suffixes[@]}"; do
47 local inputVar="${var}${suffix}"
48 if [ -v "$inputVar" ]; then
49 # "1" in the end makes `let` return success error code when
50 # expression itself evaluates to zero.
51 # We don't use `|| true` because that would silence actual
52 # syntax errors from bad variable values.
53 let "${outputVar} |= ${!inputVar:-0}" "1"
54 fi
55 done
56}
57
58# Combine a singular value from all roles. If multiple roles are being served,
59# and the value differs in these roles then the request is impossible to
60# satisfy and we abort immediately.
61mangleVarSingle() {
62 local var="$1"
63 shift
64 local -a role_suffixes=("$@")
65
66 local outputVar="${var}_@suffixSalt@"
67 for suffix in "${role_suffixes[@]}"; do
68 local inputVar="${var}${suffix}"
69 if [ -v "$inputVar" ]; then
70 if [ -v "$outputVar" ]; then
71 if [ "${!outputVar}" != "${!inputVar}" ]; then
72 {
73 echo "Multiple conflicting values defined for $outputVar"
74 echo "Existing value is ${!outputVar}"
75 echo "Attempting to set to ${!inputVar} via $inputVar"
76 } >&2
77
78 exit 1
79 fi
80 else
81 declare -gx ${outputVar}="${!inputVar}"
82 fi
83 fi
84 done
85}
86
87skip () {
88 if (( "${NIX_DEBUG:-0}" >= 1 )); then
89 echo "skipping impure path $1" >&2
90 fi
91}
92
93
94# Checks whether a path is impure. E.g., `/lib/foo.so' is impure, but
95# `/nix/store/.../lib/foo.so' isn't.
96badPath() {
97 local p=$1
98
99 # Relative paths are okay (since they're presumably relative to
100 # the temporary build directory).
101 if [ "${p:0:1}" != / ]; then return 1; fi
102
103 # Otherwise, the path should refer to the store or some temporary
104 # directory (including the build directory).
105 test \
106 "$p" != "/dev/null" -a \
107 "${p#${NIX_STORE}}" = "$p" -a \
108 "${p#${NIX_BUILD_TOP}}" = "$p" -a \
109 "${p#/tmp}" = "$p" -a \
110 "${p#${TMP:-/tmp}}" = "$p" -a \
111 "${p#${TMPDIR:-/tmp}}" = "$p" -a \
112 "${p#${TEMP:-/tmp}}" = "$p" -a \
113 "${p#${TEMPDIR:-/tmp}}" = "$p"
114}
115
116expandResponseParams() {
117 declare -ga params=("$@")
118 local arg
119 for arg in "$@"; do
120 if [[ "$arg" == @* ]]; then
121 # phase separation makes this look useless
122 # shellcheck disable=SC2157
123 if [ -x "@expandResponseParams@" ]; then
124 # params is used by caller
125 #shellcheck disable=SC2034
126 readarray -d '' params < <("@expandResponseParams@" "$@")
127 return 0
128 fi
129 fi
130 done
131}