From 8f33de92ba4fc152b0e2b63008732ce0ad4ba18c Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 11 Oct 2016 11:25:28 -0400 Subject: cmake: Factor out default generator selection into helper --- Source/cmake.cxx | 100 +++++++++++++++++++++++++++++-------------------------- Source/cmake.h | 2 ++ 2 files changed, 54 insertions(+), 48 deletions(-) diff --git a/Source/cmake.cxx b/Source/cmake.cxx index e0f4000..3bf7f38 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -1313,54 +1313,7 @@ int cmake::ActualConfigure() cmSystemTools::SetForceUnixPaths( this->GlobalGenerator->GetForceUnixPaths()); } else { -#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW) - std::string installedCompiler; - // Try to find the newest VS installed on the computer and - // use that as a default if -G is not specified - const std::string vsregBase = - "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"; - std::vector vsVerions; - vsVerions.push_back("VisualStudio\\"); - vsVerions.push_back("VCExpress\\"); - vsVerions.push_back("WDExpress\\"); - struct VSRegistryEntryName - { - const char* MSVersion; - const char* GeneratorName; - }; - VSRegistryEntryName version[] = { - /* clang-format needs this comment to break after the opening brace */ - { "7.1", "Visual Studio 7 .NET 2003" }, - { "8.0", "Visual Studio 8 2005" }, - { "9.0", "Visual Studio 9 2008" }, - { "10.0", "Visual Studio 10 2010" }, - { "11.0", "Visual Studio 11 2012" }, - { "12.0", "Visual Studio 12 2013" }, - { "14.0", "Visual Studio 14 2015" }, - { "15.0", "Visual Studio 15" }, - { 0, 0 } - }; - for (int i = 0; version[i].MSVersion != 0; i++) { - for (size_t b = 0; b < vsVerions.size(); b++) { - std::string reg = vsregBase + vsVerions[b] + version[i].MSVersion; - reg += ";InstallDir]"; - cmSystemTools::ExpandRegistryValues(reg, cmSystemTools::KeyWOW64_32); - if (!(reg == "/registry")) { - installedCompiler = version[i].GeneratorName; - break; - } - } - } - cmGlobalGenerator* gen = - this->CreateGlobalGenerator(installedCompiler.c_str()); - if (!gen) { - gen = new cmGlobalNMakeMakefileGenerator(this); - } - this->SetGlobalGenerator(gen); - std::cout << "-- Building for: " << gen->GetName() << "\n"; -#else - this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3(this)); -#endif + this->CreateDefaultGlobalGenerator(); } if (!this->GlobalGenerator) { cmSystemTools::Error("Could not create generator"); @@ -1488,6 +1441,57 @@ int cmake::ActualConfigure() return 0; } +void cmake::CreateDefaultGlobalGenerator() +{ +#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW) + std::string installedCompiler; + // Try to find the newest VS installed on the computer and + // use that as a default if -G is not specified + const std::string vsregBase = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"; + std::vector vsVerions; + vsVerions.push_back("VisualStudio\\"); + vsVerions.push_back("VCExpress\\"); + vsVerions.push_back("WDExpress\\"); + struct VSRegistryEntryName + { + const char* MSVersion; + const char* GeneratorName; + }; + VSRegistryEntryName version[] = { + /* clang-format needs this comment to break after the opening brace */ + { "7.1", "Visual Studio 7 .NET 2003" }, + { "8.0", "Visual Studio 8 2005" }, + { "9.0", "Visual Studio 9 2008" }, + { "10.0", "Visual Studio 10 2010" }, + { "11.0", "Visual Studio 11 2012" }, + { "12.0", "Visual Studio 12 2013" }, + { "14.0", "Visual Studio 14 2015" }, + { "15.0", "Visual Studio 15" }, + { 0, 0 } + }; + for (int i = 0; version[i].MSVersion != 0; i++) { + for (size_t b = 0; b < vsVerions.size(); b++) { + std::string reg = vsregBase + vsVerions[b] + version[i].MSVersion; + reg += ";InstallDir]"; + cmSystemTools::ExpandRegistryValues(reg, cmSystemTools::KeyWOW64_32); + if (!(reg == "/registry")) { + installedCompiler = version[i].GeneratorName; + break; + } + } + } + cmGlobalGenerator* gen = + this->CreateGlobalGenerator(installedCompiler.c_str()); + if (!gen) { + gen = new cmGlobalNMakeMakefileGenerator(this); + } + this->SetGlobalGenerator(gen); + std::cout << "-- Building for: " << gen->GetName() << "\n"; +#else + this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3(this)); +#endif +} + void cmake::PreLoadCMakeFiles() { std::vector args; diff --git a/Source/cmake.h b/Source/cmake.h index ae1a502..865748b 100644 --- a/Source/cmake.h +++ b/Source/cmake.h @@ -500,6 +500,8 @@ private: // Print a list of valid generators to stderr. void PrintGeneratorList(); + void CreateDefaultGlobalGenerator(); + /** * Convert a message type between a warning and an error, based on the state * of the error output CMake variables, in the cache. -- cgit v0.12 From 0838d20e506ed6bf064b51f7ef2d026c76b4af3d Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 11 Oct 2016 11:35:49 -0400 Subject: cmake: Simplify VS registry entry lookup --- Source/cmake.cxx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 3bf7f38..be08fc7 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -1447,7 +1447,7 @@ void cmake::CreateDefaultGlobalGenerator() std::string installedCompiler; // Try to find the newest VS installed on the computer and // use that as a default if -G is not specified - const std::string vsregBase = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"; + const std::string vsregBase = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"; std::vector vsVerions; vsVerions.push_back("VisualStudio\\"); vsVerions.push_back("VCExpress\\"); @@ -1472,9 +1472,11 @@ void cmake::CreateDefaultGlobalGenerator() for (int i = 0; version[i].MSVersion != 0; i++) { for (size_t b = 0; b < vsVerions.size(); b++) { std::string reg = vsregBase + vsVerions[b] + version[i].MSVersion; - reg += ";InstallDir]"; - cmSystemTools::ExpandRegistryValues(reg, cmSystemTools::KeyWOW64_32); - if (!(reg == "/registry")) { + reg += ";InstallDir"; + std::string dir; + if (cmSystemTools::ReadRegistryValue(reg, dir, + cmSystemTools::KeyWOW64_32) && + cmSystemTools::PathExists(dir)) { installedCompiler = version[i].GeneratorName; break; } -- cgit v0.12 From 10160f691c5fa6849a02383d727b30da648aa878 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 11 Oct 2016 11:50:56 -0400 Subject: cmake: Rename default generator selection internal variable --- Source/cmake.cxx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Source/cmake.cxx b/Source/cmake.cxx index be08fc7..ed4d453 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -1444,7 +1444,7 @@ int cmake::ActualConfigure() void cmake::CreateDefaultGlobalGenerator() { #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW) - std::string installedCompiler; + std::string found; // Try to find the newest VS installed on the computer and // use that as a default if -G is not specified const std::string vsregBase = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"; @@ -1477,13 +1477,12 @@ void cmake::CreateDefaultGlobalGenerator() if (cmSystemTools::ReadRegistryValue(reg, dir, cmSystemTools::KeyWOW64_32) && cmSystemTools::PathExists(dir)) { - installedCompiler = version[i].GeneratorName; + found = version[i].GeneratorName; break; } } } - cmGlobalGenerator* gen = - this->CreateGlobalGenerator(installedCompiler.c_str()); + cmGlobalGenerator* gen = this->CreateGlobalGenerator(found); if (!gen) { gen = new cmGlobalNMakeMakefileGenerator(this); } -- cgit v0.12 From d3df00ae5330de935eafd0f315017ae0f89a6968 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 11 Oct 2016 13:06:58 -0400 Subject: cmake: Rename default generator selection list variable --- Source/cmake.cxx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/cmake.cxx b/Source/cmake.cxx index ed4d453..d74b7c1 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -1452,12 +1452,12 @@ void cmake::CreateDefaultGlobalGenerator() vsVerions.push_back("VisualStudio\\"); vsVerions.push_back("VCExpress\\"); vsVerions.push_back("WDExpress\\"); - struct VSRegistryEntryName + struct VSVersionedGenerator { const char* MSVersion; const char* GeneratorName; }; - VSRegistryEntryName version[] = { + static VSVersionedGenerator const vsGenerators[] = { /* clang-format needs this comment to break after the opening brace */ { "7.1", "Visual Studio 7 .NET 2003" }, { "8.0", "Visual Studio 8 2005" }, @@ -1469,15 +1469,15 @@ void cmake::CreateDefaultGlobalGenerator() { "15.0", "Visual Studio 15" }, { 0, 0 } }; - for (int i = 0; version[i].MSVersion != 0; i++) { + for (int i = 0; vsGenerators[i].MSVersion != 0; i++) { for (size_t b = 0; b < vsVerions.size(); b++) { - std::string reg = vsregBase + vsVerions[b] + version[i].MSVersion; + std::string reg = vsregBase + vsVerions[b] + vsGenerators[i].MSVersion; reg += ";InstallDir"; std::string dir; if (cmSystemTools::ReadRegistryValue(reg, dir, cmSystemTools::KeyWOW64_32) && cmSystemTools::PathExists(dir)) { - found = version[i].GeneratorName; + found = vsGenerators[i].GeneratorName; break; } } -- cgit v0.12 From 6db64ad43202af66270d996775e1504ba676b63d Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 11 Oct 2016 13:09:38 -0400 Subject: cmake: Use array iteration for default generator list --- Source/cmake.cxx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/cmake.cxx b/Source/cmake.cxx index d74b7c1..6ffa308 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -1466,18 +1466,18 @@ void cmake::CreateDefaultGlobalGenerator() { "11.0", "Visual Studio 11 2012" }, { "12.0", "Visual Studio 12 2013" }, { "14.0", "Visual Studio 14 2015" }, - { "15.0", "Visual Studio 15" }, - { 0, 0 } + { "15.0", "Visual Studio 15" } }; - for (int i = 0; vsGenerators[i].MSVersion != 0; i++) { + for (VSVersionedGenerator const* g = cmArrayBegin(vsGenerators); + g != cmArrayEnd(vsGenerators); ++g) { for (size_t b = 0; b < vsVerions.size(); b++) { - std::string reg = vsregBase + vsVerions[b] + vsGenerators[i].MSVersion; + std::string reg = vsregBase + vsVerions[b] + g->MSVersion; reg += ";InstallDir"; std::string dir; if (cmSystemTools::ReadRegistryValue(reg, dir, cmSystemTools::KeyWOW64_32) && cmSystemTools::PathExists(dir)) { - found = vsGenerators[i].GeneratorName; + found = g->GeneratorName; break; } } -- cgit v0.12 From 24c86e1c3ff5b507815e081f2173bc5b77358e78 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 11 Oct 2016 13:14:26 -0400 Subject: cmake: Use array iteration for default generator VS variants --- Source/cmake.cxx | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 6ffa308..96a7b9a 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -1448,10 +1448,10 @@ void cmake::CreateDefaultGlobalGenerator() // Try to find the newest VS installed on the computer and // use that as a default if -G is not specified const std::string vsregBase = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\"; - std::vector vsVerions; - vsVerions.push_back("VisualStudio\\"); - vsVerions.push_back("VCExpress\\"); - vsVerions.push_back("WDExpress\\"); + static const char* const vsVariants[] = { + /* clang-format needs this comment to break after the opening brace */ + "VisualStudio\\", "VCExpress\\", "WDExpress\\" + }; struct VSVersionedGenerator { const char* MSVersion; @@ -1470,8 +1470,9 @@ void cmake::CreateDefaultGlobalGenerator() }; for (VSVersionedGenerator const* g = cmArrayBegin(vsGenerators); g != cmArrayEnd(vsGenerators); ++g) { - for (size_t b = 0; b < vsVerions.size(); b++) { - std::string reg = vsregBase + vsVerions[b] + g->MSVersion; + for (const char* const* v = cmArrayBegin(vsVariants); + v != cmArrayEnd(vsVariants); ++v) { + std::string reg = vsregBase + *v + g->MSVersion; reg += ";InstallDir"; std::string dir; if (cmSystemTools::ReadRegistryValue(reg, dir, -- cgit v0.12 From 1aee54ed87f94ceb91fb6c9693160fa41313b7ca Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 11 Oct 2016 13:29:37 -0400 Subject: cmake: Simplify default generator search by switching direction Search from newest to oldest and stop once found. --- Source/cmake.cxx | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 96a7b9a..78f8c48 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -1458,20 +1458,19 @@ void cmake::CreateDefaultGlobalGenerator() const char* GeneratorName; }; static VSVersionedGenerator const vsGenerators[] = { - /* clang-format needs this comment to break after the opening brace */ - { "7.1", "Visual Studio 7 .NET 2003" }, - { "8.0", "Visual Studio 8 2005" }, - { "9.0", "Visual Studio 9 2008" }, - { "10.0", "Visual Studio 10 2010" }, - { "11.0", "Visual Studio 11 2012" }, - { "12.0", "Visual Studio 12 2013" }, - { "14.0", "Visual Studio 14 2015" }, - { "15.0", "Visual Studio 15" } + { "15.0", "Visual Studio 15" }, // + { "14.0", "Visual Studio 14 2015" }, // + { "12.0", "Visual Studio 12 2013" }, // + { "11.0", "Visual Studio 11 2012" }, // + { "10.0", "Visual Studio 10 2010" }, // + { "9.0", "Visual Studio 9 2008" }, // + { "8.0", "Visual Studio 8 2005" }, // + { "7.1", "Visual Studio 7 .NET 2003" } }; for (VSVersionedGenerator const* g = cmArrayBegin(vsGenerators); - g != cmArrayEnd(vsGenerators); ++g) { + found.empty() && g != cmArrayEnd(vsGenerators); ++g) { for (const char* const* v = cmArrayBegin(vsVariants); - v != cmArrayEnd(vsVariants); ++v) { + found.empty() && v != cmArrayEnd(vsVariants); ++v) { std::string reg = vsregBase + *v + g->MSVersion; reg += ";InstallDir"; std::string dir; @@ -1479,7 +1478,6 @@ void cmake::CreateDefaultGlobalGenerator() cmSystemTools::KeyWOW64_32) && cmSystemTools::PathExists(dir)) { found = g->GeneratorName; - break; } } } -- cgit v0.12 From 5380948faec94270f9b799fba6f8b0e7c9ad0899 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 11 Oct 2016 13:36:41 -0400 Subject: cmake: Fix default VS generator selection for Microsoft Build Tools Some versions of the VS tools do not install the registry entry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\$v;InstallDir but all appear to set HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\$v\Setup\VC;ProductDir Update our search to consider both entries. Closes: #16360 --- Source/cmake.cxx | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 78f8c48..b4ab42e 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -1467,17 +1467,23 @@ void cmake::CreateDefaultGlobalGenerator() { "8.0", "Visual Studio 8 2005" }, // { "7.1", "Visual Studio 7 .NET 2003" } }; + static const char* const vsEntries[] = { + "\\Setup\\VC;ProductDir", // + ";InstallDir" // + }; for (VSVersionedGenerator const* g = cmArrayBegin(vsGenerators); found.empty() && g != cmArrayEnd(vsGenerators); ++g) { for (const char* const* v = cmArrayBegin(vsVariants); found.empty() && v != cmArrayEnd(vsVariants); ++v) { - std::string reg = vsregBase + *v + g->MSVersion; - reg += ";InstallDir"; - std::string dir; - if (cmSystemTools::ReadRegistryValue(reg, dir, - cmSystemTools::KeyWOW64_32) && - cmSystemTools::PathExists(dir)) { - found = g->GeneratorName; + for (const char* const* e = cmArrayBegin(vsEntries); + found.empty() && e != cmArrayEnd(vsEntries); ++e) { + std::string const reg = vsregBase + *v + g->MSVersion + *e; + std::string dir; + if (cmSystemTools::ReadRegistryValue(reg, dir, + cmSystemTools::KeyWOW64_32) && + cmSystemTools::PathExists(dir)) { + found = g->GeneratorName; + } } } } -- cgit v0.12