Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cmake: use add_custom_command to copy binary to specific location failed when location doesn't exist

Tags:

cmake

I'm trying to copy all the binaries generated by cmake to a specific directory. I don't want to use EXECUTABLE_OUTPUT_PATH, keeping them in the building tree make development easier.

So I use add_custom_command to add a post build copy

# set the final binary dir
set(PROJECT_BINARY_DIR ${PROJECT_SOURCE_DIR}/bin)

# get name and location of binary, namecan change with os (cf. ".exe" suffix on windows)
GET_TARGET_PROPERTY(EXAMPLE_BIN_NAME example LOCATION)
# copy bin to binary folder
ADD_CUSTOM_COMMAND(TARGET example
          POST_BUILD
          COMMAND ${CMAKE_COMMAND} -E copy ${EXAMPLE_BIN_NAME} ${PROJECT_BINARY_DIR}/.
)

The problem is that at the first build the "bin" folder doesn't exist (the copy fail), but the "bin" folder is created just after. So at the second build the copy works.

Is there a way to make the custom command create the bin folder and then copy the binary ? Or is it possible to have two EXECUTABLE_OUTPUT_PATH with cmake ?

Thanks !

like image 332
hush-hush Avatar asked May 25 '11 06:05

hush-hush


1 Answers

Just make sure that the target location "bin" exists before invoking the copy command, i.e.:

ADD_CUSTOM_COMMAND(TARGET example
          POST_BUILD
          COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}
          COMMAND ${CMAKE_COMMAND} -E copy ${EXAMPLE_BIN_NAME} ${PROJECT_BINARY_DIR}/.
)
like image 63
sakra Avatar answered Oct 14 '22 04:10

sakra