[visualstudio] Fix v120 toolset detection issue.
* [visualstudio] Fix v120 toolset detection when VS >= 2017 is not
installed.
* apply formatting change
* Always append VS2017 if vs160comntools is set.
* Apply formatting changes.
MODIFY
src/vcpkg/visualstudio.cpp
src/vcpkg/visualstudio.cpp
@@ -126,45 +126,44 @@ namespace vcpkg::VisualStudio
}
}
- // VS2019 instance from environment variable
- auto maybe_vs160_comntools = System::get_environment_variable("vs160comntools");
- if (const auto path_as_string = maybe_vs160_comntools.get())
+ const auto maybe_append_path = [&](fs::path&& path_root, CStringView version, bool check_cl = true) {
+ if (check_cl)
{
- // We want lexically_normal(), but it is not available
- // Correct root path might be 2 or 3 levels up, depending on if the path has trailing backslash.
- auto common7_tools = fs::u8path(*path_as_string);
- if (common7_tools.filename().empty())
- instances.emplace_back(common7_tools.parent_path().parent_path().parent_path(),
- "16.0",
- VisualStudioInstance::ReleaseType::LEGACY);
- else
- instances.emplace_back(
- common7_tools.parent_path().parent_path(), "16.0", VisualStudioInstance::ReleaseType::LEGACY);
- }
-
- const auto append_if_has_cl_vs140 = [&](fs::path&& path_root) {
const auto cl_exe = path_root / "VC" / "bin" / "cl.exe";
const auto vcvarsall_bat = path_root / "VC" / "vcvarsall.bat";
- if (fs.exists(cl_exe) && fs.exists(vcvarsall_bat))
- instances.emplace_back(std::move(path_root), "14.0", VisualStudioInstance::ReleaseType::LEGACY);
+ if (!(fs.exists(cl_exe) && fs.exists(vcvarsall_bat))) return;
+ }
+
+ instances.emplace_back(std::move(path_root), version.c_str(), VisualStudioInstance::ReleaseType::LEGACY);
};
- // VS2015 instance from environment variable
- auto maybe_vs140_comntools = System::get_environment_variable("vs140comntools");
- if (const auto path_as_string = maybe_vs140_comntools.get())
+ const auto maybe_append_comntools = [&](ZStringView env_var, CStringView version, bool check_cl = true) {
+ auto maybe_comntools = System::get_environment_variable(env_var);
+ if (const auto path_as_string = maybe_comntools.get())
{
// We want lexically_normal(), but it is not available
// Correct root path might be 2 or 3 levels up, depending on if the path has trailing backslash.
auto common7_tools = fs::u8path(*path_as_string);
if (common7_tools.filename().empty())
- append_if_has_cl_vs140(common7_tools.parent_path().parent_path().parent_path());
+ maybe_append_path(common7_tools.parent_path().parent_path().parent_path(), version, check_cl);
else
- append_if_has_cl_vs140(common7_tools.parent_path().parent_path());
+ maybe_append_path(common7_tools.parent_path().parent_path(), version, check_cl);
}
+ };
+
+ const auto maybe_append_legacy_vs = [&](ZStringView env_var, const fs::path& dir, CStringView version) {
+ // VS instance from environment variable
+ maybe_append_comntools(env_var, version);
+ // VS instance from Program Files
+ maybe_append_path(program_files_32_bit / dir, version);
+ };
- // VS2015 instance from Program Files
- append_if_has_cl_vs140(program_files_32_bit / "Microsoft Visual Studio 14.0");
+ // VS 2017 changed the installer such that cl.exe cannot be found by path navigation and
+ // the env variable is only set when vcvars has been run. Therefore we close the safety valves.
+ maybe_append_comntools("vs160comntools", "16.0", false);
+ maybe_append_legacy_vs("vs140comntools", "Microsoft Visual Studio 14.0", "14.0");
+ maybe_append_legacy_vs("vs120comntools", "Microsoft Visual Studio 12.0", "12.0");
return instances;
}

[visualstudio] Fix v120 toolset detection issue. (#4)

* [visualstudio] Fix v120 toolset detection when VS >= 2017 is not
installed.

* apply formatting change

* Always append VS2017 if vs160comntools is set.

* Apply formatting changes.

Co-authored-by: Robert Schumacher <roschuma@microsoft.com>