1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-24 03:02:46 +08:00

Refactor: Deduplicate code for VS_nn_DIR keys processing

This commit is contained in:
Alex Turbov
2021-07-29 17:23:51 +03:00
parent f9947ec9e3
commit 346f3de005

View File

@@ -17,6 +17,7 @@
# include "cmAlgorithms.h" # include "cmAlgorithms.h"
# include "cmGlobalGenerator.h" # include "cmGlobalGenerator.h"
# include "cmGlobalVisualStudioVersionedGenerator.h" # include "cmGlobalVisualStudioVersionedGenerator.h"
# include "cmStringAlgorithms.h"
# include "cmSystemTools.h" # include "cmSystemTools.h"
# include "cmVSSetupHelper.h" # include "cmVSSetupHelper.h"
# define HAVE_VS_SETUP_HELPER # define HAVE_VS_SETUP_HELPER
@@ -141,60 +142,29 @@ cm::optional<std::string> GetValue(cmsys::SystemInformation& info,
cm::optional<std::string> GetValue(cmExecutionStatus& status, cm::optional<std::string> GetValue(cmExecutionStatus& status,
std::string const& key) std::string const& key)
{ {
auto* const gg = status.GetMakefile().GetGlobalGenerator();
for (auto vs : { 15, 16, 17 }) {
if (key == cmStrCat("VS_"_s, vs, "_DIR"_s)) {
std::string value; std::string value;
if (key == "VS_15_DIR") { // If generating for the VS nn IDE, use the same instance.
// If generating for the VS 15 IDE, use the same instance.
cmGlobalGenerator* gg = status.GetMakefile().GetGlobalGenerator(); if (cmHasPrefix(gg->GetName(), cmStrCat("Visual Studio "_s, vs, ' '))) {
if (cmHasLiteralPrefix(gg->GetName(), "Visual Studio 15 ")) { cmGlobalVisualStudioVersionedGenerator* vsNNgen =
cmGlobalVisualStudioVersionedGenerator* vs15gen =
static_cast<cmGlobalVisualStudioVersionedGenerator*>(gg); static_cast<cmGlobalVisualStudioVersionedGenerator*>(gg);
if (vs15gen->GetVSInstance(value)) { if (vsNNgen->GetVSInstance(value)) {
return value; return value;
} }
} }
// Otherwise, find a VS 15 instance ourselves. // Otherwise, find a VS nn instance ourselves.
cmVSSetupAPIHelper vsSetupAPIHelper(15); cmVSSetupAPIHelper vsSetupAPIHelper(vs);
if (vsSetupAPIHelper.GetVSInstanceInfo(value)) { if (vsSetupAPIHelper.GetVSInstanceInfo(value)) {
cmSystemTools::ConvertToUnixSlashes(value); cmSystemTools::ConvertToUnixSlashes(value);
} }
return value; return value;
} else if (key == "VS_16_DIR") {
// If generating for the VS 16 IDE, use the same instance.
cmGlobalGenerator* gg = status.GetMakefile().GetGlobalGenerator();
if (cmHasLiteralPrefix(gg->GetName(), "Visual Studio 16 ")) {
cmGlobalVisualStudioVersionedGenerator* vs16gen =
static_cast<cmGlobalVisualStudioVersionedGenerator*>(gg);
if (vs16gen->GetVSInstance(value)) {
return value;
} }
} }
// Otherwise, find a VS 16 instance ourselves.
cmVSSetupAPIHelper vsSetupAPIHelper(16);
if (vsSetupAPIHelper.GetVSInstanceInfo(value)) {
cmSystemTools::ConvertToUnixSlashes(value);
}
return value;
} else if (key == "VS_17_DIR") {
// If generating for the VS 17 IDE, use the same instance.
cmGlobalGenerator* gg = status.GetMakefile().GetGlobalGenerator();
if (cmHasLiteralPrefix(gg->GetName(), "Visual Studio 17 ")) {
cmGlobalVisualStudioVersionedGenerator* vs17gen =
static_cast<cmGlobalVisualStudioVersionedGenerator*>(gg);
if (vs17gen->GetVSInstance(value)) {
return value;
}
}
// Otherwise, find a VS 17 instance ourselves.
cmVSSetupAPIHelper vsSetupAPIHelper(17);
if (vsSetupAPIHelper.GetVSInstanceInfo(value)) {
cmSystemTools::ConvertToUnixSlashes(value);
}
return value;
}
return {}; return {};
} }
#endif #endif