4

I have created cmake target, say A, and want to install it and create a Config file, so that the installed package could be relocatable. My code is:

install(EXPORT ${PROJECT_NAME}Targets
    FILE ${PROJECT_NAME}Targets.cmake
    NAMESPACE ${PROJECT_NAME}::
    DESTINATION ??? )

Here, I am having a problem with the proper destination. I want the Config file to be installed where ${CMAKE_INSTALL_PREFIX} points to. But when I put ${CMAKE_INSTALL_PREFIX} at ???, my resulting ATargets.cmake file contains the line:

set(_IMPORT_PREFIX "C:/Libraries/...")

which is the actual value of ${CMAKE_INSTALL_PREFIX}. This _IMPORT_PREFIX is later prepended to the parameters of set_target_properties() command inside the auto-generated ATargets.cmake, resulting in hard coded paths, valid only on the installation system.

I tried to use some generator expressions like <$IMPORT_PREFIX> in place of ???, but this gave me an error at cmake generation. I also tried to omit DESTINATION which in my opinion should place the file in the location relative to ${CMAKE_INSTALL_PREFIX}, but cmake complained about it too.

Can you please help me with this issue?

Piotr G
  • 959
  • 1
  • 7
  • 25
  • Package files generated by CMake are not intended to be relocatable. You may create relocatable package by writing "config" file by yourself. – Tsyvarev Aug 17 '17 at 09:27
  • @Tsyvarev: well, according to the cmake docs: ```Packages created by install(EXPORT) are designed to be relocatable``` (source: https://cmake.org/cmake/help/v3.8/manual/cmake-packages.7.html) – Piotr G Aug 17 '17 at 09:30
  • Hmm, not sure what they mean by "relocatable". However, you may try `.` (dot) or `./` as DESTINATION option for install files precisely under installation prefix. Such paths should be treated as relative by CMake, so "relocation" wouldn't suffer. – Tsyvarev Aug 17 '17 at 09:42
  • Ok. That helped a litte. I don't have absolute paths any longer. Unfortunately, there is still problem in the generated Target.cmake: to produce the _IMPORT_PREFIX it strips the filename component twice instead of once: ```get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH) get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)```. The paths produced in this way are still invalid. – Piotr G Aug 17 '17 at 11:34

1 Answers1

2

This is an old question so you have probably solved it already (or given up).

I think the critical thing is that the export DESTINATION matches the one given to INSTALL_DESTINATION to configure_package_config_file().

what we are using is:

  install(TARGETS
    foobar
    EXPORT foobarLibTargets
    LIBRARY DESTINATION lib)
  set(ConfigFileInstallDir lib/cmake/foobar)
  set(INCLUDE_INSTALL_DIR include)
  set(LIBRARY_INSTALL_DIR lib)
  configure_package_config_file(src/main/cmake/foobarConfig.cmake.in
    "${CMAKE_CURRENT_BINARY_DIR}/foobarConfig.cmake"
    INSTALL_DESTINATION "${ConfigFileInstallDir}"
    PATH_VARS INCLUDE_INSTALL_DIR LIBRARY_INSTALL_DIR)
  write_basic_package_version_file(
    "${CMAKE_CURRENT_BINARY_DIR}/foobarConfigVersion.cmake"
    VERSION "${VERSION}"
    COMPATIBILITY ExactVersion)
  export(EXPORT foobarLibTargets
    FILE "${CMAKE_CURRENT_BINARY_DIR}/foobarLibTargets.cmake")
  install(EXPORT foobarLibTargets
    FILE foobarLibTargets.cmake
    DESTINATION "${ConfigFileInstallDir}")
  install(FILES
    "${CMAKE_CURRENT_BINARY_DIR}/foobarConfig.cmake"
    "${CMAKE_CURRENT_BINARY_DIR}/foobarConfigVersion.cmake"
    "${CMAKE_CURRENT_BINARY_DIR}/foobarLibTargets.cmake"
    DESTINATION "${ConfigFileInstallDir}")

${ConfigFileInstallDir} is replaced automagically by _IMPORT_PREFIX so that the generated foobarLibTargets.cmake contains something like:

set_property(TARGET foobar APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(foobar PROPERTIES
  IMPORTED_LOCATION_RELEASE "${_IMPORT_PREFIX}/lib/foobar.so.2.0.0"
  IMPORTED_SONAME_RELEASE "libfoobar.so.2"
)

There can be troubles getting this to work. See my questions:

See you in the ward for those suffering from cmake related nervous disorders in the near future...

Bruce Adams
  • 4,953
  • 4
  • 48
  • 111