CMakePackageConfigHelpers.cmake 13 KB

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