cmake: Replace working mode with role

This commit is contained in:
Brad King
2025-11-04 18:58:46 -05:00
parent accfa7fa81
commit 3593aa59ef
13 changed files with 49 additions and 52 deletions
-1
View File
@@ -175,7 +175,6 @@ void cmCTestScriptHandler::CreateCMake()
this->CMake = cm::make_unique<cmake>(cmState::Role::CTest); this->CMake = cm::make_unique<cmake>(cmState::Role::CTest);
this->CMake->GetCurrentSnapshot().SetDefaultDefinitions(); this->CMake->GetCurrentSnapshot().SetDefaultDefinitions();
this->CMake->AddCMakePaths(); this->CMake->AddCMakePaths();
this->CMake->SetWorkingMode(cmake::SCRIPT_MODE);
this->GlobalGenerator = this->GlobalGenerator =
cm::make_unique<cmGlobalGenerator>(this->CMake.get()); cm::make_unique<cmGlobalGenerator>(this->CMake.get());
+1 -3
View File
@@ -397,9 +397,7 @@ bool cmCMakeLanguageCommand(std::vector<cmListFileArgument> const& args,
return FatalError(status, "EXIT requires one argument"); return FatalError(status, "EXIT requires one argument");
} }
auto workingMode = if (!status.GetMakefile().GetCMakeInstance()->RoleSupportsExitCode()) {
status.GetMakefile().GetCMakeInstance()->GetWorkingMode();
if (workingMode != cmake::SCRIPT_MODE) {
return FatalError(status, "EXIT can be used only in SCRIPT mode"); return FatalError(status, "EXIT can be used only in SCRIPT mode");
} }
+1 -2
View File
@@ -686,7 +686,6 @@ bool HandleGlobImpl(std::vector<std::string> const& args, bool recurse,
std::vector<std::string> files; std::vector<std::string> files;
bool configureDepends = false; bool configureDepends = false;
bool warnConfigureLate = false; bool warnConfigureLate = false;
cmake::WorkingMode const workingMode = cm->GetWorkingMode();
while (i != args.end()) { while (i != args.end()) {
if (*i == "LIST_DIRECTORIES") { if (*i == "LIST_DIRECTORIES") {
++i; // skip 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 " "CONFIGURE_DEPENDS flag was given after a glob expression was "
"already evaluated."); "already evaluated.");
} }
if (workingMode != cmake::NORMAL_MODE) { if (cm->GetState()->GetRole() != cmState::Role::Project) {
status.GetMakefile().IssueMessage( status.GetMakefile().IssueMessage(
MessageType::FATAL_ERROR, MessageType::FATAL_ERROR,
"CONFIGURE_DEPENDS is invalid for script and find package modes."); "CONFIGURE_DEPENDS is invalid for script and find package modes.");
+4 -2
View File
@@ -8,6 +8,7 @@
#include "cmExecutionStatus.h" #include "cmExecutionStatus.h"
#include "cmMakefile.h" #include "cmMakefile.h"
#include "cmState.h"
#include "cmStateTypes.h" #include "cmStateTypes.h"
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmake.h" #include "cmake.h"
@@ -31,8 +32,9 @@ bool cmLoadCacheCommand(std::vector<std::string> const& args,
return ReadWithPrefix(args, status); return ReadWithPrefix(args, status);
} }
if (status.GetMakefile().GetCMakeInstance()->GetWorkingMode() == cmState::Role const role =
cmake::SCRIPT_MODE) { status.GetMakefile().GetCMakeInstance()->GetState()->GetRole();
if (role != cmState::Role::Project) {
status.SetError( status.SetError(
"Only load_cache(READ_WITH_PREFIX) may be used in script mode"); "Only load_cache(READ_WITH_PREFIX) may be used in script mode");
return false; return false;
+3 -3
View File
@@ -585,7 +585,7 @@ bool cmMakefile::ExecuteCommand(cmListFileFunction const& lff,
} }
} }
if (this->GetCMakeInstance()->HasScriptModeExitCode() && if (this->GetCMakeInstance()->HasScriptModeExitCode() &&
this->GetCMakeInstance()->GetWorkingMode() == cmake::SCRIPT_MODE) { this->GetCMakeInstance()->RoleSupportsExitCode()) {
// pass-through the exit code from inner cmake_language(EXIT) , // pass-through the exit code from inner cmake_language(EXIT) ,
// possibly from include() or similar command... // possibly from include() or similar command...
status.SetExitCode(this->GetCMakeInstance()->GetScriptModeExitCode()); status.SetExitCode(this->GetCMakeInstance()->GetScriptModeExitCode());
@@ -3393,8 +3393,8 @@ cmState* cmMakefile::GetState() const
void cmMakefile::DisplayStatus(std::string const& message, float s) const void cmMakefile::DisplayStatus(std::string const& message, float s) const
{ {
cmake* cm = this->GetCMakeInstance(); cmake* cm = this->GetCMakeInstance();
if (cm->GetWorkingMode() == cmake::FIND_PACKAGE_MODE) { if (cm->GetState()->GetRole() == cmState::Role::FindPackage) {
// don't output any STATUS message in FIND_PACKAGE_MODE, since they will // don't output any STATUS message in --find-package mode, since they will
// directly be fed to the compiler, which will be confused. // directly be fed to the compiler, which will be confused.
return; return;
} }
+2 -1
View File
@@ -14,6 +14,7 @@
#include "cmListFileCache.h" #include "cmListFileCache.h"
#include "cmMessageType.h" #include "cmMessageType.h"
#include "cmState.h"
#include "cmStringAlgorithms.h" #include "cmStringAlgorithms.h"
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmValue.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 // normal mode, the project variable `CMAKE_EXPORT_SARIF` can also enable
// SARIF logging. // SARIF logging.
return cm.GetSarifFilePath().has_value() || return cm.GetSarifFilePath().has_value() ||
(cm.GetWorkingMode() == cmake::NORMAL_MODE && (cm.GetState()->GetRole() == cmState::Role::Project &&
cm.GetCacheDefinition(cmSarif::PROJECT_SARIF_FILE_VARIABLE).IsOn()); cm.GetCacheDefinition(cmSarif::PROJECT_SARIF_FILE_VARIABLE).IsOn());
}); });
+5
View File
@@ -817,6 +817,11 @@ void cmState::SetRoleToProjectForCMakeBuildVsReconfigure()
this->StateRole = Role::Project; this->StateRole = Role::Project;
} }
void cmState::SetRoleToHelpForListPresets()
{
this->StateRole = Role::Help;
}
cmState::Role cmState::GetRole() const cmState::Role cmState::GetRole() const
{ {
return this->StateRole; return this->StateRole;
+1
View File
@@ -232,6 +232,7 @@ public:
unsigned int GetCacheMinorVersion() const; unsigned int GetCacheMinorVersion() const;
void SetRoleToProjectForCMakeBuildVsReconfigure(); void SetRoleToProjectForCMakeBuildVsReconfigure();
void SetRoleToHelpForListPresets();
Role GetRole() const; Role GetRole() const;
std::string GetRoleString() const; std::string GetRoleString() const;
+2 -1
View File
@@ -45,7 +45,8 @@ bool cmTryCompileCommand(std::vector<std::string> const& args,
return false; return false;
} }
if (mf.GetCMakeInstance()->GetWorkingMode() == cmake::FIND_PACKAGE_MODE) { if (mf.GetCMakeInstance()->GetState()->GetRole() ==
cmState::Role::FindPackage) {
mf.IssueMessage( mf.IssueMessage(
MessageType::FATAL_ERROR, MessageType::FATAL_ERROR,
"The try_compile() command is not supported in --find-package mode."); "The try_compile() command is not supported in --find-package mode.");
+2 -1
View File
@@ -548,7 +548,8 @@ bool cmTryRunCommand(std::vector<std::string> const& args,
return false; return false;
} }
if (mf.GetCMakeInstance()->GetWorkingMode() == cmake::FIND_PACKAGE_MODE) { if (mf.GetCMakeInstance()->GetState()->GetRole() ==
cmState::Role::FindPackage) {
mf.IssueMessage( mf.IssueMessage(
MessageType::FATAL_ERROR, MessageType::FATAL_ERROR,
"The try_run() command is not supported in --find-package mode."); "The try_run() command is not supported in --find-package mode.");
+22 -14
View File
@@ -450,6 +450,12 @@ std::string cmake::ReportCapabilities() const
return result; 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 cmake::CommandFailureAction cmake::GetCommandFailureAction() const
{ {
switch (this->State->GetRole()) { 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 { auto ScriptLambda = [&](std::string const& path, cmake* state) -> bool {
assert(this->State->GetRole() == cmState::Role::Script);
#ifdef CMake_ENABLE_DEBUGGER #ifdef CMake_ENABLE_DEBUGGER
// Script mode doesn't hit the usual code path in cmake::Run() that starts // Script mode doesn't hit the usual code path in cmake::Run() that starts
// the debugger, so start it manually here instead. // the debugger, so start it manually here instead.
@@ -676,7 +683,6 @@ bool cmake::SetCacheArgs(std::vector<std::string> const& args)
GetProjectCommandsInScriptMode(state->GetState()); GetProjectCommandsInScriptMode(state->GetState());
// Documented behavior of CMAKE{,_CURRENT}_{SOURCE,BINARY}_DIR is to be // Documented behavior of CMAKE{,_CURRENT}_{SOURCE,BINARY}_DIR is to be
// set to $PWD for -P mode. // set to $PWD for -P mode.
state->SetWorkingMode(SCRIPT_MODE);
state->SetHomeDirectory(cmSystemTools::GetLogicalWorkingDirectory()); state->SetHomeDirectory(cmSystemTools::GetLogicalWorkingDirectory());
state->SetHomeOutputDirectory(cmSystemTools::GetLogicalWorkingDirectory()); state->SetHomeOutputDirectory(cmSystemTools::GetLogicalWorkingDirectory());
state->ReadListFile(args, path); 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) { for (decltype(args.size()) i = 1; i < args.size(); ++i) {
std::string const& arg = args[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 // Stop processing CMake args and avoid possible errors
// when arbitrary args are given to CMake script. // when arbitrary args are given to CMake script.
break; 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); return this->FindPackage(args);
} }
@@ -817,7 +823,7 @@ void cmake::ReadListFile(std::vector<std::string> const& args,
snapshot.GetDirectory().SetCurrentSource(this->GetHomeDirectory()); snapshot.GetDirectory().SetCurrentSource(this->GetHomeDirectory());
snapshot.SetDefaultDefinitions(); snapshot.SetDefaultDefinitions();
cmMakefile mf(gg, snapshot); cmMakefile mf(gg, snapshot);
if (this->GetWorkingMode() != NORMAL_MODE) { if (this->State->GetRole() == cmState::Role::Script) {
mf.SetScriptModeFile(cmSystemTools::ToNormalizedPathOnDisk(path)); mf.SetScriptModeFile(cmSystemTools::ToNormalizedPathOnDisk(path));
mf.SetArgcArgv(args); mf.SetArgcArgv(args);
} }
@@ -1439,7 +1445,7 @@ void cmake::SetArgs(std::vector<std::string> const& args)
// iterate each argument // iterate each argument
std::string const& arg = args[i]; 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 // Stop processing CMake args and avoid possible errors
// when arbitrary args are given to CMake script. // when arbitrary args are given to CMake script.
break; 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, this->IssueMessage(MessageType::WARNING,
cmStrCat("Ignoring extra path from command line:\n \"", cmStrCat("Ignoring extra path from command line:\n \"",
extraProvidedPath, '"')); 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(cmStrCat("Unknown argument ", possibleUnknownArg));
cmSystemTools::Error("Run 'cmake --help' for all supported options."); cmSystemTools::Error("Run 'cmake --help' for all supported options.");
exit(1); exit(1);
@@ -1539,7 +1547,7 @@ void cmake::SetArgs(std::vector<std::string> const& args)
!presetName.empty(); !presetName.empty();
#endif #endif
if (this->CurrentWorkingMode == cmake::NORMAL_MODE && !haveSourceDir && if (this->State->GetRole() == cmState::Role::Project && !haveSourceDir &&
!haveBinaryDir && !havePreset) { !haveBinaryDir && !havePreset) {
this->IssueMessage( this->IssueMessage(
MessageType::WARNING, MessageType::WARNING,
@@ -1582,7 +1590,7 @@ void cmake::SetArgs(std::vector<std::string> const& args)
presetsGraph.PrintAllPresets(); presetsGraph.PrintAllPresets();
} }
this->SetWorkingMode(WorkingMode::HELP_MODE); this->State->SetRoleToHelpForListPresets();
return; return;
} }
@@ -2149,7 +2157,7 @@ void cmake::SetHomeDirectoryViaCommandLine(std::string const& path)
auto prev_path = this->GetHomeDirectory(); auto prev_path = this->GetHomeDirectory();
if (prev_path != path && !prev_path.empty() && if (prev_path != path && !prev_path.empty() &&
this->GetWorkingMode() == NORMAL_MODE) { this->State->GetRole() == cmState::Role::Project) {
this->IssueMessage( this->IssueMessage(
MessageType::WARNING, MessageType::WARNING,
cmStrCat("Ignoring extra path from command line:\n \"", prev_path, '"')); cmStrCat("Ignoring extra path from command line:\n \"", prev_path, '"'));
@@ -2719,7 +2727,7 @@ int cmake::ActualConfigure()
auto endTime = std::chrono::steady_clock::now(); auto endTime = std::chrono::steady_clock::now();
// configure result // configure result
if (this->GetWorkingMode() == cmake::NORMAL_MODE) { if (this->State->GetRole() == cmState::Role::Project) {
std::ostringstream msg; std::ostringstream msg;
if (cmSystemTools::GetErrorOccurredFlag()) { if (cmSystemTools::GetErrorOccurredFlag()) {
msg << "Configuring incomplete, errors occurred!"; msg << "Configuring incomplete, errors occurred!";
@@ -2950,7 +2958,7 @@ int cmake::Run(std::vector<std::string> const& args, bool noconfigure)
if (cmSystemTools::GetErrorOccurredFlag()) { if (cmSystemTools::GetErrorOccurredFlag()) {
return -1; return -1;
} }
if (this->GetWorkingMode() == HELP_MODE) { if (this->State->GetRole() == cmState::Role::Help) {
return 0; return 0;
} }
@@ -2980,7 +2988,7 @@ int cmake::Run(std::vector<std::string> const& args, bool noconfigure)
return 0; return 0;
} }
if (this->GetWorkingMode() == NORMAL_MODE) { if (this->State->GetRole() == cmState::Role::Project) {
if (this->FreshCache) { if (this->FreshCache) {
this->DeleteCache(this->GetHomeOutputDirectory()); this->DeleteCache(this->GetHomeOutputDirectory());
} }
@@ -3027,7 +3035,7 @@ int cmake::Run(std::vector<std::string> const& args, bool noconfigure)
#endif #endif
// In script mode we terminate after running the script. // In script mode we terminate after running the script.
if (this->GetWorkingMode() != NORMAL_MODE) { if (this->State->GetRole() != cmState::Role::Project) {
if (cmSystemTools::GetErrorOccurredFlag()) { if (cmSystemTools::GetErrorOccurredFlag()) {
return -1; return -1;
} }
+1 -4
View File
@@ -445,9 +445,7 @@ public:
//! Do all the checks before running configure //! Do all the checks before running configure
int DoPreConfigureChecks(); int DoPreConfigureChecks();
void SetWorkingMode(WorkingMode mode) { this->CurrentWorkingMode = mode; } bool RoleSupportsExitCode() const;
WorkingMode GetWorkingMode() const { return this->CurrentWorkingMode; }
CommandFailureAction GetCommandFailureAction() const; CommandFailureAction GetCommandFailureAction() const;
@@ -801,7 +799,6 @@ private:
std::vector<std::string> cmdArgs; std::vector<std::string> cmdArgs;
std::string CMakeWorkingDirectory; std::string CMakeWorkingDirectory;
ProgressCallbackType ProgressCallback; ProgressCallbackType ProgressCallback;
WorkingMode CurrentWorkingMode = NORMAL_MODE;
bool DebugOutput = false; bool DebugOutput = false;
bool DebugFindOutput = false; bool DebugFindOutput = false;
// Elements of `cmakeLangTraceCmdStack` are "trace requests" pushed // Elements of `cmakeLangTraceCmdStack` are "trace requests" pushed
+5 -20
View File
@@ -260,7 +260,7 @@ int do_cmake(int ac, char const* const* av)
// (Regex) Filter on the cached variable(s) to print. // (Regex) Filter on the cached variable(s) to print.
std::string filter_var_name; std::string filter_var_name;
bool view_only = false; bool view_only = false;
cmake::WorkingMode workingMode = cmake::NORMAL_MODE; cmState::Role role = cmState::Role::Project;
std::vector<std::string> parsedArgs; std::vector<std::string> parsedArgs;
using CommandArgument = using CommandArgument =
@@ -306,20 +306,20 @@ int do_cmake(int ac, char const* const* av)
CommandArgument::Values::One, CommandArgument::Values::One,
CommandArgument::RequiresSeparator::No, CommandArgument::RequiresSeparator::No,
[&](std::string const& value) -> bool { [&](std::string const& value) -> bool {
workingMode = cmake::SCRIPT_MODE; role = cmState::Role::Script;
parsedArgs.emplace_back("-P"); parsedArgs.emplace_back("-P");
parsedArgs.push_back(value); parsedArgs.push_back(value);
return true; return true;
} }, } },
CommandArgument{ "--find-package", CommandArgument::Values::Zero, CommandArgument{ "--find-package", CommandArgument::Values::Zero,
[&](std::string const&) -> bool { [&](std::string const&) -> bool {
workingMode = cmake::FIND_PACKAGE_MODE; role = cmState::Role::FindPackage;
parsedArgs.emplace_back("--find-package"); parsedArgs.emplace_back("--find-package");
return true; return true;
} }, } },
CommandArgument{ "--list-presets", CommandArgument::Values::ZeroOrOne, CommandArgument{ "--list-presets", CommandArgument::Values::ZeroOrOne,
[&](std::string const& value) -> bool { [&](std::string const& value) -> bool {
workingMode = cmake::HELP_MODE; role = cmState::Role::Help;
parsedArgs.emplace_back("--list-presets"); parsedArgs.emplace_back("--list-presets");
parsedArgs.emplace_back(value); parsedArgs.emplace_back(value);
return true; return true;
@@ -336,7 +336,7 @@ int do_cmake(int ac, char const* const* av)
// Only in script mode do we stop parsing instead // Only in script mode do we stop parsing instead
// of preferring the last mode flag provided // of preferring the last mode flag provided
if (arg == "--" && workingMode == cmake::SCRIPT_MODE) { if (arg == "--" && role == cmState::Role::Script) {
parsedArgs = inputArgs; parsedArgs = inputArgs;
break; break;
} }
@@ -363,19 +363,6 @@ int do_cmake(int ac, char const* const* av)
int ret = cm.GetSystemInformation(parsedArgs); int ret = cm.GetSystemInformation(parsedArgs);
return ret; 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); cmake cm(role);
cmSystemTools::SetMessageCallback( cmSystemTools::SetMessageCallback(
[&cm](std::string const& msg, cmMessageMetadata const& md) { [&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) { cm.SetProgressCallback([&cm](std::string const& msg, float prog) {
cmakemainProgressCallback(msg, prog, &cm); cmakemainProgressCallback(msg, prog, &cm);
}); });
cm.SetWorkingMode(workingMode);
int res = cm.Run(parsedArgs, view_only); int res = cm.Run(parsedArgs, view_only);
if (list_cached || list_all_cached) { if (list_cached || list_all_cached) {
@@ -965,7 +951,6 @@ int do_install(int ac, char const* const* av)
cmakemainProgressCallback(msg, prog, &cm); cmakemainProgressCallback(msg, prog, &cm);
}); });
cm.SetDebugOutputOn(verbose); cm.SetDebugOutputOn(verbose);
cm.SetWorkingMode(cmake::SCRIPT_MODE);
ret_ = int(bool(cm.Run(cmd))); ret_ = int(bool(cm.Run(cmd)));
} }
} }