Custom command or POST_BUILD dependent on Release configuration?

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 ?

Quoting from add_custom_command() documentation:

Arguments to COMMAND may use generator expressions .
[…]
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.

1 Like

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 ?

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:

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
)
1 Like

Personnaly I like to use:

...
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