diff --git a/cabal-testsuite/PackageTests/PreProcess/TooManyCommandLineArgs/custom-cc b/cabal-testsuite/PackageTests/PreProcess/TooManyCommandLineArgs/custom-cc
new file mode 100755
index 0000000000000000000000000000000000000000..79054cac89f5de89e040e0f08534edf242748215
--- /dev/null
+++ b/cabal-testsuite/PackageTests/PreProcess/TooManyCommandLineArgs/custom-cc
@@ -0,0 +1,72 @@
+#!/usr/bin/env bash
+
+# NB only check for duplicated -L or -I arguments because that’s the
+# primary source of duplication. Some minor duplicates can also occur,
+# like -Wl,--no-as-needed but there’s not too many of them yet to
+# cause command line overflow.
+
+# Quadratic algorithm because darwin CI doesn’t seem to have bash that
+# supports associative arrays.
+check_duplicate() {
+ local test_arg="$1"
+ local occurs="0"
+
+ if ! [[ "$test_arg" == -L* || "$test_arg" == -I* ]]; then
+ return 0
+ fi
+
+ shift
+ for tmp in "${@}"; do
+ if [[ "$tmp" == @* ]]; then
+ while IFS= read -d $'\n' -r arg ; do
+ if [[ "$arg" == "$test_arg" ]]; then
+ occurs=$((occurs + 1))
+ fi
+ done <"${tmp#@}"
+ else
+ if [[ "$tmp" == "$test_arg" ]]; then
+ occurs=$((occurs + 1))
+ fi
+ fi
+ done
+
+ if [[ "$occurs" -gt 1 ]]; then
+ return 1
+ else
+ return 0
+ fi
+}
+
+i=1
+for x in "${@}"; do
+ if [[ "$x" == @* ]]; then
+ file=
+ while IFS= read -d $'\n' -r arg ; do
+ y="$arg"
+ if ! check_duplicate "$arg" "${@:$i}"; then
+ echo "Duplicate argument detected: '$y'"
+ echo "All args: ${@}"
+ exit 1
+ fi
+ done <"${x#@}"
+ else
+ if ! check_duplicate "$x" "${@:$i}"; then
+ echo "Duplicate argument detected: '$x'"
+ echo "All args: ${@}"
+ exit 1
+ fi
+ fi
+
+ i=$((i + 1))
+done
+
+if which cc >/dev/null 2>&1; then
+ cc -DNOERROR6 "${@}"
+elif which gcc >/dev/null 2>&1; then
+ gcc -DNOERROR6 "${@}"
+elif which clang >/dev/null 2>&1; then
+ clang -DNOERROR6 "${@}"
+else
+ echo "Cannot find C compiler" >&2
+ exit 1
+fi
diff --git a/cabal-testsuite/PackageTests/PreProcess/TooManyCommandLineArgs/setup.test.hs b/cabal-testsuite/PackageTests/PreProcess/TooManyCommandLineArgs/setup.test.hs
index 0a89a85e6fa277f3e639c111ef4bf635ff38b09b..036d10cbdce7c66cc329fc6110ae968bb8eb9055 100644
--- a/cabal-testsuite/PackageTests/PreProcess/TooManyCommandLineArgs/setup.test.hs
+++ b/cabal-testsuite/PackageTests/PreProcess/TooManyCommandLineArgs/setup.test.hs
@@ -87,9 +87,20 @@ main = cabalTest $ do
libFlags =
concatMap (\x -> ["--extra-lib-dirs", cwd </> x]) libDirs
+ let customCC = cwd </> "custom-cc"
+
-- Parallel flag means output of this test is nondeterministic
_<- recordMode DoNotRecord $
- cabal "v2-build" $ ["-j", "my-toplevel:exe:my-executable"] ++ includeFlags ++ libFlags
+ cabal "v2-build" $
+ ["-j", "my-toplevel:exe:my-executable"]
+ -- Don’t validate absence of duplicates on Windows because
+ -- it’s too inconvenient to do in bat files. Let’s just
+ -- assume that deduplication will happen on Windows but
+ -- still try to run the test to see whether command-line
+ -- argument length limit is not hit.
+ ++ ["--with-gcc=" ++ customCC | not isWindows]
+ ++ includeFlags
+ ++ libFlags
r <- withPlan $ runPlanExe' "my-toplevel" "my-executable" []
assertOutputContains
("Result = " ++ show (42 + 55 + 10 * (55 + 10 * 55) + 3 * 55))