cc-wrapper: Fix standalone gcc

This ensures that all salted variables are defined even if the wrapped program
is invoked outside nix-build environment.

authored by Orivej Desh and committed by Robin Gloster c8e9dcc8 c8f7f18e

+44 -67
+44 -67
pkgs/build-support/cc-wrapper/add-flags.sh
··· 4 # that case, it is cheaper/better to not repeat this step and let the forked 5 # wrapped binary just inherit the work of the forker's wrapper script. 6 7 - # Accumulate prefixes for taking in the right input parameters. See setup-hook 8 # for details. 9 - declare -a role_prefixes=() 10 - if [[ -n "${NIX_CC_WRAPPER_@infixSalt@_TARGET_BUILD:-}" ]]; then 11 - role_prefixes+=(_BUILD) 12 fi 13 - if [[ -n "${NIX_CC_WRAPPER_@infixSalt@_TARGET_HOST:-}" ]]; then 14 - role_prefixes+=('') 15 fi 16 - if [[ -n "${NIX_CC_WRAPPER_@infixSalt@_TARGET_TARGET:-}" ]]; then 17 - role_prefixes+=(_TARGET) 18 fi 19 20 - # For each role we serve, we accumulate the input parameters into our own 21 - # cc-wrapper-derivation-specific environment variables. 22 - for pre in "${role_prefixes[@]}"; do 23 - # We need to mangle names for hygiene, but also take parameters/overrides 24 - # from the environment. 25 - slurpUnsalted () { 26 - case "$1" in 27 - CC_WRAPPER_*) 28 - local firstPre=NIX_CC_WRAPPER_ 29 - local varname="${1#CC_WRAPPER_}" 30 - ;; 31 - LD_WRAPPER_*) 32 - local firstPre=NIX_LD_WRAPPER_ 33 - local varname="${1#LD_WRAPPER_}" 34 - ;; 35 - *) 36 - local firstPre=NIX_ 37 - local varname="$1" 38 - ;; 39 - esac 40 - local inputVar="${firstPre}${pre}${varname}" 41 - local outputVar="${firstPre}@infixSalt@_${varname}" 42 - local delimiter='' 43 - if [[ -n "${!outputVar:-}" && -n "${!inputVar:-}" ]]; then 44 - delimiter=' ' 45 fi 46 - # Easiest to just do this to deal with either the input or (old) output. 47 - set +u 48 - export ${outputVar}+="${delimiter}${!inputVar}" 49 - set -u 50 - } 51 - 52 - slurpUnsalted CC_WRAPPER_START_HOOK 53 - slurpUnsalted CC_WRAPPER_EXEC_HOOK 54 - slurpUnsalted LD_WRAPPER_START_HOOK 55 - slurpUnsalted LD_WRAPPER_EXEC_HOOK 56 - 57 - slurpUnsalted CFLAGS_COMPILE 58 - slurpUnsalted CFLAGS_LINK 59 - slurpUnsalted CXXSTDLIB_COMPILE 60 - slurpUnsalted CXXSTDLIB_LINK 61 - slurpUnsalted GNATFLAGS_COMPILE 62 - slurpUnsalted IGNORE_LD_THROUGH_GCC 63 - slurpUnsalted LDFLAGS 64 - slurpUnsalted LDFLAGS_BEFORE 65 - slurpUnsalted LDFLAGS_AFTER 66 - slurpUnsalted LDFLAGS_HARDEN 67 - 68 - slurpUnsalted SET_BUILD_ID 69 - slurpUnsalted DONT_SET_RPATH 70 - slurpUnsalted ENFORCE_NO_NATIVE 71 done 72 - unset -f slurpUnsalted 73 74 # `-B@out@/bin' forces cc to use ld-wrapper.sh when calling ld. 75 - export NIX_@infixSalt@_CFLAGS_COMPILE="-B@out@/bin/ $NIX_@infixSalt@_CFLAGS_COMPILE" 76 77 # Export and assign separately in order that a failing $(..) will fail 78 # the script. 79 80 if [ -e @out@/nix-support/libc-cflags ]; then 81 - export NIX_@infixSalt@_CFLAGS_COMPILE 82 NIX_@infixSalt@_CFLAGS_COMPILE="$(< @out@/nix-support/libc-cflags) $NIX_@infixSalt@_CFLAGS_COMPILE" 83 fi 84 85 if [ -e @out@/nix-support/cc-cflags ]; then 86 - export NIX_@infixSalt@_CFLAGS_COMPILE 87 NIX_@infixSalt@_CFLAGS_COMPILE="$(< @out@/nix-support/cc-cflags) $NIX_@infixSalt@_CFLAGS_COMPILE" 88 fi 89 90 if [ -e @out@/nix-support/gnat-cflags ]; then 91 - export NIX_@infixSalt@_GNATFLAGS_COMPILE 92 NIX_@infixSalt@_GNATFLAGS_COMPILE="$(< @out@/nix-support/gnat-cflags) $NIX_@infixSalt@_GNATFLAGS_COMPILE" 93 fi 94 95 if [ -e @out@/nix-support/libc-ldflags ]; then 96 - export NIX_@infixSalt@_LDFLAGS 97 NIX_@infixSalt@_LDFLAGS+=" $(< @out@/nix-support/libc-ldflags)" 98 fi 99 100 if [ -e @out@/nix-support/cc-ldflags ]; then 101 - export NIX_@infixSalt@_LDFLAGS 102 NIX_@infixSalt@_LDFLAGS+=" $(< @out@/nix-support/cc-ldflags)" 103 fi 104 105 if [ -e @out@/nix-support/libc-ldflags-before ]; then 106 - export NIX_@infixSalt@_LDFLAGS_BEFORE 107 NIX_@infixSalt@_LDFLAGS_BEFORE="$(< @out@/nix-support/libc-ldflags-before) $NIX_@infixSalt@_LDFLAGS_BEFORE" 108 fi 109 110 - # That way forked processes don't againt extend these environment variables 111 export NIX_CC_WRAPPER_@infixSalt@_FLAGS_SET=1
··· 4 # that case, it is cheaper/better to not repeat this step and let the forked 5 # wrapped binary just inherit the work of the forker's wrapper script. 6 7 + var_templates=( 8 + NIX_CC_WRAPPER+START_HOOK 9 + NIX_CC_WRAPPER+EXEC_HOOK 10 + NIX_LD_WRAPPER+START_HOOK 11 + NIX_LD_WRAPPER+EXEC_HOOK 12 + 13 + NIX+CFLAGS_COMPILE 14 + NIX+CFLAGS_LINK 15 + NIX+CXXSTDLIB_COMPILE 16 + NIX+CXXSTDLIB_LINK 17 + NIX+GNATFLAGS_COMPILE 18 + NIX+IGNORE_LD_THROUGH_GCC 19 + NIX+LDFLAGS 20 + NIX+LDFLAGS_BEFORE 21 + NIX+LDFLAGS_AFTER 22 + NIX+LDFLAGS_HARDEN 23 + 24 + NIX+SET_BUILD_ID 25 + NIX+DONT_SET_RPATH 26 + NIX+ENFORCE_NO_NATIVE 27 + ) 28 + 29 + # Accumulate infixes for taking in the right input parameters. See setup-hook 30 # for details. 31 + declare -a role_infixes=() 32 + if [ "${NIX_CC_WRAPPER_@infixSalt@_TARGET_BUILD:-}" ]; then 33 + role_infixes+=(_BUILD_) 34 fi 35 + if [ "${NIX_CC_WRAPPER_@infixSalt@_TARGET_HOST:-}" ]; then 36 + role_infixes+=(_) 37 fi 38 + if [ "${NIX_CC_WRAPPER_@infixSalt@_TARGET_TARGET:-}" ]; then 39 + role_infixes+=(_TARGET_) 40 fi 41 42 + # We need to mangle names for hygiene, but also take parameters/overrides 43 + # from the environment. 44 + for var in "${var_templates[@]}"; do 45 + outputVar="${var/+/_@infixSalt@_}" 46 + export ${outputVar}+='' 47 + # For each role we serve, we accumulate the input parameters into our own 48 + # cc-wrapper-derivation-specific environment variables. 49 + for infix in "${role_infixes[@]}"; do 50 + inputVar="${var/+/${infix}}" 51 + if [ -v "$inputVar" ]; then 52 + export ${outputVar}+="${!outputVar:+ }${!inputVar}" 53 fi 54 + done 55 done 56 57 # `-B@out@/bin' forces cc to use ld-wrapper.sh when calling ld. 58 + NIX_@infixSalt@_CFLAGS_COMPILE="-B@out@/bin/ $NIX_@infixSalt@_CFLAGS_COMPILE" 59 60 # Export and assign separately in order that a failing $(..) will fail 61 # the script. 62 63 if [ -e @out@/nix-support/libc-cflags ]; then 64 NIX_@infixSalt@_CFLAGS_COMPILE="$(< @out@/nix-support/libc-cflags) $NIX_@infixSalt@_CFLAGS_COMPILE" 65 fi 66 67 if [ -e @out@/nix-support/cc-cflags ]; then 68 NIX_@infixSalt@_CFLAGS_COMPILE="$(< @out@/nix-support/cc-cflags) $NIX_@infixSalt@_CFLAGS_COMPILE" 69 fi 70 71 if [ -e @out@/nix-support/gnat-cflags ]; then 72 NIX_@infixSalt@_GNATFLAGS_COMPILE="$(< @out@/nix-support/gnat-cflags) $NIX_@infixSalt@_GNATFLAGS_COMPILE" 73 fi 74 75 if [ -e @out@/nix-support/libc-ldflags ]; then 76 NIX_@infixSalt@_LDFLAGS+=" $(< @out@/nix-support/libc-ldflags)" 77 fi 78 79 if [ -e @out@/nix-support/cc-ldflags ]; then 80 NIX_@infixSalt@_LDFLAGS+=" $(< @out@/nix-support/cc-ldflags)" 81 fi 82 83 if [ -e @out@/nix-support/libc-ldflags-before ]; then 84 NIX_@infixSalt@_LDFLAGS_BEFORE="$(< @out@/nix-support/libc-ldflags-before) $NIX_@infixSalt@_LDFLAGS_BEFORE" 85 fi 86 87 + # That way forked processes will not extend these environment variables again. 88 export NIX_CC_WRAPPER_@infixSalt@_FLAGS_SET=1