Skip to content

Commit

Permalink
Don't assume CMAKE_INSTALL_*DIR variables are relative (#305)
Browse files Browse the repository at this point in the history
* Don't assume CMAKE_INSTALL_*DIR variables are relative

The CMAKE_INSTALL_*DIR variables are allowed to be absolute paths, so they
cannot be simply appended to CMAKE_INSTALL_PREFIX. When an absolute path is
needed, CMAKE_INSTALL_FULL_*DIR must be used.

Additionally, pkgconfig files should use ${prefix} relative paths if
CMAKE_INSTALL_*DIR are relative, but otherwise use absolute paths. This is
handled by the custom join_paths() function.

Note that CMake 3.20 provides cmake_path(APPEND) which implements the same
functionality as join_paths(), but this CMake version is not available in all
supported distros yet (notably Ubuntu older than 22.04).

Signed-off-by: Ben Wolsieffer <benwolsieffer@gmail.com>
Co-authored-by: Jose Luis Rivero <jrivero@osrfoundation.org>
  • Loading branch information
lopsided98 and j-rivero authored Aug 31, 2022
1 parent 106fd41 commit 4283fc6
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ set(ign_version_output "${PROJECT_NAME_LOWER}-config-version.cmake")
set(ign_config_install_dir "${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME_LOWER}")
set(ign_pkgconfig_input "${CMAKE_CURRENT_SOURCE_DIR}/config/ignition-cmake.pc.in")
set(ign_pkgconfig_output "${CMAKE_BINARY_DIR}/ignition-cmake${PROJECT_VERSION_MAJOR}.pc")
set(ign_pkgconfig_install_dir "${IGN_LIB_INSTALL_DIR}/pkgconfig")
set(ign_pkgconfig_install_dir "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig")
set(ign_utilities_target ${PROJECT_EXPORT_NAME}-utilities)
set(ign_utilities_import_target_name ${PROJECT_EXPORT_NAME}::${ign_utilities_target})
set(ign_utilities_target_output_filename "${ign_utilities_target}-targets.cmake")
Expand Down Expand Up @@ -96,9 +96,17 @@ install(
# Configure and install the pkgconfig file (needed for utilities headers)
file(RELATIVE_PATH
IGN_PC_CONFIG_RELATIVE_PATH_TO_PREFIX
"${CMAKE_INSTALL_PREFIX}/${ign_pkgconfig_install_dir}"
"${ign_pkgconfig_install_dir}"
"${CMAKE_INSTALL_PREFIX}"
)

# TODO(jrivero): CMake 3.20 provides cmake_path(APPEND) which implements the
# same functionality as join_paths(). Remove JoinPaths in the next major version
# if all the supported platforms are in 3.20 version.
include(JoinPaths)
join_paths(IGN_PC_LIBDIR "\${prefix}" "${CMAKE_INSTALL_LIBDIR}")
join_paths(IGN_PC_INCLUDEDIR "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}" "${IGN_INCLUDE_INSTALL_DIR_POSTFIX}")

configure_file(${ign_pkgconfig_input} ${ign_pkgconfig_output} @ONLY)

install(
Expand Down
14 changes: 9 additions & 5 deletions cmake/IgnPackaging.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,16 @@ macro(ign_setup_packages)
# (but later on when installing)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)

set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${IGN_LIB_INSTALL_DIR}")
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}")

# add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

# the RPATH to be used when installing, but only if its not a system directory
list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/${IGN_LIB_INSTALL_DIR}" isSystemDir)
list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_FULL_LIBDIR}" isSystemDir)
if("${isSystemDir}" STREQUAL "-1")
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${IGN_LIB_INSTALL_DIR}")
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}")
endif("${isSystemDir}" STREQUAL "-1")
endif()

Expand Down Expand Up @@ -198,13 +198,17 @@ function(_ign_create_pkgconfig)
endif()

set(pkgconfig_output "${CMAKE_BINARY_DIR}/cmake/pkgconfig/${target_name}.pc")
set(pkgconfig_install_dir "${IGN_LIB_INSTALL_DIR}/pkgconfig")
set(pkgconfig_install_dir "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig")
file(RELATIVE_PATH
PC_CONFIG_RELATIVE_PATH_TO_PREFIX
"${CMAKE_INSTALL_PREFIX}/${pkgconfig_install_dir}"
"${pkgconfig_install_dir}"
"${CMAKE_INSTALL_PREFIX}"
)

include("${IGNITION_CMAKE_DIR}/JoinPaths.cmake")
join_paths(PC_LIBDIR "\${prefix}" "${CMAKE_INSTALL_LIBDIR}")
join_paths(PC_INCLUDEDIR "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}" "${IGN_INCLUDE_INSTALL_DIR_POSTFIX}")

configure_file(${pkgconfig_input} ${pkgconfig_output} @ONLY)

install(
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 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()
4 changes: 2 additions & 2 deletions cmake/pkgconfig/ignition-component.pc.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
prefix=${pcfiledir}/@PC_CONFIG_RELATIVE_PATH_TO_PREFIX@
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@/@IGN_INCLUDE_INSTALL_DIR_POSTFIX@
libdir=@PC_LIBDIR@
includedir=@PC_INCLUDEDIR@

Name: Ignition @GZ_DESIGNATION@ @component_name@
Description: A set of @GZ_DESIGNATION@ @component_name@ classes for robot applications
Expand Down
4 changes: 2 additions & 2 deletions cmake/pkgconfig/ignition.pc.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
prefix=${pcfiledir}/@PC_CONFIG_RELATIVE_PATH_TO_PREFIX@
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@/@IGN_INCLUDE_INSTALL_DIR_POSTFIX@
libdir=@PC_LIBDIR@
includedir=@PC_INCLUDEDIR@

Name: Ignition @GZ_DESIGNATION@
Description: A set of @GZ_DESIGNATION@ classes for robot applications
Expand Down
2 changes: 1 addition & 1 deletion config/ignition-cmake.pc.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
prefix=${pcfiledir}/@IGN_PC_CONFIG_RELATIVE_PATH_TO_PREFIX@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@/@IGN_INCLUDE_INSTALL_DIR_POSTFIX@
includedir=@IGN_PC_INCLUDEDIR@

Name: Ignition @GZ_DESIGNATION@
Description: Build system package for the ignition libraries
Expand Down

0 comments on commit 4283fc6

Please sign in to comment.