From 15eb3ea6b437733e7a80567c1e7192107f0b0caa Mon Sep 17 00:00:00 2001 From: Silvio Traversaro Date: Thu, 3 Aug 2017 21:12:59 +0200 Subject: [PATCH] Make CMake configuration files relocatable (#45) See discussion in https://github.com/ros/console_bridge/issues/44 --- CMakeLists.txt | 22 ++++++++++++++++------ console_bridge-config.cmake.in | 23 ++++------------------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8cd7ea6..2d6a1cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,7 @@ generate_export_header(${PROJECT_NAME} EXPORT_MACRO_NAME CONSOLE_BRIDGE_DLLAPI) install(TARGETS ${PROJECT_NAME} + EXPORT ${PROJECT_NAME}-targets ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} @@ -64,10 +65,21 @@ install(DIRECTORY include/ install(FILES ${PROJECT_BINARY_DIR}/${PROJECT_NAME}_export.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +if(WIN32 AND NOT CYGWIN) + set(CMAKE_CONFIG_INSTALL_DIR CMake) +else() + set(CMAKE_CONFIG_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}/cmake/) +endif() + +include(CMakePackageConfigHelpers) set(PKG_NAME ${PROJECT_NAME}) set(PKG_LIBRARIES ${PROJECT_NAME}) set(cmake_conf_file "${PROJECT_NAME}-config.cmake") -configure_file("${cmake_conf_file}.in" "${CMAKE_BINARY_DIR}/${cmake_conf_file}" @ONLY) +configure_package_config_file("${cmake_conf_file}.in" "${CMAKE_BINARY_DIR}/${cmake_conf_file}" + INSTALL_DESTINATION ${CMAKE_CONFIG_INSTALL_DIR} + PATH_VARS CMAKE_INSTALL_FULL_INCLUDEDIR + NO_SET_AND_CHECK_MACRO + NO_CHECK_REQUIRED_COMPONENTS_MACRO) set(cmake_conf_version_file "${PROJECT_NAME}-config-version.cmake") # Use write_basic_package_version_file to generate a ConfigVersion file that # allow users of gazebo to specify the API or version to depend on @@ -79,11 +91,9 @@ write_basic_config_version_file( VERSION "${CONSOLE_BRIDGE_VERSION}" COMPATIBILITY SameMajorVersion) -if(WIN32 AND NOT CYGWIN) - set(CMAKE_CONFIG_INSTALL_DIR CMake) -else() - set(CMAKE_CONFIG_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}/cmake/) -endif() +install(EXPORT ${PROJECT_NAME}-targets + FILE ${PROJECT_NAME}-targets.cmake + DESTINATION ${CMAKE_CONFIG_INSTALL_DIR}) install(FILES "${CMAKE_BINARY_DIR}/${cmake_conf_file}" "${CMAKE_BINARY_DIR}/${cmake_conf_version_file}" diff --git a/console_bridge-config.cmake.in b/console_bridge-config.cmake.in index 9d0763f..dfaa547 100644 --- a/console_bridge-config.cmake.in +++ b/console_bridge-config.cmake.in @@ -3,24 +3,9 @@ if (@PKG_NAME@_CONFIG_INCLUDED) endif() set(@PKG_NAME@_CONFIG_INCLUDED TRUE) -set(@PKG_NAME@_INCLUDE_DIRS "@CMAKE_INSTALL_FULL_INCLUDEDIR@") +@PACKAGE_INIT@ -foreach(lib @PKG_LIBRARIES@) - set(onelib "${lib}-NOTFOUND") - find_library(onelib ${lib} - PATHS "@CMAKE_INSTALL_FULL_LIBDIR@" - NO_DEFAULT_PATH - ) - if(NOT onelib) - message(FATAL_ERROR "Library '${lib}' in package @PKG_NAME@ is not installed properly") - endif() - list(APPEND @PKG_NAME@_LIBRARIES ${onelib}) -endforeach() +set(@PKG_NAME@_INCLUDE_DIRS "@PACKAGE_CMAKE_INSTALL_FULL_INCLUDEDIR@") -foreach(dep @PKG_DEPENDS@) - if(NOT ${dep}_FOUND) - find_package(${dep}) - endif() - list(APPEND @PKG_NAME@_INCLUDE_DIRS ${${dep}_INCLUDE_DIRS}) - list(APPEND @PKG_NAME@_LIBRARIES ${${dep}_LIBRARIES}) -endforeach() +include("${CMAKE_CURRENT_LIST_DIR}/@PKG_NAME@-targets.cmake") +set(@PKG_NAME@_LIBRARIES @PKG_NAME@)