at master 5.4 kB view raw
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 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. 125badPathWithDarwinSdk() { 126 path=$1 127 if [[ "@darwinMinVersion@" ]]; then 128 sdkPath=$SDKROOT/$path 129 if [[ -e $sdkPath ]]; then 130 path=$sdkPath 131 fi 132 fi 133 badPath "$path" 134} 135 136expandResponseParams() { 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 148 fi 149 fi 150 done 151} 152 153checkLinkType() { 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" 161 fi 162 done 163 echo "$type" 164} 165 166# When building static-pie executables we cannot have rpath 167# set. At least glibc requires rpath to be empty 168filterRpathFlags() { 169 local linkType=$1 ret i 170 shift 171 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") 180 fi 181 done 182 else 183 ret=("$@") 184 fi 185 echo "${ret[@]}" 186}