[CMake] Populate INCLUDE_DIRECTORIES of object libraries from INTERFACE_INCLUDE_DIRECTORIES of other libraries

Bernhard Burgermeister bbu at burgermeister.de
Tue Nov 14 11:48:33 EST 2017


Hello,

I'm trying to improve parallelization of a massive parallel
Ninja/icecream build by using object libraries. We are already using
interface include directories extensively.

While moving the compilation to separate object libraries I need to
define the include directories for these object targets. Previously
this simply worked by target_link_library(x PRIVATE a) so that x uses
the public and interface include directories of a. With object
libraries this is not possible, for example

======================================
CMAKE_MINIMUM_REQUIRED(VERSION 3.7)
PROJECT(ObjectLibLink C)

ADD_LIBRARY(a INTERFACE)
TARGET_INCLUDE_DIRECTORIES(a INTERFACE include)

ADD_LIBRARY(b OBJECT b.c)
TARGET_LINK_LIBRARIES(b PRIVATE a)
======================================

gives the error message
  Object library target "b" may not link to anything
with CMake 3.7, 3.9 and 3.10rc3. The documentation of the later
version says "OBJECT libraries may not be used in the right hand side
of target_link_libraries()" but does not restrict the left hand side.
I would expect it to build b with include-directory "include" from a.

Currently I'm using a workaround
   FOREACH(Lib ${ARG_LINK})
      IF(TARGET ${Lib})
         TARGET_INCLUDE_DIRECTORIES(MyObjectLib PRIVATE
$<TARGET_PROPERTY:${Lib},INTERFACE_INCLUDE_DIRECTORIES>)
      ENDIF()
   ENDFOREACH()
but this only works for targets previously defined or only target
libraries may be used. Otherwise the generator fails for file names or
system library names because there is no generator expression to check
for existing targets (see
https://gitlab.kitware.com/cmake/cmake/issues/17123). I want to avoid
explicitly import all these libraries.


Is there an other reliable way to populate include_directories that I
have not yet found? Of course compile_definitions and compile_options
should be added from the other libraries too.


Regards,
Bernhard.


More information about the CMake mailing list