From adb54b527386b4eee608abdd81e7cd74a12a1a91 Mon Sep 17 00:00:00 2001 From: Stefan Profanter Date: Tue, 17 Nov 2020 10:10:39 +0100 Subject: [PATCH] fix: Use proper cmake package config for relocatable CMake config The CMake config used absolute paths which doesn't allow to move the installed cmake files. This is the case, e.g., if you create a conan package and distribute the binaries to other users. Fixes: https://github.com/osrf/gazebo/issues/2755 Fixes: https://github.com/osrf/gazebo/issues/2782 --- CMakeLists.txt | 8 +++++++- cmake/gazebo-config.cmake.in | 39 +++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7322874cdc..706d198d69 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -456,9 +456,15 @@ else (build_errors) set(PKG_LIBRARIES ${PKG_LIBRARIES} gazebo_ccd) endif() + include(CMakePackageConfigHelpers) set(cmake_conf_file "cmake/gazebo-config.cmake") set(cmake_conf_version_file "cmake/gazebo-config-version.cmake") - configure_file("${CMAKE_CURRENT_SOURCE_DIR}/${cmake_conf_file}.in" "${CMAKE_CURRENT_BINARY_DIR}/${cmake_conf_file}" @ONLY) + set(DATAROOT_INSTALL_DIR ${CMAKE_INSTALL_DATAROOTDIR}) + set(BASE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_INCLUDEDIR}) + configure_package_config_file("${CMAKE_CURRENT_SOURCE_DIR}/${cmake_conf_file}.in" "${CMAKE_CURRENT_BINARY_DIR}/${cmake_conf_file}" + INSTALL_DESTINATION ${LIB_INSTALL_DIR}/cmake/${PROJECT_NAME_LOWER}/ + PATH_VARS BASE_INSTALL_INCLUDEDIR LIB_INSTALL_DIR DATAROOT_INSTALL_DIR + ) # 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 # TODO: keep this instruction until deprecate Ubuntu/Precise and update with diff --git a/cmake/gazebo-config.cmake.in b/cmake/gazebo-config.cmake.in index 4fa403cfde..2bc8f4e0bc 100644 --- a/cmake/gazebo-config.cmake.in +++ b/cmake/gazebo-config.cmake.in @@ -5,14 +5,21 @@ set(@PKG_NAME@_CONFIG_INCLUDED TRUE) set(@PKG_NAME@_VERSION @GAZEBO_VERSION@) set(@PKG_NAME@_MAJOR_VERSION @GAZEBO_MAJOR_VERSION@) -set(@PKG_NAME@_PLUGIN_PATH "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/gazebo-@GAZEBO_MAJOR_VERSION@/plugins") +@PACKAGE_INIT@ + +# Includeing Dartsim will overwrite the PACKAGE_PREFIX_DIR, therefore make gazebo-specific vars +set(GAZEBO_INSTALL_DATAROOT_DIR @PACKAGE_DATAROOT_INSTALL_DIR@) +set(GAZEBO_INSTALL_LIB_DIR @PACKAGE_LIB_INSTALL_DIR@) +set(GAZEBO_INSTALL_INCLUDE_DIR @PACKAGE_BASE_INSTALL_INCLUDEDIR@) + +set(@PKG_NAME@_PLUGIN_PATH "${GAZEBO_INSTALL_LIB_DIR}/gazebo-@GAZEBO_MAJOR_VERSION@/plugins") # The media path contains the location on disk where images, # materials scripts, shaders, and other related resources are stored. -set(@PKG_NAME@_MEDIA_PATH "@CMAKE_INSTALL_PREFIX@/share/gazebo-@GAZEBO_MAJOR_VERSION@/media") +set(@PKG_NAME@_MEDIA_PATH "${GAZEBO_INSTALL_DATAROOT_DIR}/gazebo-@GAZEBO_MAJOR_VERSION@/media") # The model path contains the location on disk where models are stored. -set(@PKG_NAME@_MODEL_PATH "@CMAKE_INSTALL_PREFIX@/share/gazebo-@GAZEBO_MAJOR_VERSION@/models") +set(@PKG_NAME@_MODEL_PATH "${GAZEBO_INSTALL_DATAROOT_DIR}/gazebo-@GAZEBO_MAJOR_VERSION@/models") # Set whether Gazebo was built with Bullet support set (GAZEBO_HAS_BULLET @HAVE_BULLET@) @@ -72,23 +79,23 @@ include (FindPkgConfig) # target_link_libraries(your_package @PKG_NAME@_PROTO_LIBRARIES) # set(@PKG_NAME@_PROTO_PATH - "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@/gazebo-@GAZEBO_MAJOR_VERSION@/gazebo/msgs/proto") + "${GAZEBO_INSTALL_INCLUDE_DIR}/gazebo-@GAZEBO_MAJOR_VERSION@/gazebo/msgs/proto") find_library(gazebo_proto_msgs_lib gazebo_msgs - PATHS "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@" NO_DEFAULT_PATH) + PATHS "${GAZEBO_INSTALL_LIB_DIR}" NO_DEFAULT_PATH) list(APPEND @PKG_NAME@_PROTO_LIBRARIES ${gazebo_proto_msgs_lib}) list(APPEND @PKG_NAME@_PROTO_INCLUDE_DIRS - "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@/gazebo-@GAZEBO_MAJOR_VERSION@/gazebo/msgs") + "${GAZEBO_INSTALL_INCLUDE_DIR}/gazebo-@GAZEBO_MAJOR_VERSION@/gazebo/msgs") # End @PKG_NAME@_PROTO_PATH, @PKG_NAME@_PROTO_INCLUDE_DIRS, and # @PKG_NAME@_PROTO_LIBRARIES -list(APPEND @PKG_NAME@_INCLUDE_DIRS @CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@) -list(APPEND @PKG_NAME@_INCLUDE_DIRS @CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@/gazebo-@GAZEBO_MAJOR_VERSION@) +list(APPEND @PKG_NAME@_INCLUDE_DIRS ${GAZEBO_INSTALL_INCLUDE_DIR}) +list(APPEND @PKG_NAME@_INCLUDE_DIRS ${GAZEBO_INSTALL_INCLUDE_DIR}/gazebo-@GAZEBO_MAJOR_VERSION@) -list(APPEND @PKG_NAME@_LIBRARY_DIRS @CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@) -list(APPEND @PKG_NAME@_LIBRARY_DIRS @CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/gazebo-@GAZEBO_MAJOR_VERSION@/plugins) +list(APPEND @PKG_NAME@_LIBRARY_DIRS ${GAZEBO_INSTALL_LIB_DIR}) +list(APPEND @PKG_NAME@_LIBRARY_DIRS ${GAZEBO_INSTALL_LIB_DIR}/gazebo-@GAZEBO_MAJOR_VERSION@/plugins) -list(APPEND @PKG_NAME@_CFLAGS -I@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@) -list(APPEND @PKG_NAME@_CFLAGS -I@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@/gazebo-@GAZEBO_MAJOR_VERSION@) +list(APPEND @PKG_NAME@_CFLAGS -I${GAZEBO_INSTALL_INCLUDE_DIR}) +list(APPEND @PKG_NAME@_CFLAGS -I${GAZEBO_INSTALL_INCLUDE_DIR}/gazebo-@GAZEBO_MAJOR_VERSION@) if (GAZEBO_HAS_BULLET) if (PKG_CONFIG_FOUND) @@ -139,7 +146,7 @@ endif () foreach(lib @PKG_LIBRARIES@) set(onelib "${lib}-NOTFOUND") find_library(onelib ${lib} - PATHS "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@" + PATHS "${GAZEBO_INSTALL_LIB_DIR}" NO_DEFAULT_PATH ) if(NOT onelib) @@ -216,6 +223,6 @@ list(APPEND @PKG_NAME@_INCLUDE_DIRS ${IGNITION-FUEL_TOOLS_INCLUDE_DIRS}) list(APPEND @PKG_NAME@_LIBRARY_DIRS ${IGNITION-FUEL_TOOLS_LIBRARY_DIRS}) list(APPEND @PKG_NAME@_LIBRARIES ${IGNITION-FUEL_TOOLS_LIBRARIES}) -list(APPEND @PKG_NAME@_LDFLAGS -Wl,-rpath,@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/gazebo-@GAZEBO_MAJOR_VERSION@/plugins) -list(APPEND @PKG_NAME@_LDFLAGS -L@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@) -list(APPEND @PKG_NAME@_LDFLAGS -L@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/gazebo-@GAZEBO_MAJOR_VERSION@/plugins) +list(APPEND @PKG_NAME@_LDFLAGS -Wl,-rpath,${GAZEBO_INSTALL_LIB_DIR}/gazebo-@GAZEBO_MAJOR_VERSION@/plugins) +list(APPEND @PKG_NAME@_LDFLAGS -L${GAZEBO_INSTALL_LIB_DIR}) +list(APPEND @PKG_NAME@_LDFLAGS -L${GAZEBO_INSTALL_LIB_DIR}/gazebo-@GAZEBO_MAJOR_VERSION@/plugins)