Skip to content

Commit 1c13e9a

Browse files
committed
Fix assumption that CMAKE_INSTALL_*DIR paths are relative.
This solution uses relative paths if possible, allowing the package to be relocatable, but still works correctly if CMAKE_INSTALL_*DIR paths are absolute.
1 parent 1fd21b6 commit 1c13e9a

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ else()
3535
endif()
3636
string(REGEX REPLACE "[^/]+" ".." RELATIVE_PATH_CMAKE_DIR_TO_PREFIX "${CMAKE_CONFIG_INSTALL_DIR}")
3737

38+
include(cmake/JoinPaths.cmake)
39+
join_paths(cmake_conf_include_dirs
40+
"\${${PROJECT_NAME}_DIR}/${RELATIVE_PATH_CMAKE_DIR_TO_PREFIX}"
41+
"${CMAKE_INSTALL_INCLUDEDIR}")
42+
3843
set(PACKAGE_NAME ${PROJECT_NAME})
3944
set(cmake_conf_file "${PROJECT_NAME}-config.cmake")
4045
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/${cmake_conf_file}.in" "${CMAKE_BINARY_DIR}/${cmake_conf_file}" @ONLY)
@@ -56,6 +61,7 @@ install(FILES
5661
# Make the package config file
5762
set(PACKAGE_DESC "Unified Robot Description Format")
5863
set(pkg_conf_file "urdfdom_headers.pc")
64+
join_paths(pkg_conf_includedir "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}")
5965
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/pkgconfig/${pkg_conf_file}.in" "${CMAKE_BINARY_DIR}/${pkg_conf_file}" @ONLY)
6066
install(FILES "${CMAKE_BINARY_DIR}/${pkg_conf_file}" DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig/ COMPONENT pkgconfig)
6167

cmake/JoinPaths.cmake

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This module provides a function for joining paths
2+
# known from most languages
3+
#
4+
# SPDX-License-Identifier: (MIT OR CC0-1.0)
5+
# Copyright 2020 Jan Tojnar
6+
# https://github.com/jtojnar/cmake-snips
7+
#
8+
# Modelled after Python’s os.path.join
9+
# https://docs.python.org/3.7/library/os.path.html#os.path.join
10+
function(join_paths joined_path first_path_segment)
11+
set(temp_path "${first_path_segment}")
12+
foreach(current_segment IN LISTS ARGN)
13+
if(NOT ("${current_segment}" STREQUAL ""))
14+
if(IS_ABSOLUTE "${current_segment}")
15+
set(temp_path "${current_segment}")
16+
else()
17+
set(temp_path "${temp_path}/${current_segment}")
18+
endif()
19+
endif()
20+
endforeach()
21+
set(${joined_path} "${temp_path}" PARENT_SCOPE)
22+
endfunction()

cmake/pkgconfig/urdfdom_headers.pc.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This file was generated by CMake for @PROJECT_NAME@
22
prefix=@CMAKE_INSTALL_PREFIX@
33
exec_prefix=${prefix}
4-
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
4+
includedir=@pkg_conf_includedir@
55

66
Name: @PACKAGE_NAME@
77
Description: @PACKAGE_DESC@

cmake/urdfdom_headers-config.cmake.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ if (@PACKAGE_NAME@_CONFIG_INCLUDED)
33
endif()
44
set(@PACKAGE_NAME@_CONFIG_INCLUDED TRUE)
55

6-
set(@PACKAGE_NAME@_INCLUDE_DIRS "${@PROJECT_NAME@_DIR}/@RELATIVE_PATH_CMAKE_DIR_TO_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@")
6+
set(@PACKAGE_NAME@_INCLUDE_DIRS "@cmake_conf_include_dirs@")
77

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

0 commit comments

Comments
 (0)