7

I have something like this in my project:

add_custom_command(OUTPUT somefile)
add_custom_target(tgt ALL DEPENDS somefile)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/somefile DESTINATION somedir)

This works OK, but my command is being run during make because of ALL keyword in add_custom_target(). What i want is to make CMake to run this command only when make install is issued, not during build.

If i remove ALL keyword, whole target is not being built by default, so somefile is not produced and make install fails.

arrowd
  • 33,231
  • 8
  • 79
  • 110

1 Answers1

10

A possible solution is to have the make install command invoke the make tgt as a side effect. This can be done by using the CODE signature of the install command:

add_custom_command(OUTPUT somefile)
add_custom_target(tgt DEPENDS somefile)

install(CODE "execute_process(COMMAND \"${CMAKE_COMMAND}\" --build \"${CMAKE_CURRENT_BINARY_DIR}\" --target tgt)")
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/somefile DESTINATION somedir)

The execute_process invokes cmake to build the target tgt before somefile is installed.

sakra
  • 62,199
  • 16
  • 168
  • 151
  • Unfortunately, this approach does not work with Ninja on Windows due to: https://github.com/ninja-build/ninja/issues/1132 – Mika Fischer Mar 12 '19 at 09:00