كيفية نسخ الدليل من شجرة إلى شجرة مصدر الثنائية؟
سؤال
والدليل نسخ من شجرة إلى شجرة مصدر الثنائية. على سبيل المثال: كيف لنسخ شبكة الاتصالات العالمية إلى مجلد بن
work
├─bin
└─src
├─doing
│ └─www
├─include
└─lib
وشكرا.
المحلول
ومع CMake 2.8، استخدم file(COPY ...)
أ > الأمر.
ومع الإصدارات القديمة CMake، وهذا نسخ الكلي الملفات من دليل واحد إلى آخر. إذا كنت لا تريد أن تكون بديلا المتغيرات في الملفات المنسوخة، تغيير حجة configure_fileONLY.
# 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(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
. حتى أفضل، يمكنك إنشاء 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-آذار / 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()
وتحرير: وهذا لا يعمل حقا كما هو متوقع. هذا واحد يعمل لا تشوبه شائبة.
# 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()