[CMake] Copy files to build folder

Michael Wild themiwi at gmail.com
Tue Jan 17 09:43:55 EST 2012


On 01/17/2012 03:25 PM, Tim Hutton wrote:
> On 17 January 2012 13:21, David Cole <david.cole at kitware.com> wrote:
>> If you want to use file GLOB at CMake time, then you have to re-run
>> CMake manually when you add or remove a file. There's no way around
>> that, because we are not going to monitor your hard drive for
>> arbitrary file changes on a continuous basis...
> 
> On 17 January 2012 13:22, Rolf Eike Beer <eike at sf-mail.de> wrote:
>>> Thanks Andreas, but that leaves us with having to edit the
>>> CMakeLists.txt every time we add a pattern file. There must be a
>>> better way?
>>
>> How should CMake find out that it needs to update it's file list then?
> 
> It could re-run the FILE(GLOB) on every build? Instead of making a
> dependency on a list of files, I would like to make a dependency on
> the contents of a folder.
> 
> Thanks for all the replies. I understand that the recommended method
> is to have an explicit list of files. And I can see the benefits for
> avoiding slip-ups like adding a file without checking it in.
> 
> What confuses me now is that for packaging we can use the INSTALL command:
> 
> # copy the patterns from the source folder
> install( DIRECTORY "patterns" DESTINATION "." FILES_MATCHING PATTERN "*.vti")
> 
> This does exactly what I want: it finds all the *.vti files from the
> "patterns" folder and subfolders. And yet there's no matching command
> to achieve this in the normal build? Is using the INSTALL command like
> this not recommended?
> 

What you can do is something like this:


copy_patterns.cmake:
#--------------------
foreach(v SRCDIR DSTDIR)
  if(NOT DEFINED ${v})
    message(FATAL_ERROR "${v} not defined on command line")
  endif()
endforeach()

file( GLOB_RECURSE pattern_files RELATIVE
  "${SRCDIR}/" "${SRCDIR}/patterns/*.vti" )
foreach(p IN LISTS pattern_files)
  configure_file(${SRCDIR}/${p}
    ${DSTDIR}/${p} COPYONLY)
endforeach()
#--------------------

CMakeLists.txt:
#--------------
#....
add_custom_target(copy_patterns ALL
  COMMAND ${CMAKE_COMMAND} -DSRCDIR=${CMAKE_CURRENT_SOURCE_DIR}
    -DDSTDIR=${CMAKE_CURRENT_BINARY_DIR}
    -P ${CMAKE_CURRENT_SOURCE_DIR}/copy_patterns.cmake
  COMMENT "Copying pattern files to build tree")
#....
#--------------------

Now since custom targets are always out of date, this rule will always run.

HTH

Michael


More information about the CMake mailing list