获取目标依赖于CMake的所有源文件 [英] Get all source files a target depends on in CMake

查看:158
本文介绍了获取目标依赖于CMake的所有源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用CMake,我如何获得进入可执行目标的所有源文件的列表,包括该可执行文件所依赖的所有目标中的所有源?

With CMake, how can I get a list of all the source files which go into an executable target, including all sources in all targets this executable depends on?

我们在代码库中有一个模式,其中构建系统根据文件名和源树中的路径生成初始化程序调用程序。因此,我需要可执行目标依赖的所有源文件的完整路径(或相对于源根目录)。

We have a pattern in the code base where initializer callers are generated by the build system based on file names and paths in the source tree. So I need the full path (or relative to source root) to all source files an executable target depends on.

推荐答案

我的代码来获取一个目标的链接依赖性:

Here is my piece of code to get one target's link dependencies:

function(target_link_libraries _target)
    set(_mode "PUBLIC")
    foreach(_arg IN LISTS ARGN)
        if (_arg MATCHES "INTERFACE|PUBLIC|PRIVATE|LINK_PRIVATE|LINK_PUBLIC|LINK_INTERFACE_LIBRARIES")
            set(_mode "${_arg}")
        else()
            if (NOT _arg MATCHES "debug|optimized|general")
                set_property(GLOBAL APPEND PROPERTY GlobalTargetDepends${_target} ${_arg})
            endif()
        endif()
    endforeach()
    _target_link_libraries(${_target} ${ARGN})
endfunction()

function(get_link_dependencies _target _listvar)
    set(_worklist ${${_listvar}})
    if (TARGET ${_target})
        list(APPEND _worklist ${_target})
        get_property(_dependencies GLOBAL PROPERTY GlobalTargetDepends${_target})
        foreach(_dependency IN LISTS _dependencies)
            if (NOT _dependency IN_LIST _worklist)
                get_link_dependencies(${_dependency} _worklist)
            endif()
        endforeach()
        set(${_listvar} "${_worklist}" PARENT_SCOPE)
    endif()
endfunction()

对于较旧的CMake版本(3.4之前的版本),您需要替换 IN_LIST list(FIND ...)进行检查:

For older CMake versions (prior to 3.4), you will need to replace the IN_LIST check with a list(FIND ...) call:

[...]
        list(FIND _worklist ${_dependency} _idx)
        if (${_idx} EQUAL -1)
            get_link_dependencies(${_dependency} _worklist)
        endif()
[...]

这是我使用的测试代码:

And here is the test code I've used:

cmake_minimum_required(VERSION 3.4)

project(GetSources)

cmake_policy(SET CMP0057 NEW)

[... include functions posted above ...]

file(WRITE a.cc "")
add_library(A STATIC a.cc)

file(WRITE b.cc "")
add_library(B STATIC b.cc)

file(WRITE main.cc "int main() { return 0; }")
add_executable(${PROJECT_NAME} main.cc)

target_link_libraries(B A)
target_link_libraries(${PROJECT_NAME} B)

get_link_dependencies(${PROJECT_NAME} _deps)
foreach(_dep IN LISTS _deps)
    get_target_property(_srcs ${_dep} SOURCES)
    get_target_property(_src_dir ${_dep} SOURCE_DIR)
    foreach(_src IN LISTS _srcs)
        message("${_src_dir}/${_src}")
    endforeach()
endforeach()

参考

  • Recursive list of LINK_LIBRARIES in CMake

这篇关于获取目标依赖于CMake的所有源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆