1

I would like to add the following command only for the Debug configuration:

if (MSVC)
    add_custom_command(TARGET my_target PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${src} ${dst}) 
endif()

src and dst are just path strings.

I tried to adapt the code using examples I've seen in related answers here, here and here. But non of them worked. Either the specific case is slightly different or they simply execute the same command using a different argument for other configurations. All of my adaptations attempts caused the command to be malformed or simply did not do the trick.

I would like other configurations to not have to add a command at all.

How can this simple task be achieved?

Elad Maimoni
  • 3,703
  • 3
  • 20
  • 37

2 Answers2

1

I like to nullify command using true

add_custom_command(TARGET ${target_name} PRE_BUILD
  COMMAND ${CMAKE_COMMAND} -E $<IF:$<CONFIG:Debug>,copy,true> ${src} ${dst}
  COMMAND_EXPAND_LISTS)

if not in Debug you should have cmake -E true ... i.e. the true will turn the command as no op.

true Do nothing, with an exit code of 0.

src: https://cmake.org/cmake/help/latest/manual/cmake.1.html#run-a-command-line-tool

note: available since 3.16
src: https://cmake.org/cmake/help/latest/release/3.16.html#command-line

Mizux
  • 8,222
  • 7
  • 32
  • 48
0

I managed to get it working using a dummy empty string for configuration other than Debug:

string(
APPEND copy_cmd
"$<IF:$<CONFIG:Debug>,"
"${CMAKE_COMMAND};-E;copy;${pdb_src};${pdb_dst},"
""
">"
)
add_custom_command(TARGET ${target_name} PRE_BUILD COMMAND "${copy_cmd}" COMMAND_EXPAND_LISTS)

Note that the "" string is basically the 'else' of the if. it nullifies the command.

Elad Maimoni
  • 3,703
  • 3
  • 20
  • 37