Browse Source

cmake: Replace working mode with role

Brad King 1 month ago
parent
commit
3593aa59ef

+ 0 - 1
Source/CTest/cmCTestScriptHandler.cxx

@@ -175,7 +175,6 @@ void cmCTestScriptHandler::CreateCMake()
   this->CMake = cm::make_unique<cmake>(cmState::Role::CTest);
   this->CMake->GetCurrentSnapshot().SetDefaultDefinitions();
   this->CMake->AddCMakePaths();
-  this->CMake->SetWorkingMode(cmake::SCRIPT_MODE);
   this->GlobalGenerator =
     cm::make_unique<cmGlobalGenerator>(this->CMake.get());
 

+ 1 - 3
Source/cmCMakeLanguageCommand.cxx

@@ -397,9 +397,7 @@ bool cmCMakeLanguageCommand(std::vector<cmListFileArgument> const& args,
       return FatalError(status, "EXIT requires one argument");
     }
 
-    auto workingMode =
-      status.GetMakefile().GetCMakeInstance()->GetWorkingMode();
-    if (workingMode != cmake::SCRIPT_MODE) {
+    if (!status.GetMakefile().GetCMakeInstance()->RoleSupportsExitCode()) {
       return FatalError(status, "EXIT can be used only in SCRIPT mode");
     }
 

+ 1 - 2
Source/cmFileCommand.cxx

@@ -686,7 +686,6 @@ bool HandleGlobImpl(std::vector<std::string> const& args, bool recurse,
   std::vector<std::string> files;
   bool configureDepends = false;
   bool warnConfigureLate = false;
-  cmake::WorkingMode const workingMode = cm->GetWorkingMode();
   while (i != args.end()) {
     if (*i == "LIST_DIRECTORIES") {
       ++i; // skip LIST_DIRECTORIES
@@ -737,7 +736,7 @@ bool HandleGlobImpl(std::vector<std::string> const& args, bool recurse,
           "CONFIGURE_DEPENDS flag was given after a glob expression was "
           "already evaluated.");
       }
-      if (workingMode != cmake::NORMAL_MODE) {
+      if (cm->GetState()->GetRole() != cmState::Role::Project) {
         status.GetMakefile().IssueMessage(
           MessageType::FATAL_ERROR,
           "CONFIGURE_DEPENDS is invalid for script and find package modes.");

+ 4 - 2
Source/cmLoadCacheCommand.cxx

@@ -8,6 +8,7 @@
 
 #include "cmExecutionStatus.h"
 #include "cmMakefile.h"
+#include "cmState.h"
 #include "cmStateTypes.h"
 #include "cmSystemTools.h"
 #include "cmake.h"
@@ -31,8 +32,9 @@ bool cmLoadCacheCommand(std::vector<std::string> const& args,
     return ReadWithPrefix(args, status);
   }
 
-  if (status.GetMakefile().GetCMakeInstance()->GetWorkingMode() ==
-      cmake::SCRIPT_MODE) {
+  cmState::Role const role =
+    status.GetMakefile().GetCMakeInstance()->GetState()->GetRole();
+  if (role != cmState::Role::Project) {
     status.SetError(
       "Only load_cache(READ_WITH_PREFIX) may be used in script mode");
     return false;

+ 3 - 3
Source/cmMakefile.cxx

@@ -585,7 +585,7 @@ bool cmMakefile::ExecuteCommand(cmListFileFunction const& lff,
         }
       }
       if (this->GetCMakeInstance()->HasScriptModeExitCode() &&
-          this->GetCMakeInstance()->GetWorkingMode() == cmake::SCRIPT_MODE) {
+          this->GetCMakeInstance()->RoleSupportsExitCode()) {
         // pass-through the exit code from inner cmake_language(EXIT) ,
         // possibly from include() or similar command...
         status.SetExitCode(this->GetCMakeInstance()->GetScriptModeExitCode());
@@ -3393,8 +3393,8 @@ cmState* cmMakefile::GetState() const
 void cmMakefile::DisplayStatus(std::string const& message, float s) const
 {
   cmake* cm = this->GetCMakeInstance();
-  if (cm->GetWorkingMode() == cmake::FIND_PACKAGE_MODE) {
-    // don't output any STATUS message in FIND_PACKAGE_MODE, since they will
+  if (cm->GetState()->GetRole() == cmState::Role::FindPackage) {
+    // don't output any STATUS message in --find-package mode, since they will
     // directly be fed to the compiler, which will be confused.
     return;
   }

+ 2 - 1
Source/cmSarifLog.cxx

@@ -14,6 +14,7 @@
 
 #include "cmListFileCache.h"
 #include "cmMessageType.h"
+#include "cmState.h"
 #include "cmStringAlgorithms.h"
 #include "cmSystemTools.h"
 #include "cmValue.h"
@@ -375,7 +376,7 @@ bool cmSarif::LogFileWriter::ConfigureForCMakeRun(cmake& cm)
     // normal mode, the project variable `CMAKE_EXPORT_SARIF` can also enable
     // SARIF logging.
     return cm.GetSarifFilePath().has_value() ||
-      (cm.GetWorkingMode() == cmake::NORMAL_MODE &&
+      (cm.GetState()->GetRole() == cmState::Role::Project &&
        cm.GetCacheDefinition(cmSarif::PROJECT_SARIF_FILE_VARIABLE).IsOn());
   });
 

+ 5 - 0
Source/cmState.cxx

@@ -817,6 +817,11 @@ void cmState::SetRoleToProjectForCMakeBuildVsReconfigure()
   this->StateRole = Role::Project;
 }
 
+void cmState::SetRoleToHelpForListPresets()
+{
+  this->StateRole = Role::Help;
+}
+
 cmState::Role cmState::GetRole() const
 {
   return this->StateRole;

+ 1 - 0
Source/cmState.h

@@ -232,6 +232,7 @@ public:
   unsigned int GetCacheMinorVersion() const;
 
   void SetRoleToProjectForCMakeBuildVsReconfigure();
+  void SetRoleToHelpForListPresets();
   Role GetRole() const;
   std::string GetRoleString() const;
 

+ 2 - 1
Source/cmTryCompileCommand.cxx

@@ -45,7 +45,8 @@ bool cmTryCompileCommand(std::vector<std::string> const& args,
     return false;
   }
 
-  if (mf.GetCMakeInstance()->GetWorkingMode() == cmake::FIND_PACKAGE_MODE) {
+  if (mf.GetCMakeInstance()->GetState()->GetRole() ==
+      cmState::Role::FindPackage) {
     mf.IssueMessage(
       MessageType::FATAL_ERROR,
       "The try_compile() command is not supported in --find-package mode.");

+ 2 - 1
Source/cmTryRunCommand.cxx

@@ -548,7 +548,8 @@ bool cmTryRunCommand(std::vector<std::string> const& args,
     return false;
   }
 
-  if (mf.GetCMakeInstance()->GetWorkingMode() == cmake::FIND_PACKAGE_MODE) {
+  if (mf.GetCMakeInstance()->GetState()->GetRole() ==
+      cmState::Role::FindPackage) {
     mf.IssueMessage(
       MessageType::FATAL_ERROR,
       "The try_run() command is not supported in --find-package mode.");

+ 22 - 14
Source/cmake.cxx

@@ -450,6 +450,12 @@ std::string cmake::ReportCapabilities() const
   return result;
 }
 
+bool cmake::RoleSupportsExitCode() const
+{
+  cmState::Role const role = this->State->GetRole();
+  return role == cmState::Role::Script || role == cmState::Role::CTest;
+}
+
 cmake::CommandFailureAction cmake::GetCommandFailureAction() const
 {
   switch (this->State->GetRole()) {
@@ -665,6 +671,7 @@ bool cmake::SetCacheArgs(std::vector<std::string> const& args)
   };
 
   auto ScriptLambda = [&](std::string const& path, cmake* state) -> bool {
+    assert(this->State->GetRole() == cmState::Role::Script);
 #ifdef CMake_ENABLE_DEBUGGER
     // Script mode doesn't hit the usual code path in cmake::Run() that starts
     // the debugger, so start it manually here instead.
@@ -676,7 +683,6 @@ bool cmake::SetCacheArgs(std::vector<std::string> const& args)
     GetProjectCommandsInScriptMode(state->GetState());
     // Documented behavior of CMAKE{,_CURRENT}_{SOURCE,BINARY}_DIR is to be
     // set to $PWD for -P mode.
-    state->SetWorkingMode(SCRIPT_MODE);
     state->SetHomeDirectory(cmSystemTools::GetLogicalWorkingDirectory());
     state->SetHomeOutputDirectory(cmSystemTools::GetLogicalWorkingDirectory());
     state->ReadListFile(args, path);
@@ -748,7 +754,7 @@ bool cmake::SetCacheArgs(std::vector<std::string> const& args)
   for (decltype(args.size()) i = 1; i < args.size(); ++i) {
     std::string const& arg = args[i];
 
-    if (arg == "--" && this->GetWorkingMode() == SCRIPT_MODE) {
+    if (arg == "--" && this->State->GetRole() == cmState::Role::Script) {
       // Stop processing CMake args and avoid possible errors
       // when arbitrary args are given to CMake script.
       break;
@@ -763,7 +769,7 @@ bool cmake::SetCacheArgs(std::vector<std::string> const& args)
     }
   }
 
-  if (this->GetWorkingMode() == FIND_PACKAGE_MODE) {
+  if (this->State->GetRole() == cmState::Role::FindPackage) {
     return this->FindPackage(args);
   }
 
@@ -817,7 +823,7 @@ void cmake::ReadListFile(std::vector<std::string> const& args,
     snapshot.GetDirectory().SetCurrentSource(this->GetHomeDirectory());
     snapshot.SetDefaultDefinitions();
     cmMakefile mf(gg, snapshot);
-    if (this->GetWorkingMode() != NORMAL_MODE) {
+    if (this->State->GetRole() == cmState::Role::Script) {
       mf.SetScriptModeFile(cmSystemTools::ToNormalizedPathOnDisk(path));
       mf.SetArgcArgv(args);
     }
@@ -1439,7 +1445,7 @@ void cmake::SetArgs(std::vector<std::string> const& args)
     // iterate each argument
     std::string const& arg = args[i];
 
-    if (this->GetWorkingMode() == SCRIPT_MODE && arg == "--") {
+    if (this->State->GetRole() == cmState::Role::Script && arg == "--") {
       // Stop processing CMake args and avoid possible errors
       // when arbitrary args are given to CMake script.
       break;
@@ -1485,12 +1491,14 @@ void cmake::SetArgs(std::vector<std::string> const& args)
     }
   }
 
-  if (!extraProvidedPath.empty() && this->GetWorkingMode() == NORMAL_MODE) {
+  if (!extraProvidedPath.empty() &&
+      this->State->GetRole() == cmState::Role::Project) {
     this->IssueMessage(MessageType::WARNING,
                        cmStrCat("Ignoring extra path from command line:\n \"",
                                 extraProvidedPath, '"'));
   }
-  if (!possibleUnknownArg.empty() && this->GetWorkingMode() != SCRIPT_MODE) {
+  if (!possibleUnknownArg.empty() &&
+      this->State->GetRole() != cmState::Role::Script) {
     cmSystemTools::Error(cmStrCat("Unknown argument ", possibleUnknownArg));
     cmSystemTools::Error("Run 'cmake --help' for all supported options.");
     exit(1);
@@ -1539,7 +1547,7 @@ void cmake::SetArgs(std::vector<std::string> const& args)
     !presetName.empty();
 #endif
 
-  if (this->CurrentWorkingMode == cmake::NORMAL_MODE && !haveSourceDir &&
+  if (this->State->GetRole() == cmState::Role::Project && !haveSourceDir &&
       !haveBinaryDir && !havePreset) {
     this->IssueMessage(
       MessageType::WARNING,
@@ -1582,7 +1590,7 @@ void cmake::SetArgs(std::vector<std::string> const& args)
         presetsGraph.PrintAllPresets();
       }
 
-      this->SetWorkingMode(WorkingMode::HELP_MODE);
+      this->State->SetRoleToHelpForListPresets();
       return;
     }
 
@@ -2149,7 +2157,7 @@ void cmake::SetHomeDirectoryViaCommandLine(std::string const& path)
 
   auto prev_path = this->GetHomeDirectory();
   if (prev_path != path && !prev_path.empty() &&
-      this->GetWorkingMode() == NORMAL_MODE) {
+      this->State->GetRole() == cmState::Role::Project) {
     this->IssueMessage(
       MessageType::WARNING,
       cmStrCat("Ignoring extra path from command line:\n \"", prev_path, '"'));
@@ -2719,7 +2727,7 @@ int cmake::ActualConfigure()
   auto endTime = std::chrono::steady_clock::now();
 
   // configure result
-  if (this->GetWorkingMode() == cmake::NORMAL_MODE) {
+  if (this->State->GetRole() == cmState::Role::Project) {
     std::ostringstream msg;
     if (cmSystemTools::GetErrorOccurredFlag()) {
       msg << "Configuring incomplete, errors occurred!";
@@ -2950,7 +2958,7 @@ int cmake::Run(std::vector<std::string> const& args, bool noconfigure)
   if (cmSystemTools::GetErrorOccurredFlag()) {
     return -1;
   }
-  if (this->GetWorkingMode() == HELP_MODE) {
+  if (this->State->GetRole() == cmState::Role::Help) {
     return 0;
   }
 
@@ -2980,7 +2988,7 @@ int cmake::Run(std::vector<std::string> const& args, bool noconfigure)
     return 0;
   }
 
-  if (this->GetWorkingMode() == NORMAL_MODE) {
+  if (this->State->GetRole() == cmState::Role::Project) {
     if (this->FreshCache) {
       this->DeleteCache(this->GetHomeOutputDirectory());
     }
@@ -3027,7 +3035,7 @@ int cmake::Run(std::vector<std::string> const& args, bool noconfigure)
 #endif
 
   // In script mode we terminate after running the script.
-  if (this->GetWorkingMode() != NORMAL_MODE) {
+  if (this->State->GetRole() != cmState::Role::Project) {
     if (cmSystemTools::GetErrorOccurredFlag()) {
       return -1;
     }

+ 1 - 4
Source/cmake.h

@@ -445,9 +445,7 @@ public:
   //! Do all the checks before running configure
   int DoPreConfigureChecks();
 
-  void SetWorkingMode(WorkingMode mode) { this->CurrentWorkingMode = mode; }
-
-  WorkingMode GetWorkingMode() const { return this->CurrentWorkingMode; }
+  bool RoleSupportsExitCode() const;
 
   CommandFailureAction GetCommandFailureAction() const;
 
@@ -801,7 +799,6 @@ private:
   std::vector<std::string> cmdArgs;
   std::string CMakeWorkingDirectory;
   ProgressCallbackType ProgressCallback;
-  WorkingMode CurrentWorkingMode = NORMAL_MODE;
   bool DebugOutput = false;
   bool DebugFindOutput = false;
   // Elements of `cmakeLangTraceCmdStack` are "trace requests" pushed

+ 5 - 20
Source/cmakemain.cxx

@@ -260,7 +260,7 @@ int do_cmake(int ac, char const* const* av)
   // (Regex) Filter on the cached variable(s) to print.
   std::string filter_var_name;
   bool view_only = false;
-  cmake::WorkingMode workingMode = cmake::NORMAL_MODE;
+  cmState::Role role = cmState::Role::Project;
   std::vector<std::string> parsedArgs;
 
   using CommandArgument =
@@ -306,20 +306,20 @@ int do_cmake(int ac, char const* const* av)
                      CommandArgument::Values::One,
                      CommandArgument::RequiresSeparator::No,
                      [&](std::string const& value) -> bool {
-                       workingMode = cmake::SCRIPT_MODE;
+                       role = cmState::Role::Script;
                        parsedArgs.emplace_back("-P");
                        parsedArgs.push_back(value);
                        return true;
                      } },
     CommandArgument{ "--find-package", CommandArgument::Values::Zero,
                      [&](std::string const&) -> bool {
-                       workingMode = cmake::FIND_PACKAGE_MODE;
+                       role = cmState::Role::FindPackage;
                        parsedArgs.emplace_back("--find-package");
                        return true;
                      } },
     CommandArgument{ "--list-presets", CommandArgument::Values::ZeroOrOne,
                      [&](std::string const& value) -> bool {
-                       workingMode = cmake::HELP_MODE;
+                       role = cmState::Role::Help;
                        parsedArgs.emplace_back("--list-presets");
                        parsedArgs.emplace_back(value);
                        return true;
@@ -336,7 +336,7 @@ int do_cmake(int ac, char const* const* av)
 
     // Only in script mode do we stop parsing instead
     // of preferring the last mode flag provided
-    if (arg == "--" && workingMode == cmake::SCRIPT_MODE) {
+    if (arg == "--" && role == cmState::Role::Script) {
       parsedArgs = inputArgs;
       break;
     }
@@ -363,19 +363,6 @@ int do_cmake(int ac, char const* const* av)
     int ret = cm.GetSystemInformation(parsedArgs);
     return ret;
   }
-  cmState::Role role = cmState::Role::Internal;
-  switch (workingMode) {
-    case cmake::NORMAL_MODE:
-    case cmake::HELP_MODE:
-      role = cmState::Role::Project;
-      break;
-    case cmake::SCRIPT_MODE:
-      role = cmState::Role::Script;
-      break;
-    case cmake::FIND_PACKAGE_MODE:
-      role = cmState::Role::FindPackage;
-      break;
-  }
   cmake cm(role);
   cmSystemTools::SetMessageCallback(
     [&cm](std::string const& msg, cmMessageMetadata const& md) {
@@ -384,7 +371,6 @@ int do_cmake(int ac, char const* const* av)
   cm.SetProgressCallback([&cm](std::string const& msg, float prog) {
     cmakemainProgressCallback(msg, prog, &cm);
   });
-  cm.SetWorkingMode(workingMode);
 
   int res = cm.Run(parsedArgs, view_only);
   if (list_cached || list_all_cached) {
@@ -965,7 +951,6 @@ int do_install(int ac, char const* const* av)
           cmakemainProgressCallback(msg, prog, &cm);
         });
         cm.SetDebugOutputOn(verbose);
-        cm.SetWorkingMode(cmake::SCRIPT_MODE);
         ret_ = int(bool(cm.Run(cmd)));
       }
     }