バイナリツリーにソースツリーからディレクトリをコピーする方法?
質問
バイナリツリーにソースツリーからディレクトリをコピーします。たとえば、次のようにどのようにbinフォルダにWWWをコピーする
。work
├─bin
└─src
├─doing
│ └─www
├─include
└─lib
感謝します。
解決
CMakeの2.8で、使用 file(COPY ...)
コマンドます。
古いCMakeのバージョンでは、別のディレクトリから、このマクロファイルをコピーします。あなたがコピーされたファイル内の変数を代用したくない場合は、configure_fileの@ONLY引数を変更します。
# 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)
他のヒント
バージョン2.8以来、 fileコマンドには、コピーを持っています引数:
file(COPY yourDir DESTINATION yourDestination)
なおます:
相対入力経路は、電流源に関して評価されます ディレクトリ、および相対宛先に関して評価されます 現在のビルドディレクトリ
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)
誰もカスタムターゲットとしてcmake -E copy_directory
を言及しなかったように、ここで私が使用したものです。
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})
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年3月/ 028299.htmlする
ありがとうございました!それは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/")
セス・ジョンソンからの回答に基づいて、それは私がより多くの便宜のために書いたものです。
# 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:期待通りに実際に動作しません。この1は完璧に動作します。
# 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()