From accfa7fa811a0240e16595a52db299a90fcf43c9 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 4 Nov 2025 18:51:09 -0500 Subject: [PATCH] cmake: Infer command failure action from role --- Source/CTest/cmCTestScriptHandler.cxx | 3 +-- Source/cmake.cxx | 17 +++++++++++++---- Source/cmake.h | 13 ++----------- Source/cmakemain.cxx | 8 ++------ 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/Source/CTest/cmCTestScriptHandler.cxx b/Source/CTest/cmCTestScriptHandler.cxx index 856c3b6141..f4409e5e33 100644 --- a/Source/CTest/cmCTestScriptHandler.cxx +++ b/Source/CTest/cmCTestScriptHandler.cxx @@ -175,8 +175,7 @@ void cmCTestScriptHandler::CreateCMake() this->CMake = cm::make_unique(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(this->CMake.get()); diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 058ec3930b..fe714b32d4 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -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 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 const& args) presetsGraph.PrintAllPresets(); } - this->SetWorkingMode(WorkingMode::HELP_MODE, - cmake::CommandFailureAction::FATAL_ERROR); + this->SetWorkingMode(WorkingMode::HELP_MODE); return; } diff --git a/Source/cmake.h b/Source/cmake.h index 55386e4f63..43d5026b07 100644 --- a/Source/cmake.h +++ b/Source/cmake.h @@ -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 diff --git a/Source/cmakemain.cxx b/Source/cmakemain.cxx index 0c3bd3dd79..2598608f86 100644 --- a/Source/cmakemain.cxx +++ b/Source/cmakemain.cxx @@ -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))); } }