Skip to content

Commit cf07ad1

Browse files
committed
Override C compiler for testing that no duplicate arguments are passed
1 parent 22c352d commit cf07ad1

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env bash
2+
3+
# NB only check for duplicated -L or -I arguments because that’s the
4+
# primary source of duplication. Some minor duplicates can also occur,
5+
# like -Wl,--no-as-needed but there’s not too many of them yet to
6+
# cause command line overflow.
7+
8+
# Quadratic algorithm because darwin CI doesn’t seem to have bash that
9+
# supports associative arrays.
10+
check_duplicate() {
11+
local test_arg="$1"
12+
local occurs="0"
13+
14+
if ! [[ "$test_arg" == -L* || "$test_arg" == -I* ]]; then
15+
return 0
16+
fi
17+
18+
shift
19+
for tmp in "${@}"; do
20+
if [[ "$tmp" == @* ]]; then
21+
while IFS= read -d $'\n' -r arg ; do
22+
if [[ "$arg" == "$test_arg" ]]; then
23+
occurs=$((occurs + 1))
24+
fi
25+
done <"${tmp#@}"
26+
else
27+
if [[ "$tmp" == "$test_arg" ]]; then
28+
occurs=$((occurs + 1))
29+
fi
30+
fi
31+
done
32+
33+
if [[ "$occurs" -gt 1 ]]; then
34+
return 1
35+
else
36+
return 0
37+
fi
38+
}
39+
40+
i=1
41+
for x in "${@}"; do
42+
if [[ "$x" == @* ]]; then
43+
file=
44+
while IFS= read -d $'\n' -r arg ; do
45+
y="$arg"
46+
if ! check_duplicate "$arg" "${@:$i}"; then
47+
echo "Duplicate argument detected: '$y'"
48+
echo "All args: ${@}"
49+
exit 1
50+
fi
51+
done <"${x#@}"
52+
else
53+
if ! check_duplicate "$x" "${@:$i}"; then
54+
echo "Duplicate argument detected: '$x'"
55+
echo "All args: ${@}"
56+
exit 1
57+
fi
58+
fi
59+
60+
i=$((i + 1))
61+
done
62+
63+
if which cc >/dev/null 2>&1; then
64+
cc -DNOERROR6 "${@}"
65+
elif which gcc >/dev/null 2>&1; then
66+
gcc -DNOERROR6 "${@}"
67+
elif which clang >/dev/null 2>&1; then
68+
clang -DNOERROR6 "${@}"
69+
else
70+
echo "Cannot find C compiler" >&2
71+
exit 1
72+
fi

cabal-testsuite/PackageTests/PreProcess/TooManyCommandLineArgs/setup.test.hs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,20 @@ main = cabalTest $ do
8787
libFlags =
8888
concatMap (\x -> ["--extra-lib-dirs", cwd </> x]) libDirs
8989

90+
let customCC = cwd </> "custom-cc"
91+
9092
-- Parallel flag means output of this test is nondeterministic
9193
_<- recordMode DoNotRecord $
92-
cabal "v2-build" $ ["-j", "my-toplevel:exe:my-executable"] ++ includeFlags ++ libFlags
94+
cabal "v2-build" $
95+
["-j", "my-toplevel:exe:my-executable"]
96+
-- Don’t validate absence of duplicates on Windows because
97+
-- it’s too inconvenient to do in bat files. Let’s just
98+
-- assume that deduplication will happen on Windows but
99+
-- still try to run the test to see whether command-line
100+
-- argument length limit is not hit.
101+
++ ["--with-gcc=" ++ customCC | not isWindows]
102+
++ includeFlags
103+
++ libFlags
93104
r <- withPlan $ runPlanExe' "my-toplevel" "my-executable" []
94105
assertOutputContains
95106
("Result = " ++ show (42 + 55 + 10 * (55 + 10 * 55) + 3 * 55))

0 commit comments

Comments
 (0)