Recursive list of LINK_LIBRARIES in CMake

后端 未结 2 1084
名媛妹妹
名媛妹妹 2020-11-28 10:00

I am trying to acquire a list of the absolute paths to all libraries linked to a specific target in CMake for use in a call to add_custom_command. However,

2条回答
  •  甜味超标
    2020-11-28 10:52

    Recursively traversing LINK_LIBRARY property is possible.

    Here is a get_link_libraries() that does that, however it does not handle all cases (e.g. libraries not being a target, not imported libraries).

    function(get_link_libraries OUTPUT_LIST TARGET)
        get_target_property(IMPORTED ${TARGET} IMPORTED)
        list(APPEND VISITED_TARGETS ${TARGET})
        if (IMPORTED)
            get_target_property(LIBS ${TARGET} INTERFACE_LINK_LIBRARIES)
        else()
            get_target_property(LIBS ${TARGET} LINK_LIBRARIES)
        endif()
        set(LIB_FILES "")
        foreach(LIB ${LIBS})
            if (TARGET ${LIB})
                list(FIND VISITED_TARGETS ${LIB} VISITED)
                if (${VISITED} EQUAL -1)
                    get_target_property(LIB_FILE ${LIB} LOCATION)
                    get_link_libraries(LINK_LIB_FILES ${LIB})
                    list(APPEND LIB_FILES ${LIB_FILE} ${LINK_LIB_FILES})
                endif()
            endif()
        endforeach()
        set(VISITED_TARGETS ${VISITED_TARGETS} PARENT_SCOPE)
        set(${OUTPUT_LIST} ${LIB_FILES} PARENT_SCOPE)
    endfunction()
    

提交回复
热议问题