Blame cmake/compat_2.8.11/CMakePackageConfigHelpers.cmake

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