Skip to content

Commit

Permalink
Fix compiler family detection on clang-cl workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
NobodyXu authored Dec 22, 2024
1 parent 501f819 commit 59b22d8
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ impl Tool {
cargo_output: &CargoOutput,
out_dir: Option<&Path>,
) -> Result<ToolFamily, Error> {
// https://gitlab.kitware.com/cmake/cmake/-/blob/69a2eeb9dff5b60f2f1e5b425002a0fd45b7cadb/Modules/CMakeDetermineCompilerId.cmake#L267-271
// stdin is set to null to ensure that the help output is never paginated.
let accepts_cl_style_flags =
run(Command::new(path).arg("-?").stdin(Stdio::null()), path, &{
// the errors are not errors!
let mut cargo_output = cargo_output.clone();
cargo_output.warnings = cargo_output.debug;
cargo_output.output = OutputKind::Discard;
cargo_output
})
.is_ok();

let out_dir = out_dir
.map(Cow::Borrowed)
.unwrap_or_else(|| Cow::Owned(env::temp_dir()));
Expand Down Expand Up @@ -142,7 +154,8 @@ impl Tool {

let mut cmd = Command::new(path);
cmd.arg("-E");
if compiler.family == (ToolFamily::Msvc { clang_cl: true }) {
// there is no reliable way to detect clang-cl at this point
if accepts_cl_style_flags && path.file_name() == Some(OsStr::new("clang-cl")) {
// #513: For `clang-cl`, separate flags/options from the input file.
// When cross-compiling macOS -> Windows, this avoids interpreting
// common `/Users/...` paths as the `/U` flag and triggering
Expand All @@ -163,21 +176,8 @@ impl Tool {
},
)?;
let stdout = String::from_utf8_lossy(&stdout);

cargo_output.print_debug(&stdout);

// https://gitlab.kitware.com/cmake/cmake/-/blob/69a2eeb9dff5b60f2f1e5b425002a0fd45b7cadb/Modules/CMakeDetermineCompilerId.cmake#L267-271
// stdin is set to null to ensure that the help output is never paginated.
let accepts_cl_style_flags =
run(Command::new(path).arg("-?").stdin(Stdio::null()), path, &{
// the errors are not errors!
let mut cargo_output = cargo_output.clone();
cargo_output.warnings = cargo_output.debug;
cargo_output.output = OutputKind::Discard;
cargo_output
})
.is_ok();

let clang = stdout.contains(r#""clang""#);
let gcc = stdout.contains(r#""gcc""#);
let emscripten = stdout.contains(r#""emscripten""#);
Expand Down

0 comments on commit 59b22d8

Please sign in to comment.