[cmake-developers] Making Config.cmake files easier to write

Brad King brad.king at kitware.com
Tue Feb 14 16:14:53 EST 2012


On 2/14/2012 2:44 PM, Alexander Neundorf wrote:
> In the BarConfig.cmake file there would still be either a
>
> set(BAR_INCLUDE_DIR "@INCLUDE_INSTALL_DIR@")
> which would work for absolute paths, or a
>
> set(BAR_INCLUDE_DIR "${SomePrefix}/@INCLUDE_INSTALL_DIR@")
> which would work only for relative paths, but a simple set() cannot work for
> both cases. Am I missing something ?

  $ cat BarConfig.cmake.in
  @PACKAGE_INIT@
  set(BAR_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@")

  $ cat CMakeLists.txt
  ...
  set(INCLUDE_INSTALL_DIR ...)
  cmake_package_config_for_install(Bar
    DESTINATION ${CMAKECONFIG_INSTALL_DIR}
    PATH_VARS INCLUDE_INSTALL_DIR)

The macro should check the value of INSTALL_INCLUDE_DIR.

Relative case:

  $ cat BarConfig.cmake
  get_filename_component(BAR_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
  get_filename_component(BAR_INSTALL_PREFIX "${BAR_INSTALL_PREFIX}" PATH)
  get_filename_component(BAR_INSTALL_PREFIX "${BAR_INSTALL_PREFIX}" PATH)
  # ... repeated based on depth of BarConfig install location
  set(BAR_INCLUDE_DIR "${BAR_INSTALL_PREFIX}/include/bar)

Absolute case when value happens to be under ${CMAKE_INSTALL_PREFIX}:

  $ cat BarConfig.cmake
  get_filename_component(BAR_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
  get_filename_component(BAR_INSTALL_PREFIX "${BAR_INSTALL_PREFIX}" PATH)
  get_filename_component(BAR_INSTALL_PREFIX "${BAR_INSTALL_PREFIX}" PATH)
  # ... repeated based on depth of BarConfig install location
  set(BAR_INCLUDE_DIR "${BAR_INSTALL_PREFIX}/include/bar)

Outside absolute case:

  $ cat BarConfig.cmake
  get_filename_component(BAR_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
  get_filename_component(BAR_INSTALL_PREFIX "${BAR_INSTALL_PREFIX}" PATH)
  get_filename_component(BAR_INSTALL_PREFIX "${BAR_INSTALL_PREFIX}" PATH)
  # ... repeated based on depth of BarConfig install location
  set(BAR_INCLUDE_DIR "/somewhere/include/bar)

The replacement for @PACKAGE_INIT@ is always the same, and can handle
computing BAR_INSTALL_PREFIX at load time.

In all cases the macro replaces @PACKAGE_INCLUDE_INSTALL_DIR@ with
something that it knows how to compute at configuration time.  It
can replace it with a literal ${BAR_INSTALL_PREFIX} in the relative
and relocatable cases so that the proper prefix appears at load time.

-Brad


P.S. Yury, I apologize for spelling your name incorrectly in my other post.


More information about the cmake-developers mailing list