cmake - 以编程方式获取 CMake 项目中的所有目标
问题描述
我想让一个特定的目标依赖于我项目中所有其他添加的目标。换一种说法——我希望这个目标(比如lint
)在所有库和应用程序构建完成后运行。
CMakeLists.txt
在指定 的顶部文件中是否有办法获取使用添加的目录中其他文件project
添加的所有目标的列表?然后我可以使用来指定顺序。有一个属性,但它仅适用于目录级别。CMakeLists.txt
add_subdirectory
add_dependencies
BUILDSYSTEM_TARGETS
如果有其他方法可以实现这一点,请告诉我。我使用 CMake 3.14。
解决方案
为了将来的参考,我最终编写了自己的函数而不是宏。但这个概念与@thomas_f 接受的答案相同。
这是代码:
# Collect all currently added targets in all subdirectories
#
# Parameters:
# - _result the list containing all found targets
# - _dir root directory to start looking from
function(get_all_targets _result _dir)
get_property(_subdirs DIRECTORY "${_dir}" PROPERTY SUBDIRECTORIES)
foreach(_subdir IN LISTS _subdirs)
get_all_targets(${_result} "${_subdir}")
endforeach()
get_directory_property(_sub_targets DIRECTORY "${_dir}" BUILDSYSTEM_TARGETS)
set(${_result} ${${_result}} ${_sub_targets} PARENT_SCOPE)
endfunction()
推荐阅读
- css - 如何正确包含带有 @import 声明的 CSS 文件并同时使用捆绑。
- deployment - 在不同的端口上部署 Web 应用程序 (JBoss/Wildfly)
- javascript - Web 应用程序无法在蜂窝数据中访问,但在 wifi 上可访问
- javascript - 异步等待和 setTimeout 在 ReactJS 中不起作用
- hbase - HBase 根据上次更新时间按排序顺序获取数据
- python - 在 Django 中生成条形码
- nlp - 如何从嵌入向量中获取单词?
- angular - AngularFire2 app.auth 不是函数
- c# - 为什么其他语句不起作用/执行?C#
- excel - 用“” Excel VBA查找并替换任何字母和后面的所有内容