一种解决方法是推迟${target}_ASSETS
自定义目标的生成,直到所有依赖项都已通过调用ADD_RESOURCES
.
宏必须将依赖项记录在一个全局变量中,而不是立即将依赖项添加到自定义目标中,该ADD_RESOURCES
变量的名称取决于目标:
macro (ADD_RESOURCES _targetName)
set (_dependencies ${ARGN})
...
# record depencies in a target dependency variable
if (DEFINED ${_targetName}_Dependencies)
list (APPEND ${_targetName}_Dependencies ${_dependencies})
else()
set (${_targetName}_Dependencies ${_dependencies})
endif()
endmacro()
然后添加另一个帮助宏,它通过反射确定所有定义的目标依赖变量,并为每个目标设置一个自定义目标:
macro (SETUP_ASSETS_TARGETS)
get_cmake_property(_vars VARIABLES)
foreach (_var ${_vars})
if (_var MATCHES "(.+)_Dependencies")
set (_targetName ${CMAKE_MATCH_1})
set (_targetDependencies ${${_var}})
message("${_targetName} depends on ${_targetDependencies}")
add_custom_target(${_targetName}_ASSETS DEPENDS ${_targetDependencies})
endif()
endforeach()
endmacro()
在您CMakeLists.txt
通过调用添加所有必要的依赖项时ADD_RESOURCES
,然后调用SETUP_ASSETS_TARGETS
宏以定义所有自定义目标。
ADD_RESOURCES( target1 some/path pattern1 pattern2 )
ADD_RESOURCES( target1 another/path pattern1 )
ADD_RESOURCES( target2 foo/bar pattern1 )
...
...
SETUP_ASSETS_TARGETS()