cmake: Export targets to package config

Export CMake targets to package config via interface libraries. This
allows the user to simple depends on this project and thereby gets
the additionally dependencies like ZeroMQ automatically.

Signed-off-by: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
This commit is contained in:
Stefan Herbrechtsmeier 2017-04-04 12:44:52 +02:00
parent 1fdf3d1dfe
commit aa850e7ec2
2 changed files with 41 additions and 5 deletions

View File

@ -3,6 +3,10 @@ project(cppzmq)
find_package(ZeroMQ REQUIRED) find_package(ZeroMQ REQUIRED)
if (ZeroMQ_FOUND AND (NOT TARGET libzmq OR NOT TARGET libzmq-static))
message(FATAL_ERROR "ZeroMQ version not supported!")
endif()
set (${PROJECT_NAME}_VERSION ${ZeroMQ_VERSION}) set (${PROJECT_NAME}_VERSION ${ZeroMQ_VERSION})
message(STATUS "cppzmq v${${PROJECT_NAME}_VERSION}") message(STATUS "cppzmq v${${PROJECT_NAME}_VERSION}")
@ -11,21 +15,40 @@ set(CPPZMQ_HEADERS
zmq_addon.hpp zmq_addon.hpp
) )
foreach (target cppzmq cppzmq-static)
add_library(${target} INTERFACE)
target_include_directories(${target} INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>)
endforeach()
target_link_libraries(cppzmq INTERFACE libzmq)
target_link_libraries(cppzmq-static INTERFACE libzmq-static)
include(GNUInstallDirs) include(GNUInstallDirs)
include(CMakePackageConfigHelpers) include(CMakePackageConfigHelpers)
install(TARGETS cppzmq cppzmq-static
EXPORT ${PROJECT_NAME}-targets)
install(FILES ${CPPZMQ_HEADERS} install(FILES ${CPPZMQ_HEADERS}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
# GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share". # GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share".
set(CPPZMQ_CMAKECONFIG_INSTALL_DIR "share/cmake/${PROJECT_NAME}" CACHE STRING "install path for cppzmqConfig.cmake") set(CPPZMQ_CMAKECONFIG_INSTALL_DIR "share/cmake/${PROJECT_NAME}" CACHE STRING "install path for cppzmqConfig.cmake")
if (NOT CMAKE_VERSION VERSION_LESS 3.0)
export(EXPORT ${PROJECT_NAME}-targets
FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake")
endif()
configure_package_config_file(${PROJECT_NAME}Config.cmake.in configure_package_config_file(${PROJECT_NAME}Config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION ${CPPZMQ_CMAKECONFIG_INSTALL_DIR}) INSTALL_DESTINATION ${CPPZMQ_CMAKECONFIG_INSTALL_DIR})
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
VERSION ${${PROJECT_NAME}_VERSION} VERSION ${${PROJECT_NAME}_VERSION}
COMPATIBILITY AnyNewerVersion) COMPATIBILITY AnyNewerVersion)
install(EXPORT ${PROJECT_NAME}-targets
FILE ${PROJECT_NAME}Targets.cmake
DESTINATION ${CPPZMQ_CMAKECONFIG_INSTALL_DIR})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION ${CPPZMQ_CMAKECONFIG_INSTALL_DIR}) DESTINATION ${CPPZMQ_CMAKECONFIG_INSTALL_DIR})

View File

@ -1,4 +1,12 @@
# cppzmq cmake module # cppzmq cmake module
#
# The following import targets are created
#
# ::
#
# cppzmq-static
# cppzmq
#
# This module sets the following variables in your project:: # This module sets the following variables in your project::
# #
# cppzmq_FOUND - true if cppzmq found on the system # cppzmq_FOUND - true if cppzmq found on the system
@ -8,8 +16,13 @@
@PACKAGE_INIT@ @PACKAGE_INIT@
set(PN cppzmq) include(CMakeFindDependencyMacro)
set_and_check(${PN}_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/@CMAKE_INSTALL_INCLUDEDIR@") find_dependency(ZeroMQ)
set_and_check(${PN}_LIBRARY "@ZeroMQ_LIBRARY@")
set_and_check(${PN}_STATIC_LIBRARY "@ZeroMQ_STATIC_LIBRARY@") if(NOT TARGET @PROJECT_NAME@)
check_required_components(${PN}) include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
get_target_property(@PROJECT_NAME@_INCLUDE_DIR cppzmq INTERFACE_INCLUDE_DIRECTORIES)
get_target_property(@PROJECT_NAME@_LIBRARY libzmq LOCATION)
get_target_property(@PROJECT_NAME@_STATIC_LIBRARY libzmq-static LOCATION)
endif()