use std::path::{Path, PathBuf};
use cpp_parser::Dialect;
use crate::file::paths::{FileProvider, normalize_path};
use crate::include::config::{CommandLineMacro, CompileCommands, CompilerConfig, IncludePath};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Output {
pub stdout: String,
pub stderr: String,
pub status: Option<i32>,
pub succeeded: bool,
}
impl Output {
pub fn combined(&self) -> String {
let mut all = self.stdout.clone();
all.push_str(&self.stderr);
all
}
}
pub trait CommandRunner {
fn run(
&self,
program: &Path,
arguments: &[&str],
environment: &[(std::ffi::OsString, std::ffi::OsString)],
) -> Option<Output>;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct DiskCommands;
impl CommandRunner for DiskCommands {
fn run(
&self,
program: &Path,
arguments: &[&str],
environment: &[(std::ffi::OsString, std::ffi::OsString)],
) -> Option<Output> {
let mut command = std::process::Command::new(program);
command
.args(arguments)
.stdin(std::process::Stdio::null());
for (name, value) in environment {
command.env(name, value);
}
let output = command.output().ok()?;
Some(Output {
stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
status: output.status.code(),
succeeded: output.status.success(),
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Environment {
pub cxx: Option<PathBuf>,
pub cc: Option<PathBuf>,
pub path: Vec<PathBuf>,
pub path_extensions: Vec<String>,
}
impl Environment {
pub fn current() -> Self {
Environment {
cxx: std::env::var_os("CXX").map(PathBuf::from),
cc: std::env::var_os("CC").map(PathBuf::from),
path: std::env::var_os("PATH")
.map(|value| std::env::split_paths(&value).collect())
.unwrap_or_default(),
path_extensions: std::env::var("PATHEXT")
.map(|value| {
value
.split(';')
.map(|extension| extension.trim().to_string())
.filter(|extension| !extension.is_empty())
.collect()
})
.unwrap_or_default(),
}
}
}
const COMPILER_NAMES: &[&str] = &["g++", "clang++", "c++", "gcc", "clang", "cc"];
const INSTALLED_COMPILER_NAMES: &[&str] = &["clang++", "g++", "clang", "gcc", "c++"];
fn compiler_install_roots(program_files: &[PathBuf]) -> Vec<PathBuf> {
let mut roots: Vec<PathBuf> = Vec::new();
for program_files in program_files {
roots.push(program_files.join("LLVM").join("bin"));
roots.push(
program_files
.join("Microsoft Visual Studio")
.join("LLVM")
.join("bin"),
);
}
let mut versioned: Vec<PathBuf> = Vec::new();
for program_files in program_files {
let Ok(entries) = std::fs::read_dir(program_files) else {
continue;
};
for entry in entries.flatten() {
let name = entry.file_name().to_string_lossy().to_string();
if name.to_ascii_uppercase().starts_with("LLVM") && entry.path().join("bin").is_dir() {
versioned.push(entry.path().join("bin"));
}
}
}
versioned.sort_by(|one, two| two.cmp(one));
roots.extend(versioned);
roots.extend([
PathBuf::from("/usr/bin"),
PathBuf::from("/opt/homebrew/bin"),
PathBuf::from("/usr/local/bin"),
PathBuf::from("/opt/llvm/bin"),
]);
roots
}
fn installed_compilers(
files: &impl FileProvider,
environment: &Environment,
program_files: &[PathBuf],
) -> Vec<PathBuf> {
let roots = compiler_install_roots(program_files);
let extensions = extensions_for(environment);
let mut found: Vec<PathBuf> = Vec::new();
for name in INSTALLED_COMPILER_NAMES {
for root in &roots {
let candidate = candidates_in(root, name, &extensions)
.into_iter()
.find(|candidate| files.exists(candidate));
if let Some(candidate) = candidate {
if !found.contains(&candidate) {
found.push(candidate);
}
break;
}
}
}
found
}
fn extensions_for(environment: &Environment) -> Vec<String> {
if !environment.path_extensions.is_empty() {
return environment.path_extensions.clone();
}
if cfg!(windows) {
return vec![".exe".to_string()];
}
Vec::new()
}
pub fn find_compiler(
files: &impl FileProvider,
commands: Option<&CompileCommands>,
for_file: &Path,
environment: &Environment,
) -> Option<PathBuf> {
find_compiler_among(files, commands, for_file, environment, &[])
}
pub fn find_compiler_among(
files: &impl FileProvider,
commands: Option<&CompileCommands>,
for_file: &Path,
environment: &Environment,
program_files: &[PathBuf],
) -> Option<PathBuf> {
if let Some(commands) = commands
&& let Some(command) = commands.command_for(for_file)
&& let Some(program) = command.arguments.first()
{
let named = PathBuf::from(program);
if let Some(found) = locate(files, &named, environment) {
return Some(found);
}
}
for variable in [environment.cxx.as_ref(), environment.cc.as_ref()]
.into_iter()
.flatten()
{
if let Some(found) = locate(files, variable, environment) {
return Some(found);
}
}
installed_compilers(files, environment, program_files)
.into_iter()
.next()
.or_else(|| {
COMPILER_NAMES
.iter()
.find_map(|name| locate(files, Path::new(name), environment))
})
}
fn locate(files: &impl FileProvider, name: &Path, environment: &Environment) -> Option<PathBuf> {
if name.components().count() > 1 {
return files.exists(name).then(|| name.to_path_buf());
}
let file_name = name.to_string_lossy().to_string();
for directory in &environment.path {
for candidate in candidates_in(directory, &file_name, &environment.path_extensions) {
if files.exists(&candidate) {
return Some(candidate);
}
}
}
None
}
fn candidates_in(directory: &Path, name: &str, extensions: &[String]) -> Vec<PathBuf> {
if extensions.is_empty() {
return vec![directory.join(name)];
}
extensions
.iter()
.map(|extension| directory.join(format!("{name}{extension}")))
.collect()
}
fn search_list_flags(arguments: &[String]) -> Vec<String> {
let mut taken: Vec<String> = Vec::new();
let mut rest = arguments.iter().skip(1).peekable();
while let Some(argument) = rest.next() {
let takes_a_separate_value = matches!(
argument.as_str(),
"-target" | "--target" | "-isystem" | "-isysroot" | "-B" | "-stdlib"
);
let starts_with = [
"--target=",
"--gcc-toolchain=",
"--sysroot=",
"-stdlib=",
"-march=",
"-mtune=",
]
.iter()
.any(|prefix| argument.starts_with(prefix));
if starts_with || takes_a_separate_value {
taken.push(argument.clone());
if takes_a_separate_value && let Some(value) = rest.next() {
taken.push(value.clone());
}
}
}
taken
}
pub fn search_paths(
runner: &impl CommandRunner,
compiler: &Path,
standard: Option<&str>,
flags: &[String],
) -> Option<Toolchain> {
for candidate in standards_to_try(standard, &ASSUMED_STANDARDS_GNU) {
if let Some(found) =
search_paths_with(runner, compiler, candidate, standard.is_none(), flags)
{
return Some(found);
}
}
None
}
fn search_paths_with(
runner: &impl CommandRunner,
compiler: &Path,
standard: Option<&str>,
assumed: bool,
flags: &[String],
) -> Option<Toolchain> {
let language = standard.map(|standard| format!("-std={standard}"));
let mut arguments: Vec<&str> = vec!["-dM", "-E", "-v"];
arguments.extend(flags.iter().map(String::as_str));
arguments.extend(["-x", "c++"]);
if let Some(language) = &language {
arguments.push(language);
}
arguments.push("-");
let output = runner.run(compiler, &arguments, &[])?;
let combined = output.combined();
let system_include_paths = parse_search_list(&combined);
if system_include_paths.is_empty() {
return None;
}
let builtin_macros = parse_builtin_macros(&combined);
let dialect = Dialect::from_predefined_macros(
builtin_macros
.iter()
.map(|define| (define.name.as_ref(), define.value.as_deref())),
);
let cuda = a_cuda_toolchain(&builtin_macros, Some(compiler));
Some(Toolchain {
compiler: Some(compiler.to_path_buf()),
version: parse_version(&combined),
system_include_paths,
builtin_macros,
cuda,
dialect,
standard: standard.map(str::to_string),
source: ToolchainSource::Path,
note: assumed.then(|| assumed_standard_note(standard)).flatten(),
})
}
fn assumed_standard_note(standard: Option<&str>) -> Option<String> {
let standard = standard?;
Some(format!(
"no build configuration states a language standard (no compile_commands.json, no CMakeLists.txt, no \
.cppls.toml), so the compiler was asked for its newest (`{standard}`); a project that builds as an older \
standard says so in .cppls.toml"
))
}
pub fn parse_builtin_macros(output: &str) -> Vec<CommandLineMacro> {
output
.lines()
.filter_map(|line| line.strip_prefix("#define "))
.filter_map(|rest| {
let mut parts = rest.splitn(2, char::is_whitespace);
let name = parts.next()?.trim();
if name.is_empty() || name.contains('(') {
return None;
}
let value = parts.next().unwrap_or("").trim();
Some(CommandLineMacro {
name: name.into(),
value: (!value.is_empty()).then(|| value.into()),
})
})
.collect()
}
#[derive(Debug, Clone, Copy, Default)]
pub struct BuildStatement<'a> {
pub commands: Option<&'a CompileCommands>,
pub standard: Option<&'a str>,
}
impl BuildStatement<'_> {
fn standard_for(
&self,
for_file: &Path,
read: &mut dyn FnMut(&Path) -> Option<String>,
) -> Option<String> {
self.commands
.and_then(|commands| commands.command_for(for_file))
.and_then(|command| command.to_config_with(read).standard)
.map(|standard| standard.to_string())
.or_else(|| self.standard.map(str::to_string))
}
}
pub fn discover(
files: &impl FileProvider,
runner: &impl CommandRunner,
statement: BuildStatement<'_>,
for_file: &Path,
environment: &Environment,
layout: &crate::include::msvc::WindowsLayout,
) -> Option<Toolchain> {
let from_database = database_compiler(statement.commands, for_file)
.map(|compiler| (compiler, ToolchainSource::CompileDatabase));
discover_with(
files,
runner,
from_database.as_slice(),
statement,
for_file,
environment,
layout,
)
}
fn database_compiler(commands: Option<&CompileCommands>, for_file: &Path) -> Option<PathBuf> {
commands
.and_then(|commands| commands.command_for(for_file))
.and_then(|command| command.arguments.first())
.map(PathBuf::from)
}
pub fn discover_with(
files: &impl FileProvider,
runner: &impl CommandRunner,
named: &[(PathBuf, ToolchainSource)],
statement: BuildStatement<'_>,
for_file: &Path,
environment: &Environment,
layout: &crate::include::msvc::WindowsLayout,
) -> Option<Toolchain> {
let standard = statement.standard_for(for_file, &mut |path| files.read(path));
let search_flags: Vec<String> = statement
.commands
.and_then(|commands| commands.command_for(for_file))
.map(|command| search_list_flags(&command.arguments))
.unwrap_or_default();
for (name, source) in named {
if let Some(toolchain) = ask(
files,
runner,
name,
environment,
layout,
standard.as_deref(),
&search_flags,
for_file,
) {
return Some(toolchain.claiming(*source));
}
}
for variable in [environment.cxx.as_ref(), environment.cc.as_ref()]
.into_iter()
.flatten()
{
if let Some(toolchain) = ask(
files,
runner,
variable,
environment,
layout,
standard.as_deref(),
&search_flags,
for_file,
) {
return Some(toolchain.claiming(ToolchainSource::Environment));
}
}
if let Some(toolchain) = ask_msvc(runner, layout, standard.as_deref(), None, for_file) {
return Some(toolchain.claiming(ToolchainSource::PlatformDefault));
}
let mut candidates: Vec<PathBuf> =
find_compiler_among(files, None, for_file, environment, &layout.program_files)
.into_iter()
.collect();
for name in COMPILER_NAMES {
if let Some(on_path) = locate(files, Path::new(name), environment)
&& !candidates.contains(&on_path)
{
candidates.push(on_path);
}
}
for name in &candidates {
if let Some(toolchain) = ask(
files,
runner,
name,
environment,
layout,
standard.as_deref(),
&search_flags,
for_file,
) {
return Some(toolchain.claiming(ToolchainSource::Path));
}
}
system_headers_fallback(runner, layout)
}
const ASSUMED_STANDARD_MSVC: &str = "c++latest";
const ASSUMED_STANDARDS_GNU: [&str; 2] = ["c++23", "c++2b"];
fn standards_to_try<'a>(stated: Option<&'a str>, assumed: &[&'a str]) -> Vec<Option<&'a str>> {
match stated {
Some(stated) => vec![Some(stated)],
None => assumed
.iter()
.map(|standard| Some(*standard))
.chain(std::iter::once(None))
.collect(),
}
}
fn ask(
files: &impl FileProvider,
runner: &impl CommandRunner,
name: &Path,
environment: &Environment,
layout: &crate::include::msvc::WindowsLayout,
standard: Option<&str>,
flags: &[String],
for_file: &Path,
) -> Option<Toolchain> {
let found = locate(files, name, environment)?;
if is_msvc(&found) {
return ask_msvc(runner, layout, standard, Some(found), for_file);
}
search_paths(runner, &found, standard, flags)
}
fn is_msvc(program: &Path) -> bool {
program
.file_stem()
.and_then(|name| name.to_str())
.is_some_and(|name| name.eq_ignore_ascii_case("cl"))
}
fn ask_msvc(
runner: &impl CommandRunner,
layout: &crate::include::msvc::WindowsLayout,
standard: Option<&str>,
named: Option<PathBuf>,
for_file: &Path,
) -> Option<Toolchain> {
if !cfg!(windows) {
return None;
}
let mut msvc = crate::include::msvc::discover(runner, layout)?;
if let Some(named) = named {
msvc.toolset.cl = named;
}
let mut asked = None;
let mut macros = None;
let mut assumed = false;
for candidate in standards_to_try(standard, &[ASSUMED_STANDARD_MSVC]) {
let fallbacks: Vec<PathBuf> = for_file
.parent()
.map(|directory| vec![directory.to_path_buf()])
.unwrap_or_default();
if let Some(found) =
crate::include::msvc::predefined_macros_with(runner, &msvc, candidate, &fallbacks)
{
asked = candidate;
macros = Some(found);
assumed = standard.is_none();
break;
}
}
let (builtin_macros, version, note) = match macros {
Some(macros) => {
let version = crate::include::msvc::version_line(&msvc, ¯os);
let note = assumed.then(|| assumed_standard_note(asked)).flatten();
(macros, Some(version), note)
}
None => (
Vec::new(),
Some(format!("MSVC {}", msvc.toolset.version)),
Some(
"the compiler could not be asked for its predefined macros, so conditions that name one \
(``_MSC_VER``, ``_WIN32``, ``__cplusplus``) are Unknown"
.to_string(),
),
),
};
let cuda = a_cuda_toolchain(&builtin_macros, None);
Some(Toolchain {
compiler: Some(msvc.toolset.cl.clone()),
version,
system_include_paths: msvc.include_paths(),
builtin_macros,
cuda,
dialect: Some(Dialect::Msvc),
standard: asked.map(str::to_string),
source: ToolchainSource::PlatformDefault,
note,
})
}
fn system_headers_fallback(
runner: &impl CommandRunner,
layout: &crate::include::msvc::WindowsLayout,
) -> Option<Toolchain> {
let found = crate::include::system_headers::discover(runner, layout)?;
Some(Toolchain {
compiler: None,
version: None,
system_include_paths: found.directories,
builtin_macros: Vec::new(),
cuda: false,
dialect: None,
standard: None,
source: ToolchainSource::SystemHeaders,
note: Some(format!(
"no compiler could be asked; using the system's own header directories ({}), so compiler macros \
are Unknown",
found.based_on
)),
})
}
fn a_cuda_toolchain(
builtin_macros: &[CommandLineMacro],
compiler: Option<&std::path::Path>,
) -> bool {
builtin_macros
.iter()
.any(|macro_| macro_.name.as_ref() == "__CUDACC__")
|| compiler
.and_then(|compiler| compiler.file_name())
.and_then(|name| name.to_str())
.is_some_and(|name| name.contains("nvcc"))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Toolchain {
pub compiler: Option<PathBuf>,
pub version: Option<String>,
pub system_include_paths: Vec<PathBuf>,
pub builtin_macros: Vec<CommandLineMacro>,
pub cuda: bool,
pub dialect: Option<Dialect>,
pub standard: Option<String>,
pub source: ToolchainSource,
pub note: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ToolchainSource {
Configuration,
CompileDatabase,
Environment,
PlatformDefault,
Path,
SystemHeaders,
}
impl ToolchainSource {
pub fn words(self) -> &'static str {
match self {
ToolchainSource::Configuration => "named by .cppls.toml",
ToolchainSource::CompileDatabase => "named by the compile database",
ToolchainSource::Environment => "named by CXX/CC",
ToolchainSource::PlatformDefault => "the platform's own toolchain",
ToolchainSource::Path => "found on PATH",
ToolchainSource::SystemHeaders => "the system's conventional header directories",
}
}
pub fn is_a_guess(self) -> bool {
matches!(self, ToolchainSource::SystemHeaders)
}
}
impl Toolchain {
fn claiming(mut self, source: ToolchainSource) -> Self {
self.source = source;
self
}
pub fn compiler_name(&self) -> String {
match &self.compiler {
Some(compiler) => compiler.display().to_string(),
None => "no compiler — the system's conventional header directories only".to_string(),
}
}
pub fn module_note(&self, module: &str) -> String {
match self.dialect {
Some(Dialect::Msvc) => {
let source = if module == "std" {
format!(
" Its source ships with the toolchain, at `{}`.",
self.standard_module_source("std").display()
)
} else {
String::new()
};
format!(
"no file this project contains declares module `{module}`. A module outside the project is one \
only the compiler can see: MSVC needs its interface unit compiled into an `.ifc` and named with \
`/reference {module}=<file>.ifc`, or a directory `/ifcSearchDir` names.{source} Until then the \
names it exports are unknown rather than absent."
)
}
_ => format!(
"no file this project contains declares module `{module}`. A module outside the project is one only \
the compiler can see, and this one has to have been built before anything can import it — GCC \
writes `gcm.cache/{module}.gcm` beside where it is run (`-fmodules-ts`), Clang takes a prebuilt \
module file by name (`-fmodule-file={module}=<file>`) or by directory \
(`-fprebuilt-module-path=<dir>`). Until then the names it exports are unknown rather than absent."
),
}
}
pub fn partition_note(&self, module: &str, partition: &str) -> String {
match self.dialect {
Some(Dialect::Msvc) => format!(
"no file this project contains declares partition `:{partition}` of module `{module}`. A partition \
is not a module — `import :{partition};` is only legal inside `{module}` — and MSVC compiles the \
module's interface against the partition's `.ifc`: `/reference {module}:{partition}=<file>.ifc`. \
Until then the names the partition exports are unknown rather than absent."
),
_ => format!(
"no file this project contains declares partition `:{partition}` of module `{module}`. A partition \
is not a module — `import :{partition};` is only legal inside `{module}` — and it has to have been \
built into the module's own module file first (GCC: `gcm.cache/{module}.gcm`; Clang: \
`-fmodule-file={module}:{partition}=<file>`). Until then the names it exports are unknown rather \
than absent."
),
}
}
pub fn standard_module_source(&self, module: &str) -> PathBuf {
for directory in &self.system_include_paths {
if let Some(parent) = directory.parent() {
let candidate = parent.join("modules").join(format!("{module}.ixx"));
if candidate.is_file() {
return candidate;
}
}
}
match self
.system_include_paths
.first()
.and_then(|first| first.parent())
{
Some(parent) => parent.join("modules").join(format!("{module}.ixx")),
None => PathBuf::from("modules").join(format!("{module}.ixx")),
}
}
pub fn macros(&self) -> Vec<(&str, Option<&str>)> {
self.builtin_macros
.iter()
.map(|define| (define.name.as_ref(), define.value.as_deref()))
.collect()
}
pub fn include_paths(&self) -> Vec<IncludePath> {
self.system_include_paths
.iter()
.cloned()
.map(IncludePath::system)
.collect()
}
pub fn config(&self, base: &CompilerConfig) -> CompilerConfig {
let mut config = base.clone();
if self.cuda {
config = config.with_cuda(true);
}
if config.standard.is_none()
&& let Some(standard) = &self.standard
{
config = config.with_standard(standard.clone());
}
for directory in &self.system_include_paths {
let wanted = normalize_path(directory, cfg!(windows));
let known = config
.include_paths
.iter()
.any(|path| normalize_path(&path.directory, cfg!(windows)) == wanted);
if !known {
config
.include_paths
.push(IncludePath::system(directory.clone()));
}
}
config
}
}
pub fn parse_search_list(output: &str) -> Vec<PathBuf> {
const OPENS: &str = "#include <...> search starts here:";
const CLOSES: &str = "End of search list.";
let mut directories = Vec::new();
let mut inside = false;
for line in output.lines() {
let trimmed = line.trim();
if !inside {
inside = trimmed == OPENS;
continue;
}
if trimmed == CLOSES {
break;
}
if trimmed.is_empty() {
continue;
}
let without_note = trimmed
.strip_suffix(" (framework directory)")
.unwrap_or(trimmed);
let directory = PathBuf::from(normalize_path(Path::new(without_note), false));
if directory.as_os_str().is_empty() {
continue;
}
directories.push(directory);
}
directories
}
pub fn parse_version(output: &str) -> Option<String> {
const NAMES: [&str; 2] = ["clang version ", "gcc version "];
output.lines().map(str::trim).find_map(|line| {
let (at, name) = NAMES
.iter()
.filter_map(|name| line.find(name).map(|at| (at, *name)))
.min_by_key(|(at, _)| *at)?;
let prefix = &line[..at];
let version = &line[at + name.len()..];
let before_is_a_name = prefix.split_whitespace().all(|word| {
word.chars()
.all(|character| character.is_ascii_alphabetic())
});
let after_is_a_number = version.starts_with(|character: char| character.is_ascii_digit());
(before_is_a_name && after_is_a_number).then(|| line.to_string())
})
}
#[cfg(test)]
mod tests;