Skip to content

Commit a49184e

Browse files
test: ensure clang --driver-mode=cl is MSVC- and clang-cl-like (#1381)
1 parent 4990d4b commit a49184e

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

src/bin/gcc-shim.rs renamed to src/bin/cc-shim.rs

+30-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ use std::env;
88
use std::fs::File;
99
use std::io::{self, prelude::*};
1010
use std::path::PathBuf;
11+
use std::process::ExitCode;
1112

12-
fn main() {
13-
let mut args = env::args();
13+
fn main() -> ExitCode {
14+
let args = env::args().collect::<Vec<_>>();
15+
let mut args = args.iter();
1416
let program = args.next().expect("Unexpected empty args");
1517

1618
let out_dir = PathBuf::from(
17-
env::var_os("GCCTEST_OUT_DIR")
18-
.unwrap_or_else(|| panic!("{}: GCCTEST_OUT_DIR not found", program)),
19+
env::var_os("CC_SHIM_OUT_DIR")
20+
.unwrap_or_else(|| panic!("{}: CC_SHIM_OUT_DIR not found", program)),
1921
);
2022

2123
// Find the first nonexistent candidate file to which the program's args can be written.
@@ -42,7 +44,7 @@ fn main() {
4244
let mut f = io::BufWriter::new(f);
4345

4446
(|| {
45-
for arg in args {
47+
for arg in args.clone() {
4648
writeln!(f, "{}", arg)?;
4749
}
4850

@@ -61,6 +63,27 @@ fn main() {
6163
)
6264
});
6365

66+
if program.starts_with("clang") {
67+
// Validate that we got no `-?` without a preceding `--driver-mode=cl`. Compiler family
68+
// detection depends on this.
69+
if let Some(cl_like_help_option_idx) = args.clone().position(|a| a == "-?") {
70+
let has_cl_clang_driver_before_cl_like_help_option = args
71+
.clone()
72+
.take(cl_like_help_option_idx)
73+
.rev()
74+
.find_map(|a| a.strip_prefix("--driver-mode="))
75+
.is_some_and(|a| a == "cl");
76+
if has_cl_clang_driver_before_cl_like_help_option {
77+
return ExitCode::SUCCESS;
78+
} else {
79+
eprintln!(
80+
"Found `-?` argument, but it was not preceded by a `--driver-mode=cl` argument."
81+
);
82+
return ExitCode::FAILURE;
83+
}
84+
}
85+
}
86+
6487
// Create a file used by some tests.
6588
let path = &out_dir.join("libfoo.a");
6689
File::create(path).unwrap_or_else(|e| {
@@ -71,4 +94,6 @@ fn main() {
7194
e
7295
)
7396
});
97+
98+
ExitCode::SUCCESS
7499
}

tests/cc_env.rs

+16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn main() {
1515
extra_flags();
1616
path_to_ccache();
1717
more_spaces();
18+
clang_cl();
1819
}
1920

2021
fn ccache() {
@@ -110,3 +111,18 @@ fn more_spaces() {
110111
let compiler = test.gcc().file("foo.c").get_compiler();
111112
assert_eq!(compiler.path(), Path::new("cc"));
112113
}
114+
115+
fn clang_cl() {
116+
for exe_suffix in ["", ".exe"] {
117+
let test = Test::clang();
118+
let bin = format!("clang{exe_suffix}");
119+
env::set_var("CC", &format!("{bin} --driver-mode=cl"));
120+
let test_compiler = |build: cc::Build| {
121+
let compiler = build.get_compiler();
122+
assert_eq!(compiler.path(), Path::new(&*bin));
123+
assert!(compiler.is_like_msvc());
124+
assert!(compiler.is_like_clang_cl());
125+
};
126+
test_compiler(test.gcc());
127+
}
128+
}

tests/support/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ impl Test {
4444
if gcc.ends_with("deps") {
4545
gcc.pop();
4646
}
47-
let td = Builder::new().prefix("gcc-test").tempdir_in(&gcc).unwrap();
48-
gcc.push(format!("gcc-shim{}", env::consts::EXE_SUFFIX));
47+
let td = Builder::new()
48+
.prefix("cc-shim-test")
49+
.tempdir_in(&gcc)
50+
.unwrap();
51+
gcc.push(format!("cc-shim{}", env::consts::EXE_SUFFIX));
4952
Test {
5053
td,
5154
gcc,
@@ -98,7 +101,7 @@ impl Test {
98101
.debug(false)
99102
.out_dir(self.td.path())
100103
.__set_env("PATH", self.path())
101-
.__set_env("GCCTEST_OUT_DIR", self.td.path());
104+
.__set_env("CC_SHIM_OUT_DIR", self.td.path());
102105
if self.msvc {
103106
cfg.compiler(self.td.path().join("cl"));
104107
cfg.archiver(self.td.path().join("lib.exe"));

0 commit comments

Comments
 (0)