Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cmake: add_custom_command argument based on variable content

I'd like to have a Cmake function to copy some binaries to a specific location. Fo this I have the following function definition :

function ( collect_binaries TARGET_NAME DEST_DIR )
   set ( targetsToCopy ${ARGN} )

   set ( copy_cmd "COMMAND ${CMAKE_COMMAND} -E make_directory ${DEST_DIR}\n" )

   foreach ( target ${targetsToCopy} )
      LIST( APPEND copy_cmd "COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${target}> ${DEST_DIR}$<TARGET_FILE_NAME:${target}>\n")
   endforeach( target ${targetsToCopy} )

   #message( FATAL_ERROR ${copy_cmd} )
   add_custom_target( ${TARGET_NAME} )
   add_custom_command( TARGET ${TARGET_NAME} PRE_BUILD ${copy_cmd} )

endfunction( collect_binaries )

And the following usage :

collect_binaries( bin_copy ${PROJECT_BINARY_DIR}/out/ target_1 target_2 target3 )

I have target_1, target_2 and target_3 defined inside my project tree. With this in mind I got the following Cmake configure output :

CMake Warning (dev) at binary_copy.cmake:15 (add_custom_command):

Policy CMP0040 is not set: The target in the TARGET signature of add_custom_command() must exist. Run "cmake --help-policy CMP0040" for policy details. Use the cmake_policy command to set the policy and suppress this warning.

It seems that target in unknown in this context...but it does exist and there is no typo. What is the issue here?

like image 518
Dmitry Avatar asked Nov 13 '15 15:11

Dmitry


1 Answers

You are setting up the copy_cmd variable in the collect_binaries function as a CMake string. The add_custom_command however requires a CMake list to parse the arguments correctly, i.e.:

function ( collect_binaries TARGET_NAME DEST_DIR )
   set ( targetsToCopy ${ARGN} )

   set ( copy_cmd COMMAND ${CMAKE_COMMAND} -E make_directory ${DEST_DIR} )

   foreach ( target ${targetsToCopy} )
      LIST( APPEND copy_cmd COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${target}> "${DEST_DIR}$<TARGET_FILE_NAME:${target}>")
   endforeach( target ${targetsToCopy} )

   #message( FATAL_ERROR ${copy_cmd} )
   add_custom_target( ${TARGET_NAME} )
   add_custom_command( TARGET ${TARGET_NAME} PRE_BUILD ${copy_cmd} )

endfunction( collect_binaries )
like image 100
sakra Avatar answered Nov 10 '22 12:11

sakra