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
93reject() {
94 echo "impure path \`$1' used in link" >&2
95 exit 1
96}
97
98
99# Checks whether a path is impure. E.g., `/lib/foo.so' is impure, but
100# `/nix/store/.../lib/foo.so' isn't.
101badPath() {
102 local p=$1
103
104 # Relative paths are okay (since they're presumably relative to
105 # the temporary build directory).
106 if [ "${p:0:1}" != / ]; then return 1; fi
107
108 # Otherwise, the path should refer to the store or some temporary
109 # directory (including the build directory).
110 test \
111 "$p" != "/dev/null" -a \
112 "${p#"${NIX_STORE}"}" = "$p" -a \
113 "${p#"${NIX_BUILD_TOP}"}" = "$p" -a \
114 "${p#/tmp}" = "$p" -a \
115 "${p#"${TMP:-/tmp}"}" = "$p" -a \
116 "${p#"${TMPDIR:-/tmp}"}" = "$p" -a \
117 "${p#"${TEMP:-/tmp}"}" = "$p" -a \
118 "${p#"${TEMPDIR:-/tmp}"}" = "$p"
119}
120
121expandResponseParams() {
122 declare -ga params=("$@")
123 local arg
124 for arg in "$@"; do
125 if [[ "$arg" == @* ]]; then
126 # phase separation makes this look useless
127 # shellcheck disable=SC2157
128 if [ -x "@expandResponseParams@" ]; then
129 # params is used by caller
130 #shellcheck disable=SC2034
131 readarray -d '' params < <("@expandResponseParams@" "$@")
132 return 0
133 fi
134 fi
135 done
136}
137
138checkLinkType() {
139 local arg
140 type="dynamic"
141 for arg in "$@"; do
142 if [[ "$arg" = -static ]]; then
143 type="static"
144 elif [[ "$arg" = -static-pie ]]; then
145 type="static-pie"
146 fi
147 done
148 echo "$type"
149}
150
151# When building static-pie executables we cannot have rpath
152# set. At least glibc requires rpath to be empty
153filterRpathFlags() {
154 local linkType=$1 ret i
155 shift
156
157 if [[ "$linkType" == "static-pie" ]]; then
158 while [[ "$#" -gt 0 ]]; do
159 i="$1"; shift 1
160 if [[ "$i" == -rpath ]]; then
161 # also skip its argument
162 shift
163 else
164 ret+=("$i")
165 fi
166 done
167 else
168 ret=("$@")
169 fi
170 echo "${ret[@]}"
171}