cmake 如何将目录从源树复制到二叉树?

rkue9o1l  于 2022-11-11  发布在  其他
关注(0)|答案(7)|浏览(129)

正在将目录从源树复制到二进制树。例如:如何将www复制到bin文件夹。

work
├─bin
└─src
    ├─doing
    │  └─www
    ├─include
    └─lib
  • 谢谢-谢谢
y0u0uwnf

y0u0uwnf1#

自2.8版起,file命令有一个COPY子命令:

file(COPY yourDir DESTINATION yourDestination)

请注意:
相对输入路径是相对于当前源目录计算的,相对目标是相对于当前生成目录计算的

toiithl6

toiithl62#

对于CMake 2.8或更高版本,请使用file(COPY ...)命令。
对于CMake 2.8以下的版本,以下宏将文件从一个目录复制到另一个目录。如果您不想替换复制文件中的变量,请更改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)
r7knjye2

r7knjye23#

由于没有人提到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})
vd2z7a6w

vd2z7a6w4#

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)
ubof19bj

ubof19bj5#

使用execute_process并调用cmake -E。如果需要深层拷贝,可以使用copy_directory命令。更好的是,可以使用create_symlink命令创建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

piwo6bdm

piwo6bdm6#

谢谢!使用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/")
bvjxkvbb

bvjxkvbb7#

基于赛斯·Json的回答;为了更方便起见,我写道:


# 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()

相关问题