nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix

Merge pull request #229754 from rrbutani/fix/cc-wrapper-extra-positional-args

Support `--` in `cc-wrapper`

authored by

John Ericson and committed by
GitHub
f4868c6c e7799046

+41
+18
pkgs/build-support/cc-wrapper/cc-wrapper.sh
··· 33 33 expandResponseParams "$@" 34 34 linkType=$(checkLinkType "${params[@]}") 35 35 36 + declare -ag positionalArgs=() 36 37 declare -i n=0 37 38 nParams=${#params[@]} 38 39 while (( "$n" < "$nParams" )); do ··· 54 53 *-header) dontLink=1 ;; 55 54 c++*) isCxx=1 ;; 56 55 esac 56 + ;; 57 + --) # Everything else is positional args! 58 + # See: https://github.com/llvm/llvm-project/commit/ed1d07282cc9d8e4c25d585e03e5c8a1b6f63a74 59 + 60 + # Any positional arg (i.e. any argument after `--`) will be 61 + # interpreted as a "non flag" arg: 62 + if [[ -v "params[$n]" ]]; then nonFlagArgs=1; fi 63 + 64 + positionalArgs=("${params[@]:$n}") 65 + params=("${params[@]:0:$((n - 1))}") 66 + break; 57 67 ;; 58 68 -?*) ;; 59 69 *) nonFlagArgs=1 ;; # Includes a solitary dash (`-`) which signifies standard input; it is not a flag ··· 217 205 if [ "$cc1" = 1 ]; then 218 206 extraAfter=() 219 207 extraBefore=() 208 + fi 209 + 210 + # Finally, if we got any positional args, append them to `extraAfter` 211 + # now: 212 + if [[ "${#positionalArgs[@]}" -gt 0 ]]; then 213 + extraAfter+=(-- "${positionalArgs[@]}") 220 214 fi 221 215 222 216 # Optionally print debug info.
+23
pkgs/test/cc-wrapper/default.nix
··· 13 13 name = "cc-wrapper-test"; 14 14 15 15 buildCommand = '' 16 + set -o pipefail 17 + 16 18 NIX_DEBUG=1 $CC -v 17 19 NIX_DEBUG=1 $CXX -v 18 20 ··· 43 41 $CC ${staticLibc} -static-pie -o cc-static-pie ${./cc-main.c} 44 42 ${emulator} ./cc-static-pie 45 43 ''} 44 + ''} 45 + 46 + ${# See: https://github.com/llvm/llvm-project/commit/ed1d07282cc9d8e4c25d585e03e5c8a1b6f63a74 47 + # `gcc` does not support this so we gate the test on `clang` 48 + lib.optionalString stdenv.cc.isClang '' 49 + printf "checking whether cc-wrapper accepts -- followed by positional (file) args..." >&2 50 + mkdir -p positional 51 + 52 + # Make sure `--` is not parsed as a "non flag arg"; we should get an 53 + # input file error here and *not* a linker error. 54 + { ! $CC --; } |& grep -q "no input files" 55 + 56 + # And that positional file args _must_ be files (this is just testing 57 + # that we remembered to put the `--` back in the args to the compiler): 58 + { ! $CC -c -- -o foo ${./foo.c}; } \ 59 + |& grep -q "no such file or directory: '-o'" 60 + 61 + # Now check that we accept single and multiple positional file args: 62 + $CC -c -DVALUE=42 -o positional/foo.o -- ${./foo.c} 63 + $CC -o positional/main -- positional/foo.o ${./ldflags-main.c} 64 + ${emulator} ./positional/main 46 65 ''} 47 66 48 67 printf "checking whether compiler uses NIX_CFLAGS_COMPILE... " >&2