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.
  • Loading branch information
lopsided98 committed Sep 10, 2023
1 parent 1fd21b6 commit 1c13e9a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ else()
endif()
string(REGEX REPLACE "[^/]+" ".." RELATIVE_PATH_CMAKE_DIR_TO_PREFIX "${CMAKE_CONFIG_INSTALL_DIR}")

include(cmake/JoinPaths.cmake)
join_paths(cmake_conf_include_dirs
"\${${PROJECT_NAME}_DIR}/${RELATIVE_PATH_CMAKE_DIR_TO_PREFIX}"
"${CMAKE_INSTALL_INCLUDEDIR}")

set(PACKAGE_NAME ${PROJECT_NAME})
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)
Expand All @@ -56,6 +61,7 @@ install(FILES
# Make the package config file
set(PACKAGE_DESC "Unified Robot Description Format")
set(pkg_conf_file "urdfdom_headers.pc")
join_paths(pkg_conf_includedir "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}")
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
22 changes: 22 additions & 0 deletions cmake/JoinPaths.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This module provides a function for joining paths
# known from most languages
#
# SPDX-License-Identifier: (MIT OR CC0-1.0)
# Copyright 2020 Jan Tojnar
# https://github.com/jtojnar/cmake-snips
#
# Modelled after Python’s os.path.join
# https://docs.python.org/3.7/library/os.path.html#os.path.join
function(join_paths joined_path first_path_segment)
set(temp_path "${first_path_segment}")
foreach(current_segment IN LISTS ARGN)
if(NOT ("${current_segment}" STREQUAL ""))
if(IS_ABSOLUTE "${current_segment}")
set(temp_path "${current_segment}")
else()
set(temp_path "${temp_path}/${current_segment}")
endif()
endif()
endforeach()
set(${joined_path} "${temp_path}" PARENT_SCOPE)
endfunction()
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_dirs@")

include("${@PACKAGE_NAME@_DIR}/@[email protected]")

Expand Down

0 comments on commit 1c13e9a

Please sign in to comment.