anvil-editor: init at 0.4
[NixPkgs.git] / pkgs / build-support / wrapper-common / utils.bash
blobc9870d1b4d41070f8121ff7565d5ae41d903dae0
1 # Accumulate suffixes for taking in the right input parameters with the `mangle*`
2 # functions below. See setup-hook for details.
3 accumulateRoles() {
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+=('')
11 if [ "${NIX_@wrapperName@_TARGET_TARGET_@suffixSalt@:-}" ]; then
12 role_suffixes+=('_FOR_TARGET')
16 mangleVarListGeneric() {
17 local sep="$1"
18 shift
19 local var="$1"
20 shift
21 local -a role_suffixes=("$@")
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}"
32 done
35 mangleVarList() {
36 mangleVarListGeneric " " "$@"
39 mangleVarBool() {
40 local var="$1"
41 shift
42 local -a role_suffixes=("$@")
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"
55 done
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.
61 mangleVarSingle() {
62 local var="$1"
63 shift
64 local -a role_suffixes=("$@")
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
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
78 exit 1
80 else
81 declare -gx ${outputVar}="${!inputVar}"
84 done
87 skip() {
88 if (( "${NIX_DEBUG:-0}" >= 1 )); then
89 echo "skipping impure path $1" >&2
93 reject() {
94 echo "impure path \`$1' used in link" >&2
95 exit 1
99 # Checks whether a path is impure. E.g., `/lib/foo.so' is impure, but
100 # `/nix/store/.../lib/foo.so' isn't.
101 badPath() {
102 local p=$1
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
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"
121 # Like `badPath`, but handles paths that may be interpreted relative to
122 # `$SDKROOT` on Darwin. For example, `-L/usr/lib/swift` is interpreted
123 # as `-L$SDKROOT/usr/lib/swift` when `$SDKROOT` is set and
124 # `$SDKROOT/usr/lib/swift` exists.
125 badPathWithDarwinSdk() {
126 path=$1
127 if [[ "@darwinMinVersion@" ]]; then
128 sdkPath=$SDKROOT/$path
129 if [[ -e $sdkPath ]]; then
130 path=$sdkPath
133 badPath "$path"
136 expandResponseParams() {
137 declare -ga params=("$@")
138 local arg
139 for arg in "$@"; do
140 if [[ "$arg" == @* ]]; then
141 # phase separation makes this look useless
142 # shellcheck disable=SC2157
143 if [ -x "@expandResponseParams@" ]; then
144 # params is used by caller
145 #shellcheck disable=SC2034
146 readarray -d '' params < <("@expandResponseParams@" "$@")
147 return 0
150 done
153 checkLinkType() {
154 local arg
155 type="dynamic"
156 for arg in "$@"; do
157 if [[ "$arg" = -static ]]; then
158 type="static"
159 elif [[ "$arg" = -static-pie ]]; then
160 type="static-pie"
162 done
163 echo "$type"
166 # When building static-pie executables we cannot have rpath
167 # set. At least glibc requires rpath to be empty
168 filterRpathFlags() {
169 local linkType=$1 ret i
170 shift
172 if [[ "$linkType" == "static-pie" ]]; then
173 while [[ "$#" -gt 0 ]]; do
174 i="$1"; shift 1
175 if [[ "$i" == -rpath ]]; then
176 # also skip its argument
177 shift
178 else
179 ret+=("$i")
181 done
182 else
183 ret=("$@")
185 echo "${ret[@]}"