Skip to content

Commit 5b91c7b

Browse files
authored
Merge pull request #696 from ChrisDenton/detect-msvc
Detect msvc version if there is no default
2 parents c0f2118 + 755e4fe commit 5b91c7b

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

src/windows_registry.rs

+40-7
Original file line numberDiff line numberDiff line change
@@ -450,14 +450,10 @@ mod impl_ {
450450

451451
fn vs15plus_vc_paths(
452452
target: &str,
453-
instance_path: &PathBuf,
453+
instance_path: &Path,
454454
) -> Option<(PathBuf, PathBuf, PathBuf, PathBuf, PathBuf)> {
455-
let version_path =
456-
instance_path.join(r"VC\Auxiliary\Build\Microsoft.VCToolsVersion.default.txt");
457-
let mut version_file = File::open(version_path).ok()?;
458-
let mut version = String::new();
459-
version_file.read_to_string(&mut version).ok()?;
460-
let version = version.trim();
455+
let version = vs15plus_vc_read_version(instance_path)?;
456+
461457
let host = match host_arch() {
462458
X86 => "X86",
463459
X86_64 => "X64",
@@ -487,6 +483,43 @@ mod impl_ {
487483
Some((path, bin_path, host_dylib_path, lib_path, include_path))
488484
}
489485

486+
fn vs15plus_vc_read_version(dir: &Path) -> Option<String> {
487+
// Try to open the default version file.
488+
let mut version_path: PathBuf =
489+
dir.join(r"VC\Auxiliary\Build\Microsoft.VCToolsVersion.default.txt");
490+
let mut version_file = if let Ok(f) = File::open(&version_path) {
491+
f
492+
} else {
493+
// If the default doesn't exist, search for other version files.
494+
// These are in the form Microsoft.VCToolsVersion.v143.default.txt
495+
// where `143` is any three decimal digit version number.
496+
// This sorts versions by lexical order and selects the highest version.
497+
let mut version_file = String::new();
498+
version_path.pop();
499+
for file in version_path.read_dir().ok()? {
500+
let name = file.ok()?.file_name();
501+
let name = name.to_str()?;
502+
if name.starts_with("Microsoft.VCToolsVersion.v")
503+
&& name.ends_with(".default.txt")
504+
&& name > &version_file
505+
{
506+
version_file.replace_range(.., name);
507+
}
508+
}
509+
if version_file.is_empty() {
510+
return None;
511+
}
512+
version_path.push(version_file);
513+
File::open(version_path).ok()?
514+
};
515+
516+
// Get the version string from the file we found.
517+
let mut version = String::new();
518+
version_file.read_to_string(&mut version).ok()?;
519+
version.truncate(version.trim_end().len());
520+
Some(version)
521+
}
522+
490523
fn atl_paths(target: &str, path: &Path) -> Option<(PathBuf, PathBuf)> {
491524
let atl_path = path.join("atlmfc");
492525
let sub = lib_subdir(target)?;

0 commit comments

Comments
 (0)