0

I am trying to make a function, addLibrary, that takes any sort of library and will call target_link_libraries on it and then find the path to that library and copy that library into a release folder. I am currently using find_library to find the path to that library. This works fine, except when I call addLibrary on imported targets, such as addLibrary(Qt5::Widgets). This will fail because find_library doesn't work on imported targets. I want to have an if case that checks if the library passed is an imported target. If so, then it has to recursively find what are the INTERFACE_LINK_LIBRARIES of each target, find the location of said target, and then copy that file into my release folder. I found a way that uses INTERFACE_LINK_LIBRARIES and IMPORTED_LOCATION, but it is rather complicated.

Are you aware of a simple way to recursively resolve the linked interfaces with a certain imported target and get the paths to each of these files?

In my use case, I would like to call addLibrary(Qt5::Widgets) and add libQt5Widgets.so, libQt5Gui.so, and libQt5Core.so to my release folder.

JigsawCorp
  • 479
  • 2
  • 6
  • 15
  • You are not the first person who want that list, see that question: https://stackoverflow.com/questions/29400312/displaying-a-targets-list-of-linked-libraries-in-cmake. – Tsyvarev Jul 18 '19 at 18:25
  • @Tsyvarev Thanks for pointing me to a similar question. I guess there isn't a simple way, but I added an answer on this post with a function that does just that. – JigsawCorp Jul 18 '19 at 18:47

1 Answers1

1

Well, I didn't find a simple way, but I found a way:

macro(getAllLinkedLibraries iTarget iReturnValue)
   if(NOT TARGET ${iTarget})
        message(WARNING "${iTarget} is not a target")
    else()
        get_target_property(path ${iTarget} LOCATION)
        if(NOT ${path} IN_LIST ${iReturnValue})
            list(APPEND ${iReturnValue} ${path})
        endif()
        get_target_property(linkedLibrairies ${iTarget} INTERFACE_LINK_LIBRARIES)
        if(NOT "${linkedLibrairies}" STREQUAL "")
            FOREACH(linkedLibrary ${linkedLibrairies})
                getAllLinkedLibraries(${linkedLibrary} ${iReturnValue})
            ENDFOREACH()
        endif()
    endif()
endmacro()

Usage:

find_package(Qt5Widgets REQUIRED)
getAllLinkedLibraries("Qt5::Widgets" someList)
message(STATUS "List = ${someList}")

Output:

-- List = /home/user/Documents/work/project/build/venv/.conan/data/Qt/5.11.3/org/dev/package/80043e232e8ab07f4b25e67652a9490d9ad33d91/lib/libQt5Widgets.so.5.11.3;/home/user/Documents/work/project/build/venv/.conan/data/Qt/5.11.3/org/dev/package/80043e232e8ab07f4b25e67652a9490d9ad33d91/lib/libQt5Gui.so.5.11.3;/home/user/Documents/work/project/build/venv/.conan/data/Qt/5.11.3/org/dev/package/80043e232e8ab07f4b25e67652a9490d9ad33d91/lib/libQt5Core.so.5.11.3

This function will recursively find all linked interfaces to a given imported target and add their paths into a list, including the target.

JigsawCorp
  • 479
  • 2
  • 6
  • 15