我有两个cmake函数来检查资源/着色器文件是否未找到/修改匹配构建。
Check.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函数?
答案 0 :(得分:0)
我认为你正在寻找add_custom_command
add_custom_command(TARGET target
PRE_BUILD | PRE_LINK | POST_BUILD
COMMAND command1 [ARGS] [args1...]
[COMMAND command2 [ARGS] [args2...] ...]
[WORKING_DIRECTORY dir]
[COMMENT comment] [VERBATIM])
这定义了一个与构建指定目标相关联的新命令。当命令发生时,由下列哪一项确定:
PRE_BUILD - 在所有其他依赖项之前运行
PRE_LINK - 在其他依赖项之后运行 POST_BUILD - 在构建目标后运行