From c9c993147bbf18d5ec83bae684c5780281e529b4 Mon Sep 17 00:00:00 2001 From: Ben Wolsieffer Date: Mon, 6 Jul 2020 01:00:26 -0400 Subject: [PATCH] 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. --- CMakeLists.txt | 6 ++++++ cmake/JoinPaths.cmake | 22 ++++++++++++++++++++++ cmake/pkgconfig/urdfdom_headers.pc.in | 2 +- cmake/urdfdom_headers-config.cmake.in | 2 +- 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 cmake/JoinPaths.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 25c3704..89447b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -57,6 +62,7 @@ install(FILES if (NOT MSVC) 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) endif() diff --git a/cmake/JoinPaths.cmake b/cmake/JoinPaths.cmake new file mode 100644 index 0000000..bbe9d22 --- /dev/null +++ b/cmake/JoinPaths.cmake @@ -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() diff --git a/cmake/pkgconfig/urdfdom_headers.pc.in b/cmake/pkgconfig/urdfdom_headers.pc.in index 7903698..3abcacf 100644 --- a/cmake/pkgconfig/urdfdom_headers.pc.in +++ b/cmake/pkgconfig/urdfdom_headers.pc.in @@ -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@ diff --git a/cmake/urdfdom_headers-config.cmake.in b/cmake/urdfdom_headers-config.cmake.in index b53e466..f2ae7d1 100644 --- a/cmake/urdfdom_headers-config.cmake.in +++ b/cmake/urdfdom_headers-config.cmake.in @@ -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}/@PACKAGE_NAME@Export.cmake")