# Custom command or POST\_BUILD dependent on Release configuration?

**URL:** <https://discourse.cmake.org/t/custom-command-or-post-build-dependent-on-release-configuration/1912>\
**Category:** Code\
**Tags:** os:windows, comp:msvc\
**Created:** [September 25, 2020, 3:28pm UTC](https://discourse.cmake.org/t/custom-command-or-post-build-dependent-on-release-configuration/1912 "2020-09-25T15:28:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ProTur1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/34f0e0/32.png) [@ProTur1](https://discourse.cmake.org/u/ProTur1)\
**Post date:** [September 25, 2020, 3:28pm UTC](https://discourse.cmake.org/t/custom-command-or-post-build-dependent-on-release-configuration/1912/1 "2020-09-25T15:28:51Z")

</div>

How can I execute a custom command or POST\_BUILD dependent on VS release configuration?  
I want to copy an executable to another folder, only if Release in VS has been selected.  
Generator expressions with custom\_command seem to be inapplicable in that case ?

---

<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 25, 2020, 3:51pm UTC](https://discourse.cmake.org/t/custom-command-or-post-build-dependent-on-release-configuration/1912/2 "2020-09-25T15:51:53Z")

</div>

Quoting from [`add_custom_command()` documentation](https://cmake.org/cmake/help/latest/command/add_custom_command.html):

> Arguments to `COMMAND` may use [`generator expressions`](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#manual:cmake-generator-expressions(7)).  
> […]  
> **Note:** Because generator expressions can be used in custom commands, it is possible to define `COMMAND` lines or whole custom commands which evaluate to empty strings for certain configurations. For **Visual Studio 2010 (and newer)** generators these command lines or custom commands will be omitted for the specific configuration and no “empty-string-command” will be added.  
> This allows to add individual build events for every configuration.

So this should be prefectly possible:

```
add_custom_command(
  TARGET whatever
  POST_BUILD
  COMMAND $<$<CONFIG:Release>:somehow_copy> ARGS "$<$<CONFIG:Release>:something;somewhere>"
  COMMAND_EXPAND_LISTS
  VERBATIM
)

```

This should produce the command in Release configuration only.

If for some reason this doesn’t work for you, you can always fall back to a workaround of passing the configuration in:

```
add_custom_command(
  TARGET whatever
  POST_BUILD
  COMMAND ${CMAKE_COMMAND} -P my_copy_script.cmake something somewhere $<CONFIG>
  VERBATIM
)

```

And inside `my_copy_script.cmake`, do something or nothing based on the value of the 3rd argument.

---

<div class="post-metadata">

**Author:** ![ProTur1](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/34f0e0/32.png) [@ProTur1](https://discourse.cmake.org/u/ProTur1)\
**Post date:** [September 28, 2020, 10:53am UTC](https://discourse.cmake.org/t/custom-command-or-post-build-dependent-on-release-configuration/1912/3 "2020-09-28T10:53:07Z")

</div>

Thanks. That worked.  
Although in the documentation Im reading  
`The optional `ARGS` argument is for backward compatibility and will be ignored.`

Btw do you know why `COMMENT` doesnt output anything under `Linux` ?

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [September 28, 2020, 11:06pm UTC](https://discourse.cmake.org/t/custom-command-or-post-build-dependent-on-release-configuration/1912/4 "2020-09-28T23:06:20Z")

</div>

> [@ProTur1](#):
>
> Btw do you know why `COMMENT` doesnt output anything under `Linux` ?

It’s the generator that matters, not the platform – some CMake generators honour `COMMENT` and some don’t. That’s annoying and makes its use unreliable at best. If you want to ensure that a comment is recorded regardless of the generator used, you can do something like this instead:

```cmake
add_custom_command(
  TARGET whatever
  POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E echo "Put your comment here"
  COMMAND ${CMAKE_COMMAND} -P my_copy_script.cmake something somewhere $<CONFIG>
  VERBATIM
)

```

---

<div class="post-metadata">

**Author:** ![Mizux](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mizux/32/1920_2.png) [@Mizux](https://discourse.cmake.org/u/Mizux)\
**Post date:** [October 29, 2021, 3:34pm UTC](https://discourse.cmake.org/t/custom-command-or-post-build-dependent-on-release-configuration/1912/5 "2021-10-29T15:34:39Z")

</div>

Personnaly I like to use:

```cmake
...
COMMAND ${CMAKE_COMMAND} -E $<IF:$<CONFIG:Release>,copy,true> ....

```

here the command will be replaced by cmake command `true` (a no-op) if not in Release config otherwise it will use `cmake -E copy ...`

note: You may need `COMMAND_EXPAND_LISTS`
