Copying files with destination folder depending on build config

In CMake 3.26 there is now copy_directory_if_different.

Instead of using POST_BUILD which only runs when the target is built; I think a custom target may be more appropriate. Since your data can change without your program having to build.

add_custom_target(copy_data
	COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different 
        "${CMAKE_CURRENT_SOURCE_DIR}/datadir/"
        "$<TARGET_FILE_DIR:mytarget>/datadir"
    COMMENT "Copying data"
)
add_dependencies(mytarget copy_data)
2 Likes