summaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2024-01-17 14:53:24 (GMT)
committerKitware Robot <kwrobot@kitware.com>2024-01-17 14:53:46 (GMT)
commitfa4a499238502dead01c482a168eb52b6599dc22 (patch)
treebf5979cea353f228c43b993194e22555928afaab /Source
parentc7ebec770f19faa7b16df280a1f4804fa98b1011 (diff)
parent1bb17692359d675eee12996c43446fa9c9fe5175 (diff)
downloadCMake-fa4a499238502dead01c482a168eb52b6599dc22.zip
CMake-fa4a499238502dead01c482a168eb52b6599dc22.tar.gz
CMake-fa4a499238502dead01c482a168eb52b6599dc22.tar.bz2
Merge topic 'cmake-language-exit-code'
1bb1769235 cmake_language: Add EXIT subcommand 4f160f7906 cmakemain: Return the SCRIPT_MODE exit code if it was set b62dcbf5d2 cmMakefile: check cmake script mode exit code after command 3d9d504646 cmMakefile: Store the exit code from cmExecutionStatus to cmake instance 9f6c937408 Source/cmake.h: Add ScriptModeExitCode for proper storing exit code 1082b9cb9a cmExecutionStatus: Add ability to set optional custom exit code Acked-by: Kitware Robot <kwrobot@kitware.com> Acked-by: buildbot <buildbot@kitware.com> Acked-by: Leonid Pospelov <pospelovlm@yandex.ru> Acked-by: NoMaY (a user of Renesas Rulz Japanese Forum) <nomay-jp@outlook.com> Merge-request: !8228
Diffstat (limited to 'Source')
-rw-r--r--Source/cmCMakeLanguageCommand.cxx26
-rw-r--r--Source/cmExecutionStatus.h8
-rw-r--r--Source/cmMakefile.cxx11
-rw-r--r--Source/cmake.cxx2
-rw-r--r--Source/cmake.h6
-rw-r--r--Source/cmakemain.cxx11
6 files changed, 59 insertions, 5 deletions
diff --git a/Source/cmCMakeLanguageCommand.cxx b/Source/cmCMakeLanguageCommand.cxx
index 329427c..9ffc363 100644
--- a/Source/cmCMakeLanguageCommand.cxx
+++ b/Source/cmCMakeLanguageCommand.cxx
@@ -398,6 +398,32 @@ bool cmCMakeLanguageCommand(std::vector<cmListFileArgument> const& args,
if (!moreArgs()) {
return FatalError(status, "called with incorrect number of arguments");
}
+ if (expArgs[expArg] == "EXIT"_s) {
+ ++expArg; // consume "EXIT".
+
+ if (!moreArgs()) {
+ return FatalError(status, "EXIT requires one argument");
+ }
+
+ auto workingMode =
+ status.GetMakefile().GetCMakeInstance()->GetWorkingMode();
+ if (workingMode != cmake::SCRIPT_MODE) {
+ return FatalError(status, "EXIT can be used only in SCRIPT mode");
+ }
+
+ long retCode = 0;
+
+ if (!cmStrToLong(expArgs[expArg], &retCode)) {
+ return FatalError(status,
+ cmStrCat("EXIT requires one integral argument, got \"",
+ expArgs[expArg], '\"'));
+ }
+
+ if (workingMode == cmake::SCRIPT_MODE) {
+ status.SetExitCode(static_cast<int>(retCode));
+ }
+ return true;
+ }
if (expArgs[expArg] == "SET_DEPENDENCY_PROVIDER"_s) {
finishArgs();
diff --git a/Source/cmExecutionStatus.h b/Source/cmExecutionStatus.h
index ced3548..e023971 100644
--- a/Source/cmExecutionStatus.h
+++ b/Source/cmExecutionStatus.h
@@ -7,6 +7,8 @@
#include <string>
#include <vector>
+#include <cm/optional>
+
class cmMakefile;
/** \class cmExecutionStatus
@@ -53,6 +55,11 @@ public:
void SetNestedError() { this->NestedError = true; }
bool GetNestedError() const { return this->NestedError; }
+ void SetExitCode(int code) noexcept { this->ExitCode = code; }
+ bool HasExitCode() const noexcept { return this->ExitCode.has_value(); }
+ void CleanExitCode() noexcept { this->ExitCode.reset(); }
+ int GetExitCode() const noexcept { return this->ExitCode.value_or(-1); }
+
private:
cmMakefile& Makefile;
std::string Error;
@@ -60,5 +67,6 @@ private:
bool BreakInvoked = false;
bool ContinueInvoked = false;
bool NestedError = false;
+ cm::optional<int> ExitCode;
std::vector<std::string> Variables;
};
diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx
index 936b282..6fb7734 100644
--- a/Source/cmMakefile.cxx
+++ b/Source/cmMakefile.cxx
@@ -529,6 +529,12 @@ bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff,
cmSystemTools::SetFatalErrorOccurred();
}
}
+ if (this->GetCMakeInstance()->HasScriptModeExitCode() &&
+ this->GetCMakeInstance()->GetWorkingMode() == cmake::SCRIPT_MODE) {
+ // pass-through the exit code from inner cmake_language(EXIT) ,
+ // possibly from include() or similar command...
+ status.SetExitCode(this->GetCMakeInstance()->GetScriptModeExitCode());
+ }
}
} else {
if (!cmSystemTools::GetFatalErrorOccurred()) {
@@ -898,6 +904,11 @@ void cmMakefile::RunListFile(cmListFile const& listFile,
if (cmSystemTools::GetFatalErrorOccurred()) {
break;
}
+ if (status.HasExitCode()) {
+ // cmake_language EXIT was requested, early break.
+ this->GetCMakeInstance()->SetScriptModeExitCode(status.GetExitCode());
+ break;
+ }
if (status.GetReturnInvoked()) {
this->RaiseScope(status.GetReturnVariables());
// Exit early due to return command.
diff --git a/Source/cmake.cxx b/Source/cmake.cxx
index 32064b4..7ab7600 100644
--- a/Source/cmake.cxx
+++ b/Source/cmake.cxx
@@ -2820,7 +2820,7 @@ int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
if (cmSystemTools::GetErrorOccurredFlag()) {
return -1;
}
- return 0;
+ return this->HasScriptModeExitCode() ? this->GetScriptModeExitCode() : 0;
}
// If MAKEFLAGS are given in the environment, remove the environment
diff --git a/Source/cmake.h b/Source/cmake.h
index 58f90c9..ccf5154 100644
--- a/Source/cmake.h
+++ b/Source/cmake.h
@@ -835,7 +835,13 @@ private:
std::string DebuggerDapLogFile;
#endif
+ cm::optional<int> ScriptModeExitCode;
+
public:
+ bool HasScriptModeExitCode() const { return ScriptModeExitCode.has_value(); }
+ void SetScriptModeExitCode(int code) { ScriptModeExitCode = code; }
+ int GetScriptModeExitCode() const { return ScriptModeExitCode.value_or(-1); }
+
static cmDocumentationEntry CMAKE_STANDARD_OPTIONS_TABLE[18];
};
diff --git a/Source/cmakemain.cxx b/Source/cmakemain.cxx
index ced83dc..8462734 100644
--- a/Source/cmakemain.cxx
+++ b/Source/cmakemain.cxx
@@ -389,13 +389,16 @@ int do_cmake(int ac, char const* const* av)
}
}
- // Always return a non-negative value. Windows tools do not always
- // interpret negative return values as errors.
+ // Always return a non-negative value (except exit code from SCRIPT_MODE).
+ // Windows tools do not always interpret negative return values as errors.
if (res != 0) {
+ auto scriptModeExitCode =
+ cm.HasScriptModeExitCode() ? cm.GetScriptModeExitCode() : 0;
+ res = scriptModeExitCode ? scriptModeExitCode : 1;
#ifdef CMake_ENABLE_DEBUGGER
- cm.StopDebuggerIfNeeded(1);
+ cm.StopDebuggerIfNeeded(res);
#endif
- return 1;
+ return res;
}
#ifdef CMake_ENABLE_DEBUGGER
cm.StopDebuggerIfNeeded(0);