use std::ffi::{OsStr, OsString};
use std::path::Path;
use std::sync::atomic::{AtomicU32, Ordering};
use cuno_core::Env;
use crate::compiler::Family;
use crate::flags::{CompileInput, Mode, ToolContext, compile_args, for_clang_tools};
use crate::select::{LlvmChoice, Selected};
use crate::{Runner, ToolchainError, Version};
#[derive(Clone, Copy, Debug)]
pub struct ProbeInputs<'a> {
pub scratch: &'a Path,
pub input: &'a CompileInput<'a>,
pub cwd: &'a Path,
}
pub fn check(
selected: &Selected,
env: &Env,
inputs: ProbeInputs<'_>,
run: &dyn Runner,
) -> Result<(), ToolchainError> {
match selected.kind.family() {
Family::Llvm if selected.msvc.is_some() => {}
Family::Llvm | Family::Msvc => return Ok(()),
}
stl_probe(
selected.kind.name(),
selected.version,
inputs.scratch,
|probe| {
let args = check_command(&inputs, probe);
run.run_in(&selected.executable, &as_args(&args), env, inputs.cwd)
},
)
}
fn check_command(inputs: &ProbeInputs<'_>, probe: &Path) -> Vec<OsString> {
let input = CompileInput {
mode: Mode::Check,
..*inputs.input
};
compile_args(&input, probe, None)
}
pub fn check_tidy(
tidy: &LlvmChoice,
env: &Env,
context: ToolContext<'_>,
inputs: ProbeInputs<'_>,
run: &dyn Runner,
) -> Result<(), ToolchainError> {
stl_probe("clang-tidy", tidy.version, inputs.scratch, |probe| {
let mut args: Vec<OsString> = vec![
"--checks=-*,readability-braces-around-statements".into(),
"--config={}".into(),
probe.into(),
"--".into(),
];
let input = CompileInput {
mode: Mode::Compile,
..*inputs.input
};
let command = for_clang_tools(context, &compile_args(&input, probe, None));
args.extend(
command
.after_double_dash()
.into_iter()
.filter(|arg| arg.as_os_str() != probe.as_os_str()),
);
run.run_in(&tidy.path, &as_args(&args), env, inputs.cwd)
})
}
fn as_args(args: &[OsString]) -> Vec<&OsStr> {
args.iter().map(|a| a.as_os_str()).collect()
}
fn stl_probe(
name: &'static str,
version: Version,
scratch: &Path,
invoke: impl FnOnce(&Path) -> std::io::Result<crate::Output>,
) -> Result<(), ToolchainError> {
let failed = |output: String| ToolchainError::ProbeFailed {
compiler: name,
output,
};
static NEXT: AtomicU32 = AtomicU32::new(0);
let probe = scratch.join(format!(
"cuno-stl-probe-{}-{}.cpp",
std::process::id(),
NEXT.fetch_add(1, Ordering::Relaxed)
));
std::fs::write(&probe, "#include <yvals_core.h>\n")
.map_err(|e| failed(format!("could not write {}: {e}", probe.display())))?;
let output = invoke(&probe);
let _ = std::fs::remove_file(&probe);
let output = output.map_err(|e| failed(e.to_string()))?;
if output.success {
return Ok(());
}
let text = format!(
"{}{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
match text.lines().find(|line| line.contains("STL1000:")) {
Some(line) => Err(ToolchainError::RejectedByStl {
compiler: name,
version,
needed: needed_clang(line),
message: line.trim().to_owned(),
}),
None => Err(failed(text.trim().to_owned())),
}
}
fn needed_clang(message: &str) -> Option<String> {
let rest = &message[message.find("expected Clang ")? + "expected Clang ".len()..];
let version = &rest[..rest.find(" or newer")?];
(!version.is_empty()).then(|| version.to_owned())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Output;
use crate::compiler::CompilerKind;
use crate::flags::{
BuildSettings, Edition, Lang, OptLevel, ProfileSettings, Runtime, TARGET_SWITCH, Warnings,
};
use crate::msvc::MsvcToolset;
use crate::select::Source;
use std::cell::RefCell;
use std::path::PathBuf;
fn v(major: u32, minor: u32, patch: u32) -> Version {
Version {
major,
minor,
patch,
}
}
fn toolset(path: &str, version: Version) -> MsvcToolset {
MsvcToolset {
dir: Path::new(path).join(format!("VC/Tools/MSVC/{version}")),
cl_version: version,
}
}
struct Answers {
ok: bool,
stdout: &'static str,
stderr: &'static str,
args: RefCell<Vec<String>>,
calls: RefCell<u32>,
}
fn answers(ok: bool, stdout: &'static str, stderr: &'static str) -> Answers {
Answers {
ok,
stdout,
stderr,
args: Default::default(),
calls: Default::default(),
}
}
impl Runner for Answers {
fn run(&self, _p: &Path, args: &[&OsStr], _e: &Env) -> std::io::Result<Output> {
*self.calls.borrow_mut() += 1;
*self.args.borrow_mut() = args
.iter()
.map(|a| a.to_string_lossy().into_owned())
.collect();
Ok(Output {
success: self.ok,
stdout: self.stdout.as_bytes().to_vec(),
stderr: self.stderr.as_bytes().to_vec(),
})
}
}
struct CannotStart;
impl Runner for CannotStart {
fn run(&self, _p: &Path, _a: &[&OsStr], _e: &Env) -> std::io::Result<Output> {
Err(std::io::Error::other("cannot start"))
}
}
struct TimesOut;
impl Runner for TimesOut {
fn run(&self, _p: &Path, _a: &[&OsStr], _e: &Env) -> std::io::Result<Output> {
Err(std::io::Error::new(
std::io::ErrorKind::TimedOut,
"did not finish within 30 seconds and was stopped",
))
}
}
fn selected(kind: CompilerKind) -> Selected {
Selected {
kind,
executable: "clang".into(),
version: v(20, 1, 8),
msvc: Some(toolset("VS", v(19, 52, 1))),
source: Source::CommandLine,
}
}
fn no_rtti() -> (BuildSettings, ProfileSettings) {
let build = BuildSettings {
warnings: Warnings::Default,
warnings_as_errors: false,
defines: vec!["_HAS_STATIC_RTTI=0".into()],
include_dirs: vec![],
links: vec![],
lib_dirs: vec![],
link_flags: vec![],
flags: vec![],
runtime: Runtime::Dynamic,
exceptions: true,
rtti: false,
};
let profile = ProfileSettings {
opt_level: OptLevel::O0,
debug_info: false,
debug_assertions: true,
lto: false,
};
(build, profile)
}
fn no_context(kind: CompilerKind) -> ToolContext<'static> {
ToolContext {
kind,
include: &[],
msvc: None,
}
}
fn input(
kind: CompilerKind,
settings: &(BuildSettings, ProfileSettings),
mode: Mode,
) -> CompileInput<'_> {
CompileInput {
kind,
lang: Lang::Cxx,
edition: Edition::Cxx20,
build: &settings.0,
profile: &settings.1,
include_dirs: &[],
test: false,
mode,
}
}
fn strings(args: Vec<OsString>) -> Vec<String> {
args.into_iter().map(|a| a.into_string().unwrap()).collect()
}
#[test]
fn the_probe_verdict_follows_the_standard_library() {
let root = tempfile::tempdir().unwrap();
let settings = no_rtti();
let compile = input(CompilerKind::Clang, &settings, Mode::Check);
let inputs = ProbeInputs {
scratch: root.path(),
input: &compile,
cwd: root.path(),
};
let clang = selected(CompilerKind::Clang);
let stl = "x\\yvals_core.h(922,1): error: STL1000: Unexpected compiler version, expected Clang 22 or newer.\n";
let err = check(&clang, &Env::default(), inputs, &answers(false, "", stl)).unwrap_err();
assert_eq!(
err.to_string(),
"clang 20.1.8 is not accepted by the MSVC standard library in use (STL1000)"
);
let ToolchainError::RejectedByStl {
needed, message, ..
} = &err
else {
panic!("{err:?}");
};
assert_eq!(
(needed.as_deref(), message.as_str()),
(Some("22"), stl.trim())
);
let err = check(
&clang,
&Env::default(),
inputs,
&answers(false, "fatal error: 'yvals_core.h' file not found", ""),
)
.unwrap_err();
assert!(
matches!(&err, ToolchainError::ProbeFailed { compiler: "clang", output } if output == "fatal error: 'yvals_core.h' file not found"),
"{err:?}"
);
let err = check(&clang, &Env::default(), inputs, &TimesOut).unwrap_err();
assert!(
matches!(&err, ToolchainError::ProbeFailed { output, .. } if output.contains("within 30 seconds")),
"{err:?}"
);
let err = check(&clang, &Env::default(), inputs, &CannotStart).unwrap_err();
assert!(
matches!(&err, ToolchainError::ProbeFailed { output, .. } if output == "cannot start"),
"{err:?}"
);
check(&clang, &Env::default(), inputs, &answers(true, "", "")).unwrap();
assert_eq!(
std::fs::read_dir(root.path()).unwrap().count(),
0,
"probe file left behind"
);
assert_eq!(
needed_clang("expected Clang 17.0.0 or newer."),
Some("17.0.0".into())
);
assert_eq!(needed_clang("expected Clang or newer."), None);
assert_eq!(needed_clang("STL1000: Unexpected compiler version"), None);
}
#[test]
fn the_probe_runs_the_builds_command_in_check_mode() {
let root = tempfile::tempdir().unwrap();
let settings = no_rtti();
let mut probes = Vec::new();
for (kind, switch, cxx, rtti) in [
(CompilerKind::Clang, "-fsyntax-only", "c++", "-fno-rtti"),
(CompilerKind::ClangCl, "/Zs", "/TP", "/GR-"),
] {
let handed = input(kind, &settings, Mode::Compile);
let inputs = ProbeInputs {
scratch: root.path(),
input: &handed,
cwd: root.path(),
};
let run = answers(true, "", "");
check(&selected(kind), &Env::default(), inputs, &run).unwrap();
let args = run.args.borrow().clone();
let probe = args.last().unwrap().clone();
assert!(probe.ends_with(".cpp"), "{probe}");
let expected = strings(compile_args(
&input(kind, &settings, Mode::Check),
Path::new(&probe),
None,
));
assert_eq!(args, expected, "{kind:?}");
assert_eq!(args[0], switch, "{kind:?}");
for needed in [cxx, rtti, "-D_HAS_STATIC_RTTI=0"] {
let needed = match kind {
CompilerKind::ClangCl if needed.starts_with("-D") => "/D_HAS_STATIC_RTTI=0",
_ => needed,
};
assert!(args.iter().any(|a| a == needed), "{kind:?}: {args:?}");
}
assert_eq!(
args.iter().filter(|a| a.starts_with("--target")).count(),
1,
"{kind:?}: {args:?}"
);
probes.push(probe);
}
assert_ne!(probes[0], probes[1]);
let compile = input(CompilerKind::Msvc, &settings, Mode::Check);
let inputs = ProbeInputs {
scratch: root.path(),
input: &compile,
cwd: root.path(),
};
let never = answers(false, "STL1000:", "");
check(
&selected(CompilerKind::Msvc),
&Env::default(),
inputs,
&never,
)
.unwrap();
let mut elsewhere = selected(CompilerKind::Clang);
elsewhere.msvc = None;
check(&elsewhere, &Env::default(), inputs, &never).unwrap();
assert_eq!(*never.calls.borrow(), 0);
}
#[test]
fn clang_tidy_is_asked_the_standard_librarys_verdict() {
let root = tempfile::tempdir().unwrap();
let settings = no_rtti();
let tidy = LlvmChoice {
path: "clang-tidy".into(),
version: v(20, 1, 8),
};
let stl = "x\\yvals_core.h(922,1): error: STL1000: Unexpected compiler version, expected Clang 22 or newer. [clang-diagnostic-error]\n";
let include = [PathBuf::from("C:/inc")];
let version = v(19, 52, 1);
let context = |kind| ToolContext {
kind,
include: &include,
msvc: Some(&version),
};
let msvc = input(CompilerKind::Msvc, &settings, Mode::Check);
let err = check_tidy(
&tidy,
&Env::default(),
context(CompilerKind::Msvc),
ProbeInputs {
scratch: root.path(),
input: &msvc,
cwd: root.path(),
},
&answers(false, stl, ""),
)
.unwrap_err();
assert_eq!(
err.to_string(),
"clang-tidy 20.1.8 is not accepted by the MSVC standard library in use (STL1000)"
);
assert_eq!(
cuno_core::CunoError::help(&err).unwrap(),
"install clang-tidy 22 or newer"
);
for (kind, mode) in [
(CompilerKind::Msvc, &["--driver-mode=cl", TARGET_SWITCH][..]),
(CompilerKind::ClangCl, &["--driver-mode=cl"][..]),
(CompilerKind::Clang, &[][..]),
] {
let handed = input(kind, &settings, Mode::Check);
let inputs = ProbeInputs {
scratch: root.path(),
input: &handed,
cwd: root.path(),
};
let run = answers(true, "", "");
check_tidy(&tidy, &Env::default(), context(kind), inputs, &run).unwrap();
let compile = input(kind, &settings, Mode::Compile);
let args = run.args.borrow().clone();
assert_eq!(args[0], "--checks=-*,readability-braces-around-statements");
let probe = &args[2];
assert!(probe.ends_with(".cpp"), "{args:?}");
let command: Vec<String> = strings(
for_clang_tools(
context(kind),
&compile_args(&compile, Path::new(probe), None),
)
.args()
.to_vec(),
)
.into_iter()
.filter(|a| a != probe)
.collect();
for each in ["-fms-compatibility-version=19.52.1", "C:/inc"] {
assert!(args.iter().any(|a| a.contains(each)), "{kind:?}: {args:?}");
}
let expected: Vec<String> = ["--"]
.iter()
.chain(mode)
.map(|s| (*s).to_owned())
.chain(command)
.collect();
assert_eq!(args[3..], expected, "{kind:?}");
assert_eq!(
args.iter().filter(|a| a.starts_with("--target")).count(),
1,
"{kind:?}: {args:?}"
);
}
assert_eq!(std::fs::read_dir(root.path()).unwrap().count(), 0);
}
#[test]
fn clang_tidy_reads_an_msvc_standard_as_clang_cl_does() {
let root = tempfile::tempdir().unwrap();
let settings = no_rtti();
let mut compile = input(CompilerKind::Msvc, &settings, Mode::Check);
compile.edition = Edition::Cxx23;
let run = answers(true, "", "");
let tidy = LlvmChoice {
path: "clang-tidy".into(),
version: v(22, 1, 8),
};
let inputs = ProbeInputs {
scratch: root.path(),
input: &compile,
cwd: root.path(),
};
check_tidy(
&tidy,
&Env::default(),
no_context(CompilerKind::Msvc),
inputs,
&run,
)
.unwrap();
let args = run.args.borrow().clone();
assert!(args.iter().any(|a| a == "/clang:-std=c++23"), "{args:?}");
assert!(!args.iter().any(|a| a == "/std:c++23"), "{args:?}");
}
#[test]
fn clang_tidy_is_not_given_link_time_optimization() {
let root = tempfile::tempdir().unwrap();
let mut settings = no_rtti();
settings.1.lto = true;
let compile = input(CompilerKind::Msvc, &settings, Mode::Check);
let built = strings(compile_args(&compile, Path::new("x.cpp"), None));
assert!(built.iter().any(|a| a == "/GL"), "{built:?}");
let run = answers(true, "", "");
let tidy = LlvmChoice {
path: "clang-tidy".into(),
version: v(22, 1, 8),
};
let inputs = ProbeInputs {
scratch: root.path(),
input: &compile,
cwd: root.path(),
};
check_tidy(
&tidy,
&Env::default(),
no_context(CompilerKind::Msvc),
inputs,
&run,
)
.unwrap();
let args = run.args.borrow().clone();
assert!(!args.iter().any(|a| a == "/GL"), "{args:?}");
}
}