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?

Calvin
  • 2,872
  • 2
  • 21
  • 31

2 Answers2

62

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})
sakra
  • 62,199
  • 16
  • 168
  • 151
  • 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 '15 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})
antonakos
  • 8,261
  • 2
  • 31
  • 34