# Exit with custom status code

**URL:** https://discourse.cmake.org/t/exit-with-custom-status-code/12944
**Category:** Code
**Tags:** tool:cmake
**Created:** [November 11, 2024, 11:28pm UTC](https://discourse.cmake.org/t/exit-with-custom-status-code/12944 "2024-11-11T23:28:34Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tomoyo255](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/a183cd/32.png) [@tomoyo255](https://discourse.cmake.org/u/tomoyo255)
#### Post date: [November 11, 2024, 11:28pm UTC](https://discourse.cmake.org/t/exit-with-custom-status-code/12944/1 "2024-11-11T23:28:35Z")

</div>

Hello,

I would like to know if there’s a way to exit at will from a cmake CMakeLists.txt with a status code of my choice.

If the current cmake list corresponds to one of a subdirectory suggesting a nested execution, it’s fine that execution resumes in the parent context with the process status that I supplied to the exit feature and the expected effect on that context level.

Rationale: I’m in a situation where I’m executing a command from a macro and if the result pleases me, I might be inclined to short cut and exit immediately. But, as I suggest, that might not be a fatal condition, it might be an easy win in success instead (zero return status).

To generate a fatal condition is easy with message(FATAL\_ERROR …), but I would need something more like a message(SUCCESS\_EXIT …) that would return with zero status.

In particular, I must insist that, in my case, I need that behavior to be encapsulatable in a macro for cleanliness of the CMakeLists.txt code at the top level. I could of course do an if that would encompass the rest of the script entirely and only enter the then-clause if my macro didn’t want to exit in shortcut, but wouldn’t be satisfactory obviously.

E.g.:

```auto
macro print_help_and_exit_if_asked(opt_help)
    if(NOT "${opt_help}" STREQUAL "")
        print_help()
        exit(0)
    endif()
endmacro()

```

So, is there any exit() function available or cmake\_exit() as another person suggested?

I must insist here that I’m only requesting something very common that is: when entering some conditional part of the code where executing the rest of the script is not expected anymore like printing some help notice or things like that, I would like it to be easy to cut things short. Of course, a giant if can achieve that, but is it necessarily cleaner?

Also the help printing above is just an example, that’s not my true need.

Thanks in advance.

---

<div class="post-metadata">

### Author: ![tomoyo255](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/a183cd/32.png) [@tomoyo255](https://discourse.cmake.org/u/tomoyo255)
#### Post date: [November 12, 2024, 12:43am UTC](https://discourse.cmake.org/t/exit-with-custom-status-code/12944/2 "2024-11-12T00:43:24Z")

</div>

Question: might return() be the answer to my question?

> <https://stackoverflow.com/questions/1511994/how-to-abort-processing-cmakelists-for-current-directory>

---

<div class="post-metadata">

### Author: ![leha-bot](https://discourse.cmake.org/user_avatar/discourse.cmake.org/leha-bot/32/921_2.png) [@leha-bot](https://discourse.cmake.org/u/leha-bot)
#### Post date: [November 12, 2024, 9:06am UTC](https://discourse.cmake.org/t/exit-with-custom-status-code/12944/3 "2024-11-12T09:06:25Z")

</div>

Hello, I implemented `cmake_language(EXIT <exitCode>)` some times ago (it available since CMake 3.29), check out it here please: [https://cmake.org/cmake/help/git-master/command/cmake\_language.html#terminating-scripts](https://cmake.org/cmake/help/git-master/command/cmake_language.html#terminating-scripts)

---

<div class="post-metadata">

### Author: ![leha-bot](https://discourse.cmake.org/user_avatar/discourse.cmake.org/leha-bot/32/921_2.png) [@leha-bot](https://discourse.cmake.org/u/leha-bot)
#### Post date: [November 12, 2024, 9:08am UTC](https://discourse.cmake.org/t/exit-with-custom-status-code/12944/4 "2024-11-12T09:08:07Z")

</div>

In general, no, it just returns you to the outer scope of your directories, and it couldn’t set custom error code if used in top-level CMakeLists.txt scope
