# add\_custom\_command() POST\_BUILD generator expression expansion not working

**URL:** https://discourse.cmake.org/t/add-custom-command-post-build-generator-expression-expansion-not-working/3244
**Category:** Code
**Tags:** gen:ninja
**Created:** [May 1, 2021, 7:51pm UTC](https://discourse.cmake.org/t/add-custom-command-post-build-generator-expression-expansion-not-working/3244 "2021-05-01T19:51:42Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![sthlm58](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@sthlm58](https://discourse.cmake.org/u/sthlm58)
#### Post date: [May 1, 2021, 7:51pm UTC](https://discourse.cmake.org/t/add-custom-command-post-build-generator-expression-expansion-not-working/3244/1 "2021-05-01T19:51:42Z")

</div>

I want to run a POST\_BUILD action after the build (but only in the Debug configuration).

After reading [add\_custom\_command docs](https://cmake.org/cmake/help/latest/command/add_custom_command.html) and a [possible solution](https://discourse.cmake.org/t/custom-command-or-post-build-dependent-on-release-configuration/1912) (here on Discourse) I understood that I can “wrap” my COMMAND into `$<CONFIG:Debug>` generator expression (to be sure it’s “empty” in Release mode).

I tried the following:

```auto
cmake_minimum_required(VERSION 3.18)

project(post-build CXX)
file(WRITE main.cxx "int main() {}")
add_executable(foo main.cxx)

add_custom_command(
  TARGET foo POST_BUILD
  COMMAND $<$<CONFIG:Debug>:${CMAKE_COMMAND} -E echo "hi there from debug build">
)

```

But this gives me the CMake configure-time warnings and a **hard failure during a build-time** (using Ninja generator):

```auto
(...) && "$<1:C:\Program Files\CMake\bin\cmake.exe" -E echo "hi there from debug build" >""
[build] The system cannot find the path specified.
[build] ninja: build stopped: subcommand failed.
[build] Build finished with exit code 1

```

I tried many possible quotes combinations (including escaped quotes):  
` COMMAND $<$<CONFIG:Debug>:"${CMAKE_COMMAND} -E echo \"hi there from debug build\"">`  
and  
` COMMAND "$<$<CONFIG:Debug>:${CMAKE_COMMAND} -E echo \"hi there from debug build\">"`  
etc.

But even though it removed the configure-time warning, it still yields a hard error during the build-time.

**Question** : What would be the correct way to achieve what I want? Is it possible like this or there is a CMake limitation here?

(Note: if possible I’d like to keep the command to be executed as a whole, in one place. I am aware of other workarounds possible, as shown in the post mentioned at the beginning)

---

<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: [May 2, 2021, 10:40am UTC](https://discourse.cmake.org/t/add-custom-command-post-build-generator-expression-expansion-not-working/3244/2 "2021-05-02T10:40:11Z")

</div>

Spaces generally aren’t well-formed inside of genexes. You’ll need to replace the spaces with `;` to make it parse properly (which is why you’re seeing half-expanded remnants in the build command).
