You use the TARGET signature add_custom_command , which means that the commands are executed as part of the TARGET build. In your case, POST_BUILD , which means that the commands will be executed after the MyLib build is MyLib . If MyLib updated and does not need to be rebuilt, the commands will not be executed.
Instead, you can use an output generating signature ( OUTPUT ). Something like that:
set(copiedQmls "") foreach(QmlFile ${QMLS}) get_filename_component(nam ${QmlFile} NAME) add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${nam} COMMAND ${CMAKE_COMMAND} -E copy_if_different ${QmlFile} ${CMAKE_CURRENT_BINARY_DIR} DEPENDS ${QmlFile} COMMENT "Copying ${QmlFile}" VERBATIM ) list(APPEND copiedQmls ${CMAKE_CURRENT_BINARY_DIR}/${nam}) endforeach() add_custom_target( CopyQMLs ALL DEPENDS ${copiedQmls} )
Please note: unfortunately, the OUTPUT add_custom_command argument add_custom_command not support generator expressions, so $<TARGET_FILE_DIR> cannot be used there. I used ${CMAKE_CURRENT_BINARY_DIR} in the above example, you may need to customize it to suit your needs. ${CMAKE_CFG_INTDIR} can be used to specify the directory for each configuration for multi-configuration generators.
Please note that there is an additional custom purpose in my code above. This is necessary because CMake will only execute the user command that produces the output, if something depends on this output. This command does this by listing the outputs in the DEPENDS section.
Angew source share