How to copy directory from source tree to binary t

2020-02-02 06:30发布

Copying directory from source tree to binary tree. For example: How to copy www to bin folder.

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

Thanks.

标签: c copy cmake
7条回答
不美不萌又怎样
2楼-- · 2020-02-02 06:46

As nobody has mentioned cmake -E copy_directory as a custom target, here's what I've used:

add_custom_target(copy-runtime-files ALL
    COMMAND cmake -E copy_directory ${CMAKE_SOURCE_DIR}/runtime-files-dir ${CMAKE_BINARY_DIR}/runtime-files-dir
    DEPENDS ${MY_TARGET})
查看更多
\"骚年 ilove
3楼-- · 2020-02-02 06:49

Use execute_process and call cmake -E. If you want a deep copy, you can use the copy_directory command. Even better, you could create a symlink (if your platform supports it) with the create_symlink command. The latter can be achieved like this:

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

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

查看更多
老娘就宠你
4楼-- · 2020-02-02 06:50

Thank! That is really helpful advice to use bunch of add_custom_target and add_custom_command. I wrote the following function to use everywhere in my projects. Is also specifies the installation rule. I use it primarily to export interface header files.

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

Usage looks like this:

cm_export_file("API/someHeader0.hpp" "include/API/")
cm_export_file("API/someHeader1.hpp" "include/API/")
查看更多
甜甜的少女心
5楼-- · 2020-02-02 06:52

Based on the answer from Seth Johnson, that's what I wrote for more convenience.

# Always define the target
add_custom_target(copy_resources ALL COMMENT "Copying resources…")

# Copy single files
macro(add_files_to_environment files)
    add_custom_command(TARGET copy_resources POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy ${ARGV} ${CMAKE_CURRENT_BINARY_DIR})
endmacro()

# Copy full directories
macro(add_directory_to_environment distant local_name)
    file(GLOB_RECURSE DistantFiles
        RELATIVE ${distant}
        ${distant}/*)
    foreach(Filename ${DistantFiles})
        set(SRC "${distant}/${Filename}")
        set(DST "${CURRENT_BUILD_DIR}/${local_name}/${Filename}")
        add_custom_command(TARGET copy_resources POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy ${SRC} ${DST})

        message(STATUS "file ${Filename}")
    endforeach(Filename)
endmacro()

EDIT : That doesn't really work as expected. This one works flawlessly.

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

# Copy full 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()
查看更多
Explosion°爆炸
6楼-- · 2020-02-02 07:04

With CMake 2.8, use the file(COPY ...) command.

With older CMake versions, this macro copies files from one directory to another. If you don't want to substitute variables in the copied files, then change the configure_file @ONLY argument (for example to 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)
查看更多
冷血范
7楼-- · 2020-02-02 07:06

Since version 2.8, the file command has a copy argument:

file(COPY yourDir DESTINATION yourDestination)

Note that:

Relative input paths are evaluated with respect to the current source directory, and a relative destination is evaluated with respect to the current build directory

查看更多
登录 后发表回答