# # # # 使用方法: 在某个target编译完后用costom_command来调用此脚本 # # add_custom_command( # TARGET ${TG_APP} # POST_BUILD # # COMMAND "${CMAKE_COMMAND}" # "-DSRC_DIR=${PROJECT_SOURCE_DIR}/assets" # "-DDST_DIR=$/assets" # -P "${PROJECT_SOURCE_DIR}/cmake/copy_assets_to_output.cmake" # # COMMENT "Copying assets..." # VERBATIM # ) # # # # # 若希望把 assets 内部的内容直接复制到 exe 目录,不保留最外层 assets,命令改成 # # "-DDST_DIR=$" # # # # # if(NOT DEFINED SRC_DIR) message(FATAL_ERROR "SRC_DIR is not defined") endif() if(NOT DEFINED DST_DIR) message(FATAL_ERROR "DST_DIR is not defined") endif() if(NOT EXISTS "${SRC_DIR}") message(FATAL_ERROR "Source directory does not exist: ${SRC_DIR}") endif() # 创建目标根目录 file(MAKE_DIRECTORY "${DST_DIR}") # 获取所有文件和目录。 # RELATIVE 确保获得相对于 assets 的路径,以便保留目录结构。 file( GLOB_RECURSE entries LIST_DIRECTORIES true RELATIVE "${SRC_DIR}" "${SRC_DIR}/*" ) foreach(relative_path IN LISTS entries) set(source_path "${SRC_DIR}/${relative_path}") set(target_path "${DST_DIR}/${relative_path}") if(IS_DIRECTORY "${source_path}") # 保留目录,包括空目录 file(MAKE_DIRECTORY "${target_path}") else() # 确保文件的父目录存在 get_filename_component(target_parent "${target_path}" DIRECTORY) file(MAKE_DIRECTORY "${target_parent}") # 内容相同时不覆盖 file( COPY_FILE "${source_path}" "${target_path}" ONLY_IF_DIFFERENT ) endif() endforeach()