1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-14 19:08:07 +08:00

cmake_language: Add EXIT subcommand

Add tests to cover these cases:

* run as regular CMake module, in NORMAL_MODE (expected to fail);
* run as CMake script in SCRIPT_MODE (expected to exit with given code);
* run as CMake script that `include()`-s another script with EXIT subcommand;
* run as CMake script which EVAL-uates EXIT subcommand via
  `cmake_language(EVAL CODE "<cmake code>")`.

Fixes: #23162
This commit is contained in:
leha-bot
2024-01-05 19:12:22 +03:00
parent 4f160f7906
commit 1bb1769235
29 changed files with 93 additions and 0 deletions

View File

@@ -15,6 +15,7 @@ Synopsis
cmake_language(`DEFER`_ <options>... CALL <command> [<arg>...])
cmake_language(`SET_DEPENDENCY_PROVIDER`_ <command> SUPPORTED_METHODS <methods>...)
cmake_language(`GET_MESSAGE_LOG_LEVEL`_ <out-var>)
cmake_language(`EXIT`_ <exit-code>)
Introduction
^^^^^^^^^^^^
@@ -506,3 +507,25 @@ Getting current message log level
If both the command line option and the variable are set, the command line
option takes precedence. If neither are set, the default logging level
is returned.
Terminating Scripts
^^^^^^^^^^^^^^^^^^^
.. versionadded:: 3.29
.. _EXIT:
.. _exit-code:
.. code-block:: cmake
.. signature::
cmake_language(EXIT <exit-code>)
Terminate the current :option:`cmake -P` script and exit with ``<exit-code>``.
This command works only in :ref:`script mode <Script Processing Mode>`.
The ``<exit-code>`` should be non-negative.
If ``<exit-code>`` is negative then the behavior
is unspecified (e.g., on Windows the error code -1
becomes ``0xffffffff``, and on Linux it becomes ``255``).

View File

@@ -0,0 +1,5 @@
cmake-language-exit
-------------------
* The :command:`cmake_language()` command gained a new ``EXIT``
sub-command to exit scripts with a specified exit code.

View File

@@ -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();

View File

@@ -84,6 +84,13 @@ run_cmake(defer_get_call_id_var)
run_cmake(defer_missing_arg)
run_cmake(defer_missing_call)
run_cmake(defer_unknown_option)
run_cmake(exit_0)
run_cmake(exit_5)
run_cmake_script(exit_0_script)
run_cmake_script(exit_5_script)
run_cmake_script(exit_0_script_with_command)
run_cmake_script(exit_7_script_in_include)
run_cmake_script(exit_8_script_in_recursive_cmake_language)
# Default log level
run_cmake_command(

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,2 @@
CMake Error at exit_0.cmake:1 \(cmake_language\):
cmake_language EXIT can be used only in SCRIPT mode

View File

@@ -0,0 +1 @@
cmake_language(EXIT 0)

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@
cmake_language(EXIT 0)

View File

@@ -0,0 +1,3 @@
cmake_language(EXIT 0)
message(FATAL_ERROR "cmake_language(EXIT 0) doesn't work")

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,2 @@
CMake Error at exit_5.cmake:1 \(cmake_language\):
cmake_language EXIT can be used only in SCRIPT mode

View File

@@ -0,0 +1 @@
cmake_language(EXIT 5)

View File

@@ -0,0 +1 @@
5

View File

@@ -0,0 +1 @@
cmake_language(EXIT 5)

View File

@@ -0,0 +1,3 @@
cmake_language(EXIT 5)
message(FATAL_ERROR "cmake_language(EXIT 5) doesn't work")

View File

@@ -0,0 +1,3 @@
include(${CMAKE_CURRENT_LIST_DIR}/exit_7_script_included_with_exit.cmake)
message(FATAL_ERROR "The cmake_language(EXIT 7) from include()-d script doesn't work")

View File

@@ -0,0 +1,3 @@
cmake_language(EXIT 7)
message(FATAL_ERROR "The include()-d script with EXIT 7 doesn't work")

View File

@@ -0,0 +1,3 @@
cmake_language(EVAL CODE "cmake_language(EXIT 8)")
message(FATAL_ERROR "The cmake_language EVAL of EXIT 8 test doesn't work")