about summary refs log tree commit diff
path: root/pkgs
diff options
context:
space:
mode:
authorJohn Ericson <git@JohnEricson.me>2023-05-08 06:55:52 -0400
committerGitHub <noreply@github.com>2023-05-08 06:55:52 -0400
commitf4868c6c7da2f28aca762f69759f07ffe9536356 (patch)
treed2b15595a645d054c9c05d319f904528ea6e7503 /pkgs
parente7799046c76fc7105e859ef50c2e5f8f8872a0b2 (diff)
parent40c914f1be018545992a5b2e3182f3b940f907ce (diff)
Merge pull request #229754 from rrbutani/fix/cc-wrapper-extra-positional-args
Support `--` in `cc-wrapper`
Diffstat (limited to 'pkgs')
-rw-r--r--pkgs/build-support/cc-wrapper/cc-wrapper.sh18
-rw-r--r--pkgs/test/cc-wrapper/default.nix23
2 files changed, 41 insertions, 0 deletions
diff --git a/pkgs/build-support/cc-wrapper/cc-wrapper.sh b/pkgs/build-support/cc-wrapper/cc-wrapper.sh
index e8eb579e15add..5350fc3cc9ae5 100644
--- a/pkgs/build-support/cc-wrapper/cc-wrapper.sh
+++ b/pkgs/build-support/cc-wrapper/cc-wrapper.sh
@@ -33,6 +33,7 @@ cInclude=1
 expandResponseParams "$@"
 linkType=$(checkLinkType "${params[@]}")
 
+declare -ag positionalArgs=()
 declare -i n=0
 nParams=${#params[@]}
 while (( "$n" < "$nParams" )); do
@@ -54,6 +55,17 @@ while (( "$n" < "$nParams" )); do
                 c++*) isCxx=1 ;;
             esac
             ;;
+        --) # Everything else is positional args!
+            # See: https://github.com/llvm/llvm-project/commit/ed1d07282cc9d8e4c25d585e03e5c8a1b6f63a74
+
+            # Any positional arg (i.e. any argument after `--`) will be
+            # interpreted as a "non flag" arg:
+            if [[ -v "params[$n]" ]]; then nonFlagArgs=1; fi
+
+            positionalArgs=("${params[@]:$n}")
+            params=("${params[@]:0:$((n - 1))}")
+            break;
+            ;;
         -?*) ;;
         *) nonFlagArgs=1 ;; # Includes a solitary dash (`-`) which signifies standard input; it is not a flag
     esac
@@ -207,6 +219,12 @@ if [ "$cc1" = 1 ]; then
   extraBefore=()
 fi
 
+# Finally, if we got any positional args, append them to `extraAfter`
+# now:
+if [[ "${#positionalArgs[@]}" -gt 0 ]]; then
+    extraAfter+=(-- "${positionalArgs[@]}")
+fi
+
 # Optionally print debug info.
 if (( "${NIX_DEBUG:-0}" >= 1 )); then
     # Old bash workaround, see ld-wrapper for explanation.
diff --git a/pkgs/test/cc-wrapper/default.nix b/pkgs/test/cc-wrapper/default.nix
index 6fd7d9f45db87..43e8e7a214268 100644
--- a/pkgs/test/cc-wrapper/default.nix
+++ b/pkgs/test/cc-wrapper/default.nix
@@ -13,6 +13,8 @@ in stdenv.mkDerivation {
   name = "cc-wrapper-test";
 
   buildCommand = ''
+    set -o pipefail
+
     NIX_DEBUG=1 $CC -v
     NIX_DEBUG=1 $CXX -v
 
@@ -43,6 +45,27 @@ in stdenv.mkDerivation {
       ''}
     ''}
 
+    ${# See: https://github.com/llvm/llvm-project/commit/ed1d07282cc9d8e4c25d585e03e5c8a1b6f63a74
+      # `gcc` does not support this so we gate the test on `clang`
+      lib.optionalString stdenv.cc.isClang ''
+        printf "checking whether cc-wrapper accepts -- followed by positional (file) args..." >&2
+        mkdir -p positional
+
+        # Make sure `--` is not parsed as a "non flag arg"; we should get an
+        # input file error here and *not* a linker error.
+        { ! $CC --; } |& grep -q "no input files"
+
+        # And that positional file args _must_ be files (this is just testing
+        # that we remembered to put the `--` back in the args to the compiler):
+        { ! $CC -c -- -o foo ${./foo.c}; } \
+          |& grep -q "no such file or directory: '-o'"
+
+        # Now check that we accept single and multiple positional file args:
+        $CC -c -DVALUE=42 -o positional/foo.o -- ${./foo.c}
+        $CC -o positional/main -- positional/foo.o ${./ldflags-main.c}
+        ${emulator} ./positional/main
+    ''}
+
     printf "checking whether compiler uses NIX_CFLAGS_COMPILE... " >&2
     mkdir -p foo/include
     cp ${./foo.c} foo/include/foo.h