diff --git a/Source/CTest/cmCTestScriptHandler.cxx b/Source/CTest/cmCTestScriptHandler.cxx index f4409e5e33..bd4fa3818b 100644 --- a/Source/CTest/cmCTestScriptHandler.cxx +++ b/Source/CTest/cmCTestScriptHandler.cxx @@ -175,7 +175,6 @@ void cmCTestScriptHandler::CreateCMake() this->CMake = cm::make_unique(cmState::Role::CTest); this->CMake->GetCurrentSnapshot().SetDefaultDefinitions(); this->CMake->AddCMakePaths(); - this->CMake->SetWorkingMode(cmake::SCRIPT_MODE); this->GlobalGenerator = cm::make_unique(this->CMake.get()); diff --git a/Source/cmCMakeLanguageCommand.cxx b/Source/cmCMakeLanguageCommand.cxx index 157532046a..2a90065497 100644 --- a/Source/cmCMakeLanguageCommand.cxx +++ b/Source/cmCMakeLanguageCommand.cxx @@ -397,9 +397,7 @@ bool cmCMakeLanguageCommand(std::vector 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"); } diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx index 53fddcd358..1f4a6f8e47 100644 --- a/Source/cmFileCommand.cxx +++ b/Source/cmFileCommand.cxx @@ -686,7 +686,6 @@ bool HandleGlobImpl(std::vector const& args, bool recurse, std::vector 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 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."); diff --git a/Source/cmLoadCacheCommand.cxx b/Source/cmLoadCacheCommand.cxx index d88dbb3b1b..5a665cbcdd 100644 --- a/Source/cmLoadCacheCommand.cxx +++ b/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 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; diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index b42243b2ed..0a5dd19d71 100644 --- a/Source/cmMakefile.cxx +++ b/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; } diff --git a/Source/cmSarifLog.cxx b/Source/cmSarifLog.cxx index 35b67f7914..f251b9cb90 100644 --- a/Source/cmSarifLog.cxx +++ b/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()); }); diff --git a/Source/cmState.cxx b/Source/cmState.cxx index c1bb19bf4f..bfd97362bb 100644 --- a/Source/cmState.cxx +++ b/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; diff --git a/Source/cmState.h b/Source/cmState.h index 31ee63aac9..885ade5fc0 100644 --- a/Source/cmState.h +++ b/Source/cmState.h @@ -232,6 +232,7 @@ public: unsigned int GetCacheMinorVersion() const; void SetRoleToProjectForCMakeBuildVsReconfigure(); + void SetRoleToHelpForListPresets(); Role GetRole() const; std::string GetRoleString() const; diff --git a/Source/cmTryCompileCommand.cxx b/Source/cmTryCompileCommand.cxx index 40d3d8a18e..b830792fd2 100644 --- a/Source/cmTryCompileCommand.cxx +++ b/Source/cmTryCompileCommand.cxx @@ -45,7 +45,8 @@ bool cmTryCompileCommand(std::vector 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."); diff --git a/Source/cmTryRunCommand.cxx b/Source/cmTryRunCommand.cxx index 0889ff0f87..5c46d7d85d 100644 --- a/Source/cmTryRunCommand.cxx +++ b/Source/cmTryRunCommand.cxx @@ -548,7 +548,8 @@ bool cmTryRunCommand(std::vector 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."); diff --git a/Source/cmake.cxx b/Source/cmake.cxx index fe714b32d4..787b744e64 100644 --- a/Source/cmake.cxx +++ b/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 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 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 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 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 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 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 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 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 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 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 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 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; } diff --git a/Source/cmake.h b/Source/cmake.h index 43d5026b07..c7ae2108b1 100644 --- a/Source/cmake.h +++ b/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 cmdArgs; std::string CMakeWorkingDirectory; ProgressCallbackType ProgressCallback; - WorkingMode CurrentWorkingMode = NORMAL_MODE; bool DebugOutput = false; bool DebugFindOutput = false; // Elements of `cmakeLangTraceCmdStack` are "trace requests" pushed diff --git a/Source/cmakemain.cxx b/Source/cmakemain.cxx index 2598608f86..d3ccf560c2 100644 --- a/Source/cmakemain.cxx +++ b/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 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))); } }