From 3c3bce133dda14a60d15eaa03edff527a53c01d4 Mon Sep 17 00:00:00 2001
From: Juan Ramos <cmake@jpr.mozmail.com>
Date: Thu, 20 Oct 2022 19:53:14 -0600
Subject: cmake: Honor CMAKE_GENERATOR env var in --help output

---
 Source/CursesDialog/ccmake.cxx                                | 2 +-
 Source/QtDialog/CMakeSetup.cxx                                | 2 +-
 Source/cmState.cxx                                            | 2 ++
 Source/cmState.h                                              | 1 +
 Source/cmake.cxx                                              | 2 +-
 Source/cmakemain.cxx                                          | 2 +-
 Tests/RunCMake/CommandLine/Envgen-bad-help-stderr.txt         | 1 +
 Tests/RunCMake/CommandLine/Envgen-bad-help-stdout.txt         | 2 ++
 Tests/RunCMake/CommandLine/Envgen-ninja-multi-help-stdout.txt | 1 +
 Tests/RunCMake/CommandLine/RunCMakeTest.cmake                 | 7 +++++++
 10 files changed, 18 insertions(+), 4 deletions(-)
 create mode 100644 Tests/RunCMake/CommandLine/Envgen-bad-help-stderr.txt
 create mode 100644 Tests/RunCMake/CommandLine/Envgen-bad-help-stdout.txt
 create mode 100644 Tests/RunCMake/CommandLine/Envgen-ninja-multi-help-stdout.txt

diff --git a/Source/CursesDialog/ccmake.cxx b/Source/CursesDialog/ccmake.cxx
index 1f7776c..70ed648 100644
--- a/Source/CursesDialog/ccmake.cxx
+++ b/Source/CursesDialog/ccmake.cxx
@@ -77,7 +77,7 @@ int main(int argc, char const* const* argv)
   cmDocumentation doc;
   doc.addCMakeStandardDocSections();
   if (doc.CheckOptions(argc, argv)) {
-    cmake hcm(cmake::RoleInternal, cmState::Unknown);
+    cmake hcm(cmake::RoleInternal, cmState::Help);
     hcm.SetHomeDirectory("");
     hcm.SetHomeOutputDirectory("");
     hcm.AddCMakePaths();
diff --git a/Source/QtDialog/CMakeSetup.cxx b/Source/QtDialog/CMakeSetup.cxx
index fb12b7d..591b793 100644
--- a/Source/QtDialog/CMakeSetup.cxx
+++ b/Source/QtDialog/CMakeSetup.cxx
@@ -79,7 +79,7 @@ int main(int argc, char** argv)
   doc.addCMakeStandardDocSections();
   if (argc2 > 1 && doc.CheckOptions(argc2, argv2)) {
     // Construct and print requested documentation.
-    cmake hcm(cmake::RoleInternal, cmState::Unknown);
+    cmake hcm(cmake::RoleInternal, cmState::Help);
     hcm.SetHomeDirectory("");
     hcm.SetHomeOutputDirectory("");
     hcm.AddCMakePaths();
diff --git a/Source/cmState.cxx b/Source/cmState.cxx
index 3d38e73..17ee7f3 100644
--- a/Source/cmState.cxx
+++ b/Source/cmState.cxx
@@ -786,6 +786,8 @@ std::string cmState::ModeToString(cmState::Mode mode)
       return "CTEST";
     case CPack:
       return "CPACK";
+    case Help:
+      return "HELP";
     case Unknown:
       return "UNKNOWN";
   }
diff --git a/Source/cmState.h b/Source/cmState.h
index 2d0c521..9a17b22 100644
--- a/Source/cmState.h
+++ b/Source/cmState.h
@@ -53,6 +53,7 @@ public:
     FindPackage,
     CTest,
     CPack,
+    Help
   };
 
   enum class ProjectKind
diff --git a/Source/cmake.cxx b/Source/cmake.cxx
index c4c638c..8cdecc6 100644
--- a/Source/cmake.cxx
+++ b/Source/cmake.cxx
@@ -196,7 +196,7 @@ cmake::cmake(Role role, cmState::Mode mode, cmState::ProjectKind projectKind)
     this->AddProjectCommands();
   }
 
-  if (mode == cmState::Project) {
+  if (mode == cmState::Project || mode == cmState::Help) {
     this->LoadEnvironmentPresets();
   }
 
diff --git a/Source/cmakemain.cxx b/Source/cmakemain.cxx
index b754b72..928c1ba 100644
--- a/Source/cmakemain.cxx
+++ b/Source/cmakemain.cxx
@@ -208,7 +208,7 @@ int do_cmake(int ac, char const* const* av)
   doc.addCMakeStandardDocSections();
   if (doc.CheckOptions(ac, av, "--")) {
     // Construct and print requested documentation.
-    cmake hcm(cmake::RoleInternal, cmState::Unknown);
+    cmake hcm(cmake::RoleInternal, cmState::Help);
     hcm.SetHomeDirectory("");
     hcm.SetHomeOutputDirectory("");
     hcm.AddCMakePaths();
diff --git a/Tests/RunCMake/CommandLine/Envgen-bad-help-stderr.txt b/Tests/RunCMake/CommandLine/Envgen-bad-help-stderr.txt
new file mode 100644
index 0000000..cdfe857
--- /dev/null
+++ b/Tests/RunCMake/CommandLine/Envgen-bad-help-stderr.txt
@@ -0,0 +1 @@
+CMake Error: CMAKE_GENERATOR was set but the specified generator doesn't exist. Using CMake default.
diff --git a/Tests/RunCMake/CommandLine/Envgen-bad-help-stdout.txt b/Tests/RunCMake/CommandLine/Envgen-bad-help-stdout.txt
new file mode 100644
index 0000000..075c48c
--- /dev/null
+++ b/Tests/RunCMake/CommandLine/Envgen-bad-help-stdout.txt
@@ -0,0 +1,2 @@
+Generators.*
+\* (Unix Makefiles|Visual Studio).*
diff --git a/Tests/RunCMake/CommandLine/Envgen-ninja-multi-help-stdout.txt b/Tests/RunCMake/CommandLine/Envgen-ninja-multi-help-stdout.txt
new file mode 100644
index 0000000..ece6e5d
--- /dev/null
+++ b/Tests/RunCMake/CommandLine/Envgen-ninja-multi-help-stdout.txt
@@ -0,0 +1 @@
+\* Ninja Multi-Config[ ]+= Generates build-<Config>.ninja files.
diff --git a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake
index 50b1f90..327b772 100644
--- a/Tests/RunCMake/CommandLine/RunCMakeTest.cmake
+++ b/Tests/RunCMake/CommandLine/RunCMakeTest.cmake
@@ -349,6 +349,13 @@ function(run_EnvironmentGenerator)
   run_cmake_command(Envgen-bad ${CMAKE_COMMAND} -G)
   unset(ENV{CMAKE_GENERATOR})
 
+  # Honor CMAKE_GENERATOR env var in --help output
+  set(ENV{CMAKE_GENERATOR} "Ninja Multi-Config")
+  run_cmake_command(Envgen-ninja-multi-help ${CMAKE_COMMAND} --help)
+  set(ENV{CMAKE_GENERATOR} "NoSuchGenerator")
+  run_cmake_command(Envgen-bad-help ${CMAKE_COMMAND} --help)
+  unset(ENV{CMAKE_GENERATOR})
+
   if(RunCMake_GENERATOR MATCHES "Visual Studio.*")
     set(ENV{CMAKE_GENERATOR} "${RunCMake_GENERATOR}")
     run_cmake_command(Envgen ${CMAKE_COMMAND} ${source_dir})
-- 
cgit v0.12