Merge pull request #27879 from obsidiansystems/cc-wrapper-shellcheck

cc-wrapper: Pass shellcheck and other cleanups

authored by John Ericson and committed by GitHub fdd07f62 dd61dbf5

+195 -161
+15 -6
pkgs/build-support/cc-wrapper/add-flags.sh
··· 1 # `-B@out@/bin' forces cc to use ld-wrapper.sh when calling ld. 2 export NIX_CFLAGS_COMPILE="-B@out@/bin/ $NIX_CFLAGS_COMPILE" 3 4 if [ -e @out@/nix-support/libc-cflags ]; then 5 - export NIX_CFLAGS_COMPILE="$(cat @out@/nix-support/libc-cflags) $NIX_CFLAGS_COMPILE" 6 fi 7 8 if [ -e @out@/nix-support/cc-cflags ]; then 9 - export NIX_CFLAGS_COMPILE="$(cat @out@/nix-support/cc-cflags) $NIX_CFLAGS_COMPILE" 10 fi 11 12 if [ -e @out@/nix-support/gnat-cflags ]; then 13 - export NIX_GNATFLAGS_COMPILE="$(cat @out@/nix-support/gnat-cflags) $NIX_GNATFLAGS_COMPILE" 14 fi 15 16 if [ -e @out@/nix-support/libc-ldflags ]; then 17 - export NIX_LDFLAGS+=" $(cat @out@/nix-support/libc-ldflags)" 18 fi 19 20 if [ -e @out@/nix-support/cc-ldflags ]; then 21 - export NIX_LDFLAGS+=" $(cat @out@/nix-support/cc-ldflags)" 22 fi 23 24 if [ -e @out@/nix-support/libc-ldflags-before ]; then 25 - export NIX_LDFLAGS_BEFORE="$(cat @out@/nix-support/libc-ldflags-before) $NIX_LDFLAGS_BEFORE" 26 fi 27 28 export NIX_CC_WRAPPER_FLAGS_SET=1
··· 1 # `-B@out@/bin' forces cc to use ld-wrapper.sh when calling ld. 2 export NIX_CFLAGS_COMPILE="-B@out@/bin/ $NIX_CFLAGS_COMPILE" 3 4 + # Export and assign separately in order that a failing $(..) will fail 5 + # the script. 6 + 7 if [ -e @out@/nix-support/libc-cflags ]; then 8 + export NIX_CFLAGS_COMPILE 9 + NIX_CFLAGS_COMPILE="$(< @out@/nix-support/libc-cflags) $NIX_CFLAGS_COMPILE" 10 fi 11 12 if [ -e @out@/nix-support/cc-cflags ]; then 13 + export NIX_CFLAGS_COMPILE 14 + NIX_CFLAGS_COMPILE="$(< @out@/nix-support/cc-cflags) $NIX_CFLAGS_COMPILE" 15 fi 16 17 if [ -e @out@/nix-support/gnat-cflags ]; then 18 + export NIX_GNATFLAGS_COMPILE 19 + NIX_GNATFLAGS_COMPILE="$(< @out@/nix-support/gnat-cflags) $NIX_GNATFLAGS_COMPILE" 20 fi 21 22 if [ -e @out@/nix-support/libc-ldflags ]; then 23 + export NIX_LDFLAGS 24 + NIX_LDFLAGS+=" $(< @out@/nix-support/libc-ldflags)" 25 fi 26 27 if [ -e @out@/nix-support/cc-ldflags ]; then 28 + export NIX_LDFLAGS 29 + NIX_LDFLAGS+=" $(< @out@/nix-support/cc-ldflags)" 30 fi 31 32 if [ -e @out@/nix-support/libc-ldflags-before ]; then 33 + export NIX_LDFLAGS_BEFORE 34 + NIX_LDFLAGS_BEFORE="$(< @out@/nix-support/libc-ldflags-before) $NIX_LDFLAGS_BEFORE" 35 fi 36 37 export NIX_CC_WRAPPER_FLAGS_SET=1
+22 -8
pkgs/build-support/cc-wrapper/add-hardening.sh
··· 1 hardeningFlags=(fortify stackprotector pic strictoverflow format relro bindnow) 2 - hardeningFlags+=("${hardeningEnable[@]}") 3 hardeningCFlags=() 4 hardeningLDFlags=() 5 - hardeningDisable=${hardeningDisable:-""} 6 7 - hardeningDisable+=" @hardening_unsupported_flags@" 8 9 - if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: Value of '$hardeningDisable': $hardeningDisable >&2; fi 10 11 - if [[ ! $hardeningDisable =~ "all" ]]; then 12 - if [[ -n "$NIX_DEBUG" ]]; then echo 'HARDENING: Is active (not completely disabled with "all" flag)' >&2; fi 13 for flag in "${hardeningFlags[@]}" 14 do 15 - if [[ ! "${hardeningDisable}" =~ "$flag" ]]; then 16 case $flag in 17 fortify) 18 if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling fortify >&2; fi ··· 20 ;; 21 stackprotector) 22 if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling stackprotector >&2; fi 23 - hardeningCFlags+=('-fstack-protector-strong' '--param ssp-buffer-size=4') 24 ;; 25 pie) 26 if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling CFlags -fPIE >&2; fi
··· 1 hardeningFlags=(fortify stackprotector pic strictoverflow format relro bindnow) 2 + # Intentionally word-split in case 'hardeningEnable' is defined in Nix. 3 + hardeningFlags+=(${hardeningEnable[@]}) 4 hardeningCFlags=() 5 hardeningLDFlags=() 6 7 + declare -A hardeningDisableMap 8 9 + # Intentionally word-split in case 'hardeningDisable' is defined in Nix. The 10 + # array expansion also prevents undefined variables from causing trouble with 11 + # `set -u`. 12 + for flag in ${hardeningDisable[@]} @hardening_unsupported_flags@ 13 + do 14 + hardeningDisableMap[$flag]=1 15 + done 16 17 + if [[ -n "$NIX_DEBUG" ]]; then 18 + printf 'HARDENING: disabled flags:' >&2 19 + (( "${#hardeningDisableMap[@]}" )) && printf ' %q' "${!hardeningDisableMap[@]}" >&2 20 + echo >&2 21 + fi 22 + 23 + if [[ -z "${hardeningDisableMap[all]}" ]]; then 24 + if [[ -n "$NIX_DEBUG" ]]; then 25 + echo 'HARDENING: Is active (not completely disabled with "all" flag)' >&2; 26 + fi 27 for flag in "${hardeningFlags[@]}" 28 do 29 + if [[ -z "${hardeningDisableMap[$flag]}" ]]; then 30 case $flag in 31 fortify) 32 if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling fortify >&2; fi ··· 34 ;; 35 stackprotector) 36 if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling stackprotector >&2; fi 37 + hardeningCFlags+=('-fstack-protector-strong' '--param' 'ssp-buffer-size=4') 38 ;; 39 pie) 40 if [[ -n "$NIX_DEBUG" ]]; then echo HARDENING: enabling CFlags -fPIE >&2; fi
+42 -40
pkgs/build-support/cc-wrapper/cc-wrapper.sh
··· 1 - #! @shell@ -e 2 path_backup="$PATH" 3 - if [ -n "@coreutils_bin@" ]; then 4 - PATH="@coreutils_bin@/bin:@gnugrep_bin@/bin" 5 fi 6 7 if [ -n "$NIX_CC_WRAPPER_START_HOOK" ]; then ··· 19 # For instance, figure out if linker flags should be passed. 20 # GCC prints annoying warnings when they are not needed. 21 dontLink=0 22 - getVersion=0 23 nonFlagArgs=0 24 [[ "@prog@" = *++ ]] && isCpp=1 || isCpp=0 25 cppInclude=1 26 27 expandResponseParams "$@" 28 - n=0 29 - while [ $n -lt ${#params[*]} ]; do 30 p=${params[n]} 31 - p2=${params[$((n+1))]} 32 if [ "$p" = -c ]; then 33 dontLink=1 34 elif [ "$p" = -S ]; then ··· 55 nonFlagArgs=1 56 elif [ "$p" = -m32 ]; then 57 if [ -e @out@/nix-support/dynamic-linker-m32 ]; then 58 - NIX_LDFLAGS+=" -dynamic-linker $(cat @out@/nix-support/dynamic-linker-m32)" 59 fi 60 fi 61 - n=$((n + 1)) 62 done 63 64 # If we pass a flag like -Wl, then gcc will call the linker unless it ··· 71 fi 72 73 # Optionally filter out paths not refering to the store. 74 - if [ "$NIX_ENFORCE_PURITY" = 1 -a -n "$NIX_STORE" ]; then 75 rest=() 76 - n=0 77 - while [ $n -lt ${#params[*]} ]; do 78 p=${params[n]} 79 - p2=${params[$((n+1))]} 80 if [ "${p:0:3}" = -L/ ] && badPath "${p:2}"; then 81 - skip $p 82 elif [ "$p" = -L ] && badPath "$p2"; then 83 - n=$((n + 1)); skip $p2 84 elif [ "${p:0:3}" = -I/ ] && badPath "${p:2}"; then 85 - skip $p 86 elif [ "$p" = -I ] && badPath "$p2"; then 87 - n=$((n + 1)); skip $p2 88 elif [ "$p" = -isystem ] && badPath "$p2"; then 89 - n=$((n + 1)); skip $p2 90 else 91 rest+=("$p") 92 fi 93 - n=$((n + 1)) 94 done 95 params=("${rest[@]}") 96 fi ··· 99 # Clear march/mtune=native -- they bring impurity. 100 if [ "$NIX_ENFORCE_NO_NATIVE" = 1 ]; then 101 rest=() 102 - for i in "${params[@]}"; do 103 - if [[ "$i" = -m*=native ]]; then 104 - skip $i 105 else 106 - rest+=("$i") 107 fi 108 done 109 params=("${rest[@]}") ··· 116 NIX_CFLAGS_LINK+=" $NIX_CXXSTDLIB_LINK" 117 fi 118 119 - LD=@ldPath@/ld 120 source @out@/nix-support/add-hardening.sh 121 122 # Add the flags for the C compiler proper. 123 - extraAfter=($NIX_CFLAGS_COMPILE ${hardeningCFlags[@]}) 124 extraBefore=() 125 126 if [ "$dontLink" != 1 ]; then 127 128 # Add the flags that should only be passed to the compiler when 129 # linking. 130 - extraAfter+=($NIX_CFLAGS_LINK ${hardeningLDFlags[@]}) 131 132 # Add the flags that should be passed to the linker (and prevent 133 # `ld-wrapper' from adding NIX_LDFLAGS again). 134 for i in $NIX_LDFLAGS_BEFORE; do 135 - extraBefore=(${extraBefore[@]} "-Wl,$i") 136 done 137 for i in $NIX_LDFLAGS; do 138 if [ "${i:0:3}" = -L/ ]; then ··· 155 156 # Optionally print debug info. 157 if [ -n "$NIX_DEBUG" ]; then 158 - echo "original flags to @prog@:" >&2 159 - for i in "${params[@]}"; do 160 - echo " $i" >&2 161 - done 162 - echo "extraBefore flags to @prog@:" >&2 163 - for i in ${extraBefore[@]}; do 164 - echo " $i" >&2 165 - done 166 - echo "extraAfter flags to @prog@:" >&2 167 - for i in ${extraAfter[@]}; do 168 - echo " $i" >&2 169 - done 170 fi 171 172 if [ -n "$NIX_CC_WRAPPER_EXEC_HOOK" ]; then ··· 174 fi 175 176 PATH="$path_backup" 177 - exec @prog@ ${extraBefore[@]} "${params[@]}" "${extraAfter[@]}"
··· 1 + #! @shell@ 2 + set -e -o pipefail 3 + shopt -s nullglob 4 + 5 path_backup="$PATH" 6 + 7 + # That @-vars are substituted separately from bash evaluation makes 8 + # shellcheck think this, and others like it, are useless conditionals. 9 + # shellcheck disable=SC2157 10 + if [[ -n "@coreutils_bin@" && -n "@gnugrep_bin@" ]]; then 11 + PATH="@coreutils_bin@/bin:@gnugrep_bin@/bin" 12 fi 13 14 if [ -n "$NIX_CC_WRAPPER_START_HOOK" ]; then ··· 26 # For instance, figure out if linker flags should be passed. 27 # GCC prints annoying warnings when they are not needed. 28 dontLink=0 29 nonFlagArgs=0 30 + # shellcheck disable=SC2193 31 [[ "@prog@" = *++ ]] && isCpp=1 || isCpp=0 32 cppInclude=1 33 34 expandResponseParams "$@" 35 + declare -i n=0 36 + nParams=${#params[@]} 37 + while [ "$n" -lt "$nParams" ]; do 38 p=${params[n]} 39 + p2=${params[n+1]} 40 if [ "$p" = -c ]; then 41 dontLink=1 42 elif [ "$p" = -S ]; then ··· 63 nonFlagArgs=1 64 elif [ "$p" = -m32 ]; then 65 if [ -e @out@/nix-support/dynamic-linker-m32 ]; then 66 + NIX_LDFLAGS+=" -dynamic-linker $(< @out@/nix-support/dynamic-linker-m32)" 67 fi 68 fi 69 + n+=1 70 done 71 72 # If we pass a flag like -Wl, then gcc will call the linker unless it ··· 79 fi 80 81 # Optionally filter out paths not refering to the store. 82 + if [[ "$NIX_ENFORCE_PURITY" = 1 && -n "$NIX_STORE" ]]; then 83 rest=() 84 + nParams=${#params[@]} 85 + declare -i n=0 86 + while [ "$n" -lt "$nParams" ]; do 87 p=${params[n]} 88 + p2=${params[n+1]} 89 if [ "${p:0:3}" = -L/ ] && badPath "${p:2}"; then 90 + skip "${p:2}" 91 elif [ "$p" = -L ] && badPath "$p2"; then 92 + n+=1; skip "$p2" 93 elif [ "${p:0:3}" = -I/ ] && badPath "${p:2}"; then 94 + skip "${p:2}" 95 elif [ "$p" = -I ] && badPath "$p2"; then 96 + n+=1; skip "$p2" 97 elif [ "$p" = -isystem ] && badPath "$p2"; then 98 + n+=1; skip "$p2" 99 else 100 rest+=("$p") 101 fi 102 + n+=1 103 done 104 params=("${rest[@]}") 105 fi ··· 108 # Clear march/mtune=native -- they bring impurity. 109 if [ "$NIX_ENFORCE_NO_NATIVE" = 1 ]; then 110 rest=() 111 + for p in "${params[@]}"; do 112 + if [[ "$p" = -m*=native ]]; then 113 + skip "$p" 114 else 115 + rest+=("$p") 116 fi 117 done 118 params=("${rest[@]}") ··· 125 NIX_CFLAGS_LINK+=" $NIX_CXXSTDLIB_LINK" 126 fi 127 128 source @out@/nix-support/add-hardening.sh 129 130 # Add the flags for the C compiler proper. 131 + extraAfter=($NIX_CFLAGS_COMPILE "${hardeningCFlags[@]}") 132 extraBefore=() 133 134 if [ "$dontLink" != 1 ]; then 135 136 # Add the flags that should only be passed to the compiler when 137 # linking. 138 + extraAfter+=($NIX_CFLAGS_LINK "${hardeningLDFlags[@]}") 139 140 # Add the flags that should be passed to the linker (and prevent 141 # `ld-wrapper' from adding NIX_LDFLAGS again). 142 for i in $NIX_LDFLAGS_BEFORE; do 143 + extraBefore+=("-Wl,$i") 144 done 145 for i in $NIX_LDFLAGS; do 146 if [ "${i:0:3}" = -L/ ]; then ··· 163 164 # Optionally print debug info. 165 if [ -n "$NIX_DEBUG" ]; then 166 + echo "extra flags before to @prog@:" >&2 167 + printf " %q\n" "${extraBefore[@]}" >&2 168 + echo "original flags to @prog@:" >&2 169 + printf " %q\n" "${params[@]}" >&2 170 + echo "extra flags after to @prog@:" >&2 171 + printf " %q\n" "${extraAfter[@]}" >&2 172 fi 173 174 if [ -n "$NIX_CC_WRAPPER_EXEC_HOOK" ]; then ··· 176 fi 177 178 PATH="$path_backup" 179 + exec @prog@ "${extraBefore[@]}" "${params[@]}" "${extraAfter[@]}"
+43 -38
pkgs/build-support/cc-wrapper/gnat-wrapper.sh
··· 1 - #! @shell@ -e 2 path_backup="$PATH" 3 if [ -n "@coreutils_bin@" ]; then 4 - PATH="@coreutils_bin@/bin" 5 fi 6 7 if [ -n "$NIX_GNAT_WRAPPER_START_HOOK" ]; then ··· 18 # Figure out if linker flags should be passed. GCC prints annoying 19 # warnings when they are not needed. 20 dontLink=0 21 - getVersion=0 22 nonFlagArgs=0 23 24 for i in "$@"; do ··· 30 nonFlagArgs=1 31 elif [ "$i" = -m32 ]; then 32 if [ -e @out@/nix-support/dynamic-linker-m32 ]; then 33 - NIX_LDFLAGS="$NIX_LDFLAGS -dynamic-linker $(cat @out@/nix-support/dynamic-linker-m32)" 34 fi 35 fi 36 done ··· 47 48 # Optionally filter out paths not refering to the store. 49 params=("$@") 50 - if [ "$NIX_ENFORCE_PURITY" = 1 -a -n "$NIX_STORE" ]; then 51 rest=() 52 - n=0 53 - while [ $n -lt ${#params[*]} ]; do 54 - p=${params[n]} 55 - p2=${params[$((n+1))]} 56 if [ "${p:0:3}" = -L/ ] && badPath "${p:2}"; then 57 - skip $p 58 elif [ "${p:0:3}" = -I/ ] && badPath "${p:2}"; then 59 - skip $p 60 elif [ "${p:0:4}" = -aI/ ] && badPath "${p:3}"; then 61 - skip $p 62 elif [ "${p:0:4}" = -aO/ ] && badPath "${p:3}"; then 63 - skip $p 64 else 65 rest+=("$p") 66 fi 67 - n=$((n + 1)) 68 done 69 params=("${rest[@]}") 70 fi ··· 73 # Clear march/mtune=native -- they bring impurity. 74 if [ "$NIX_ENFORCE_NO_NATIVE" = 1 ]; then 75 rest=() 76 - for i in "${params[@]}"; do 77 - if [[ "$i" = -m*=native ]]; then 78 - skip $i 79 else 80 - rest+=("$i") 81 fi 82 done 83 params=("${rest[@]}") ··· 88 extraAfter=($NIX_GNATFLAGS_COMPILE) 89 extraBefore=() 90 91 - if [ "`basename $0`x" = "gnatmakex" ]; then 92 - extraBefore=("--GNATBIND=@out@/bin/gnatbind --GNATLINK=@out@/bin/gnatlink ") 93 fi 94 95 - # Add the flags that should be passed to the linker (and prevent 96 - # `ld-wrapper' from adding NIX_LDFLAGS again). 97 - #for i in $NIX_LDFLAGS_BEFORE; do 98 - # extraBefore=(${extraBefore[@]} "-largs $i") 99 - #done 100 101 # Optionally print debug info. 102 if [ -n "$NIX_DEBUG" ]; then 103 - echo "original flags to @prog@:" >&2 104 - for i in "${params[@]}"; do 105 - echo " $i" >&2 106 - done 107 - echo "extraBefore flags to @prog@:" >&2 108 - for i in ${extraBefore[@]}; do 109 - echo " $i" >&2 110 - done 111 - echo "extraAfter flags to @prog@:" >&2 112 - for i in ${extraAfter[@]}; do 113 - echo " $i" >&2 114 - done 115 fi 116 117 if [ -n "$NIX_GNAT_WRAPPER_EXEC_HOOK" ]; then ··· 119 fi 120 121 PATH="$path_backup" 122 - exec @prog@ ${extraBefore[@]} "${params[@]}" ${extraAfter[@]}
··· 1 + #! @shell@ 2 + set -e -o pipefail 3 + shopt -s nullglob 4 + 5 path_backup="$PATH" 6 + 7 + # phase separation makes this look useless 8 + # shellcheck disable=SC2157 9 if [ -n "@coreutils_bin@" ]; then 10 + PATH="@coreutils_bin@/bin" 11 fi 12 13 if [ -n "$NIX_GNAT_WRAPPER_START_HOOK" ]; then ··· 24 # Figure out if linker flags should be passed. GCC prints annoying 25 # warnings when they are not needed. 26 dontLink=0 27 nonFlagArgs=0 28 29 for i in "$@"; do ··· 35 nonFlagArgs=1 36 elif [ "$i" = -m32 ]; then 37 if [ -e @out@/nix-support/dynamic-linker-m32 ]; then 38 + NIX_LDFLAGS+=" -dynamic-linker $(< @out@/nix-support/dynamic-linker-m32)" 39 fi 40 fi 41 done ··· 52 53 # Optionally filter out paths not refering to the store. 54 params=("$@") 55 + if [[ "$NIX_ENFORCE_PURITY" = 1 && -n "$NIX_STORE" ]]; then 56 rest=() 57 + for p in "${params[@]}"; do 58 if [ "${p:0:3}" = -L/ ] && badPath "${p:2}"; then 59 + skip "${p:2}" 60 elif [ "${p:0:3}" = -I/ ] && badPath "${p:2}"; then 61 + skip "${p:2}" 62 elif [ "${p:0:4}" = -aI/ ] && badPath "${p:3}"; then 63 + skip "${p:2}" 64 elif [ "${p:0:4}" = -aO/ ] && badPath "${p:3}"; then 65 + skip "${p:2}" 66 else 67 rest+=("$p") 68 fi 69 done 70 params=("${rest[@]}") 71 fi ··· 74 # Clear march/mtune=native -- they bring impurity. 75 if [ "$NIX_ENFORCE_NO_NATIVE" = 1 ]; then 76 rest=() 77 + for p in "${params[@]}"; do 78 + if [[ "$p" = -m*=native ]]; then 79 + skip "$p" 80 else 81 + rest+=("$p") 82 fi 83 done 84 params=("${rest[@]}") ··· 89 extraAfter=($NIX_GNATFLAGS_COMPILE) 90 extraBefore=() 91 92 + if [ "$(basename "$0")x" = "gnatmakex" ]; then 93 + extraBefore=("--GNATBIND=@out@/bin/gnatbind" "--GNATLINK=@out@/bin/gnatlink ") 94 fi 95 96 + #if [ "$dontLink" != 1 ]; then 97 + # # Add the flags that should be passed to the linker (and prevent 98 + # # `ld-wrapper' from adding NIX_LDFLAGS again). 99 + # for i in $NIX_LDFLAGS_BEFORE; do 100 + # extraBefore+=("-largs" "$i") 101 + # done 102 + # for i in $NIX_LDFLAGS; do 103 + # if [ "${i:0:3}" = -L/ ]; then 104 + # extraAfter+=("$i") 105 + # else 106 + # extraAfter+=("-largs" "$i") 107 + # fi 108 + # done 109 + # export NIX_LDFLAGS_SET=1 110 + #fi 111 112 # Optionally print debug info. 113 if [ -n "$NIX_DEBUG" ]; then 114 + echo "extra flags before to @prog@:" >&2 115 + printf " %q\n" "${extraBefore[@]}" >&2 116 + echo "original flags to @prog@:" >&2 117 + printf " %q\n" "${params[@]}" >&2 118 + echo "extra flags after to @prog@:" >&2 119 + printf " %q\n" "${extraAfter[@]}" >&2 120 fi 121 122 if [ -n "$NIX_GNAT_WRAPPER_EXEC_HOOK" ]; then ··· 124 fi 125 126 PATH="$path_backup" 127 + exec @prog@ "${extraBefore[@]}" "${params[@]}" "${extraAfter[@]}"
+10 -25
pkgs/build-support/cc-wrapper/ld-solaris-wrapper.sh
··· 1 #!@shell@ 2 3 - set -e 4 - set -u 5 - 6 # I've also tried adding -z direct and -z lazyload, but it gave too many problems with C++ exceptions :'( 7 # Also made sure libgcc would not be lazy-loaded, as suggested here: https://www.illumos.org/issues/2534#note-3 8 # but still no success. 9 - cmd="@ld@ -z ignore" 10 - 11 - args=("$@"); 12 13 # This loop makes sure all -L arguments are before -l arguments, or ld may complain it cannot find a library. 14 # GNU binutils does not have this problem: 15 # http://stackoverflow.com/questions/5817269/does-the-order-of-l-and-l-options-in-the-gnu-linker-matter 16 - i=0; 17 - while [[ $i -lt $# ]]; do 18 case "${args[$i]}" in 19 - -L) cmd="$cmd ${args[$i]} ${args[($i+1)]}"; i=($i+1); ;; 20 - -L*) cmd="$cmd ${args[$i]}" ;; 21 - *) ;; 22 esac 23 - i=($i+1); 24 - done 25 - 26 - i=0; 27 - while [[ $i -lt $# ]]; do 28 - case "${args[$i]}" in 29 - -L) i=($i+1); ;; 30 - -L*) ;; 31 - *) cmd="$cmd ${args[$i]}" ;; 32 - esac 33 - i=($i+1); 34 done 35 36 # Trace: 37 set -x 38 - exec $cmd 39 - 40 - exit 0
··· 1 #!@shell@ 2 + set -eu -o pipefail 3 + shopt -s nullglob 4 5 + declare -a args=("$@") 6 # I've also tried adding -z direct and -z lazyload, but it gave too many problems with C++ exceptions :'( 7 # Also made sure libgcc would not be lazy-loaded, as suggested here: https://www.illumos.org/issues/2534#note-3 8 # but still no success. 9 + declare -a argsBefore=(-z ignore) argsAfter=() 10 11 # This loop makes sure all -L arguments are before -l arguments, or ld may complain it cannot find a library. 12 # GNU binutils does not have this problem: 13 # http://stackoverflow.com/questions/5817269/does-the-order-of-l-and-l-options-in-the-gnu-linker-matter 14 + while (( $# )); do 15 case "${args[$i]}" in 16 + -L) argsBefore+=("$1" "$2"); shift ;; 17 + -L?*) argsBefore+=("$1") ;; 18 + *) argsAfter+=("$1") ;; 19 esac 20 + shift 21 done 22 23 # Trace: 24 set -x 25 + exec "@ld@" "${argsBefore[@]}" "${argsAfter[@]}"
+22 -16
pkgs/build-support/cc-wrapper/ld-wrapper.sh
··· 1 - #! @shell@ -e 2 shopt -s nullglob 3 path_backup="$PATH" 4 if [ -n "@coreutils_bin@" ]; then 5 - PATH="@coreutils_bin@/bin" 6 fi 7 8 if [ -n "$NIX_LD_WRAPPER_START_HOOK" ]; then ··· 18 19 # Optionally filter out paths not refering to the store. 20 expandResponseParams "$@" 21 - if [ "$NIX_ENFORCE_PURITY" = 1 -a -n "$NIX_STORE" \ 22 - -a \( -z "$NIX_IGNORE_LD_THROUGH_GCC" -o -z "$NIX_LDFLAGS_SET" \) ]; then 23 rest=() 24 nParams=${#params[@]} 25 declare -i n=0 26 - while [ $n -lt $nParams ]; do 27 p=${params[n]} 28 - p2=${params[$((n+1))]} 29 if [ "${p:0:3}" = -L/ ] && badPath "${p:2}"; then 30 skip "${p:2}" 31 elif [ "$p" = -L ] && badPath "$p2"; then ··· 49 params=("${rest[@]}") 50 fi 51 52 - LD=@prog@ 53 source @out@/nix-support/add-hardening.sh 54 55 - extra=("${hardeningLDFlags[@]}") 56 extraBefore=() 57 58 if [ -z "$NIX_LDFLAGS_SET" ]; then 59 - extra+=($NIX_LDFLAGS) 60 extraBefore+=($NIX_LDFLAGS_BEFORE) 61 fi 62 63 - extra+=($NIX_LDFLAGS_AFTER $NIX_LDFLAGS_HARDEN) 64 65 declare -a libDirs 66 declare -A libs ··· 69 # Find all -L... switches for rpath, and relocatable flags for build id. 70 if [ "$NIX_DONT_SET_RPATH" != 1 ] || [ "$NIX_SET_BUILD_ID" = 1 ]; then 71 prev= 72 - for p in "${params[@]}" "${extra[@]}"; do 73 case "$prev" in 74 -L) 75 libDirs+=("$p") ··· 127 libs["$file"]= 128 if [ ! "${rpaths[$dir]}" ]; then 129 rpaths["$dir"]=1 130 - extra+=(-rpath "$dir") 131 fi 132 fi 133 done ··· 138 # Only add --build-id if this is a final link. FIXME: should build gcc 139 # with --enable-linker-build-id instead? 140 if [ "$NIX_SET_BUILD_ID" = 1 ] && [ ! "$relocatable" ]; then 141 - extra+=(--build-id) 142 fi 143 144 145 # Optionally print debug info. 146 if [ -n "$NIX_DEBUG" ]; then 147 echo "original flags to @prog@:" >&2 148 printf " %q\n" "${params[@]}" >&2 149 - echo "extra flags to @prog@:" >&2 150 - printf " %q\n" "${extraBefore[@]}" "${extra[@]}" >&2 151 fi 152 153 if [ -n "$NIX_LD_WRAPPER_EXEC_HOOK" ]; then ··· 155 fi 156 157 PATH="$path_backup" 158 - exec @prog@ "${extraBefore[@]}" "${params[@]}" "${extra[@]}"
··· 1 + #! @shell@ 2 + set -e -o pipefail 3 shopt -s nullglob 4 + 5 path_backup="$PATH" 6 + 7 + # phase separation makes this look useless 8 + # shellcheck disable=SC2157 9 if [ -n "@coreutils_bin@" ]; then 10 + PATH="@coreutils_bin@/bin" 11 fi 12 13 if [ -n "$NIX_LD_WRAPPER_START_HOOK" ]; then ··· 23 24 # Optionally filter out paths not refering to the store. 25 expandResponseParams "$@" 26 + if [[ "$NIX_ENFORCE_PURITY" = 1 && -n "$NIX_STORE" 27 + && ( -z "$NIX_IGNORE_LD_THROUGH_GCC" || -z "$NIX_LDFLAGS_SET" ) ]]; then 28 rest=() 29 nParams=${#params[@]} 30 declare -i n=0 31 + while [ "$n" -lt "$nParams" ]; do 32 p=${params[n]} 33 + p2=${params[n+1]} 34 if [ "${p:0:3}" = -L/ ] && badPath "${p:2}"; then 35 skip "${p:2}" 36 elif [ "$p" = -L ] && badPath "$p2"; then ··· 54 params=("${rest[@]}") 55 fi 56 57 source @out@/nix-support/add-hardening.sh 58 59 + extraAfter=("${hardeningLDFlags[@]}") 60 extraBefore=() 61 62 if [ -z "$NIX_LDFLAGS_SET" ]; then 63 + extraAfter+=($NIX_LDFLAGS) 64 extraBefore+=($NIX_LDFLAGS_BEFORE) 65 fi 66 67 + extraAfter+=($NIX_LDFLAGS_AFTER $NIX_LDFLAGS_HARDEN) 68 69 declare -a libDirs 70 declare -A libs ··· 73 # Find all -L... switches for rpath, and relocatable flags for build id. 74 if [ "$NIX_DONT_SET_RPATH" != 1 ] || [ "$NIX_SET_BUILD_ID" = 1 ]; then 75 prev= 76 + for p in "${extraBefore[@]}" "${params[@]}" "${extraAfter[@]}"; do 77 case "$prev" in 78 -L) 79 libDirs+=("$p") ··· 131 libs["$file"]= 132 if [ ! "${rpaths[$dir]}" ]; then 133 rpaths["$dir"]=1 134 + extraAfter+=(-rpath "$dir") 135 fi 136 fi 137 done ··· 142 # Only add --build-id if this is a final link. FIXME: should build gcc 143 # with --enable-linker-build-id instead? 144 if [ "$NIX_SET_BUILD_ID" = 1 ] && [ ! "$relocatable" ]; then 145 + extraAfter+=(--build-id) 146 fi 147 148 149 # Optionally print debug info. 150 if [ -n "$NIX_DEBUG" ]; then 151 + echo "extra flags before to @prog@:" >&2 152 + printf " %q\n" "${extraBefore[@]}" >&2 153 echo "original flags to @prog@:" >&2 154 printf " %q\n" "${params[@]}" >&2 155 + echo "extra flags after to @prog@:" >&2 156 + printf " %q\n" "${extraAfter[@]}" >&2 157 fi 158 159 if [ -n "$NIX_LD_WRAPPER_EXEC_HOOK" ]; then ··· 161 fi 162 163 PATH="$path_backup" 164 + exec @prog@ "${extraBefore[@]}" "${params[@]}" "${extraAfter[@]}"
+14 -9
pkgs/build-support/cc-wrapper/setup-hook.sh
··· 1 addCVars () { 2 - if [ -d $1/include ]; then 3 export NIX_CFLAGS_COMPILE+=" ${ccIncludeFlag:--isystem} $1/include" 4 fi 5 6 - if [ -d $1/lib64 -a ! -L $1/lib64 ]; then 7 export NIX_LDFLAGS+=" -L$1/lib64" 8 fi 9 10 - if [ -d $1/lib ]; then 11 export NIX_LDFLAGS+=" -L$1/lib" 12 fi 13 14 - if test -d $1/Library/Frameworks; then 15 - export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -F$1/Library/Frameworks" 16 fi 17 } 18 19 envHooks+=(addCVars) 20 21 - # Note: these come *after* $out in the PATH (see setup.sh). 22 23 if [ -n "@cc@" ]; then 24 addToSearchPath _PATH @cc@/bin 25 fi 26 27 if [ -n "@binutils_bin@" ]; then 28 addToSearchPath _PATH @binutils_bin@/bin 29 fi 30 31 if [ -n "@libc_bin@" ]; then 32 addToSearchPath _PATH @libc_bin@/bin 33 fi 34 35 if [ -n "@coreutils_bin@" ]; then 36 addToSearchPath _PATH @coreutils_bin@/bin 37 fi 38 39 - if [ -z "$crossConfig" ]; then 40 - ENV_PREFIX="" 41 else 42 - ENV_PREFIX="BUILD_" 43 fi 44 45 export NIX_${ENV_PREFIX}CC=@out@
··· 1 addCVars () { 2 + if [[ -d "$1/include" ]]; then 3 export NIX_CFLAGS_COMPILE+=" ${ccIncludeFlag:--isystem} $1/include" 4 fi 5 6 + if [[ -d "$1/lib64" && ! -L "$1/lib64" ]]; then 7 export NIX_LDFLAGS+=" -L$1/lib64" 8 fi 9 10 + if [[ -d "$1/lib" ]]; then 11 export NIX_LDFLAGS+=" -L$1/lib" 12 fi 13 14 + if [[ -d "$1/Library/Frameworks" ]]; then 15 + export NIX_CFLAGS_COMPILE+=" -F$1/Library/Frameworks" 16 fi 17 } 18 19 envHooks+=(addCVars) 20 21 + # Note 1: these come *after* $out in the PATH (see setup.sh). 22 + # Note 2: phase separation makes this look useless to shellcheck. 23 24 + # shellcheck disable=SC2157 25 if [ -n "@cc@" ]; then 26 addToSearchPath _PATH @cc@/bin 27 fi 28 29 + # shellcheck disable=SC2157 30 if [ -n "@binutils_bin@" ]; then 31 addToSearchPath _PATH @binutils_bin@/bin 32 fi 33 34 + # shellcheck disable=SC2157 35 if [ -n "@libc_bin@" ]; then 36 addToSearchPath _PATH @libc_bin@/bin 37 fi 38 39 + # shellcheck disable=SC2157 40 if [ -n "@coreutils_bin@" ]; then 41 addToSearchPath _PATH @coreutils_bin@/bin 42 fi 43 44 + if [ -z "${crossConfig:-}" ]; then 45 + ENV_PREFIX="" 46 else 47 + ENV_PREFIX="BUILD_" 48 fi 49 50 export NIX_${ENV_PREFIX}CC=@out@
+5 -1
pkgs/build-support/cc-wrapper/utils.sh
··· 24 } 25 26 expandResponseParams() { 27 - params=("$@") 28 local arg 29 for arg in "$@"; do 30 if [[ "$arg" == @* ]]; then 31 if [ -n "@expandResponseParams@" ]; then 32 readarray -d '' params < <("@expandResponseParams@" "$@") 33 return 0 34 else
··· 24 } 25 26 expandResponseParams() { 27 + declare -g params=("$@") 28 local arg 29 for arg in "$@"; do 30 if [[ "$arg" == @* ]]; then 31 + # phase separation makes this look useless 32 + # shellcheck disable=SC2157 33 if [ -n "@expandResponseParams@" ]; then 34 + # params is used by caller 35 + #shellcheck disable=SC2034 36 readarray -d '' params < <("@expandResponseParams@" "$@") 37 return 0 38 else