CMakePackageConfigHelpers.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #.rst:
  2. # CMakePackageConfigHelpers
  3. # -------------------------
  4. #
  5. # Helpers functions for creating config files that can be included by other
  6. # projects to find and use a package.
  7. #
  8. # Adds the :command:`configure_package_config_file()` and
  9. # :command:`write_basic_package_version_file()` commands.
  10. #
  11. # Generating a Package Configuration File
  12. # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  13. #
  14. # .. command:: configure_package_config_file
  15. #
  16. # Create a config file for a project::
  17. #
  18. # configure_package_config_file(<input> <output> INSTALL_DESTINATION <path>
  19. # [PATH_VARS <var1> <var2> ... <varN>]
  20. # [NO_SET_AND_CHECK_MACRO]
  21. # [NO_CHECK_REQUIRED_COMPONENTS_MACRO]
  22. # [INSTALL_PREFIX <path>])
  23. #
  24. #
  25. # ``configure_package_config_file()`` should be used instead of the plain
  26. # :command:`configure_file()` command when creating the ``<Name>Config.cmake``
  27. # or ``<Name>-config.cmake`` file for installing a project or library. It helps
  28. # making the resulting package relocatable by avoiding hardcoded paths in the
  29. # installed ``Config.cmake`` file.
  30. #
  31. # In a ``FooConfig.cmake`` file there may be code like this to make the install
  32. # destinations know to the using project:
  33. #
  34. # .. code-block:: cmake
  35. #
  36. # set(FOO_INCLUDE_DIR "@CMAKE_INSTALL_FULL_INCLUDEDIR@" )
  37. # set(FOO_DATA_DIR "@CMAKE_INSTALL_PREFIX@/@RELATIVE_DATA_INSTALL_DIR@" )
  38. # set(FOO_ICONS_DIR "@CMAKE_INSTALL_PREFIX@/share/icons" )
  39. # ...logic to determine installedPrefix from the own location...
  40. # set(FOO_CONFIG_DIR "${installedPrefix}/@CONFIG_INSTALL_DIR@" )
  41. #
  42. # All 4 options shown above are not sufficient, since the first 3 hardcode the
  43. # absolute directory locations, and the 4th case works only if the logic to
  44. # determine the ``installedPrefix`` is correct, and if ``CONFIG_INSTALL_DIR``
  45. # contains a relative path, which in general cannot be guaranteed. This has the
  46. # effect that the resulting ``FooConfig.cmake`` file would work poorly under
  47. # Windows and OSX, where users are used to choose the install location of a
  48. # binary package at install time, independent from how
  49. # :variable:`CMAKE_INSTALL_PREFIX` was set at build/cmake time.
  50. #
  51. # Using ``configure_package_config_file`` helps. If used correctly, it makes
  52. # the resulting ``FooConfig.cmake`` file relocatable. Usage:
  53. #
  54. # 1. write a ``FooConfig.cmake.in`` file as you are used to
  55. # 2. insert a line containing only the string ``@PACKAGE_INIT@``
  56. # 3. instead of ``set(FOO_DIR "@SOME_INSTALL_DIR@")``, use
  57. # ``set(FOO_DIR "@PACKAGE_SOME_INSTALL_DIR@")`` (this must be after the
  58. # ``@PACKAGE_INIT@`` line)
  59. # 4. instead of using the normal :command:`configure_file()`, use
  60. # ``configure_package_config_file()``
  61. #
  62. #
  63. #
  64. # The ``<input>`` and ``<output>`` arguments are the input and output file, the
  65. # same way as in :command:`configure_file()`.
  66. #
  67. # The ``<path>`` given to ``INSTALL_DESTINATION`` must be the destination where
  68. # the ``FooConfig.cmake`` file will be installed to. This path can either be
  69. # absolute, or relative to the ``INSTALL_PREFIX`` path.
  70. #
  71. # The variables ``<var1>`` to ``<varN>`` given as ``PATH_VARS`` are the
  72. # variables which contain install destinations. For each of them the macro will
  73. # create a helper variable ``PACKAGE_<var...>``. These helper variables must be
  74. # used in the ``FooConfig.cmake.in`` file for setting the installed location.
  75. # They are calculated by ``configure_package_config_file`` so that they are
  76. # always relative to the installed location of the package. This works both for
  77. # relative and also for absolute locations. For absolute locations it works
  78. # only if the absolute location is a subdirectory of ``INSTALL_PREFIX``.
  79. #
  80. # If the ``INSTALL_PREFIX`` argument is passed, this is used as base path to
  81. # calculate all the relative paths. The ``<path>`` argument must be an absolute
  82. # path. If this argument is not passed, the :variable:`CMAKE_INSTALL_PREFIX`
  83. # variable will be used instead. The default value is good when generating a
  84. # FooConfig.cmake file to use your package from the install tree. When
  85. # generating a FooConfig.cmake file to use your package from the build tree this
  86. # option should be used.
  87. #
  88. # By default ``configure_package_config_file`` also generates two helper macros,
  89. # ``set_and_check()`` and ``check_required_components()`` into the
  90. # ``FooConfig.cmake`` file.
  91. #
  92. # ``set_and_check()`` should be used instead of the normal ``set()`` command for
  93. # setting directories and file locations. Additionally to setting the variable
  94. # it also checks that the referenced file or directory actually exists and fails
  95. # with a ``FATAL_ERROR`` otherwise. This makes sure that the created
  96. # ``FooConfig.cmake`` file does not contain wrong references.
  97. # When using the ``NO_SET_AND_CHECK_MACRO``, this macro is not generated
  98. # into the ``FooConfig.cmake`` file.
  99. #
  100. # ``check_required_components(<package_name>)`` should be called at the end of
  101. # the ``FooConfig.cmake`` file if the package supports components. This macro
  102. # checks whether all requested, non-optional components have been found, and if
  103. # this is not the case, sets the ``Foo_FOUND`` variable to ``FALSE``, so that
  104. # the package is considered to be not found. It does that by testing the
  105. # ``Foo_<Component>_FOUND`` variables for all requested required components.
  106. # When using the ``NO_CHECK_REQUIRED_COMPONENTS_MACRO`` option, this macro is
  107. # not generated into the ``FooConfig.cmake`` file.
  108. #
  109. # For an example see below the documentation for
  110. # :command:`write_basic_package_version_file()`.
  111. #
  112. # Generating a Package Version File
  113. # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  114. #
  115. # .. command:: write_basic_package_version_file
  116. #
  117. # Create a version file for a project::
  118. #
  119. # write_basic_package_version_file(<filename>
  120. # [VERSION <major.minor.patch>]
  121. # COMPATIBILITY <AnyNewerVersion|SameMajorVersion|ExactVersion> )
  122. #
  123. #
  124. # Writes a file for use as ``<package>ConfigVersion.cmake`` file to
  125. # ``<filename>``. See the documentation of :command:`find_package()` for
  126. # details on this.
  127. #
  128. # ``<filename>`` is the output filename, it should be in the build tree.
  129. # ``<major.minor.patch>`` is the version number of the project to be installed.
  130. #
  131. # If no ``VERSION`` is given, the :variable:`PROJECT_VERSION` variable is used.
  132. # If this hasn't been set, it errors out.
  133. #
  134. # The ``COMPATIBILITY`` mode ``AnyNewerVersion`` means that the installed
  135. # package version will be considered compatible if it is newer or exactly the
  136. # same as the requested version. This mode should be used for packages which
  137. # are fully backward compatible, also across major versions.
  138. # If ``SameMajorVersion`` is used instead, then the behaviour differs from
  139. # ``AnyNewerVersion`` in that the major version number must be the same as
  140. # requested, e.g. version 2.0 will not be considered compatible if 1.0 is
  141. # requested. This mode should be used for packages which guarantee backward
  142. # compatibility within the same major version.
  143. # If ``ExactVersion`` is used, then the package is only considered compatible if
  144. # the requested version matches exactly its own version number (not considering
  145. # the tweak version). For example, version 1.2.3 of a package is only
  146. # considered compatible to requested version 1.2.3. This mode is for packages
  147. # without compatibility guarantees.
  148. # If your project has more elaborated version matching rules, you will need to
  149. # write your own custom ``ConfigVersion.cmake`` file instead of using this
  150. # macro.
  151. #
  152. # Internally, this macro executes :command:`configure_file()` to create the
  153. # resulting version file. Depending on the ``COMPATIBLITY``, either the file
  154. # ``BasicConfigVersion-SameMajorVersion.cmake.in`` or
  155. # ``BasicConfigVersion-AnyNewerVersion.cmake.in`` is used. Please note that
  156. # these two files are internal to CMake and you should not call
  157. # :command:`configure_file()` on them yourself, but they can be used as starting
  158. # point to create more sophisticted custom ``ConfigVersion.cmake`` files.
  159. #
  160. # Example Generating Package Files
  161. # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  162. #
  163. # Example using both :command:`configure_package_config_file` and
  164. # ``write_basic_package_version_file()``:
  165. #
  166. # ``CMakeLists.txt``:
  167. #
  168. # .. code-block:: cmake
  169. #
  170. # set(INCLUDE_INSTALL_DIR include/ ... CACHE )
  171. # set(LIB_INSTALL_DIR lib/ ... CACHE )
  172. # set(SYSCONFIG_INSTALL_DIR etc/foo/ ... CACHE )
  173. # ...
  174. # include(CMakePackageConfigHelpers)
  175. # configure_package_config_file(FooConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake
  176. # INSTALL_DESTINATION ${LIB_INSTALL_DIR}/Foo/cmake
  177. # PATH_VARS INCLUDE_INSTALL_DIR SYSCONFIG_INSTALL_DIR)
  178. # write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake
  179. # VERSION 1.2.3
  180. # COMPATIBILITY SameMajorVersion )
  181. # install(FILES ${CMAKE_CURRENT_BINARY_DIR}/FooConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/FooConfigVersion.cmake
  182. # DESTINATION ${LIB_INSTALL_DIR}/Foo/cmake )
  183. #
  184. # ``FooConfig.cmake.in``:
  185. #
  186. # .. code-block:: cmake
  187. #
  188. # set(FOO_VERSION x.y.z)
  189. # ...
  190. # @PACKAGE_INIT@
  191. # ...
  192. # set_and_check(FOO_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@")
  193. # set_and_check(FOO_SYSCONFIG_DIR "@PACKAGE_SYSCONFIG_INSTALL_DIR@")
  194. #
  195. # check_required_components(Foo)
  196. #=============================================================================
  197. # Copyright 2012 Alexander Neundorf <[email protected]>
  198. #
  199. # Distributed under the OSI-approved BSD License (the "License");
  200. # see accompanying file Copyright.txt for details.
  201. #
  202. # This software is distributed WITHOUT ANY WARRANTY; without even the
  203. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  204. # See the License for more information.
  205. #=============================================================================
  206. # (To distribute this file outside of CMake, substitute the full
  207. # License text for the above reference.)
  208. include(CMakeParseArguments)
  209. include(WriteBasicConfigVersionFile)
  210. macro(WRITE_BASIC_PACKAGE_VERSION_FILE)
  211. write_basic_config_version_file(${ARGN})
  212. endmacro()
  213. function(CONFIGURE_PACKAGE_CONFIG_FILE _inputFile _outputFile)
  214. set(options NO_SET_AND_CHECK_MACRO NO_CHECK_REQUIRED_COMPONENTS_MACRO)
  215. set(oneValueArgs INSTALL_DESTINATION INSTALL_PREFIX)
  216. set(multiValueArgs PATH_VARS )
  217. cmake_parse_arguments(CCF "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  218. if(CCF_UNPARSED_ARGUMENTS)
  219. message(FATAL_ERROR "Unknown keywords given to CONFIGURE_PACKAGE_CONFIG_FILE(): \"${CCF_UNPARSED_ARGUMENTS}\"")
  220. endif()
  221. if(NOT CCF_INSTALL_DESTINATION)
  222. message(FATAL_ERROR "No INSTALL_DESTINATION given to CONFIGURE_PACKAGE_CONFIG_FILE()")
  223. endif()
  224. if(DEFINED CCF_INSTALL_PREFIX)
  225. if(IS_ABSOLUTE "${CCF_INSTALL_PREFIX}")
  226. set(installPrefix "${CCF_INSTALL_PREFIX}")
  227. else()
  228. message(FATAL_ERROR "INSTALL_PREFIX must be an absolute path")
  229. endif()
  230. else()
  231. set(installPrefix "${CMAKE_INSTALL_PREFIX}")
  232. endif()
  233. if(IS_ABSOLUTE "${CCF_INSTALL_DESTINATION}")
  234. set(absInstallDir "${CCF_INSTALL_DESTINATION}")
  235. else()
  236. set(absInstallDir "${installPrefix}/${CCF_INSTALL_DESTINATION}")
  237. endif()
  238. file(RELATIVE_PATH PACKAGE_RELATIVE_PATH "${absInstallDir}" "${installPrefix}" )
  239. foreach(var ${CCF_PATH_VARS})
  240. if(NOT DEFINED ${var})
  241. message(FATAL_ERROR "Variable ${var} does not exist")
  242. else()
  243. if(IS_ABSOLUTE "${${var}}")
  244. string(REPLACE "${installPrefix}" "\${PACKAGE_PREFIX_DIR}"
  245. PACKAGE_${var} "${${var}}")
  246. else()
  247. set(PACKAGE_${var} "\${PACKAGE_PREFIX_DIR}/${${var}}")
  248. endif()
  249. endif()
  250. endforeach()
  251. get_filename_component(inputFileName "${_inputFile}" NAME)
  252. set(PACKAGE_INIT "
  253. ####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
  254. ####### Any changes to this file will be overwritten by the next CMake run ####
  255. ####### The input file was ${inputFileName} ########
  256. get_filename_component(PACKAGE_PREFIX_DIR \"\${CMAKE_CURRENT_LIST_DIR}/${PACKAGE_RELATIVE_PATH}\" ABSOLUTE)
  257. ")
  258. if("${absInstallDir}" MATCHES "^(/usr)?/lib(64)?/.+")
  259. # Handle "/usr move" symlinks created by some Linux distros.
  260. set(PACKAGE_INIT "${PACKAGE_INIT}
  261. # Use original install prefix when loaded through a \"/usr move\"
  262. # cross-prefix symbolic link such as /lib -> /usr/lib.
  263. get_filename_component(_realCurr \"\${CMAKE_CURRENT_LIST_DIR}\" REALPATH)
  264. get_filename_component(_realOrig \"${absInstallDir}\" REALPATH)
  265. if(_realCurr STREQUAL _realOrig)
  266. set(PACKAGE_PREFIX_DIR \"${installPrefix}\")
  267. endif()
  268. unset(_realOrig)
  269. unset(_realCurr)
  270. ")
  271. endif()
  272. if(NOT CCF_NO_SET_AND_CHECK_MACRO)
  273. set(PACKAGE_INIT "${PACKAGE_INIT}
  274. macro(set_and_check _var _file)
  275. set(\${_var} \"\${_file}\")
  276. if(NOT EXISTS \"\${_file}\")
  277. message(FATAL_ERROR \"File or directory \${_file} referenced by variable \${_var} does not exist !\")
  278. endif()
  279. endmacro()
  280. ")
  281. endif()
  282. if(NOT CCF_NO_CHECK_REQUIRED_COMPONENTS_MACRO)
  283. set(PACKAGE_INIT "${PACKAGE_INIT}
  284. macro(check_required_components _NAME)
  285. foreach(comp \${\${_NAME}_FIND_COMPONENTS})
  286. if(NOT \${_NAME}_\${comp}_FOUND)
  287. if(\${_NAME}_FIND_REQUIRED_\${comp})
  288. set(\${_NAME}_FOUND FALSE)
  289. endif()
  290. endif()
  291. endforeach()
  292. endmacro()
  293. ")
  294. endif()
  295. set(PACKAGE_INIT "${PACKAGE_INIT}
  296. ####################################################################################")
  297. configure_file("${_inputFile}" "${_outputFile}" @ONLY)
  298. endfunction()