2016-09-27 22:01:08 +03:00
|
|
|
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
# file Copyright.txt or https://cmake.org/licensing for details.
|
|
|
|
|
2013-10-15 19:17:36 +04:00
|
|
|
#.rst:
|
|
|
|
# CMakePackageConfigHelpers
|
|
|
|
# -------------------------
|
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# Helpers functions for creating config files that can be included by other
|
|
|
|
# projects to find and use a package.
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# Adds the :command:`configure_package_config_file()` and
|
|
|
|
# :command:`write_basic_package_version_file()` commands.
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# Generating a Package Configuration File
|
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# .. command:: configure_package_config_file
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# Create a config file for a project::
|
|
|
|
#
|
2014-10-22 23:52:31 +04:00
|
|
|
# configure_package_config_file(<input> <output>
|
|
|
|
# INSTALL_DESTINATION <path>
|
|
|
|
# [PATH_VARS <var1> <var2> ... <varN>]
|
|
|
|
# [NO_SET_AND_CHECK_MACRO]
|
|
|
|
# [NO_CHECK_REQUIRED_COMPONENTS_MACRO]
|
|
|
|
# [INSTALL_PREFIX <path>]
|
|
|
|
# )
|
2012-02-17 02:12:14 +04:00
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# ``configure_package_config_file()`` should be used instead of the plain
|
|
|
|
# :command:`configure_file()` command when creating the ``<Name>Config.cmake``
|
|
|
|
# or ``<Name>-config.cmake`` file for installing a project or library. It helps
|
|
|
|
# making the resulting package relocatable by avoiding hardcoded paths in the
|
|
|
|
# installed ``Config.cmake`` file.
|
2012-02-17 02:12:14 +04:00
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# In a ``FooConfig.cmake`` file there may be code like this to make the install
|
|
|
|
# destinations know to the using project:
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# .. code-block:: cmake
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
|
|
|
# set(FOO_INCLUDE_DIR "@CMAKE_INSTALL_FULL_INCLUDEDIR@" )
|
|
|
|
# set(FOO_DATA_DIR "@CMAKE_INSTALL_PREFIX@/@RELATIVE_DATA_INSTALL_DIR@" )
|
|
|
|
# set(FOO_ICONS_DIR "@CMAKE_INSTALL_PREFIX@/share/icons" )
|
|
|
|
# ...logic to determine installedPrefix from the own location...
|
|
|
|
# set(FOO_CONFIG_DIR "${installedPrefix}/@CONFIG_INSTALL_DIR@" )
|
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# All 4 options shown above are not sufficient, since the first 3 hardcode the
|
|
|
|
# absolute directory locations, and the 4th case works only if the logic to
|
|
|
|
# determine the ``installedPrefix`` is correct, and if ``CONFIG_INSTALL_DIR``
|
|
|
|
# contains a relative path, which in general cannot be guaranteed. This has the
|
|
|
|
# effect that the resulting ``FooConfig.cmake`` file would work poorly under
|
|
|
|
# Windows and OSX, where users are used to choose the install location of a
|
|
|
|
# binary package at install time, independent from how
|
|
|
|
# :variable:`CMAKE_INSTALL_PREFIX` was set at build/cmake time.
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# Using ``configure_package_config_file`` helps. If used correctly, it makes
|
|
|
|
# the resulting ``FooConfig.cmake`` file relocatable. Usage:
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
2014-10-22 23:52:31 +04:00
|
|
|
# 1. write a ``FooConfig.cmake.in`` file as you are used to
|
|
|
|
# 2. insert a line containing only the string ``@PACKAGE_INIT@``
|
|
|
|
# 3. instead of ``set(FOO_DIR "@SOME_INSTALL_DIR@")``, use
|
|
|
|
# ``set(FOO_DIR "@PACKAGE_SOME_INSTALL_DIR@")`` (this must be after the
|
|
|
|
# ``@PACKAGE_INIT@`` line)
|
|
|
|
# 4. instead of using the normal :command:`configure_file()`, use
|
|
|
|
# ``configure_package_config_file()``
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# The ``<input>`` and ``<output>`` arguments are the input and output file, the
|
|
|
|
# same way as in :command:`configure_file()`.
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# The ``<path>`` given to ``INSTALL_DESTINATION`` must be the destination where
|
2014-07-15 21:10:54 +04:00
|
|
|
# the ``FooConfig.cmake`` file will be installed to. This path can either be
|
|
|
|
# absolute, or relative to the ``INSTALL_PREFIX`` path.
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# The variables ``<var1>`` to ``<varN>`` given as ``PATH_VARS`` are the
|
|
|
|
# variables which contain install destinations. For each of them the macro will
|
|
|
|
# create a helper variable ``PACKAGE_<var...>``. These helper variables must be
|
|
|
|
# used in the ``FooConfig.cmake.in`` file for setting the installed location.
|
|
|
|
# They are calculated by ``configure_package_config_file`` so that they are
|
|
|
|
# always relative to the installed location of the package. This works both for
|
|
|
|
# relative and also for absolute locations. For absolute locations it works
|
2014-07-15 21:10:54 +04:00
|
|
|
# only if the absolute location is a subdirectory of ``INSTALL_PREFIX``.
|
|
|
|
#
|
|
|
|
# If the ``INSTALL_PREFIX`` argument is passed, this is used as base path to
|
|
|
|
# calculate all the relative paths. The ``<path>`` argument must be an absolute
|
|
|
|
# path. If this argument is not passed, the :variable:`CMAKE_INSTALL_PREFIX`
|
|
|
|
# variable will be used instead. The default value is good when generating a
|
|
|
|
# FooConfig.cmake file to use your package from the install tree. When
|
|
|
|
# generating a FooConfig.cmake file to use your package from the build tree this
|
|
|
|
# option should be used.
|
2014-07-15 21:10:28 +04:00
|
|
|
#
|
|
|
|
# By default ``configure_package_config_file`` also generates two helper macros,
|
|
|
|
# ``set_and_check()`` and ``check_required_components()`` into the
|
|
|
|
# ``FooConfig.cmake`` file.
|
|
|
|
#
|
|
|
|
# ``set_and_check()`` should be used instead of the normal ``set()`` command for
|
|
|
|
# setting directories and file locations. Additionally to setting the variable
|
|
|
|
# it also checks that the referenced file or directory actually exists and fails
|
|
|
|
# with a ``FATAL_ERROR`` otherwise. This makes sure that the created
|
|
|
|
# ``FooConfig.cmake`` file does not contain wrong references.
|
|
|
|
# When using the ``NO_SET_AND_CHECK_MACRO``, this macro is not generated
|
|
|
|
# into the ``FooConfig.cmake`` file.
|
|
|
|
#
|
|
|
|
# ``check_required_components(<package_name>)`` should be called at the end of
|
|
|
|
# the ``FooConfig.cmake`` file if the package supports components. This macro
|
|
|
|
# checks whether all requested, non-optional components have been found, and if
|
|
|
|
# this is not the case, sets the ``Foo_FOUND`` variable to ``FALSE``, so that
|
|
|
|
# the package is considered to be not found. It does that by testing the
|
|
|
|
# ``Foo_<Component>_FOUND`` variables for all requested required components.
|
|
|
|
# When using the ``NO_CHECK_REQUIRED_COMPONENTS_MACRO`` option, this macro is
|
|
|
|
# not generated into the ``FooConfig.cmake`` file.
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
|
|
|
# For an example see below the documentation for
|
2014-07-15 21:10:28 +04:00
|
|
|
# :command:`write_basic_package_version_file()`.
|
|
|
|
#
|
|
|
|
# Generating a Package Version File
|
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
#
|
|
|
|
# .. command:: write_basic_package_version_file
|
|
|
|
#
|
|
|
|
# Create a version file for a project::
|
|
|
|
#
|
2014-10-22 23:52:31 +04:00
|
|
|
# write_basic_package_version_file(<filename>
|
|
|
|
# [VERSION <major.minor.patch>]
|
|
|
|
# COMPATIBILITY <AnyNewerVersion|SameMajorVersion|ExactVersion> )
|
2014-07-15 21:10:28 +04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
# Writes a file for use as ``<package>ConfigVersion.cmake`` file to
|
|
|
|
# ``<filename>``. See the documentation of :command:`find_package()` for
|
|
|
|
# details on this.
|
|
|
|
#
|
|
|
|
# ``<filename>`` is the output filename, it should be in the build tree.
|
|
|
|
# ``<major.minor.patch>`` is the version number of the project to be installed.
|
|
|
|
#
|
|
|
|
# If no ``VERSION`` is given, the :variable:`PROJECT_VERSION` variable is used.
|
|
|
|
# If this hasn't been set, it errors out.
|
|
|
|
#
|
|
|
|
# The ``COMPATIBILITY`` mode ``AnyNewerVersion`` means that the installed
|
|
|
|
# package version will be considered compatible if it is newer or exactly the
|
|
|
|
# same as the requested version. This mode should be used for packages which
|
|
|
|
# are fully backward compatible, also across major versions.
|
|
|
|
# If ``SameMajorVersion`` is used instead, then the behaviour differs from
|
|
|
|
# ``AnyNewerVersion`` in that the major version number must be the same as
|
|
|
|
# requested, e.g. version 2.0 will not be considered compatible if 1.0 is
|
|
|
|
# requested. This mode should be used for packages which guarantee backward
|
|
|
|
# compatibility within the same major version.
|
|
|
|
# If ``ExactVersion`` is used, then the package is only considered compatible if
|
|
|
|
# the requested version matches exactly its own version number (not considering
|
|
|
|
# the tweak version). For example, version 1.2.3 of a package is only
|
|
|
|
# considered compatible to requested version 1.2.3. This mode is for packages
|
|
|
|
# without compatibility guarantees.
|
|
|
|
# If your project has more elaborated version matching rules, you will need to
|
|
|
|
# write your own custom ``ConfigVersion.cmake`` file instead of using this
|
|
|
|
# macro.
|
|
|
|
#
|
|
|
|
# Internally, this macro executes :command:`configure_file()` to create the
|
|
|
|
# resulting version file. Depending on the ``COMPATIBLITY``, either the file
|
|
|
|
# ``BasicConfigVersion-SameMajorVersion.cmake.in`` or
|
|
|
|
# ``BasicConfigVersion-AnyNewerVersion.cmake.in`` is used. Please note that
|
2013-10-15 19:17:36 +04:00
|
|
|
# these two files are internal to CMake and you should not call
|
2014-07-15 21:10:28 +04:00
|
|
|
# :command:`configure_file()` on them yourself, but they can be used as starting
|
|
|
|
# point to create more sophisticted custom ``ConfigVersion.cmake`` files.
|
2012-02-17 03:36:18 +04:00
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# Example Generating Package Files
|
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
2012-02-17 03:36:18 +04:00
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# Example using both :command:`configure_package_config_file` and
|
|
|
|
# ``write_basic_package_version_file()``:
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# ``CMakeLists.txt``:
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# .. code-block:: cmake
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
|
|
|
# set(INCLUDE_INSTALL_DIR include/ ... CACHE )
|
|
|
|
# set(LIB_INSTALL_DIR lib/ ... CACHE )
|
|
|
|
# set(SYSCONFIG_INSTALL_DIR etc/foo/ ... CACHE )
|
|
|
|
# ...
|
|
|
|
# include(CMakePackageConfigHelpers)
|
2014-10-22 23:52:31 +04:00
|
|
|
# configure_package_config_file(FooConfig.cmake.in
|
|
|
|
# ${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake
|
|
|
|
# INSTALL_DESTINATION ${LIB_INSTALL_DIR}/Foo/cmake
|
|
|
|
# PATH_VARS INCLUDE_INSTALL_DIR SYSCONFIG_INSTALL_DIR)
|
|
|
|
# write_basic_package_version_file(
|
|
|
|
# ${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake
|
|
|
|
# VERSION 1.2.3
|
|
|
|
# COMPATIBILITY SameMajorVersion )
|
|
|
|
# install(FILES ${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake
|
|
|
|
# ${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake
|
2013-10-15 19:17:36 +04:00
|
|
|
# DESTINATION ${LIB_INSTALL_DIR}/Foo/cmake )
|
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# ``FooConfig.cmake.in``:
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
2014-07-15 21:10:28 +04:00
|
|
|
# .. code-block:: cmake
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
|
|
|
# set(FOO_VERSION x.y.z)
|
|
|
|
# ...
|
|
|
|
# @PACKAGE_INIT@
|
|
|
|
# ...
|
|
|
|
# set_and_check(FOO_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@")
|
|
|
|
# set_and_check(FOO_SYSCONFIG_DIR "@PACKAGE_SYSCONFIG_INSTALL_DIR@")
|
|
|
|
#
|
|
|
|
# check_required_components(Foo)
|
2012-02-17 02:12:14 +04:00
|
|
|
|
|
|
|
|
|
|
|
include(CMakeParseArguments)
|
|
|
|
|
2012-02-17 03:36:18 +04:00
|
|
|
include(WriteBasicConfigVersionFile)
|
|
|
|
|
|
|
|
macro(WRITE_BASIC_PACKAGE_VERSION_FILE)
|
|
|
|
write_basic_config_version_file(${ARGN})
|
|
|
|
endmacro()
|
|
|
|
|
2012-02-17 02:12:14 +04:00
|
|
|
function(CONFIGURE_PACKAGE_CONFIG_FILE _inputFile _outputFile)
|
2013-12-24 04:15:42 +04:00
|
|
|
set(options NO_SET_AND_CHECK_MACRO NO_CHECK_REQUIRED_COMPONENTS_MACRO)
|
2014-07-15 21:10:54 +04:00
|
|
|
set(oneValueArgs INSTALL_DESTINATION INSTALL_PREFIX)
|
2012-02-17 02:12:14 +04:00
|
|
|
set(multiValueArgs PATH_VARS )
|
|
|
|
|
|
|
|
cmake_parse_arguments(CCF "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
|
|
|
|
|
|
|
if(CCF_UNPARSED_ARGUMENTS)
|
|
|
|
message(FATAL_ERROR "Unknown keywords given to CONFIGURE_PACKAGE_CONFIG_FILE(): \"${CCF_UNPARSED_ARGUMENTS}\"")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(NOT CCF_INSTALL_DESTINATION)
|
|
|
|
message(FATAL_ERROR "No INSTALL_DESTINATION given to CONFIGURE_PACKAGE_CONFIG_FILE()")
|
|
|
|
endif()
|
|
|
|
|
2014-07-15 21:10:54 +04:00
|
|
|
if(DEFINED CCF_INSTALL_PREFIX)
|
|
|
|
if(IS_ABSOLUTE "${CCF_INSTALL_PREFIX}")
|
|
|
|
set(installPrefix "${CCF_INSTALL_PREFIX}")
|
|
|
|
else()
|
|
|
|
message(FATAL_ERROR "INSTALL_PREFIX must be an absolute path")
|
|
|
|
endif()
|
2016-03-19 18:21:29 +03:00
|
|
|
elseif(IS_ABSOLUTE "${CMAKE_INSTALL_PREFIX}")
|
2014-07-15 21:10:54 +04:00
|
|
|
set(installPrefix "${CMAKE_INSTALL_PREFIX}")
|
2016-03-19 18:21:29 +03:00
|
|
|
else()
|
|
|
|
get_filename_component(installPrefix "${CMAKE_INSTALL_PREFIX}" ABSOLUTE)
|
2014-07-15 21:10:54 +04:00
|
|
|
endif()
|
|
|
|
|
2012-02-17 02:12:14 +04:00
|
|
|
if(IS_ABSOLUTE "${CCF_INSTALL_DESTINATION}")
|
|
|
|
set(absInstallDir "${CCF_INSTALL_DESTINATION}")
|
|
|
|
else()
|
2014-07-15 21:10:54 +04:00
|
|
|
set(absInstallDir "${installPrefix}/${CCF_INSTALL_DESTINATION}")
|
2012-02-17 02:12:14 +04:00
|
|
|
endif()
|
2013-01-25 00:15:58 +04:00
|
|
|
|
2014-07-15 21:10:54 +04:00
|
|
|
file(RELATIVE_PATH PACKAGE_RELATIVE_PATH "${absInstallDir}" "${installPrefix}" )
|
2012-02-17 02:12:14 +04:00
|
|
|
|
|
|
|
foreach(var ${CCF_PATH_VARS})
|
|
|
|
if(NOT DEFINED ${var})
|
|
|
|
message(FATAL_ERROR "Variable ${var} does not exist")
|
|
|
|
else()
|
2013-04-03 18:54:08 +04:00
|
|
|
if(IS_ABSOLUTE "${${var}}")
|
2014-07-15 21:10:54 +04:00
|
|
|
string(REPLACE "${installPrefix}" "\${PACKAGE_PREFIX_DIR}"
|
2013-04-03 18:54:08 +04:00
|
|
|
PACKAGE_${var} "${${var}}")
|
2013-01-25 00:15:58 +04:00
|
|
|
else()
|
2013-04-03 18:54:08 +04:00
|
|
|
set(PACKAGE_${var} "\${PACKAGE_PREFIX_DIR}/${${var}}")
|
2013-01-25 00:15:58 +04:00
|
|
|
endif()
|
2012-02-17 02:12:14 +04:00
|
|
|
endif()
|
|
|
|
endforeach()
|
|
|
|
|
2012-12-07 01:24:43 +04:00
|
|
|
get_filename_component(inputFileName "${_inputFile}" NAME)
|
|
|
|
|
2012-02-17 02:12:14 +04:00
|
|
|
set(PACKAGE_INIT "
|
|
|
|
####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
|
2012-12-07 01:24:43 +04:00
|
|
|
####### Any changes to this file will be overwritten by the next CMake run ####
|
|
|
|
####### The input file was ${inputFileName} ########
|
|
|
|
|
2012-02-17 02:12:14 +04:00
|
|
|
get_filename_component(PACKAGE_PREFIX_DIR \"\${CMAKE_CURRENT_LIST_DIR}/${PACKAGE_RELATIVE_PATH}\" ABSOLUTE)
|
|
|
|
")
|
|
|
|
|
2013-04-03 18:54:08 +04:00
|
|
|
if("${absInstallDir}" MATCHES "^(/usr)?/lib(64)?/.+")
|
|
|
|
# Handle "/usr move" symlinks created by some Linux distros.
|
2016-07-28 01:41:13 +03:00
|
|
|
string(APPEND PACKAGE_INIT "
|
2013-04-03 18:54:08 +04:00
|
|
|
# Use original install prefix when loaded through a \"/usr move\"
|
|
|
|
# cross-prefix symbolic link such as /lib -> /usr/lib.
|
|
|
|
get_filename_component(_realCurr \"\${CMAKE_CURRENT_LIST_DIR}\" REALPATH)
|
|
|
|
get_filename_component(_realOrig \"${absInstallDir}\" REALPATH)
|
|
|
|
if(_realCurr STREQUAL _realOrig)
|
2014-07-15 21:10:54 +04:00
|
|
|
set(PACKAGE_PREFIX_DIR \"${installPrefix}\")
|
2013-04-03 18:54:08 +04:00
|
|
|
endif()
|
|
|
|
unset(_realOrig)
|
|
|
|
unset(_realCurr)
|
|
|
|
")
|
|
|
|
endif()
|
|
|
|
|
2012-02-17 02:12:14 +04:00
|
|
|
if(NOT CCF_NO_SET_AND_CHECK_MACRO)
|
2016-07-28 01:41:13 +03:00
|
|
|
string(APPEND PACKAGE_INIT "
|
2012-02-17 02:12:14 +04:00
|
|
|
macro(set_and_check _var _file)
|
|
|
|
set(\${_var} \"\${_file}\")
|
|
|
|
if(NOT EXISTS \"\${_file}\")
|
|
|
|
message(FATAL_ERROR \"File or directory \${_file} referenced by variable \${_var} does not exist !\")
|
|
|
|
endif()
|
|
|
|
endmacro()
|
|
|
|
")
|
|
|
|
endif()
|
|
|
|
|
2012-03-19 00:26:37 +04:00
|
|
|
|
|
|
|
if(NOT CCF_NO_CHECK_REQUIRED_COMPONENTS_MACRO)
|
2016-07-28 01:41:13 +03:00
|
|
|
string(APPEND PACKAGE_INIT "
|
2012-03-19 00:26:37 +04:00
|
|
|
macro(check_required_components _NAME)
|
|
|
|
foreach(comp \${\${_NAME}_FIND_COMPONENTS})
|
|
|
|
if(NOT \${_NAME}_\${comp}_FOUND)
|
|
|
|
if(\${_NAME}_FIND_REQUIRED_\${comp})
|
|
|
|
set(\${_NAME}_FOUND FALSE)
|
|
|
|
endif()
|
|
|
|
endif()
|
2012-08-13 21:50:14 +04:00
|
|
|
endforeach()
|
2012-03-19 00:26:37 +04:00
|
|
|
endmacro()
|
|
|
|
")
|
|
|
|
endif()
|
|
|
|
|
2016-07-28 01:41:13 +03:00
|
|
|
string(APPEND PACKAGE_INIT "
|
2012-02-17 02:12:14 +04:00
|
|
|
####################################################################################")
|
|
|
|
|
|
|
|
configure_file("${_inputFile}" "${_outputFile}" @ONLY)
|
|
|
|
|
|
|
|
endfunction()
|