Skip to content

Commit 87737e9

Browse files
committed
Introduce relocatable AMENT_IMPORT_PREFIX
This moves the logic to compute the install prefix to a single location in the main config file. Extra config files can use the new AMENT_IMPORT_PREFIX variable, which simplifies the code and makes it more robust to future changes. The logic to compute AMENT_IMPORT_PREFIX has been taken from the CMakePackageConfigHelpers module. The code is well-tested and also handles symlink aliasing. Signed-off-by: Timo Röhling <[email protected]>
1 parent 6948e3c commit 87737e9

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

ament_cmake_core/cmake/core/templates/nameConfig.cmake.in

+12
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ endif()
3535
# flag package as ament-based to distinguish it after being find_package()-ed
3636
set(@PROJECT_NAME@_FOUND_AMENT_PACKAGE TRUE)
3737

38+
# Compute relocatable install prefix. This is pretty much the same code as it
39+
# is generated by the CMakePackageConfigHelpers module.
40+
get_filename_component(AMENT_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
41+
# Use original install prefix if the package was merely found via symlink alias
42+
get_filename_component(_realCurr "${CMAKE_CURRENT_LIST_DIR}" REALPATH)
43+
get_filename_component(_realOrig "@CMAKE_INSTALL_PREFIX@/share/@PROJECT_NAME@/cmake" REALPATH)
44+
if(_realCurr STREQUAL _realOrig)
45+
set(AMENT_IMPORT_PREFIX "@CMAKE_INSTALL_PREFIX@")
46+
endif()
47+
unset(_realCurr)
48+
unset(_realOrig)
49+
3850
# include all config extra files
3951
set(_extras "@PACKAGE_CONFIG_EXTRA_FILES@")
4052
foreach(_extra ${_extras})

ament_cmake_export_include_directories/cmake/ament_export_include_directories.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ macro(ament_export_include_directories)
3737
if(NOT IS_ABSOLUTE "${_arg}")
3838
# prefix relative paths with CMAKE_INSTALL_PREFIX
3939
# while avoiding to embed any absolute path
40-
set(_arg "\${${PROJECT_NAME}_DIR}/../../../${_arg}")
40+
set(_arg "\${AMENT_IMPORT_PREFIX}/${_arg}")
4141
list_append_unique(_AMENT_EXPORT_RELATIVE_INCLUDE_DIRECTORIES "${_arg}")
4242
else()
4343
if(NOT IS_DIRECTORY "${_arg}")

ament_cmake_export_libraries/cmake/ament_cmake_export_libraries-extras.cmake.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ if(NOT _exported_libraries STREQUAL "")
3939
set(_lib "NOTFOUND")
4040
find_library(
4141
_lib NAMES "${_library}"
42-
PATHS "${@PROJECT_NAME@_DIR}/../../../lib"
42+
PATHS "${AMENT_IMPORT_PREFIX}/lib"
4343
NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH
4444
)
4545

0 commit comments

Comments
 (0)