cmake: Find the default generator only when printing the generator list

`cmake --version` and the other documentation options build the
generator documentation even when they do not print it.  That evaluates
the default generator, which on Windows queries the Visual Studio Setup
COM API for VS 18, 17, 16 and 15 in turn and constructs the newest
Visual Studio generator found.  It makes `cmake --version` take about
100 ms on Windows, which is a lot for a command that tools run often
to detect CMake.

Only `--help` and the generator listing print generators.  Evaluate them
only for those requests.

Fixes: #28220
This commit is contained in:
Alexandre Zollinger Chohfi committed 2026-10-28 12:02:31 -07:00
1 parent cfb3000e9e
commit da59d7769e
5 files changed
+22 -1

No files matched your search

+10
View File
@@ -207,6 +207,16 @@ bool cmDocumentation::PrintDocumentation(Type ht, std::ostream& os)
}
}
bool cmDocumentation::RequestsGenerators() const
{
return std::any_of(this->RequestedHelpItems.begin(),
this->RequestedHelpItems.end(),
[this](RequestedHelpItem const& rhi) {
return rhi.HelpType == ListGenerators ||
(rhi.HelpType == Help && this->ShowGenerators);
});
}
bool cmDocumentation::PrintRequestedDocumentation(std::ostream& os)
{
int count = 0;
+3
View File
@@ -73,6 +73,9 @@ public:
void SetShowGenerators(bool showGen) { this->ShowGenerators = showGen; }
/** Whether the requested documentation prints the generator list. */
bool RequestsGenerators() const;
/** Set the program name for standard document generation. */
void SetName(std::string const& name);
+6 -1
View File
@@ -237,7 +237,12 @@ int do_cmake(int ac, char const* const* av)
std::vector<std::string> args(av, av + ac);
hcm.SetCacheArgs(args);
auto generators = hcm.GetGeneratorsDocumentation();
// Finding the default generator can query Visual Studio instances,
// so do it only when the generator list will be printed.
std::vector<cmDocumentationEntry> generators;
if (doc.RequestsGenerators()) {
generators = hcm.GetGeneratorsDocumentation();
}
doc.SetName("cmake");
doc.SetSection("Name", cmDocumentationName);
@@ -0,0 +1 @@
^cmake version [0-9]
@@ -463,6 +463,8 @@ function(run_EnvironmentGenerator)
run_cmake_command(Envgen-ninja-multi-help ${CMAKE_COMMAND} --help)
set(ENV{CMAKE_GENERATOR} "NoSuchGenerator")
run_cmake_command(Envgen-bad-help ${CMAKE_COMMAND} --help)
# --version does not print generators, so it does not evaluate them
run_cmake_command(Envgen-bad-version ${CMAKE_COMMAND} --version)
unset(ENV{CMAKE_GENERATOR})
if(RunCMake_GENERATOR MATCHES "Visual Studio")