The Target.cmake target list file generated by install(EXPORT…) has line that set _IMPORT_PREFIX to absolute path of the project that generates this file. Therefore the consuming project is not able to find files in imported package directory.
I’d like to fix this such that _IMPORT_PREFIX is set to “${CMAKE_CURRENT_LIST_FILE}”. What should be changed for this?
DETAILS
Based on cmake source code GenerateImportPrefix function it picks case // The export file is being installed to an absolute path despite my setting in install(EXPORT DESTINATION to relative path…
My project generates Target.cmake which sets _IMPORT_PREFIX to target.cmake location in under ${CMAKE_INSTALL_PREFIX} and then _IMPORT_PREFIX is used to set_target_properties of each target
# The installation prefix configured by this project.
set(_IMPORT_PREFIX "/home/myspace/install/armp")
in my CMakeLists.txt, the DESTINATION dir the same relative path value is set in
install(EXPORT ... DESTINATION "${CONFIG_INSTALL_DIR}"
and in
configure_package_config_file( ... INSTALL_DESTINATION "${CONFIG_INSTALL_DIR}"
I see that in a sample project _IMPORT_PREFIX is being set properly in generated:
FooBarLibinstall/opt/lib/cmake/FooBarLib/FooBarLibTargets.cmake file
# Compute the installation prefix relative to this file.
get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
but I am not clear on what part of code is responsible for that because the correct generation occurs despite that the stackoverflow-56135785-answer CMakeLists.txt examplesets not matching DESTINATION and INSTALL_DESTINATION values, and INSTALL_DESTINATION is set to an absolute path
/home/myspace/stackoverflow-56135785-answer/FooBarLib-build
as per
configure_package_config_file( INSTALL_DESTINATION "${PROJECT_BINARY_DIR}"
which differs from the relative path lib/cmake/FooBarLib set by
install( EXPORT DESTINATION ${FOOBARLIB_INSTALL_CONFIG_DIR}
The same question was asked before in Create relocatable package with proper autogenerated config cmake and correctly set the location of imported cmake targets for an installed package but I could not pinpoint what is the difference in my code.
I see several cache variables are set to the same absolute path as the value written in the to set command in the “The installation prefix configured by this project.” section of Targets.cmake file : CMAKE INSTALL_PREFIX and CPACK_PACKAGING_INSTALL_PREFIX, (and other CPACK_* vars) but I think both should be in absolute path
There is another problem with the additional absolute path (e.g /home/myspace/mylib/include) added after the proper path in the INTERFACE_INCLUDE_DIRECTORIES and Id like to drop that:
# Create imported target dalint::cnhlogger
add_library(myspace::mylib SHARED IMPORTED)
set_target_properties(myspace::mylib PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/mylib/include;/home/myspace/mylib/include"
)