74

将目录从源代码树复制到二叉树。例如:如何将 www 复制到 bin 文件夹。

work
├─bin
└─src
    ├─doing
    │  └─www
    ├─include
    └─lib

谢谢。

4

7 回答 7

140

从 2.8 版开始,file 命令有一个 copy 参数:

file(COPY yourDir DESTINATION yourDestination)

注意:

相对输入路径是相对于当前源目录评估的,相对目标是相对于当前构建目录评估的

于 2010-02-01T16:37:34.270 回答
45

在 CMake 2.8 中,使用file(COPY ...)命令。

对于较旧的 CMake 版本,此宏将文件从一个目录复制到另一个目录。如果您不想替换复制文件中的变量,请更改 configure_file@ONLY参数(例如更改为COPYONLY)。

# Copy files from source directory to destination directory, substituting any
# variables.  Create destination directory if it does not exist.

macro(configure_files srcDir destDir)
    message(STATUS "Configuring directory ${destDir}")
    make_directory(${destDir})

    file(GLOB templateFiles RELATIVE ${srcDir} ${srcDir}/*)
    foreach(templateFile ${templateFiles})
        set(srcTemplatePath ${srcDir}/${templateFile})
        if(NOT IS_DIRECTORY ${srcTemplatePath})
            message(STATUS "Configuring file ${templateFile}")
            configure_file(
                    ${srcTemplatePath}
                    ${destDir}/${templateFile}
                    @ONLY)
        endif(NOT IS_DIRECTORY ${srcTemplatePath})
    endforeach(templateFile)
endmacro(configure_files)
于 2009-03-30T15:39:56.737 回答
38

由于没有人提到cmake -E copy_directory自定义目标,这就是我使用的:

add_custom_target(copy-runtime-files ALL
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/runtime-files-dir ${CMAKE_BINARY_DIR}/runtime-files-dir
    DEPENDS ${MY_TARGET})
于 2014-12-09T10:50:28.383 回答
27

configure命令仅在cmake运行时复制文件。另一种选择是创建一个新目标,并使用 custom_command 选项。这是我使用的一个(如果您多次运行它,则必须修改该add_custom_target行以使其对每个调用都是唯一的)。

macro(copy_files GLOBPAT DESTINATION)
  file(GLOB COPY_FILES
    RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
    ${GLOBPAT})
  add_custom_target(copy ALL
    COMMENT "Copying files: ${GLOBPAT}")

  foreach(FILENAME ${COPY_FILES})
    set(SRC "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}")
    set(DST "${DESTINATION}/${FILENAME}")

    add_custom_command(
      TARGET copy
      COMMAND ${CMAKE_COMMAND} -E copy ${SRC} ${DST}
      )
  endforeach(FILENAME)
endmacro(copy_files)
于 2009-06-07T21:11:18.227 回答
16

使用 execute_process 并调用 cmake -E。如果你想要一个深拷贝,你可以使用copy_directory命令。更好的是,您可以symlink使用 create_symlink 命令创建一个(如果您的平台支持)。后者可以这样实现:

execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_SOURCE_DIR}/path/to/www
                                                           ${CMAKE_BINARY_DIR}/path/to/www)

来自:http ://www.cmake.org/pipermail/cmake/2009-March/028299.html

于 2009-03-31T01:58:47.417 回答
2

谢谢!这是使用一堆 add_custom_target 和 add_custom_command 的真正有用的建议。我编写了以下函数以在我的项目中随处使用。也是指定安装规则。我主要用它来导出接口头文件。

#
# export file: copy it to the build tree on every build invocation and add rule for installation
#
function    (cm_export_file FILE DEST)
  if    (NOT TARGET export-files)
    add_custom_target(export-files ALL COMMENT "Exporting files into build tree")
  endif (NOT TARGET export-files)
  get_filename_component(FILENAME "${FILE}" NAME)
  add_custom_command(TARGET export-files COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_SOURCE_DIR}/${FILE}" "${CMAKE_CURRENT_BINARY_DIR}/${DEST}/${FILENAME}")
  install(FILES "${FILE}" DESTINATION "${DEST}")
endfunction (cm_export_file)

用法如下所示:

cm_export_file("API/someHeader0.hpp" "include/API/")
cm_export_file("API/someHeader1.hpp" "include/API/")
于 2010-10-08T08:33:40.637 回答
1

根据赛斯约翰逊的回答;为了更方便而写:

# Copy files
macro(resource_files files)
    foreach(file ${files})
        message(STATUS "Copying resource ${file}")
        file(COPY ${file} DESTINATION ${Work_Directory})
    endforeach()
endmacro()

# Copy directories
macro(resource_dirs dirs)
    foreach(dir ${dirs})
        # Replace / at the end of the path (copy dir content VS copy dir)
        string(REGEX REPLACE "/+$" "" dirclean "${dir}")
        message(STATUS "Copying resource ${dirclean}")
        file(COPY ${dirclean} DESTINATION ${Work_Directory})
    endforeach()
endmacro()
于 2017-02-01T16:41:52.097 回答