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

Yury G. Kudryashov urkud.urkud at gmail.com
Tue Feb 14 05:34:16 EST 2012


Brad King wrote:

> On 2/13/2012 4:52 PM, Alexander Neundorf wrote:
>> we are hoping that more and more libraries will install Config.cmake
>> files (and for kdelibs this is actually happening right now), so we
>> should make sure it is straighforward to create proper Config.cmake
>> files.
> 
> This is a worthwhile goal, but I do not like the approach proposed:
> 
> (1) It makes BarConfig.cmake depend on a file not distributed with it.
> 
> (2) The content of a relocatable BarConfig.cmake should not depend on
> where it happened to be installed when the package was built.
> 
> I'd rather see an approach that processes paths during configuration
> of the original package.  Provide some helper macros that can configure
> BarConfig.cmake with the proper code in it to compute paths relative
> to its location:
> 
>   include(CMakePackageConfigHelper)
>   cmake_package_config_for_install(Bar
>     DESTINATION lib/cmake/Bar # install destination
>     # INPUT BarConfig-install.cmake.in # alternate input
>     )
> 
> This would look for "BarConfig.cmake.in" (or whatever INPUT says) and
> do configure_file along with install(FILES).  The module can document
> some standard @CONFIG@ variables that can be used inside the input
> file to get various paths:
> 
>   $ cat BarConfig.cmake.in
>   @PACKAGE_INIT@
>   set(BAR_INCLUDE_DIRS "@PACKAGE_PREFIX@/include/bar")
Another way to achieve the same result:

set(INCLUDE_INSTALL_DIR "include") # relative
set(MYPKGDATA_INSTALL_DIR ${CMAKE_PREFIX_PATH}/share/mypkg) # absolute

cmake_package_config_for_install(Bar
  DESTINATION lib/cmake/Bar
  INPUT BarConfig-install.cmake.in
  PATH_VARS
    INCLUDE_INSTALL_DIR   # interpreted as relative to install prefix
    MYPKGDATA_INSTALL_DIR # absolute
  )

will substitute @PACKAGE_INCLUDE_INSTALL_DIR@ by "../../../include" and 
@PACKAGE_MYPKGDATA_INSTALL_DIR@ by "../../../share/mypkg" (both transformed 
to be relative to DESTINATION).

The main advantage of this solution is that the relative path is calculated 
only once (when package is installed), not every time Config.cmake file is 
sourced. The main drawback is that one has to specify all required path 
variables explicitly.
-- 
Yury G. Kudryashov,
mailto: urkud at mccme.ru




More information about the cmake-developers mailing list