Skip to content

Commit

Permalink
fix: Use proper cmake package config for relocatable CMake config
Browse files Browse the repository at this point in the history
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: gazebosim#2755
Fixes: gazebosim#2782
  • Loading branch information
Pro committed Nov 17, 2020
1 parent a9f093f commit 926fe5a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,14 @@ 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})
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 INCLUDE_INSTALL_DIR 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
Expand Down
39 changes: 23 additions & 16 deletions cmake/gazebo-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -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_PATH, 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_INCLUDE_INSTALL_DIR@)

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@)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

0 comments on commit 926fe5a

Please sign in to comment.