cmakeheader-files

How to propagate FILE_SET of a CMake OBJECT library when linking it into a SHARED library?


Assume I have an OBJECT library and a SHARED library like this:

add_library(objlib OBJECT)

target_sources(objlib
    PUBLIC FILE_SET HEADERS FILES
        foo.h
        bar.h
    PRIVATE
        foo.cpp
        bar.cpp
)

add_library(shrlib SHARED)

target_sources(shrlib
    PUBLIC FILE_SET HEADERS FILES
        baz.h
    PRIVATE
        baz.cpp
)

target_link_libraries(shrlib PRIVATE objlib)

install(TARGETS shrlib
    FILE_SET HEADERS
        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mysubdir
)

This does not install the headers of objlib because it is not installed directly.

I can add objlib's headers manuall to shrlib of course:

get_target_property(OBJLIB_HEADERS objlib HEADER_SET)

target_sources(shrlib
    PUBLIC FILE_SET HEADERS FILES
        ${OBJLIB_HEADERS}
        baz.h
    PRIVATE
        baz.cpp
)

But that seems like an ugly workaround for something that should be done automatically by target_link_libraries. Is there a proper way to do this?


Solution

  • It seems that install command ignores artifacts which a target obtains via propagation from the linked libraries.

    You may install artifacts from objlib target by adding it in the install() invocation:

    install(TARGETS shrlib objlib
        FILE_SET HEADERS
            DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mysubdir
    )
    

    This command will install the library file for shrlib target and the headers from shrlib and objlib targets.

    This command won't install object files, associated with objlib target: those files are installed only with OBJECTS clause, and given command invocation doesn't specify such clause. This behavior is described in the documentation for the command:

    Interface Libraries may be listed among the targets to install. They install no artifacts but will be included in an associated EXPORT. If Object Libraries are listed but given no destination for their object files, they will be exported as Interface Libraries.