【发布时间】:2016-04-22 11:28:39
【问题描述】:
我有两个 cmake 函数来检查资源/着色器文件是否找不到/修改匹配以构建。
检查.cmake
function(Check sourcefile destinationfile)
if(NOT EXISTS ${destinationfile})
execute_process(COMMAND ${CMAKE_COMMAND}
-E copy ${sourcefile} ${destinationfile})
elseif(${sourcefile} IS_NEWER_THAN ${destinationfile})
execute_process(COMMAND ${MSGMERGE_EXECUTABLE}
"--update" ${destinationfile} ${sourcefile}
OUTPUT_QUIET ERROR_VARIABLE error RESULT_VARIABLE ret)
if(ret) # Have to do this hack as msgmerge prints to stderr.
message(SEND_ERROR "${error}")
endif()
endif()
endfunction()
AddResources.cmake
include(./Check.cmake)
function(AddResources project)
file(GLOB_RECURSE resources ${PROJECT_SOURCE_DIR}/src/project1/Resources/*)
file(GLOB_RECURSE shaders ${PROJECT_SOURCE_DIR}/src/project1/Shaders/*)
foreach( each_file1 ${resources} )
get_filename_component(targetFile ${each_file1} NAME)
if(WIN32)
set(destinationfile "${CMAKE_CURRENT_SOURCE_DIR}/bin/Release/${targetFile}")
set(destinationfile2 "${CMAKE_CURRENT_SOURCE_DIR}/bin/Debug/${targetFile}")
set(sourcefile ${each_file1})
Check(${sourcefile} ${destinationfile})
Check(${sourcefile} ${destinationfile2})
else ()
set(destinationfile "${CMAKE_CURRENT_SOURCE_DIR}/bin/${targetFile}")
set(sourcefile ${each_file1})
Check(${sourcefile} ${destinationfile})
endif()
endforeach(each_file1)
foreach( each_file2 ${shaders} )
get_filename_component(targetFile ${each_file2} NAME)
if(WIN32)
set(destinationfile "${CMAKE_CURRENT_SOURCE_DIR}/bin/Release/${targetFile}")
set(destinationfile2 "${CMAKE_CURRENT_SOURCE_DIR}/bin/Debug/${targetFile}")
set(sourcefile ${each_file2})
Check(${sourcefile} ${destinationfile})
Check(${sourcefile} ${destinationfile2})
else()
set(destinationfile "${CMAKE_CURRENT_SOURCE_DIR}/bin/${targetFile}")
set(sourcefile ${each_file2})
Check(${sourcefile} ${destinationfile})
endif()
endforeach(each_file2)
endfunction()
在 CMakeLists.txt 中执行 AddResources(project1)。
AddResources 会将所有资源和着色器复制到 bin 文件夹,但它只复制一次(CMake 配置过程),而不是每次构建时。
有没有办法在每次构建时使用参数执行 CMake 函数?
【问题讨论】:
-
你是需要你的
Check函数在build阶段执行,还是你只是想要@每当source文件更改时,987654326@ 文件就会更新?如果你只想要依赖检查(第二种情况),它甚至可以在 configuration 阶段完成。见,例如this question。顺便说一句,在 source 目录(CMAKE_CURRENT_SOURCE_DIR下)中生成文件通常不好。可能,您想在 build 目录(CMAKE_CURRENT_BINARY_DIR下)中生成它们?
标签: cmake