Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 44 additions & 19 deletions src/tool.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
use crate::{
command_helpers::{run_output, spawn, CargoOutput},
run,
tempfile::NamedTempfile,
Error, ErrorKind, OutputKind,
};
use std::io::Read;
use std::{
borrow::Cow,
collections::HashMap,
Expand All @@ -9,13 +16,6 @@ use std::{
sync::RwLock,
};

use crate::{
command_helpers::{run_output, CargoOutput},
run,
tempfile::NamedTempfile,
Error, ErrorKind, OutputKind,
};

pub(crate) type CompilerFamilyLookupCache = HashMap<Box<[Box<OsStr>]>, ToolFamily>;

/// Configuration used to represent an invocation of a C compiler.
Expand Down Expand Up @@ -198,22 +198,47 @@ impl Tool {
let mut compiler_detect_output = cargo_output.clone();
compiler_detect_output.warnings = compiler_detect_output.debug;

let stdout = run_output(
Command::new(path).arg("-E").arg(tmp.path()),
&compiler_detect_output,
)?;
let stdout = String::from_utf8_lossy(&stdout);
let mut cmd = Command::new(path);
cmd.arg("-E").arg(tmp.path());

// The -Wslash-u-filename warning is normally part of stdout.
// But with clang-cl it can be part of stderr instead and exit with a
// non-zero exit code.
let mut captured_cargo_output = compiler_detect_output.clone();
captured_cargo_output.output = OutputKind::Capture;
captured_cargo_output.warnings = true;
let mut child = spawn(&mut cmd, &captured_cargo_output)?;

let mut out = vec![];
let mut err = vec![];
child.stdout.take().unwrap().read_to_end(&mut out)?;
child.stderr.take().unwrap().read_to_end(&mut err)?;

if stdout.contains("-Wslash-u-filename") {
let stdout = run_output(
let status = child.wait()?;

let stdout = if [&out, &err]
.iter()
.any(|o| String::from_utf8_lossy(o).contains("-Wslash-u-filename"))
{
run_output(
Command::new(path).arg("-E").arg("--").arg(tmp.path()),
&compiler_detect_output,
)?;
let stdout = String::from_utf8_lossy(&stdout);
guess_family_from_stdout(&stdout, path, args, cargo_output)
)?
} else {
guess_family_from_stdout(&stdout, path, args, cargo_output)
}
if !status.success() {
return Err(Error::new(
ErrorKind::ToolExecError,
format!(
"command did not execute successfully (status code {status}): {cmd:?}"
),
));
}

out
};

let stdout = String::from_utf8_lossy(&stdout);
guess_family_from_stdout(&stdout, path, args, cargo_output)
}
let detect_family = |path: &Path, args: &[String]| -> Result<ToolFamily, Error> {
let cache_key = [path.as_os_str()]
Expand Down