# add\_custom\_command() and failing COMMANDs

**URL:** https://discourse.cmake.org/t/add-custom-command-and-failing-commands/11751
**Category:** Code
**Created:** [September 23, 2024, 12:18pm UTC](https://discourse.cmake.org/t/add-custom-command-and-failing-commands/11751 "2024-09-23T12:18:43Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)
#### Post date: [September 23, 2024, 12:18pm UTC](https://discourse.cmake.org/t/add-custom-command-and-failing-commands/11751/1 "2024-09-23T12:18:43Z")

</div>

Hello,

What is the expected behavior when one of the COMMANDs given to `add_custom_command()` fail ? Are the following commands processed ?  
Is there a log ? Does the CMake command return an error code ?

Thanks.

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [September 23, 2024, 12:26pm UTC](https://discourse.cmake.org/t/add-custom-command-and-failing-commands/11751/2 "2024-09-23T12:26:54Z")

</div>

Custom commands run at build time, which means their behaviour is controlled by the build driver (e.g. make, msbuild) more than it’s controlled by CMake. Since CMake docs don’t specify otherwise, I’ve always expected them to behave like other build steps:

- No implicit logging (who would be responsible for it?)
- Failure terminates the currently running build rule, so no further `COMMAND`s from the same `add_custom_command` processed.

---

<div class="post-metadata">

### Author: ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)
#### Post date: [September 23, 2024, 2:22pm UTC](https://discourse.cmake.org/t/add-custom-command-and-failing-commands/11751/3 "2024-09-23T14:22:55Z")

</div>

OK, I tried to find the info from Visual Studio. There is not realy documentation about this point, but by reading the Post-Build events, I see the **[%errorlevel%](https://ss64.com/nt/errorlevel.html)** system variable is set when any command fails.

Does CMake read this value ? Can I get it in order to check it ?

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [September 23, 2024, 2:28pm UTC](https://discourse.cmake.org/t/add-custom-command-and-failing-commands/11751/4 "2024-09-23T14:28:34Z")

</div>

`%errorlevel%` is the Windows syntax for getting the exit code of the last command run, similar to `$?` in Unix.

Please note that at build-time, it’s not CMake which executes the build steps such as compilation, linking, and custom commands. Unless you build using `cmake --build`, CMake is not even running at build time.

If you need complex custom command processing and logic, I suggest you create a script in an appropriate shell or scripting language (including perhaps CMake with execution via `cmake -P`) and use that as your custom command, instead of putting too much logic into the `add_custom_command()` call itself.

---

<div class="post-metadata">

### Author: ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)
#### Post date: [September 23, 2024, 2:33pm UTC](https://discourse.cmake.org/t/add-custom-command-and-failing-commands/11751/5 "2024-09-23T14:33:16Z")

</div>

Actually, I intended to test `$ENV{ERRORLEVEL}` to potententially exit with a `message(FAILURE ...)`.

The purpose is to exit from CMake with a status error so that our Jenkins see the building process as failed.

But [this post](https://stackoverflow.com/questions/46709956/how-do-i-handle-cmake-exe-errors) le me think an `execute_process()` may be needed…

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [September 23, 2024, 2:46pm UTC](https://discourse.cmake.org/t/add-custom-command-and-failing-commands/11751/6 "2024-09-23T14:46:57Z")

</div>

It might be the case that `execute_process()` is what you’re looking for. That is for running code _when CMake is running to generate your buildsystem,_ which seems to be what you want.

Your original post contains `add_custom_command()`, which is for defining commands that will run _as part of the build, at build time._

---

<div class="post-metadata">

### Author: ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)
#### Post date: [September 23, 2024, 2:52pm UTC](https://discourse.cmake.org/t/add-custom-command-and-failing-commands/11751/7 "2024-09-23T14:52:49Z")

</div>

I use `add_custom_command()` to retrieve the needed Qt dependencies (which are determined from the built files) and to generate a MD5 file. So I guess it is the appropriate command.

But from the post, what I should do is not so clear…
