cmake: Infer command failure action from role

This commit is contained in:
Brad King
2025-11-04 18:51:09 -05:00
parent e290d4f2a3
commit accfa7fa81
4 changed files with 18 additions and 23 deletions

View File

@@ -175,8 +175,7 @@ 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,
cmake::CommandFailureAction::EXIT_CODE);
this->CMake->SetWorkingMode(cmake::SCRIPT_MODE);
this->GlobalGenerator =
cm::make_unique<cmGlobalGenerator>(this->CMake.get());

View File

@@ -450,6 +450,17 @@ std::string cmake::ReportCapabilities() const
return result;
}
cmake::CommandFailureAction cmake::GetCommandFailureAction() const
{
switch (this->State->GetRole()) {
case cmState::Role::Project:
case cmState::Role::CTest:
return CommandFailureAction::EXIT_CODE;
default:
return CommandFailureAction::FATAL_ERROR;
}
}
void cmake::CleanupCommandsAndMacros()
{
this->CurrentSnapshot = this->State->Reset();
@@ -665,8 +676,7 @@ 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,
cmake::CommandFailureAction::FATAL_ERROR);
state->SetWorkingMode(SCRIPT_MODE);
state->SetHomeDirectory(cmSystemTools::GetLogicalWorkingDirectory());
state->SetHomeOutputDirectory(cmSystemTools::GetLogicalWorkingDirectory());
state->ReadListFile(args, path);
@@ -1572,8 +1582,7 @@ void cmake::SetArgs(std::vector<std::string> const& args)
presetsGraph.PrintAllPresets();
}
this->SetWorkingMode(WorkingMode::HELP_MODE,
cmake::CommandFailureAction::FATAL_ERROR);
this->SetWorkingMode(WorkingMode::HELP_MODE);
return;
}

View File

@@ -445,18 +445,11 @@ public:
//! Do all the checks before running configure
int DoPreConfigureChecks();
void SetWorkingMode(WorkingMode mode, CommandFailureAction policy)
{
this->CurrentWorkingMode = mode;
this->CurrentCommandFailureAction = policy;
}
void SetWorkingMode(WorkingMode mode) { this->CurrentWorkingMode = mode; }
WorkingMode GetWorkingMode() const { return this->CurrentWorkingMode; }
CommandFailureAction GetCommandFailureAction() const
{
return this->CurrentCommandFailureAction;
}
CommandFailureAction GetCommandFailureAction() const;
//! Debug the try compile stuff by not deleting the files
bool GetDebugTryCompile() const { return this->DebugTryCompile; }
@@ -809,8 +802,6 @@ private:
std::string CMakeWorkingDirectory;
ProgressCallbackType ProgressCallback;
WorkingMode CurrentWorkingMode = NORMAL_MODE;
CommandFailureAction CurrentCommandFailureAction =
CommandFailureAction::FATAL_ERROR;
bool DebugOutput = false;
bool DebugFindOutput = false;
// Elements of `cmakeLangTraceCmdStack` are "trace requests" pushed

View File

@@ -376,9 +376,6 @@ int do_cmake(int ac, char const* const* av)
role = cmState::Role::FindPackage;
break;
}
auto const failurePolicy = workingMode == cmake::NORMAL_MODE
? cmake::CommandFailureAction::EXIT_CODE
: cmake::CommandFailureAction::FATAL_ERROR;
cmake cm(role);
cmSystemTools::SetMessageCallback(
[&cm](std::string const& msg, cmMessageMetadata const& md) {
@@ -387,7 +384,7 @@ 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, failurePolicy);
cm.SetWorkingMode(workingMode);
int res = cm.Run(parsedArgs, view_only);
if (list_cached || list_all_cached) {
@@ -968,8 +965,7 @@ int do_install(int ac, char const* const* av)
cmakemainProgressCallback(msg, prog, &cm);
});
cm.SetDebugOutputOn(verbose);
cm.SetWorkingMode(cmake::SCRIPT_MODE,
cmake::CommandFailureAction::FATAL_ERROR);
cm.SetWorkingMode(cmake::SCRIPT_MODE);
ret_ = int(bool(cm.Run(cmd)));
}
}