50

I've seen that I can use this command in order to copy a directory using cmake:

file(COPY "myDir" DESTINATION "myDestination")

(from this post)

My problem is that I don't want to copy all of myDir, but only the .h files that are in there. I've tried with

file(COPY "myDir/*.h" DESTINATION "myDestination")

but I obtain the following error:

CMake Error at CMakeLists.txt:23 (file):
  file COPY cannot find
  "/full/path/to/myDIR/*.h".

How can I filter the files that I want to copy to a destination folder?

1

4 Answers 4

70

I've found the solution by myself:

file(GLOB MY_PUBLIC_HEADERS
  "myDir/*.h"
)
file(COPY ${MY_PUBLIC_HEADERS} DESTINATION myDestination)
2
  • Also works on copy_if_different in post build step, great!
    – Darkiwi
    Commented Aug 14, 2019 at 8:19
  • 2
    Be careful. Due to a limitation on windows on the number of characters in a single command line, this approach may randomly fail depending on the number of headers that are in the folder. More info here support.microsoft.com/en-gb/help/830473/…
    – dgmz
    Commented Dec 5, 2019 at 10:36
17

this also works for me:

install(DIRECTORY "myDir/" 
        DESTINATION "myDestination" 
        FILES_MATCHING PATTERN "*.h" )
2
  • 1
    This is only generated at install time. So in order to make this command work, you have to build the "install" cmake target.
    – jaques-sam
    Commented Nov 12, 2018 at 15:38
  • 1
    yes you are right. the solution file(COPY <some glob var>) is the good one. Commented Nov 12, 2018 at 15:55
4

The alternative approach provided by jepessen does not take into account the fact that sometimes the number of files to be copied is too high. I encountered the issue when doing such thing (more than 110 files)

Due to a limitation on Windows on the number of characters (2047 or 8191) in a single command line, this approach may randomly fail depending on the number of headers that are in the folder. More info here https://support.microsoft.com/en-gb/help/830473/command-prompt-cmd-exe-command-line-string-limitation

Here is my solution:

file(GLOB MY_HEADERS myDir/*.h)
foreach(CurrentHeaderFile IN LISTS MY_HEADERS)
    add_custom_command(
                TARGET MyTarget PRE_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CurrentHeaderFile} ${myDestination}
                COMMENT "Copying header: ${CurrentHeaderFile}")
endforeach()

This works like a charm on MacOS. However, if you have another target that depends on MyTarget and needs to use these headers, you may have some compile errors due to not found includes on Windows. Therefore you may want to prefer the following option that defines an intermediate target.

function (CopyFile ORIGINAL_TARGET FILE_PATH COPY_OUTPUT_DIRECTORY)
    # Copy to the disk at build time so that when the header file changes, it is detected by the build system.
    set(input ${FILE_PATH})
    get_filename_component(file_name ${FILE_PATH} NAME)
    set(output ${COPY_OUTPUT_DIRECTORY}/${file_name})
    set(copyTarget ${ORIGINAL_TARGET}-${file_name})

    add_custom_target(${copyTarget} DEPENDS ${output})
    add_dependencies(${ORIGINAL_TARGET} ${copyTarget})
    add_custom_command(
            DEPENDS ${input}
            OUTPUT ${output}
            COMMAND ${CMAKE_COMMAND} -E copy_if_different ${input} ${output}
            COMMENT "Copying file to ${output}."
    )
endfunction ()

foreach(HeaderFile IN LISTS MY_HEADERS)
    CopyFile(MyTarget ${HeaderFile} ${myDestination})
endforeach()

The downside indeed is that you end up with multiple target (one per copied file) but they should all end up together (alphabetically) since they start with the same prefix ORIGINAL_TARGET -> "MyTarget"

2
  • How should MyTarget look like? I'm getting add_custom_target called with invalid target name "MyTarget" Target names may not contain a slash. Use ADD_CUSTOM_COMMAND to generate files.
    – TalG
    Commented Aug 3, 2020 at 8:36
  • It should match the name of the target the copy operation depends on. You should have declared your target previously using add_executable(MyTarget ...) or add_library(MyTarget ...)
    – dgmz
    Commented Oct 25, 2020 at 1:58
1

Example of how to copy files of exact pattern to current build directory (for example):

script.cmake

file(COPY ${CMAKE_CURRENT_LIST_DIR}/mydir/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR} FILES_MATCHING PATTERN "*.h")

cmakelists.txt

add_custom_target(copy_target COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_LIST_DIR}/script.cmake)
add_dependencies(target copy_target)

Your Answer

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

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