# add\_custom\_command with multpile commands when first fails

**URL:** https://discourse.cmake.org/t/add-custom-command-with-multpile-commands-when-first-fails/8647
**Category:** Usage
**Created:** [July 31, 2023, 4:10pm UTC](https://discourse.cmake.org/t/add-custom-command-with-multpile-commands-when-first-fails/8647 "2023-07-31T16:10:53Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Barry\_Revzin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/barry_revzin/32/1283_2.png) [@Barry\_Revzin](https://discourse.cmake.org/u/Barry_Revzin)
#### Post date: [July 31, 2023, 4:10pm UTC](https://discourse.cmake.org/t/add-custom-command-with-multpile-commands-when-first-fails/8647/1 "2023-07-31T16:10:53Z")

</div>

Given:

```cmake
add_custom_command(
    COMMAND c1
    COMMAND c2
    ...
)

```

Is it necessarily the case that if `c1` fails, `c2` is not run?

The documentation just says that `c1` and `c2` are [executed in order](https://cmake.org/cmake/help/latest/command/add_custom_command.html):

> Specify the command-line(s) to execute at build time. If more than one `COMMAND` is specified they will be executed in order, but _not_ necessarily composed into a stateful shell or batch script.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [August 1, 2023, 2:55am UTC](https://discourse.cmake.org/t/add-custom-command-with-multpile-commands-when-first-fails/8647/2 "2023-08-01T02:55:31Z")

</div>

Commands are chained using `&&`, so if `c1` fails, `c2` is not run.

```cmake
cmake_minimum_required(VERSION 3.20)
project(foo NONE)

add_custom_target(blah)

add_custom_command(
    TARGET blah
    COMMAND true
    COMMAND touch "${CMAKE_CURRENT_BINARY_DIR}/ran-true")

add_custom_command(
    TARGET blah
    COMMAND false
    COMMAND touch "${CMAKE_CURRENT_BINARY_DIR}/ran-false")

```

```auto
[0-1->1/1@250.0] cd /home/boeckb/misc/code/sb/cm-cc-chain/build && true && touch /home/boeckb/misc/code/sb/cm-cc-chain/build/ran-true && cd /home/boeckb/misc/code/sb/cm-cc-chain/build && false && touch /home/boeckb/misc/code/sb/cm-cc-chain/build/ran-false
FAILED: CMakeFiles/blah.util 
cd /home/boeckb/misc/code/sb/cm-cc-chain/build && true && touch /home/boeckb/misc/code/sb/cm-cc-chain/build/ran-true && cd /home/boeckb/misc/code/sb/cm-cc-chain/build && false && touch /home/boeckb/misc/code/sb/cm-cc-chain/build/ran-false
ninja: build stopped: cannot make progress due to previous errors.

```

In fact, it seems that custom commands all attached to the same target get `&&`'d together.

The “not necessarily composed into a stateful script” part means that `cd` and `export` on variables may not affect future commands.
