Skip to content

Commit

Permalink
cmake: Modernize exported CMake config files
Browse files Browse the repository at this point in the history
The CMake configuration files exported by CommonAPI require users to add
the ${COMMONAPI_INCLUDE_DIRS} variable to their project's include
directories. This was done to support building against a build tree of
CommonAPI by generating two different CommonAPIConfig.cmake files that
defined this variable with different values.

This is now no longer necessary since CMake supports the
$<BUILD_INTERFACE> and $<INSTALL_INTERFACE> generator expressions which
can be used to make the same distinction with only a single
CommonAPIConfig.cmake file.

This also allows users of CommonAPI to get the required include
directories into their build by using

| target_link_libraries(${target} CommonAPI)

since the exported CommonAPI CMake target now has the required include
directories set as a property. This is much more idiomatic for users of
CMake and is thus preferred.

Additionally, this enables downstream projects that build CommonAPI
interface libraries to no longer care about what the CommonAPI include
directories are, which simplifies writing CMake config files for them.

Signed-off-by: Clemens Lang <[email protected]>
  • Loading branch information
Clemens Lang committed Feb 11, 2019
1 parent d3e6606 commit 5f0347e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
14 changes: 4 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ list(SORT CAPI_SRCS)
add_library(CommonAPI SHARED ${CAPI_SRCS})
target_link_libraries(CommonAPI PRIVATE ${DL_LIBRARY} ${DLT_LIBRARIES})
set_target_properties(CommonAPI PROPERTIES VERSION ${LIBCOMMONAPI_MAJOR_VERSION}.${LIBCOMMONAPI_MINOR_VERSION}.${LIBCOMMONAPI_PATCH_VERSION} SOVERSION ${LIBCOMMONAPI_MAJOR_VERSION}.${LIBCOMMONAPI_MINOR_VERSION}.${LIBCOMMONAPI_PATCH_VERSION} LINKER_LANGUAGE C)
target_include_directories(CommonAPI INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${INSTALL_INCLUDE_DIR}>)
set_target_properties (CommonAPI PROPERTIES INTERFACE_LINK_LIBRARY "")

##############################################################################
Expand Down Expand Up @@ -167,23 +170,14 @@ export(PACKAGE CommonAPI)
# Create the CommonAPIConfig.cmake and CommonAPIConfigVersion files ...
file(RELATIVE_PATH REL_INCLUDE_DIR "${ABSOLUTE_INSTALL_CMAKE_DIR}" "${ABSOLUTE_INSTALL_INCLUDE_DIR}")

# ... for the build tree
set(CONF_INCLUDE_DIRS "${PROJECT_SOURCE_DIR}/include" )
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/CommonAPIConfig.cmake.in
"${PROJECT_BINARY_DIR}/CommonAPIConfig.cmake" @ONLY)

# ... for the install tree
set(CONF_INCLUDE_DIRS "\${COMMONAPI_CMAKE_DIR}/${REL_INCLUDE_DIR}")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/CommonAPIConfig.cmake.in
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CommonAPIConfig.cmake" @ONLY)

# ... for both
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/CommonAPIConfigVersion.cmake.in
"${PROJECT_BINARY_DIR}/CommonAPIConfigVersion.cmake" @ONLY)

# Install the CommonAPIConfig.cmake and CommonAPIConfigVersion.cmake
install(FILES
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CommonAPIConfig.cmake"
"${PROJECT_BINARY_DIR}/CommonAPIConfig.cmake"
"${PROJECT_BINARY_DIR}/CommonAPIConfigVersion.cmake"
DESTINATION "${INSTALL_CMAKE_DIR}")

Expand Down
10 changes: 7 additions & 3 deletions cmake/CommonAPIConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# Config file for the CommonAPI package
# It defines the following variables
# COMMONAPI_INCLUDE_DIRS - include directories for CommonAPI
# Exports the follwing targets:
# CommonAPI - CMake target for CommonAPI SomeIP
# Additionally, the following variables are defined:
# COMMONAPI_VERSION - The CommonAPI version number

# Compute paths
get_filename_component(COMMONAPI_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(COMMONAPI_INCLUDE_DIRS "@CONF_INCLUDE_DIRS@")

# Our library dependencies (contains definitions for IMPORTED targets)
include("${COMMONAPI_CMAKE_DIR}/CommonAPITargets.cmake")

# Legacy variable, kept for compatibility
get_target_property(COMMONAPI_INCLUDE_DIRS CommonAPI INTERFACE_INCLUDE_DIRECTORIES)

set(COMMONAPI_VERSION @PACKAGE_VERSION@)
set(COMMONAPI_VERSION_STRING "@PACKAGE_VERSION@")

0 comments on commit 5f0347e

Please sign in to comment.