use crate::llvm::Tool;
use crate::select::Source;
use crate::{ToolchainError, Version};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CompilerKind {
Msvc,
Clang,
ClangCl,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Syntax {
Cl,
Gnu,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Family {
Msvc,
Llvm,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Dependencies {
SourceDependencies,
DepFile,
}
impl CompilerKind {
pub const ALL: [CompilerKind; 3] = [
CompilerKind::Msvc,
CompilerKind::Clang,
CompilerKind::ClangCl,
];
pub fn name(self) -> &'static str {
match self {
CompilerKind::Msvc => "msvc",
CompilerKind::Clang => "clang",
CompilerKind::ClangCl => "clang-cl",
}
}
pub fn syntax(self) -> Syntax {
match self {
CompilerKind::Msvc | CompilerKind::ClangCl => Syntax::Cl,
CompilerKind::Clang => Syntax::Gnu,
}
}
pub fn family(self) -> Family {
match self {
CompilerKind::Msvc => Family::Msvc,
CompilerKind::Clang | CompilerKind::ClangCl => Family::Llvm,
}
}
pub fn echoes_source_name(self) -> bool {
match self {
CompilerKind::Msvc => true,
CompilerKind::Clang | CompilerKind::ClangCl => false,
}
}
pub fn checks_by_compiling(self) -> bool {
match self {
CompilerKind::Msvc => true,
CompilerKind::Clang | CompilerKind::ClangCl => false,
}
}
pub fn dependencies(self) -> Dependencies {
match self {
CompilerKind::Msvc => Dependencies::SourceDependencies,
CompilerKind::Clang | CompilerKind::ClangCl => Dependencies::DepFile,
}
}
pub fn llvm_tool(self) -> Option<Tool> {
match self {
CompilerKind::Msvc => None,
CompilerKind::Clang => Some(Tool::Clang),
CompilerKind::ClangCl => Some(Tool::ClangCl),
}
}
pub(crate) fn minimum(self) -> Version {
match self {
CompilerKind::Msvc => Version {
major: 19,
minor: 29,
patch: 0,
},
CompilerKind::Clang | CompilerKind::ClangCl => Version {
major: 17,
minor: 0,
patch: 0,
},
}
}
pub(crate) fn parse(name: &str, source: Source) -> Result<CompilerKind, ToolchainError> {
if let Some(kind) = CompilerKind::ALL.into_iter().find(|k| k.name() == name) {
return Ok(kind);
}
match name {
"gcc" | "safec" => Err(ToolchainError::UnsupportedCompiler {
name: name.to_owned(),
source: source.to_string(),
}),
_ => Err(ToolchainError::UnknownCompiler {
name: name.to_owned(),
source: source.to_string(),
}),
}
}
pub(crate) fn names() -> String {
let names: Vec<&str> = CompilerKind::ALL.iter().map(|k| k.name()).collect();
match names.split_last() {
Some((last, [])) => (*last).to_owned(),
Some((last, rest)) => format!("{} or {last}", rest.join(", ")),
None => String::new(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_cl_echoes_the_source_name() {
assert!(CompilerKind::Msvc.echoes_source_name());
assert!(!CompilerKind::Clang.echoes_source_name());
assert!(!CompilerKind::ClangCl.echoes_source_name());
}
#[test]
fn all_lists_every_compiler_once_in_order() {
let position = |kind: CompilerKind| match kind {
CompilerKind::Msvc => 0,
CompilerKind::Clang => 1,
CompilerKind::ClangCl => 2,
};
let positions: Vec<usize> = CompilerKind::ALL.iter().map(|k| position(*k)).collect();
assert_eq!(positions, (0..positions.len()).collect::<Vec<_>>());
assert_eq!(CompilerKind::ALL.len(), position(CompilerKind::ClangCl) + 1);
}
#[test]
fn each_compiler_answers_its_properties() {
let answers: Vec<_> = CompilerKind::ALL
.iter()
.map(|k| (k.name(), k.syntax(), k.family(), k.llvm_tool()))
.collect();
assert_eq!(
answers,
[
("msvc", Syntax::Cl, Family::Msvc, None),
("clang", Syntax::Gnu, Family::Llvm, Some(Tool::Clang)),
("clang-cl", Syntax::Cl, Family::Llvm, Some(Tool::ClangCl)),
]
);
assert_eq!(CompilerKind::names(), "msvc, clang or clang-cl");
}
}