Skip to content

Commit

Permalink
Fix assumptions that CMAKE_INSTALL_*DIR paths are relative.
Browse files Browse the repository at this point in the history
When an absolute path is desired, the CMAKE_INSTALL_FULL_*DIR variables should
be used instead of concatenating with CMAKE_INSTALL_PREFIX. Special handling is
also needed for pkg-config and CMake config files to retain relative paths if
the lib/include directories are subdirectories of the install prefix.
  • Loading branch information
lopsided98 committed Sep 10, 2023
1 parent 6d3c5de commit 244edd2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
18 changes: 14 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ option(APPEND_PROJECT_NAME_TO_INCLUDEDIR

if(APPEND_PROJECT_NAME_TO_INCLUDEDIR)
set(CMAKE_INSTALL_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}")
set(CMAKE_INSTALL_FULL_INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}/${PROJECT_NAME}")
endif()

# set the default build type
Expand Down Expand Up @@ -67,19 +68,25 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}"
enable_testing()

if(WIN32 AND NOT CYGWIN)
set(CMAKE_CONFIG_INSTALL_DIR CMake)
set(CMAKE_CONFIG_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/CMake")
else()
set(CMAKE_CONFIG_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}/cmake)
set(CMAKE_CONFIG_INSTALL_DIR "${CMAKE_INSTALL_FULL_LIBDIR}/${PROJECT_NAME}/cmake")
endif()
string(REGEX REPLACE "[^/]+" ".." RELATIVE_PATH_CMAKE_DIR_TO_PREFIX "${CMAKE_CONFIG_INSTALL_DIR}")
string(REGEX REPLACE "[^/]+" ".." RELATIVE_PATH_LIBDIR_TO_PREFIX "${CMAKE_INSTALL_LIBDIR}")
file(RELATIVE_PATH RELATIVE_PATH_CMAKE_DIR_TO_PREFIX "${CMAKE_CONFIG_INSTALL_DIR}" "${CMAKE_INSTALL_PREFIX}")
file(RELATIVE_PATH RELATIVE_PATH_LIBDIR_TO_PREFIX "${CMAKE_INSTALL_FULL_LIBDIR}" "${CMAKE_INSTALL_PREFIX}")

add_subdirectory(urdf_parser)

set(PKG_NAME ${PROJECT_NAME})
set(PKG_LIBRARIES urdfdom_sensor urdfdom_model_state urdfdom_model urdfdom_world)
set(PKG_DEPENDS urdfdom_headers console_bridge)
set(PKG_EXPORTS urdfdom)
string(REPLACE
"${CMAKE_INSTALL_PREFIX}" "\${${PROJECT_NAME}_DIR}/${RELATIVE_PATH_CMAKE_DIR_TO_PREFIX}"
CMAKE_CONF_LIBDIR "${CMAKE_INSTALL_FULL_LIBDIR}")
string(REPLACE
"${CMAKE_INSTALL_PREFIX}" "\${${PROJECT_NAME}_DIR}/${RELATIVE_PATH_CMAKE_DIR_TO_PREFIX}"
CMAKE_CONF_INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
set(cmake_conf_file "cmake/urdfdom-config.cmake")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/${cmake_conf_file}.in" "${CMAKE_BINARY_DIR}/${cmake_conf_file}" @ONLY)
install(FILES package.xml DESTINATION share/${PROJECT_NAME})
Expand All @@ -90,7 +97,10 @@ install(FILES ${CMAKE_BINARY_DIR}/${cmake_conf_file}
set(PKG_DESC "Unified Robot Description Format")
set(PKG_DEPENDS "urdfdom_headers console_bridge") # make the list separated by spaces instead of ;
set(PKG_URDF_LIBS "-lurdfdom_sensor -lurdfdom_model_state -lurdfdom_model -lurdfdom_world")
string(REPLACE "${CMAKE_INSTALL_PREFIX}" "\${prefix}" PKG_CONF_LIBDIR "${CMAKE_INSTALL_FULL_LIBDIR}")
string(REPLACE "${CMAKE_INSTALL_PREFIX}" "\${prefix}" PKG_CONF_INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
set(pkg_conf_file "cmake/pkgconfig/urdfdom.pc")

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/${pkg_conf_file}.in" "${CMAKE_BINARY_DIR}/${pkg_conf_file}" @ONLY)
install(FILES ${CMAKE_BINARY_DIR}/${pkg_conf_file}
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig/ COMPONENT pkgconfig)
Expand Down
4 changes: 2 additions & 2 deletions cmake/pkgconfig/urdfdom.pc.in
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# This file was generated by CMake for @PROJECT_NAME@
prefix=${pcfiledir}/../@RELATIVE_PATH_LIBDIR_TO_PREFIX@
exec_prefix=${prefix}
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
libdir=@PKG_CONF_LIBDIR@
includedir=@PKG_CONF_INCLUDEDIR@

Name: @PKG_NAME@
Description: @PKG_DESC@
Expand Down
6 changes: 3 additions & 3 deletions cmake/urdfdom-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ if (@PKG_NAME@_CONFIG_INCLUDED)
endif()
set(@PKG_NAME@_CONFIG_INCLUDED TRUE)

set(@PKG_NAME@_INCLUDE_DIRS "${@PROJECT_NAME@_DIR}/@RELATIVE_PATH_CMAKE_DIR_TO_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@" "@TinyXML_INCLUDE_DIRS@")
set(@PKG_NAME@_INCLUDE_DIRS "@CMAKE_CONF_INCLUDEDIR@" "@TinyXML_INCLUDE_DIRS@")

foreach(lib @PKG_LIBRARIES@)
set(onelib "${lib}-NOTFOUND")
set(onelibd "${lib}-NOTFOUND")
find_library(onelib ${lib}
PATHS "${@PROJECT_NAME@_DIR}/@RELATIVE_PATH_CMAKE_DIR_TO_PREFIX@/@CMAKE_INSTALL_LIBDIR@"
PATHS "@CMAKE_CONF_LIBDIR@"
NO_DEFAULT_PATH)
find_library(onelibd ${lib}d
PATHS "${@PROJECT_NAME@_DIR}/@RELATIVE_PATH_CMAKE_DIR_TO_PREFIX@/@CMAKE_INSTALL_LIBDIR@"
PATHS "@CMAKE_CONF_LIBDIR@"
NO_DEFAULT_PATH)
if(onelib-NOTFOUND AND onelibd-NOTFOUND)
message(FATAL_ERROR "Library '${lib}' in package @PKG_NAME@ is not installed properly")
Expand Down

0 comments on commit 244edd2

Please sign in to comment.