use super::{
Dialect, Environment, Output, a_cuda_toolchain, parse_search_list, parse_version,
search_list_flags,
};
#[test]
fn a_cuda_toolchain_is_recognised_and_turns_the_parsers_switch_on() {
let macros = super::parse_builtin_macros("#define __GNUC__ 12\n#define __CUDACC__ 1\n");
assert!(
a_cuda_toolchain(¯os, None),
"`__CUDACC__` is nvcc's own word"
);
assert!(
a_cuda_toolchain(&[], Some(std::path::Path::new("/usr/local/cuda/bin/nvcc"))),
"an nvcc that could not be asked is still nvcc"
);
let gnu = super::parse_builtin_macros("#define __GNUC__ 12\n");
assert!(!a_cuda_toolchain(&gnu, None));
assert!(!a_cuda_toolchain(
&[],
Some(std::path::Path::new("/usr/bin/g++"))
));
let toolchain = super::Toolchain {
compiler: Some("/usr/local/cuda/bin/nvcc".into()),
version: None,
system_include_paths: Vec::new(),
builtin_macros: macros,
cuda: true,
dialect: Some(Dialect::Gnu),
standard: None,
source: super::ToolchainSource::CompileDatabase,
note: None,
};
let config = toolchain.config(&super::CompilerConfig::new());
assert!(config.cuda(), "the parser must read this project as CUDA");
assert_eq!(
config.dialect(),
Dialect::Gnu,
"and the host dialect is still its own"
);
}
const GCC: &str = "\
Using built-in specs.
COLLECT_GCC=C:\\tools\\mingw64\\bin\\g++.exe
Target: x86_64-w64-mingw32
gcc version 15.1.0 (x86_64-win32-seh-rev0, Built by MinGW-Builds project)
COLLECT_GCC_OPTIONS='-E' '-v' '-shared-libgcc' '-mtune=core2' '-march=nocona'
ignoring duplicate directory \"C:/tools/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/15.1.0/include/c++\"
#include \"...\" search starts here:
#include <...> search starts here:
C:/tools/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/15.1.0/include/c++
C:/tools/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/15.1.0/include/c++/x86_64-w64-mingw32
C:/tools/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/15.1.0/include/c++/backward
End of search list.
";
const CLANG: &str = "\
clang version 18.1.8
Target: x86_64-pc-windows-msvc
Thread model: posix
#include \"...\" search starts here:
#include <...> search starts here:
C:\\Program Files\\LLVM\\lib\\clang\\18\\include
C:\\Program Files\\Microsoft Visual Studio\\18\\Community\\VC\\Tools\\MSVC\\14.51.36231\\include
End of search list.
";
#[test]
fn both_compilers_search_lists_are_read_the_same_way() {
let gcc = parse_search_list(GCC);
let clang = parse_search_list(CLANG);
assert_eq!(
gcc.len(),
3,
"three entries, and nothing from the quoted block"
);
assert_eq!(clang.len(), 2);
assert_eq!(
gcc[0],
std::path::Path::new("C:/tools/mingw64/lib/gcc/x86_64-w64-mingw32/15.1.0/include/c++"),
"the `bin/../lib` in the compiler's own output is folded"
);
assert_eq!(
clang[0],
std::path::Path::new("C:/Program Files/LLVM/lib/clang/18/include")
);
}
#[test]
fn the_quoted_block_is_not_read() {
let output = "\
#include \"...\" search starts here:
/only/for/quotes
#include <...> search starts here:
/for/angles
End of search list.
";
assert_eq!(
parse_search_list(output),
[std::path::PathBuf::from("/for/angles")]
);
}
#[test]
fn an_apple_framework_directory_loses_its_note() {
let output = "\
#include <...> search starts here:
/System/Library/Frameworks (framework directory)
/usr/include
End of search list.
";
assert_eq!(
parse_search_list(output),
[
std::path::PathBuf::from("/System/Library/Frameworks"),
std::path::PathBuf::from("/usr/include")
]
);
}
#[test]
fn output_without_a_search_list_has_no_directories() {
assert!(parse_search_list("usage: not-a-compiler [options] file").is_empty());
assert!(
parse_search_list("#include <...> search starts here:\n").is_empty(),
"a list that never ends has no entries, rather than everything after it"
);
}
#[test]
fn the_version_line_is_the_one_the_compiler_printed() {
assert_eq!(
parse_version(GCC).as_deref(),
Some("gcc version 15.1.0 (x86_64-win32-seh-rev0, Built by MinGW-Builds project)")
);
assert_eq!(
parse_version(CLANG).as_deref(),
Some("clang version 18.1.8")
);
assert_eq!(
parse_version(
"Configured with: ../configure --prefix=/usr --with-gcc-major-version-only gcc version 1.2.3\n"
),
None
);
assert_eq!(parse_version("clang version unknown\n"), None);
assert_eq!(
parse_version("Ubuntu clang version 1.2.3\nTarget: x86_64-pc-linux-gnu\n").as_deref(),
Some("Ubuntu clang version 1.2.3")
);
assert_eq!(
parse_version("Apple clang version 1.2.3 (clang-1000)\nTarget: arm64-apple-darwin23.0.0\n")
.as_deref(),
Some("Apple clang version 1.2.3 (clang-1000)")
);
assert_eq!(
parse_version("Debian clang version 1.2.3").as_deref(),
Some("Debian clang version 1.2.3")
);
assert_eq!(
parse_version("Using built-in specs.\ngcc version 1.2.3 (Ubuntu 1.2.3-1ubuntu4) ")
.as_deref(),
Some("gcc version 1.2.3 (Ubuntu 1.2.3-1ubuntu4)")
);
}
#[test]
fn the_output_documents_both_streams() {
let output = Output {
stdout: "from stdout".to_string(),
stderr: "from stderr".to_string(),
status: Some(0),
succeeded: true,
};
let combined = output.combined();
assert!(combined.contains("from stdout") && combined.contains("from stderr"));
}
#[test]
fn the_environment_is_read_as_data() {
let environment = Environment {
cxx: Some(std::path::PathBuf::from("/opt/gcc/bin/g++")),
cc: None,
path: vec![std::path::PathBuf::from("/usr/bin")],
path_extensions: Vec::new(),
};
assert_eq!(environment.path.len(), 1);
}
use super::{
COMPILER_NAMES, CommandRunner, CompilerConfig, IncludePath, Path, PathBuf, Toolchain,
ToolchainSource, discover, find_compiler, find_compiler_among, installed_compilers, is_msvc,
parse_builtin_macros, search_paths,
};
use crate::file::paths::{MemoryFiles, normalize_path};
use crate::include::config::{CompileCommand, CompileCommands};
fn environment() -> Environment {
Environment {
cxx: None,
cc: None,
path: vec![Path::new("/bin").into(), Path::new("/usr/bin").into()],
path_extensions: Vec::new(),
}
}
struct Answers {
program: &'static str,
output: &'static str,
}
impl CommandRunner for Answers {
fn run(
&self,
program: &Path,
arguments: &[&str],
environment: &[(std::ffi::OsString, std::ffi::OsString)],
) -> Option<Output> {
if program.file_name()?.to_str()? != self.program {
return None;
}
assert!(
environment.is_empty(),
"a GNU-like compiler is asked with an empty environment: {environment:?}"
);
assert!(
arguments.starts_with(&["-dM", "-E", "-v", "-x", "c++"]),
"the predefined macros and the search list come from one call: {arguments:?}"
);
assert!(
arguments.iter().all(|argument| *argument == "-dM"
|| *argument == "-E"
|| *argument == "-v"
|| *argument == "-x"
|| *argument == "c++"
|| *argument == "-"
|| argument.starts_with("-std=")),
"and nothing else is asked for: {arguments:?}"
);
Some(Output {
stdout: String::new(),
stderr: self.output.to_string(),
status: Some(0),
succeeded: true,
})
}
}
fn from_database(commands: &CompileCommands) -> super::BuildStatement<'_> {
super::BuildStatement {
commands: Some(commands),
standard: None,
}
}
fn command_line(compiler: &str) -> CompileCommands {
CompileCommands {
commands: vec![CompileCommand {
file: "/p/main.cpp".into(),
directory: None,
arguments: vec![compiler.to_string(), "-c".to_string()],
}],
malformed: 0,
}
}
#[test]
fn the_compiler_the_compile_database_names_is_the_one_asked() {
let files = MemoryFiles::new()
.with_file("/usr/bin/g++", "")
.with_file("/opt/clang/bin/clang++", "");
let commands = command_line("/opt/clang/bin/clang++");
let found = find_compiler(
&files,
Some(&commands),
Path::new("/p/main.cpp"),
&environment(),
);
assert_eq!(
found,
Some(Path::new("/opt/clang/bin/clang++").to_path_buf())
);
}
#[test]
fn a_named_compiler_that_is_not_there_falls_through_to_the_environment() {
let files = MemoryFiles::new().with_file("/usr/bin/clang++", "");
let commands = command_line("/opt/gcc/bin/g++");
let mut environment = environment();
environment.cxx = Some(Path::new("/usr/bin/clang++").into());
let found = find_compiler(
&files,
Some(&commands),
Path::new("/p/main.cpp"),
&environment,
);
assert_eq!(found, Some(Path::new("/usr/bin/clang++").to_path_buf()));
}
#[test]
fn cxx_beats_cc_and_path() {
let files = MemoryFiles::new()
.with_file("/usr/bin/g++", "")
.with_file("/usr/bin/cc", "");
let mut environment = environment();
environment.cc = Some(Path::new("/usr/bin/cc").into());
environment.cxx = Some(Path::new("/usr/bin/g++").into());
assert_eq!(
find_compiler(&files, None, Path::new("/p/main.cpp"), &environment),
Some(Path::new("/usr/bin/g++").to_path_buf()),
"`CXX` is the project's stated C++ compiler and `CC` is only a fallback"
);
}
#[test]
fn a_bare_name_is_searched_on_the_path_and_a_path_is_used_where_it_says() {
let files = MemoryFiles::new()
.with_file("/bin/g++", "")
.with_file("/usr/bin/g++", "")
.with_file("/opt/other/g++", "");
let mut environment = environment();
environment.cxx = Some(Path::new("g++").into());
assert_eq!(
find_compiler(&files, None, Path::new("/p/main.cpp"), &environment),
Some(Path::new("/bin/g++").to_path_buf()),
"a bare name is searched on `PATH`, in order"
);
environment.cxx = Some(Path::new("/opt/other/g++").into());
assert_eq!(
find_compiler(&files, None, Path::new("/p/main.cpp"), &environment),
Some(Path::new("/opt/other/g++").to_path_buf()),
"a name with a separator in it says where it is"
);
}
#[test]
fn the_path_is_searched_in_the_order_the_names_are_tried() {
let mut environment = environment();
environment.path = vec![Path::new("/p/bin").into()];
let files = MemoryFiles::new().with_file("/p/bin/clang++", "");
let found = find_compiler(&files, None, Path::new("/p/main.cpp"), &environment);
assert_eq!(found, Some(Path::new("/p/bin/clang++").to_path_buf()));
let files = MemoryFiles::new()
.with_file("/p/bin/clang++", "")
.with_file("/p/bin/g++", "");
let found = find_compiler(&files, None, Path::new("/p/main.cpp"), &environment);
assert_eq!(
found,
Some(Path::new("/p/bin/g++").to_path_buf()),
"the first name on the list that exists, not the first directory that has any of them"
);
assert!(
COMPILER_NAMES.contains(&"c++"),
"the one name POSIX guarantees"
);
}
#[test]
fn no_compiler_anywhere_is_no_answer_rather_than_an_error() {
let files = MemoryFiles::new();
assert_eq!(
find_compiler(&files, None, Path::new("/p/main.cpp"), &environment()),
None
);
}
#[test]
fn a_compiler_that_is_installed_is_found_when_the_path_does_not_say() {
let files = MemoryFiles::new()
.with_case_insensitive(true)
.with_file(
"C:/Program Files/LLVM/bin/clang++.exe",
"the installed clang",
)
.with_file("/mingw/bin/g++.exe", "what `PATH` says");
let mut mingw_on_path = environment();
mingw_on_path.path = vec![Path::new("/mingw/bin").into()];
mingw_on_path.path_extensions = vec![".exe".to_string()];
let found = find_compiler_among(
&files,
None,
Path::new("C:/p/main.cpp"),
&mingw_on_path,
&[PathBuf::from("C:/Program Files")],
);
assert_eq!(
found,
Some(PathBuf::from("C:/Program Files/LLVM/bin/clang++.exe")),
"the installed clang, not the MinGW `g++` on `PATH`: they are different standard libraries"
);
}
#[test]
fn an_installed_clang_is_preferred_to_an_installed_gcc() {
let files = MemoryFiles::new()
.with_case_insensitive(true)
.with_file("/usr/local/bin/g++.exe", "")
.with_file("/opt/homebrew/bin/clang++.exe", "");
let mut nothing_on_path = environment();
nothing_on_path.path = Vec::new();
nothing_on_path.path_extensions = vec![".exe".to_string()];
let found = installed_compilers(&files, ¬hing_on_path, &[PathBuf::from("/nothing/there")]);
assert!(
found.iter().any(|path| path.ends_with("clang++.exe")),
"the clang is offered: {found:?}"
);
assert!(
found.iter().any(|path| path.ends_with("g++.exe")),
"and so is the `g++`, after it: {found:?}"
);
assert!(
found
.iter()
.position(|path| path.ends_with("clang++.exe"))
.zip(found.iter().position(|path| path.ends_with("g++.exe")))
.is_some_and(|(clang, gcc)| clang < gcc),
"clang first, whichever directory it is in: {found:?}"
);
}
#[test]
fn an_empty_installation_directory_is_not_a_compiler() {
let files = MemoryFiles::new()
.with_case_insensitive(true)
.with_file("/opt/homebrew/bin/brew", "")
.with_file("/usr/local/bin/cmake", "");
let mut nothing_on_path = environment();
nothing_on_path.path = Vec::new();
nothing_on_path.path_extensions = vec![".exe".to_string()];
assert!(
installed_compilers(&files, ¬hing_on_path, &[]).is_empty(),
"no file named for a compiler is a compiler"
);
let files = MemoryFiles::new().with_file("/mingw/bin/g++.exe", "");
let mut on_path = environment();
on_path.path = vec![Path::new("/mingw/bin").into()];
on_path.path_extensions = vec![".exe".to_string()];
assert_eq!(
find_compiler_among(&files, None, Path::new("/p/main.cpp"), &on_path, &[]),
Some(std::path::PathBuf::from("/mingw/bin/g++.exe"))
);
}
#[test]
fn clang_cl_is_not_mistaken_for_microsofts_compiler() {
assert!(is_msvc(Path::new(
"/VS/VC/Tools/MSVC/14.51.36231/bin/Hostx64/x64/cl.exe"
)));
assert!(
is_msvc(Path::new("cl.exe")),
"the name is matched, not the path"
);
assert!(
!is_msvc(Path::new("C:/Program Files/LLVM/bin/clang-cl.exe")),
"clang's MSVC driver: it accepts `cl`'s flags and has no macro dump"
);
assert!(!is_msvc(Path::new("C:/Program Files/LLVM/bin/clang++.exe")));
assert!(!is_msvc(Path::new("/usr/bin/c++")));
}
#[test]
fn windows_tries_the_extensions_in_the_order_pathext_gives() {
let files = MemoryFiles::new()
.with_case_insensitive(true)
.with_file("/bin/g++", "")
.with_file("/bin/g++.exe", "");
let mut environment = environment();
environment.cxx = Some(Path::new("g++").into());
environment.path_extensions = vec![".COM".to_string(), ".EXE".to_string()];
let found = find_compiler(&files, None, Path::new("/p/main.cpp"), &environment)
.expect("`g++` is on the path");
assert_eq!(
normalize_path(&found, true),
normalize_path(Path::new("/bin/g++.exe"), true),
"the executable spelling, not the bare name that sits beside it"
);
}
#[test]
fn a_compiler_that_answers_gives_its_directories_its_version_and_its_macros() {
let runner = Answers {
program: "g++",
output: GCC,
};
let toolchain =
search_paths(&runner, Path::new("/usr/bin/g++"), None, &[]).expect("the fixture answers");
assert_eq!(
toolchain.compiler.as_deref(),
Some(Path::new("/usr/bin/g++"))
);
assert_eq!(toolchain.system_include_paths.len(), 3);
assert!(
toolchain
.version
.as_deref()
.is_some_and(|version| version.starts_with("gcc version"))
);
assert!(
toolchain.include_paths().iter().all(|path| path.is_system),
"a toolchain's own directories are what `-isystem` would name, which is what decides whether a \
diagnostic inside them is the project's business"
);
}
#[test]
fn a_program_that_is_not_a_compiler_is_not_asked_twice() {
let runner = Answers {
program: "g++",
output: "usage: g++ [options] file\n",
};
assert_eq!(
search_paths(&runner, Path::new("/usr/bin/g++"), None, &[]),
None
);
}
#[test]
fn the_predefined_macros_are_read_out_of_the_same_answer() {
let macros = parse_builtin_macros(
"#define __cplusplus 201703L\n\
#define __linux 1\n\
#define __STDC_HOSTED__ 1\n\
#define assert(expr) ((expr) ? (void)0 : abort())\n\
#define __FLT_MIN__ 1.17549435082228750797e-38F\n\
gcc version 11.2.0\n\
#include <...> search starts here:\n",
);
let value_of = |name: &str| {
macros
.iter()
.find(|define| define.name.as_ref() == name)
.map(|define| define.value.as_deref())
};
assert_eq!(value_of("__cplusplus"), Some(Some("201703L")));
assert_eq!(value_of("__STDC_HOSTED__"), Some(Some("1")));
assert_eq!(
value_of("assert"),
None,
"a function-like macro is not a name a condition can test"
);
assert_eq!(
value_of("__FLT_MIN__"),
Some(Some("1.17549435082228750797e-38F")),
"a value is everything up to the line's end, floats included"
);
assert_eq!(value_of("gcc"), None, "`-v`'s own output is not a macro");
}
#[test]
fn a_standard_the_caller_names_is_passed_to_the_compiler() {
let runner = Answers {
program: "g++",
output: GCC,
};
let toolchain = search_paths(&runner, Path::new("/usr/bin/g++"), Some("c++20"), &[])
.expect("the fixture answers");
assert_eq!(
toolchain.compiler.as_deref(),
Some(Path::new("/usr/bin/g++"))
);
}
#[test]
fn discovery_walks_from_the_environment_to_the_answer() {
let files = MemoryFiles::new().with_file("/usr/bin/g++", "");
let runner = Answers {
program: "g++",
output: GCC,
};
let toolchain = discover(
&files,
&runner,
super::BuildStatement::default(),
Path::new("/p/main.cpp"),
&environment(),
&crate::include::msvc::WindowsLayout::default(),
)
.expect("the compiler is on the path and answers");
assert_eq!(
toolchain.compiler.as_deref(),
Some(Path::new("/usr/bin/g++"))
);
assert_eq!(toolchain.source, ToolchainSource::Path);
assert_eq!(
toolchain.standard.as_deref(),
Some("c++23"),
"nothing stated a standard, so the newest this compiler takes is what it was asked about"
);
assert!(
toolchain
.note
.as_deref()
.is_some_and(|note| note.contains("newest") && note.contains("c++23")),
"and the report says so rather than leaving it implicit: {:?}",
toolchain.note
);
assert_eq!(toolchain.system_include_paths.len(), 3);
}
#[test]
fn a_project_that_states_no_standard_is_asked_for_the_newest() {
let files = MemoryFiles::new().with_file("/usr/bin/g++", "");
let runner = Answers {
program: "g++",
output: GCC,
};
let toolchain = discover(
&files,
&runner,
super::BuildStatement::default(),
Path::new("/p/main.cpp"),
&environment(),
&crate::include::msvc::WindowsLayout::default(),
)
.expect("the compiler answers");
assert_eq!(toolchain.standard.as_deref(), Some("c++23"));
let config = toolchain.config(&CompilerConfig::new());
assert_eq!(config.standard.as_deref(), Some("c++23"));
let stated = crate::include::config::predefined_macros_of(&config);
assert!(
stated
.iter()
.any(|define| define.name.as_ref() == "__cplusplus"
&& define.value.as_deref() == Some("202302L")),
"`__cplusplus` is what every `#if __cplusplus >= …` in every header asks: {stated:?}"
);
}
#[test]
fn a_standard_the_project_states_is_not_replaced_by_the_assumption() {
let files = MemoryFiles::new().with_file("/usr/bin/g++", "");
let runner = Answers {
program: "g++",
output: GCC,
};
let mut commands = command_line("/usr/bin/g++");
commands.commands[0]
.arguments
.push("-std=c++17".to_string());
let toolchain = discover(
&files,
&runner,
from_database(&commands),
Path::new("/p/main.cpp"),
&environment(),
&crate::include::msvc::WindowsLayout::default(),
)
.expect("the compiler answers");
assert_eq!(toolchain.standard.as_deref(), Some("c++17"));
assert!(
toolchain.note.is_none(),
"nothing was assumed, so there is nothing to report: {:?}",
toolchain.note
);
assert_eq!(
toolchain.config(&CompilerConfig::new()).standard.as_deref(),
Some("c++17")
);
}
#[test]
fn a_compiler_the_database_names_is_asked_before_the_environment_is() {
let files = MemoryFiles::new()
.with_file("/toolchains/bin/g++", "")
.with_file("/usr/bin/g++", "");
let runner = Answers {
program: "g++",
output: GCC,
};
let mut environment = environment();
environment.path = vec![Path::new("/toolchains/bin").into()];
let commands = command_line("/toolchains/bin/g++");
let toolchain = discover(
&files,
&runner,
from_database(&commands),
Path::new("/p/main.cpp"),
&environment,
&crate::include::msvc::WindowsLayout::default(),
)
.expect("the database's compiler answers");
assert_eq!(
toolchain.compiler.as_deref(),
Some(Path::new("/toolchains/bin/g++"))
);
assert_eq!(
toolchain.source,
ToolchainSource::CompileDatabase,
"and the report says which step answered"
);
}
#[test]
fn a_machine_with_no_compiler_still_has_the_systems_headers_and_no_macros() {
let files = MemoryFiles::new();
let runner = Answers {
program: "nothing",
output: "",
};
let mut environment = environment();
environment.path = Vec::new();
environment.cxx = None;
environment.cc = None;
let found = discover(
&files,
&runner,
super::BuildStatement::default(),
Path::new("/p/main.cpp"),
&environment,
&crate::include::msvc::WindowsLayout::default(),
);
if let Some(toolchain) = found {
assert_eq!(toolchain.source, ToolchainSource::SystemHeaders);
assert!(toolchain.source.is_a_guess());
assert_eq!(toolchain.compiler, None);
assert!(toolchain.builtin_macros.is_empty());
assert!(
toolchain
.note
.as_deref()
.is_some_and(|note| note.contains("Unknown")),
"the note says what is missing: {:?}",
toolchain.note
);
assert!(
!toolchain.system_include_paths.is_empty(),
"and every directory is one that exists"
);
for directory in &toolchain.system_include_paths {
assert!(directory.is_dir(), "{}", directory.display());
}
}
}
#[test]
fn the_directories_are_appended_so_the_project_keeps_its_own_order() {
let toolchain = Toolchain {
compiler: Some("/usr/bin/g++".into()),
version: None,
system_include_paths: vec!["/gcc/include/c++".into(), "/gcc/include".into()],
builtin_macros: Vec::new(),
cuda: false,
dialect: None,
standard: None,
source: ToolchainSource::Path,
note: None,
};
let base = CompilerConfig::new().with_include_path("project/inc");
let config = toolchain.config(&base);
assert_eq!(
config.include_paths,
[
IncludePath::user("project/inc"),
IncludePath::system("/gcc/include/c++"),
IncludePath::system("/gcc/include"),
]
);
}
#[test]
fn preparing_a_configuration_twice_changes_nothing_the_second_time() {
let toolchain = Toolchain {
compiler: Some("/usr/bin/g++".into()),
version: None,
system_include_paths: vec!["/gcc/include".into(), "/gcc/include".into()],
builtin_macros: Vec::new(),
cuda: false,
dialect: None,
standard: None,
source: ToolchainSource::Path,
note: None,
};
let once = toolchain.config(&CompilerConfig::new());
let twice = toolchain.config(&once);
assert_eq!(once, twice);
assert_eq!(
once.include_paths.len(),
1,
"and the duplicate inside is folded too"
);
let already_there = CompilerConfig::new().with_system_include_path("/gcc/include");
assert_eq!(
toolchain.config(&already_there),
already_there,
"a directory the project already lists is not added again"
);
}
fn toolchain_of(dialect: Option<Dialect>, include_paths: &[&str]) -> Toolchain {
Toolchain {
compiler: None,
version: None,
system_include_paths: include_paths.iter().map(PathBuf::from).collect(),
builtin_macros: Vec::new(),
cuda: false,
dialect,
standard: None,
source: ToolchainSource::SystemHeaders,
note: None,
}
}
#[test]
fn a_module_nobody_declares_gets_a_note_about_where_the_compiler_keeps_modules() {
let msvc = toolchain_of(Some(Dialect::Msvc), &["C:/VC/Tools/MSVC/14.35/include"]);
let about_std = msvc.module_note("std");
assert!(
about_std.contains("/reference std="),
"MSVC is told the switch that names a module: {about_std}"
);
assert!(
about_std.contains("std.ixx") && about_std.contains("modules"),
"and where the standard module's own source is, because that file exists on the machine and is the whole \
of the fix: {about_std}"
);
assert!(
about_std.contains("unknown rather than absent"),
"the note never claims the names do not exist: {about_std}"
);
assert!(
!about_std.contains("\\\\"),
"the path is not escaped as if it were in a string literal: {about_std}"
);
let about_another = msvc.module_note("mylib");
assert!(about_another.contains("/reference mylib="));
assert!(
!about_another.contains("std.ixx"),
"no standard library path for a module that is not one: {about_another}"
);
let gnu = toolchain_of(Some(Dialect::Gnu), &["/usr/include/c++/15"]);
let about_std = gnu.module_note("std");
assert!(
about_std.contains("gcm.cache/std.gcm") && about_std.contains("-fmodule-file=std="),
"both mechanisms are named, because the dialect does not tell them apart: {about_std}"
);
assert!(
!about_std.contains("/reference"),
"and MSVC's switch is not offered on a compiler that would reject it: {about_std}"
);
let unknown = toolchain_of(None, &[]).module_note("std");
assert!(
unknown.contains("unknown rather than absent") && !unknown.contains("/reference"),
"an unknown compiler is not guessed at: {unknown}"
);
}
#[test]
fn a_partition_gets_a_note_about_the_module_it_belongs_to() {
let msvc = toolchain_of(Some(Dialect::Msvc), &["C:/VC/Tools/MSVC/14.35/include"]);
let note = msvc.partition_note("shapes", "area");
assert!(
note.contains("`shapes:area`") || note.contains("/reference shapes:area="),
"MSVC is told the switch, spelled with the module *and* the partition: {note}"
);
assert!(
note.contains(":area") && note.contains("`shapes`"),
"and which module the partition belongs to: {note}"
);
assert!(
!note.contains("declares module `area`"),
"the note must not read a partition as a module — nothing declares a module called `area`: {note}"
);
assert!(note.contains("unknown rather than absent"));
let gnu =
toolchain_of(Some(Dialect::Gnu), &["/usr/include/c++/15"]).partition_note("shapes", "area");
assert!(
!gnu.contains("/reference"),
"MSVC's switch is not offered to a compiler that would reject it: {gnu}"
);
assert!(
gnu.contains("gcm.cache/shapes.gcm"),
"GCC builds it into the module file: {gnu}"
);
}
#[test]
fn a_standard_modules_source_is_looked_for_beside_the_include_directories() {
let scratch = std::env::temp_dir().join(format!("cppls-toolchain-{}", std::process::id()));
let include = scratch
.join("VC")
.join("Tools")
.join("MSVC")
.join("14.35")
.join("include");
let modules = include.parent().expect("a parent").join("modules");
std::fs::create_dir_all(&modules).expect("a scratch tree");
std::fs::write(modules.join("std.ixx"), "export module std;\n").expect("the shipped source");
let toolchain = toolchain_of(Some(Dialect::Msvc), &[include.to_string_lossy().as_ref()]);
assert_eq!(
toolchain.standard_module_source("std"),
modules.join("std.ixx"),
"the file beside the include directory is the one that ships with the toolchain"
);
let absent = toolchain.standard_module_source("mylib");
assert_eq!(absent, modules.join("mylib.ixx"));
let _ = std::fs::remove_dir_all(&scratch);
}
#[test]
fn the_search_list_flags_of_a_command_are_taken_and_nothing_else() {
let flags = |arguments: &[&str]| -> Vec<String> {
search_list_flags(&arguments.iter().map(|a| a.to_string()).collect::<Vec<_>>())
};
assert_eq!(
flags(&[
"clang++",
"--target=x86_64-w64-mingw32",
"--gcc-toolchain=C:/mingw64",
"-std=c++23",
"-Iproj",
"-c",
"src/main.cpp",
"-o",
"build/main.o",
]),
vec![
"--target=x86_64-w64-mingw32".to_string(),
"--gcc-toolchain=C:/mingw64".to_string()
],
"the two flags that decide which library, and nothing else"
);
assert_eq!(
flags(&[
"clang++",
"-target",
"aarch64-linux-gnu",
"-isystem",
"/opt/inc",
"-c",
"a.cpp",
]),
vec![
"-target".to_string(),
"aarch64-linux-gnu".to_string(),
"-isystem".to_string(),
"/opt/inc".to_string(),
],
"a flag whose value is the next token takes that token with it"
);
}