[CMake] Install libraries defined in INTERFACE targets

rozelak at volny.cz rozelak at volny.cz
Fri May 4 07:25:23 EDT 2018


Hello,
I build a project which consists of several "modules" with various mutual dependencies. Each module defines its OBJECT target as:
 
 
    # Module 1:
    add_library(Module1 OBJECT ....)
    target_include_directories(Module1 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
 
    add_library(Module1_LIBRARIES INTERFACE)
    target_link_libraries(Module1_LIBRARIES INTERFACE $<BUILD_INTERFACE:....>)
 
 
    # Module 2:
    add_library(Module2 OBJECT ....)
    target_include_directories(Module2 PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
 
    add_library(Module2_LIBRARIES INTERFACE)
    target_link_libraries(Module2_LIBRARIES INTERFACE $<BUILD_INTERFACE:....> $<BUILD_INTERFACE:Module1_LIBRARIES>)
 
    add_library(Module2_INCLUDES  INTERFACE)
    target_include_directories(Module2_INCLUDES  INTERFACE $<TARGET_PROPERTY:Module2,INCLUDE_DIRECTORIES>)
    target_sources(Module2_INCLUDES  INTERFACE ....)
 
    add_library(Module_CONFFILE  INTERFACE)
    target_sources(Module2_CONFFILE  INTERFACE Module.config)
 
 
And when building a final product (or one of the final products), I use the individual targets:
 
    add_library(Product1 STATIC $<TARGET_OBJECTS:Module1> $<TARGET_OBJECTS:Module2> ...)
    target_link_libraries(Product1 LINK_INTERFACE_LIBRARIES $<BUILD_INTERFACE:Module1_LIBRARIES> $<BUILD_INTERFACE:Module2_LIBRARIES> ...)
 
 
 
It works perfectly, the library is built using all the sources. Even when defining INSTALL, all the properties of the targets (include files, config files, etc.) can be accessed and are installed correctly
 
    install(TARGETS Product1  DESTINATION "./" EXPORT "libProduct1")
    install(FILES $<TARGET_PROPERTY:Module2_CONFFILE,INTERFACE_SOURCES>          DESTINATION "./config")
    install(FILES $<TARGET_PROPERTY:Module2_INCLUDES,INTERFACE_SOURCES>          DESTINATION "./include")
 
    
The problem starts when I want to export a CMake file listing all the libraries which were defined by the individual modules (and are accessed by $<BUILD_INTERFACE:Module2_LIBRARIES>) since they know what they need (how they were configured).
 
When building a static library (which is shown here), I need to export a list of additional libraries necessary to link the Product1 correctly. But using $<BUILD_INTERFACE:Module[]_LIBRARIES> in the INSTALL() command does not work, neither works something like:
 
    install(EXPORT "llibProduct1" DESTINATION "." EXPORT_LINK_INTERFACE_LIBRARIES)
 
 
So the question is: how to export the libraries defined by the individual modules to an CMake file which would be installed together with the Product1 library and header files?
 
 
Thank you very much,
Dan
 
P.S. I use CMake 3.6.2.


More information about the CMake mailing list