CMake:如何只将特定的文件扩展名从一个目录复制到另一个目录 [英] CMake: How to copy only specific file extensions from one directory into another
问题描述
我使用以下自定义命令将 config
目录中的所有文件复制到 build
目录中。问题是我不想复制 .svn
目录。我正在寻找一种方法,要么排除 .svn
目录或复制具有特定扩展名的文件。例如我只想要复制 xml
或 conf
扩展名的文件。我应该怎么办?
I am using the following custom command to copy all the files within the config
directory into the build
directory. The problem is that I don't want the .svn
directory to be copied as well. I am looking for a way to either exclude the .svn
directory or to copy files with specific extension. e.g. I want only files with xml
or conf
extensions to be copied. What should I do?
add_custom_command(TARGET MyTarget PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/config $<TARGET_FILE_DIR:MyTarget>)
推荐答案
要仅复制 .xml
和 .conf
文件,可以使用 文件(GLOB ...)
命令:
To copy just the .xml
and .conf
files, you can use the file(GLOB ...)
command:
# Gather list of all .xml and .conf files in "/config"
file(GLOB ConfigFiles ${CMAKE_SOURCE_DIR}/config/*.xml
${CMAKE_SOURCE_DIR}/config/*.conf)
foreach(ConfigFile ${ConfigFiles})
add_custom_command(TARGET MyTarget PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E
copy ${ConfigFile} $<TARGET_FILE_DIR:MyTarget>)
endforeach()
这是一个类似的过程,以获取所有文件不在 .svn
子目录:
It's a similar process to get all files not in the .svn
subdirectory:
# Gather list of all files in "/config"
file(GLOB ConfigFiles RELATIVE ${CMAKE_SOURCE_DIR}/config
${CMAKE_SOURCE_DIR}/config/*)
# Gather list of all files in "/config/.svn"
file(GLOB SvnConfigFiles RELATIVE ${CMAKE_SOURCE_DIR}/config
${CMAKE_SOURCE_DIR}/config/.svn/*)
# Remove ".svn" and its contents from the list
list(REMOVE_ITEM ConfigFiles .svn ${SvnConfigFiles})
foreach(ConfigFile ${ConfigFiles})
add_custom_command(TARGET MyTarget PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E
copy ${CMAKE_SOURCE_DIR}/config/${ConfigFile}
$<TARGET_FILE_DIR:MyTarget>)
endforeach()
这篇关于CMake:如何只将特定的文件扩展名从一个目录复制到另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!