Petter Kvalvaag
Petter Kvalvaag

Reputation: 363

Get all source files a target depends on in 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.

Upvotes: 11

Views: 8185

Answers (1)

Florian
Florian

Reputation: 43078

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()

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()

References

Upvotes: 10

Related Questions