use std::ffi::OsString;
use std::path::{Path, PathBuf};
use crate::include::config::CommandLineMacro;
use crate::include::toolchain::{CommandRunner, Output};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct WindowsLayout {
pub program_files: Vec<PathBuf>,
pub windows_sdk_dir: Option<PathBuf>,
pub vc_tools_install_dir: Option<PathBuf>,
}
impl WindowsLayout {
pub fn current() -> Self {
if !cfg!(windows) {
return WindowsLayout::default();
}
let mut program_files = Vec::new();
for name in ["ProgramFiles(x86)", "ProgramFiles", "ProgramW6432"] {
if let Some(value) = std::env::var_os(name) {
let path = PathBuf::from(value);
if !program_files.contains(&path) {
program_files.push(path);
}
}
}
WindowsLayout {
program_files,
windows_sdk_dir: std::env::var_os("WindowsSdkDir").map(PathBuf::from),
vc_tools_install_dir: std::env::var_os("VCToolsInstallDir").map(PathBuf::from),
}
}
pub fn vswhere(&self) -> Option<PathBuf> {
self.program_files.iter().find_map(|program_files| {
let candidate = program_files
.join("Microsoft Visual Studio")
.join("Installer")
.join("vswhere.exe");
candidate.is_file().then_some(candidate)
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VisualStudio {
pub installation: PathBuf,
pub version: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Toolset {
pub root: PathBuf,
pub version: String,
pub cl: PathBuf,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WindowsSdk {
pub root: PathBuf,
pub version: String,
}
impl WindowsSdk {
pub const PRODUCT: &'static str = "10";
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Msvc {
pub toolset: Toolset,
pub sdk: Option<WindowsSdk>,
pub include: Vec<PathBuf>,
pub from_environment: bool,
}
impl Msvc {
pub fn environment(&self) -> Vec<(OsString, OsString)> {
if self.from_environment {
return Vec::new();
}
let joined = std::env::join_paths(&self.include).unwrap_or_default();
vec![(OsString::from("INCLUDE"), joined)]
}
pub fn include_paths(&self) -> Vec<PathBuf> {
self.include.clone()
}
}
pub fn discover(runner: &impl CommandRunner, layout: &WindowsLayout) -> Option<Msvc> {
if !cfg!(windows) {
return None;
}
let from_prompt: Vec<PathBuf> = std::env::var_os("INCLUDE")
.map(|value| std::env::split_paths(&value).collect())
.unwrap_or_default();
if !from_prompt.is_empty() {
let toolset = layout
.vc_tools_install_dir
.as_ref()
.and_then(|root| toolset_at(root))
.or_else(|| newest_toolset(&newest_installation(runner, layout)?));
let sdk = layout
.windows_sdk_dir
.as_ref()
.and_then(|root| newest_sdk(root))
.or_else(|| find_sdk(&layout.program_files));
if let Some(toolset) = toolset {
return Some(Msvc {
toolset,
sdk,
include: from_prompt,
from_environment: true,
});
}
}
let installation = newest_installation(runner, layout)?;
let toolset = newest_toolset(&installation)?;
let sdk = find_sdk(&layout.program_files);
let include = include_directories(&installation, &toolset, sdk.as_ref());
if include.is_empty() {
return None;
}
Some(Msvc {
toolset,
sdk,
include,
from_environment: false,
})
}
pub fn newest_installation(
runner: &impl CommandRunner,
layout: &WindowsLayout,
) -> Option<VisualStudio> {
let vswhere = layout.vswhere()?;
let output = runner.run(
&vswhere,
&[
"-latest",
"-products",
"*",
"-requires",
"Microsoft.VisualStudio.Component.VC.Tools.x86.x64",
"-format",
"json",
],
&[],
)?;
if !output.succeeded {
return None;
}
let installation = json_string_field(&output.stdout, "installationPath")?;
Some(VisualStudio {
installation: PathBuf::from(installation),
version: json_string_field(&output.stdout, "installationVersion"),
})
}
pub fn toolsets(installation: &Path) -> Vec<Toolset> {
let root = installation.join("VC").join("Tools").join("MSVC");
let Ok(entries) = std::fs::read_dir(&root) else {
return Vec::new();
};
let mut found: Vec<Toolset> = entries
.flatten()
.filter(|entry| entry.file_type().is_ok_and(|kind| kind.is_dir()))
.filter_map(|entry| toolset_at(&entry.path()))
.collect();
found.sort_by(|one, two| compare_versions(&two.version, &one.version));
found
}
pub fn newest_toolset(installation: &VisualStudio) -> Option<Toolset> {
toolsets(&installation.installation).into_iter().next()
}
fn toolset_at(root: &Path) -> Option<Toolset> {
let version = root.file_name()?.to_str()?.to_string();
if !root.is_dir() {
return None;
}
let cl = cl_for_this_host(root)?;
Some(Toolset {
root: root.to_path_buf(),
version,
cl,
})
}
fn cl_for_this_host(toolset: &Path) -> Option<PathBuf> {
const PAIRS: &[(&str, &str)] = &[
("Hostx64", "x64"),
("Hostx64", "x86"),
("Hostx86", "x64"),
("Hostx86", "x86"),
];
PAIRS.iter().find_map(|(host, target)| {
let candidate = toolset.join("bin").join(host).join(target).join("cl.exe");
candidate.is_file().then_some(candidate)
})
}
pub fn windows_sdks(layout: &WindowsLayout) -> Vec<WindowsSdk> {
let mut roots: Vec<PathBuf> = Vec::new();
if let Some(sdk_dir) = &layout.windows_sdk_dir {
roots.push(sdk_dir.clone());
}
for program_files in &layout.program_files {
roots.push(program_files.join("Windows Kits").join(WindowsSdk::PRODUCT));
}
let mut found: Vec<WindowsSdk> = Vec::new();
for root in roots {
let Ok(entries) = std::fs::read_dir(root.join("Include")) else {
continue;
};
for entry in entries.flatten() {
let Some(version) = entry.file_name().to_str().map(str::to_string) else {
continue;
};
if entry.file_type().is_ok_and(|kind| kind.is_dir())
&& !found.iter().any(|known| known.version == version)
{
found.push(WindowsSdk {
root: root.clone(),
version,
});
}
}
}
found.sort_by(|one, two| compare_versions(&two.version, &one.version));
found
}
pub fn newest_sdk(root: &Path) -> Option<WindowsSdk> {
let layout = WindowsLayout {
program_files: Vec::new(),
windows_sdk_dir: Some(root.to_path_buf()),
vc_tools_install_dir: None,
};
windows_sdks(&layout).into_iter().next()
}
fn find_sdk(program_files: &[PathBuf]) -> Option<WindowsSdk> {
windows_sdks(&WindowsLayout {
program_files: program_files.to_vec(),
windows_sdk_dir: None,
vc_tools_install_dir: None,
})
.into_iter()
.next()
}
fn include_directories(
installation: &VisualStudio,
toolset: &Toolset,
sdk: Option<&WindowsSdk>,
) -> Vec<PathBuf> {
let mut include = Vec::new();
let mut push = |path: PathBuf| {
if path.is_dir() && !include.contains(&path) {
include.push(path);
}
};
push(toolset.root.join("include"));
push(toolset.root.join("ATLMFC").join("include"));
push(
installation
.installation
.join("VC")
.join("Auxiliary")
.join("VS")
.join("include"),
);
if let Some(sdk) = sdk {
let version = sdk.root.join("Include").join(&sdk.version);
for part in ["ucrt", "um", "shared", "winrt", "cppwinrt"] {
push(version.join(part));
}
}
include
}
pub fn predefined_macros(
runner: &impl CommandRunner,
msvc: &Msvc,
standard: Option<&str>,
) -> Option<Vec<CommandLineMacro>> {
predefined_macros_with(runner, msvc, standard, &[])
}
pub fn predefined_macros_with(
runner: &impl CommandRunner,
msvc: &Msvc,
standard: Option<&str>,
fallbacks: &[std::path::PathBuf],
) -> Option<Vec<CommandLineMacro>> {
let scratch = Scratch::new(fallbacks)?;
let environment = msvc.environment();
let requested = standard.map(|standard| format!("/std:{standard}"));
let object = scratch.path.join("predefined.obj");
let mut arguments: Vec<&str> = vec![
"/nologo",
"/Zc:preprocessor",
"/Zc:__cplusplus",
"/PD",
"/c",
];
if let Some(requested) = &requested {
arguments.push(requested);
}
let object_argument = format!("/Fo{}", object.display());
arguments.push(&object_argument);
let source_argument = scratch.source.to_string_lossy().into_owned();
arguments.push(&source_argument);
let output: Output = runner.run(&msvc.toolset.cl, &arguments, &environment)?;
if !output.succeeded {
return None;
}
let mut macros = crate::include::toolchain::parse_builtin_macros(&output.stdout);
macros.sort_by(|one, two| one.name.cmp(&two.name));
macros.dedup_by(|one, two| one.name == two.name);
(!macros.is_empty()).then_some(macros)
}
pub fn version_line(msvc: &Msvc, macros: &[CommandLineMacro]) -> String {
let value_of = |name: &str| {
macros
.iter()
.find(|define| define.name.as_ref() == name)
.and_then(|define| define.value.as_deref())
};
match value_of("_MSC_FULL_VER").or_else(|| value_of("_MSC_VER")) {
Some(version) => format!("MSVC {} (_MSC_FULL_VER {version})", msvc.toolset.version),
None => format!("MSVC {}", msvc.toolset.version),
}
}
struct Scratch {
path: PathBuf,
source: PathBuf,
}
impl Scratch {
fn new(fallbacks: &[std::path::PathBuf]) -> Option<Scratch> {
static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
let unique = format!(
"cppls-msvc-{}-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|elapsed| elapsed.as_nanos())
.unwrap_or_default(),
NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
);
let mut bases = vec![std::env::temp_dir()];
bases.extend(fallbacks.iter().cloned());
for base in bases {
let path = base.join(&unique);
if let Err(error) = std::fs::create_dir_all(&path) {
eprintln!(
"cppls: cannot create the scratch directory {} for asking MSVC its predefined macros: {error}",
path.display()
);
continue;
}
let source = path.join("predefined.cpp");
if let Err(error) = std::fs::write(&source, "") {
eprintln!(
"cppls: cannot write the scratch source {} for asking MSVC its predefined macros: {error}",
source.display()
);
continue;
}
return Some(Scratch { path, source });
}
eprintln!(
"cppls: no writable directory for the scratch file, so MSVC's predefined macros are unknown and every \
condition that names a builtin will be answered Unknown"
);
None
}
}
impl Drop for Scratch {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.path);
}
}
fn compare_versions(one: &str, two: &str) -> std::cmp::Ordering {
let parts = |version: &str| -> Vec<u64> {
version
.split('.')
.map(|part| part.parse::<u64>().unwrap_or(0))
.collect()
};
let (one, two) = (parts(one), parts(two));
one.cmp(&two)
}
fn json_string_field(json: &str, field: &str) -> Option<String> {
let key = format!("\"{field}\"");
let start = json.find(&key)? + key.len();
let rest = json[start..].trim_start();
let rest = rest.strip_prefix(':')?.trim_start();
let rest = rest.strip_prefix('"')?;
let mut value = String::new();
let mut characters = rest.chars();
while let Some(character) = characters.next() {
match character {
'"' => return Some(value),
'\\' => match characters.next()? {
'n' => value.push('\n'),
't' => value.push('\t'),
'r' => value.push('\r'),
'u' => {
for _ in 0..4 {
characters.next()?;
}
}
other => value.push(other),
},
other => value.push(other),
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Mutex;
type Asked = (PathBuf, Vec<String>, Vec<(OsString, OsString)>);
struct Scripted {
answers: Mutex<Vec<(String, Output)>>,
asked: Mutex<Vec<Asked>>,
}
impl Scripted {
fn new(answers: Vec<(&str, Output)>) -> Self {
Scripted {
answers: Mutex::new(
answers
.into_iter()
.map(|(program, output)| (program.to_string(), output))
.collect(),
),
asked: Mutex::new(Vec::new()),
}
}
fn answering(stdout: &str) -> Self {
Scripted::new(vec![(
"vswhere.exe",
Output {
stdout: stdout.to_string(),
stderr: String::new(),
status: Some(0),
succeeded: true,
},
)])
}
}
impl CommandRunner for Scripted {
fn run(
&self,
program: &Path,
arguments: &[&str],
environment: &[(OsString, OsString)],
) -> Option<Output> {
self.asked.lock().expect("not poisoned").push((
program.to_path_buf(),
arguments
.iter()
.map(|argument| argument.to_string())
.collect(),
environment.to_vec(),
));
let name = program.file_name()?.to_string_lossy().to_string();
let mut answers = self.answers.lock().expect("not poisoned");
let position = answers.iter().position(|(known, _)| known == &name)?;
Some(answers.remove(position).1)
}
}
#[test]
fn vswhere_json_gives_the_installation_and_its_version() {
let runner = Scripted::answering(
"[\n {\n \"instanceId\": \"3d1f0b4c\",\n \"installationVersion\": \"17.5.33424.131\",\n \
\"installationPath\": \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\",\n \
\"description\": \"localized text nobody reads\"\n }\n]\n",
);
let layout = WindowsLayout {
program_files: vec![PathBuf::from("C:/Program Files (x86)")],
..WindowsLayout::default()
};
let vswhere = layout
.program_files
.first()
.expect("a program files directory")
.join("Microsoft Visual Studio")
.join("Installer")
.join("vswhere.exe");
let output = runner
.run(&vswhere, &["-latest", "-format", "json"], &[])
.expect("the script answers");
let installation = json_string_field(&output.stdout, "installationPath").expect("a path");
let version = json_string_field(&output.stdout, "installationVersion").expect("a version");
assert_eq!(
installation,
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community"
);
assert_eq!(version, "17.5.33424.131");
assert_eq!(
json_string_field(&output.stdout, "nothing"),
None,
"a field that is not there is absent, not empty"
);
}
#[test]
fn the_newest_toolset_and_the_newest_sdk_are_chosen_by_version_and_not_by_spelling() {
assert_eq!(
compare_versions("14.35.32215", "14.9.1"),
std::cmp::Ordering::Greater
);
assert_eq!(
compare_versions("10.0.22621.0", "10.0.22000.0"),
std::cmp::Ordering::Greater
);
}
#[test]
fn a_toolset_is_enumerated_from_disk_with_the_four_host_target_pairs() {
let root = std::env::temp_dir().join("cppls-msvc-toolsets");
let _ = std::fs::remove_dir_all(&root);
let installation = root.join("VS");
for (toolset, host, target) in [
("14.9.1", "Hostx64", "x64"),
("14.35.32215", "Hostx64", "x64"),
("14.35.32215", "Hostx64", "x86"),
] {
let bin = installation
.join("VC/Tools/MSVC")
.join(toolset)
.join("bin")
.join(host)
.join(target);
std::fs::create_dir_all(&bin).expect("the fixture directory");
std::fs::write(bin.join("cl.exe"), "").expect("the fixture writes");
}
std::fs::create_dir_all(installation.join("VC/Tools/MSVC/14.40.0/include"))
.expect("the fixture directory");
let found = toolsets(&installation);
assert_eq!(found.len(), 2, "two usable toolsets: {found:?}");
assert_eq!(found[0].version, "14.35.32215", "the newest first");
assert_eq!(found[1].version, "14.9.1");
assert!(
found[0]
.cl
.to_string_lossy()
.replace('\\', "/")
.ends_with("bin/Hostx64/x64/cl.exe"),
"a 64-bit host targeting 64-bit code: {}",
found[0].cl.display()
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn only_the_sdk_parts_that_exist_are_in_the_search_list() {
let root = std::env::temp_dir().join("cppls-msvc-include");
let _ = std::fs::remove_dir_all(&root);
let installation = VisualStudio {
installation: root.join("VS"),
version: None,
};
let toolset_root = installation.installation.join("VC/Tools/MSVC/14.35.32215");
let sdk_root = root.join("Kits/10");
for directory in [
toolset_root.join("include"),
toolset_root.join("ATLMFC/include"),
installation.installation.join("VC/Auxiliary/VS/include"),
sdk_root.join("Include/10.0.22621.0/ucrt"),
sdk_root.join("Include/10.0.22621.0/um"),
sdk_root.join("Include/10.0.22621.0/shared"),
] {
std::fs::create_dir_all(&directory).expect("the fixture directory");
}
let toolset = Toolset {
root: toolset_root.clone(),
version: "14.35.32215".to_string(),
cl: toolset_root.join("bin/Hostx64/x64/cl.exe"),
};
let sdk = WindowsSdk {
root: sdk_root.clone(),
version: "10.0.22621.0".to_string(),
};
let include = include_directories(&installation, &toolset, Some(&sdk));
let shown: Vec<String> = include
.iter()
.map(|path| {
path.strip_prefix(&root)
.unwrap_or(path)
.to_string_lossy()
.replace('\\', "/")
.to_string()
})
.collect();
assert_eq!(
shown,
[
"VS/VC/Tools/MSVC/14.35.32215/include",
"VS/VC/Tools/MSVC/14.35.32215/ATLMFC/include",
"VS/VC/Auxiliary/VS/include",
"Kits/10/Include/10.0.22621.0/ucrt",
"Kits/10/Include/10.0.22621.0/um",
"Kits/10/Include/10.0.22621.0/shared",
],
"vcvars64's order, with the parts that do not exist left out"
);
let _ = std::fs::remove_dir_all(&root);
}
#[test]
fn the_macro_dump_is_asked_for_with_the_flags_that_make_it_work() {
let toolset_root = std::env::temp_dir().join("cppls-msvc-dump/VC/Tools/MSVC/14.35.32215");
let msvc = Msvc {
toolset: Toolset {
root: toolset_root.clone(),
version: "14.35.32215".to_string(),
cl: toolset_root.join("bin/Hostx64/x64/cl.exe"),
},
sdk: None,
include: vec![toolset_root.join("include")],
from_environment: false,
};
let runner = Scripted::new(vec![(
"cl.exe",
Output {
stdout: "#define _MSC_VER 1935\n#define _WIN32 1\n#define _MSC_VER 1935\n"
.to_string(),
stderr: String::new(),
status: Some(0),
succeeded: true,
},
)]);
let macros = predefined_macros(&runner, &msvc, Some("c++20")).expect("the script answers");
let asked = runner.asked.lock().expect("not poisoned").clone();
let (program, arguments, environment) = asked.first().expect("cl was asked");
assert!(program.ends_with("cl.exe"));
for required in [
"/nologo",
"/Zc:preprocessor",
"/Zc:__cplusplus",
"/PD",
"/c",
] {
assert!(
arguments.iter().any(|argument| argument == required),
"{required} is missing: {arguments:?}"
);
}
assert!(
arguments.iter().any(|argument| argument == "/std:c++20"),
"{arguments:?}"
);
assert!(
arguments.iter().any(|argument| argument.starts_with("/Fo")),
"the object file does not go next to whatever directory the server was started in: {arguments:?}"
);
assert!(
arguments
.last()
.is_some_and(|argument| argument.ends_with("predefined.cpp")),
"cl needs a real file: {arguments:?}"
);
assert_eq!(
environment.len(),
1,
"the search list is handed over in the environment, which is what a developer prompt does"
);
assert_eq!(environment[0].0, OsString::from("INCLUDE"));
let names: Vec<&str> = macros.iter().map(|define| define.name.as_ref()).collect();
assert_eq!(names, ["_MSC_VER", "_WIN32"]);
}
#[test]
fn a_compiler_that_could_not_be_asked_is_not_a_compiler_with_no_macros() {
let msvc = Msvc {
toolset: Toolset {
root: PathBuf::from("/VS/VC/Tools/MSVC/14.35.32215"),
version: "14.35.32215".to_string(),
cl: PathBuf::from("/VS/VC/Tools/MSVC/14.35.32215/bin/Hostx64/x64/cl.exe"),
},
sdk: None,
include: vec![PathBuf::from("/VS/VC/Tools/MSVC/14.35.32215/include")],
from_environment: false,
};
let runner = Scripted::new(vec![(
"cl.exe",
Output {
stdout: String::new(),
stderr: String::new(),
status: Some(1),
succeeded: false,
},
)]);
assert_eq!(predefined_macros(&runner, &msvc, None), None);
let status = Output {
status: Some(4),
succeeded: false,
..Output::default()
};
assert_eq!(status.status, Some(4));
}
}