CMake Custom Command copy multiple files

前端 未结 4 1576
清歌不尽
清歌不尽 2020-12-17 09:15

I am attempting to copy multiple files using the ${CMAKE_COMMAND} -E copy format, but I was wondering if there was a way to provide a nu

4条回答
  •  被撕碎了的回忆
    2020-12-17 09:57

    A relatively simple workaround would be to use ${CMAKE_COMMAND} -E tar to bundle the sources, move the tarball and extract it in the destination directory.

    This could be more trouble than it's worth if your sources are scattered across many different directories, since extracting would retain the original directory structure (unlike using cp). If all the files are in one directory however, you could achieve the copy in just 2 add_custom_command calls.

    Say your sources to be moved are all in ${CMAKE_SOURCE_DIR}/source_dir, the destination is ${CMAKE_SOURCE_DIR}/destination_dir and your list of filenames (not full paths) are in ${FileList}. You could do:

    add_custom_command(
        TARGET MyExe POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E tar cfj ${CMAKE_BINARY_DIR}/temp.tar ${FileList}
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/source_dir)
    
    add_custom_command(
        TARGET MyExe POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E rename ${CMAKE_BINARY_DIR}/temp.tar temp.tar
        COMMAND ${CMAKE_COMMAND} -E tar xfj temp.tar ${FileList}
        COMMAND ${CMAKE_COMMAND} -E remove temp.tar
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/destination_dir)
    

提交回复
热议问题