Skip to content

Commit

Permalink
Fix assumption that CMAKE_INSTALL_*DIR paths are relative.
Browse files Browse the repository at this point in the history
This solution uses relative paths if possible, allowing the package to be
relocatable, but still works correctly if CMAKE_INSTALL_*DIR paths are absolute.

Signed-off-by: Ben Wolsieffer <benwolsieffer@gmail.com>
  • Loading branch information
lopsided98 committed Jan 21, 2024
1 parent 424643d commit 6e0cea1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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 (URDF_MAJOR_VERSION 1)
Expand All @@ -33,9 +34,14 @@ if(WIN32 AND NOT CYGWIN)
else()
set(CMAKE_CONFIG_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}/cmake)
endif()
string(REGEX REPLACE "[^/]+" ".." RELATIVE_PATH_CMAKE_DIR_TO_PREFIX "${CMAKE_CONFIG_INSTALL_DIR}")
file(RELATIVE_PATH RELATIVE_PATH_CMAKE_DIR_TO_PREFIX "${CMAKE_CONFIG_INSTALL_DIR}" "${CMAKE_INSTALL_PREFIX}")

set(PACKAGE_NAME ${PROJECT_NAME})
string(REPLACE
"${CMAKE_INSTALL_PREFIX}"
"\${${PROJECT_NAME}_DIR}/${RELATIVE_PATH_CMAKE_DIR_TO_PREFIX}"
cmake_conf_include_dir
"${CMAKE_INSTALL_FULL_INCLUDEDIR}")
set(cmake_conf_file "${PROJECT_NAME}-config.cmake")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/${cmake_conf_file}.in" "${CMAKE_BINARY_DIR}/${cmake_conf_file}" @ONLY)
set(cmake_conf_version_file "${PROJECT_NAME}-config-version.cmake")
Expand All @@ -55,6 +61,7 @@ install(FILES

# Make the package config file
set(PACKAGE_DESC "Unified Robot Description Format")
string(REPLACE "${CMAKE_INSTALL_PREFIX}" "\${prefix}" pkg_conf_includedir "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
set(pkg_conf_file "urdfdom_headers.pc")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/pkgconfig/${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
2 changes: 1 addition & 1 deletion cmake/pkgconfig/urdfdom_headers.pc.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file was generated by CMake for @PROJECT_NAME@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=${prefix}
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
includedir=@pkg_conf_includedir@

Name: @PACKAGE_NAME@
Description: @PACKAGE_DESC@
Expand Down
2 changes: 1 addition & 1 deletion cmake/urdfdom_headers-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ if (@PACKAGE_NAME@_CONFIG_INCLUDED)
endif()
set(@PACKAGE_NAME@_CONFIG_INCLUDED TRUE)

set(@PACKAGE_NAME@_INCLUDE_DIRS "${@PROJECT_NAME@_DIR}/@RELATIVE_PATH_CMAKE_DIR_TO_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@")
set(@PACKAGE_NAME@_INCLUDE_DIRS "@cmake_conf_include_dir@")

include("${@PACKAGE_NAME@_DIR}/@PACKAGE_NAME@Export.cmake")

Expand Down

0 comments on commit 6e0cea1

Please sign in to comment.