42

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?

2 Answers 2

63

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})
1
  • 1
    If I'm not mistaken this will actually perform two separate globs. Is there a way to make CMake only perform one glob like you can do with extglob in the shell? This does unfortunately not work: file(GLOB_RECURSE files *.{ext1,ext2,ext3})
    – kynan
    Mar 30, 2015 at 12:46
39

I'd construct a list for each of the patterns and then concatenate the lists:

file(GLOB files1 "path/to/files1/*")
file(GLOB files2 "path/to/files2/*")
set(files ${files1} ${files2})
1
  • 2
    This is useful for mixing GLOB with GLOB_RECURSE
    – Tim MB
    Apr 22, 2013 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.