バージョン 2.8 以降、file コマンドには copy 引数があります:
file(COPY yourDir DESTINATION yourDestination)
次の点に注意してください:
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)
configure
コマンドは cmake
の場合にのみファイルをコピーします 実行されます。もう 1 つのオプションは、新しいターゲットを作成し、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)