Simplify installing of associated include directories
I propose adding an ASSOCIATED
argument to target_include_directories()
that adds the directories to a set of include directories associated with the
library. When the library is installed, the contents of all associated include
directories is installed to the INCLUDES DESTINATION
, which also should have
a reasonable default. Also, INCLUDES
needs to support COMPONENT
.
TL;DR
A simple library with some include directory as a usage requirement is defined like this:
add_library(mylib ...)
target_include_directories(mylib
PUBLIC
# This is a relative path.
# CMake will prefix it with the current source dir.
include
)
But when I want to export the library, I have to add some boilerplate:
add_library(mylib ...)
target_include_directories(mylib
PUBLIC
# This is an expression.
# CMake will *not* prefix it with the current source dir.
# We have to use ${CMAKE_CURRENT_SOURCE_DIR}
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
# We need to make sure that the value matches the actual install location.
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}-${PROJECT_VERSION}>
)
install(DIRECTORY "include/" # Terminate with / to install the content.
COMPONENT "Develop"
# Must match the INSTALL_INTERFACE of include directories, see above.
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}-${PROJECT_VERSION}"
)
install(TARGETS mylib EXPORT MY_TARGETS
ARCHIVE
COMPONENT "Develop"
DESTINATION "${CMAKE_INSTALL_LIBDIR}" # No longer needed in 3.14!
LIBRARY
COMPONENT "Runtime"
NAMELINK_COMPONENT "Develop"
DESTINATION "${CMAKE_INSTALL_LIBDIR}" # No longer needed in 3.14!
RUNTIME
COMPONENT "Runtime"
DESTINATION "${CMAKE_INSTALL_BINDIR}" # No longer needed in 3.14!
)
With support for ASSOCIATED
, client code will look like this:
add_library(mylib ...)
target_include_directories(mylib
ASSOCIATED PUBLIC
# This is a relative path.
# CMake will prefix it with the current source dir.
include
)
install(TARGETS mylib EXPORT MY_TARGETS
ARCHIVE COMPONENT "Develop"
LIBRARY COMPONENT "Runtime" NAMELINK_COMPONENT "Develop"
RUNTIME COMPONENT "Runtime"
INCLUDES COMPONENT "Develop"
)
Edited by Daniel Pfeifer