Search code examples
filelistcmakeconcatenationglob

How can I merge multiple lists of files together with CMake?


I have a project built with CMake that needs to copy some resources to the destination folder. Currently I use this code:

file(GLOB files "path/to/files/*")
foreach(file ${files})
    ADD_CUSTOM_COMMAND(
        TARGET MyProject
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy "${file}" "${CMAKE_BINARY_DIR}/Debug"
    )
endforeach()

Now I want to copy more files from a different folder. So we want to copy files from both path/to/files and path/to/files2 to the same place in the binary folder. One way would be to just duplicate the above code, but it seems unnecessary to duplicate the lengthy custom command.

Is there an easy way to use file (and possibly the list command as well) to concatenate two GLOB lists?


Solution

  • The file (GLOB ...) command allows for specifying multiple globbing expressions:

    file (GLOB files "path/to/files/*" "path/to/files2*")
    

    Alternatively, use the list (APPEND ...) sub-command to merge lists, e.g.:

    file (GLOB files "path/to/files/*")
    file (GLOB files2 "path/to/files2*")
    list (APPEND files ${files2})