Skip to content

Commit

Permalink
fix find_tool and find_vs_version might retruns incompatible values
Browse files Browse the repository at this point in the history
On my PC, I have both MSVC 15 and 16 and found out that find_vs_version returns MSVC 16 while find_tool return MSVC 15 linker
  • Loading branch information
Speedy37 committed Apr 21, 2020
1 parent 383f2df commit ccfa0a0
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/windows_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn find_tool(target: &str, tool: &str) -> Option<Tool> {
// environment variables like `LIB`, `INCLUDE`, and `PATH` to ensure that
// the tool is actually usable.

return impl_::find_msvc_15(tool, target)
return impl_::find_msvc_15plus(tool, target)
.or_else(|| impl_::find_msvc_14(tool, target))
.or_else(|| impl_::find_msvc_12(tool, target))
.or_else(|| impl_::find_msvc_11(tool, target));
Expand Down Expand Up @@ -210,7 +210,7 @@ mod impl_ {

#[allow(bare_trait_objects)]
fn vs16_instances() -> Box<Iterator<Item = PathBuf>> {
let instances = if let Some(instances) = vs15_instances() {
let instances = if let Some(instances) = vs15plus_instances() {
instances
} else {
return Box::new(iter::empty());
Expand Down Expand Up @@ -253,24 +253,34 @@ mod impl_ {
// Note that much of this logic can be found [online] wrt paths, COM, etc.
//
// [online]: https://blogs.msdn.microsoft.com/vcblog/2017/03/06/finding-the-visual-c-compiler-tools-in-visual-studio-2017/
fn vs15_instances() -> Option<EnumSetupInstances> {
//
// Returns MSVC 15+ instances (15, 16 right now), the order should be consider undefined.
fn vs15plus_instances() -> Option<EnumSetupInstances> {
com::initialize().ok()?;

let config = SetupConfiguration::new().ok()?;
config.enum_all_instances().ok()
}

pub fn find_msvc_15(tool: &str, target: &str) -> Option<Tool> {
let iter = vs15_instances()?;
pub fn find_msvc_15plus(tool: &str, target: &str) -> Option<Tool> {
let iter = vs15plus_instances()?;
let mut best = None;
for instance in iter {
let instance = instance.ok()?;
let installation_name = instance.installation_name().ok()?;
let tool = tool_from_vs15_instance(tool, target, &instance);
if tool.is_some() {
return tool;
}
best = match (tool, best) {
(Some(tool), None) => Some((installation_name, tool)),
(Some(tool), Some((best_installation_name, _)))
if best_installation_name < installation_name =>
{
Some((installation_name, tool))
}
_ => None,
};
}

None
best.map(|(_installation_name, tool)| tool)
}

// While the paths to Visual Studio 2017's devenv and MSBuild could
Expand All @@ -281,7 +291,7 @@ mod impl_ {
//
// [more reliable]: https://github.com/alexcrichton/cc-rs/pull/331
fn find_tool_in_vs15_path(tool: &str, target: &str) -> Option<Tool> {
let mut path = match vs15_instances() {
let mut path = match vs15plus_instances() {
Some(instances) => instances
.filter_map(|instance| {
instance
Expand Down

0 comments on commit ccfa0a0

Please sign in to comment.