From 96c7e7659a402eb2b44228fb6f7a8ad87f7e8614 Mon Sep 17 00:00:00 2001 From: PENGUINLIONG Date: Tue, 4 Jan 2022 23:15:18 +0800 Subject: [PATCH] Identify Visual Studio 2022 by MSBuild (#648) --- src/windows_registry.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/windows_registry.rs b/src/windows_registry.rs index 65cd5f15..7cb40eba 100644 --- a/src/windows_registry.rs +++ b/src/windows_registry.rs @@ -134,7 +134,9 @@ pub fn find_vs_version() -> Result { _ => { // Check for the presence of a specific registry key // that indicates visual studio is installed. - if impl_::has_msbuild_version("16.0") { + if impl_::has_msbuild_version("17.0") { + Ok(VsVers::Vs17) + } else if impl_::has_msbuild_version("16.0") { Ok(VsVers::Vs16) } else if impl_::has_msbuild_version("15.0") { Ok(VsVers::Vs15) @@ -250,18 +252,22 @@ mod impl_ { } } + fn find_msbuild_vs17(target: &str) -> Option { + find_tool_in_vs16plus_path(r"MSBuild\Current\Bin\MSBuild.exe", target, "17") + } + #[allow(bare_trait_objects)] - fn vs16_instances(target: &str) -> Box> { + fn vs16plus_instances(target: &str, version: &'static str) -> Box> { let instances = if let Some(instances) = vs15plus_instances(target) { instances } else { return Box::new(iter::empty()); }; - Box::new(instances.into_iter().filter_map(|instance| { + Box::new(instances.into_iter().filter_map(move |instance| { let installation_name = instance.installation_name()?; - if installation_name.starts_with("VisualStudio/16.") { + if installation_name.starts_with(&format!("VisualStudio/{}.", version)) { Some(instance.installation_path()?) - } else if installation_name.starts_with("VisualStudioPreview/16.") { + } else if installation_name.starts_with(&format!("VisualStudioPreview/{}.", version)) { Some(instance.installation_path()?) } else { None @@ -269,8 +275,8 @@ mod impl_ { })) } - fn find_tool_in_vs16_path(tool: &str, target: &str) -> Option { - vs16_instances(target) + fn find_tool_in_vs16plus_path(tool: &str, target: &str, version: &'static str) -> Option { + vs16plus_instances(target, version) .filter_map(|path| { let path = path.join(tool); if !path.is_file() { @@ -289,7 +295,7 @@ mod impl_ { } fn find_msbuild_vs16(target: &str) -> Option { - find_tool_in_vs16_path(r"MSBuild\Current\Bin\MSBuild.exe", target) + find_tool_in_vs16plus_path(r"MSBuild\Current\Bin\MSBuild.exe", target, "16") } // In MSVC 15 (2017) MS once again changed the scheme for locating @@ -816,6 +822,11 @@ mod impl_ { pub fn has_msbuild_version(version: &str) -> bool { match version { + "17.0" => { + find_msbuild_vs17("x86_64-pc-windows-msvc").is_some() + || find_msbuild_vs17("i686-pc-windows-msvc").is_some() + || find_msbuild_vs17("aarch64-pc-windows-msvc").is_some() + } "16.0" => { find_msbuild_vs16("x86_64-pc-windows-msvc").is_some() || find_msbuild_vs16("i686-pc-windows-msvc").is_some()