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

Brad King brad.king at kitware.com
Tue Feb 14 09:52:51 EST 2012


On 2/14/2012 5:34 AM, Yury G. Kudryashov wrote:
> 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.

That's an interesting interface idea.  In my proposal the paths are also
computed only during the initial generation of the file.  What needs to
be computed every time the file is loaded is the current installation
prefix relative to the file's location.  This cannot be precomputed,
but once it is known the other paths can all be made relative to that
(so would not contain ../).

What your suggestion adds is custom variable mapping that is very
convenient.  If the input value of INCLUDE_INSTALL_DIR is a relative
path it can be used verbatim as relative to the install prefix.  If
it is a full path that sits under the install prefix the macro can
handle conversion to a relative path automatically.  This is the part
that can be tedious to get right by hand.

Thanks,
-Brad



More information about the cmake-developers mailing list