cmake-packages.7.rst 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. .. cmake-manual-description: CMake Packages Reference
  2. cmake-packages(7)
  3. *****************
  4. .. only:: html
  5. .. contents::
  6. Introduction
  7. ============
  8. Packages provide dependency information to CMake based buildsystems. Packages
  9. are found with the :command:`find_package` command. The result of
  10. using ``find_package`` is either a set of :prop_tgt:`IMPORTED` targets, or
  11. a set of variables corresponding to build-relevant information.
  12. Using Packages
  13. ==============
  14. CMake provides direct support for two forms of packages,
  15. `Config-file Packages`_ and `Find-module Packages`_.
  16. Indirect support for ``pkg-config`` packages is also provided via
  17. the :module:`FindPkgConfig` module. In all cases, the basic form
  18. of :command:`find_package` calls is the same:
  19. .. code-block:: cmake
  20. find_package(Qt4 4.7.0 REQUIRED) # CMake provides a Qt4 find-module
  21. find_package(Qt5Core 5.1.0 REQUIRED) # Qt provides a Qt5 package config file.
  22. find_package(LibXml2 REQUIRED) # Use pkg-config via the LibXml2 find-module
  23. In cases where it is known that a package configuration file is provided by
  24. upstream, and only that should be used, the ``CONFIG`` keyword may be passed
  25. to :command:`find_package`:
  26. .. code-block:: cmake
  27. find_package(Qt5Core 5.1.0 CONFIG REQUIRED)
  28. find_package(Qt5Gui 5.1.0 CONFIG)
  29. Similarly, the ``MODULE`` keyword says to use only a find-module:
  30. .. code-block:: cmake
  31. find_package(Qt4 4.7.0 MODULE REQUIRED)
  32. Specifying the type of package explicitly improves the error message shown to
  33. the user if it is not found.
  34. Both types of packages also support specifying components of a package,
  35. either after the ``REQUIRED`` keyword:
  36. .. code-block:: cmake
  37. find_package(Qt5 5.1.0 CONFIG REQUIRED Widgets Xml Sql)
  38. or as a separate ``COMPONENTS`` list:
  39. .. code-block:: cmake
  40. find_package(Qt5 5.1.0 COMPONENTS Widgets Xml Sql)
  41. or as a separate ``OPTIONAL_COMPONENTS`` list:
  42. .. code-block:: cmake
  43. find_package(Qt5 5.1.0 COMPONENTS Widgets
  44. OPTIONAL_COMPONENTS Xml Sql
  45. )
  46. Handling of ``COMPONENTS`` and ``OPTIONAL_COMPONENTS`` is defined by the
  47. package.
  48. By setting the :variable:`CMAKE_DISABLE_FIND_PACKAGE_<PackageName>` variable to
  49. ``TRUE``, the ``PackageName`` package will not be searched, and will always
  50. be ``NOTFOUND``.
  51. Config-file Packages
  52. --------------------
  53. A config-file package is a set of files provided by upstreams for downstreams
  54. to use. CMake searches in a number of locations for package configuration files, as
  55. described in the :command:`find_package` documentation. The most simple way for
  56. a CMake user to tell :manual:`cmake(1)` to search in a non-standard prefix for
  57. a package is to set the ``CMAKE_PREFIX_PATH`` cache variable.
  58. Config-file packages are provided by upstream vendors as part of development
  59. packages, that is, they belong with the header files and any other files
  60. provided to assist downsteams in using the package.
  61. A set of variables which provide package status information are also set
  62. automatically when using a config-file package. The ``<Package>_FOUND``
  63. variable is set to true or false, depending on whether the package was
  64. found. The ``<Package>_DIR`` cache variable is set to the location of the
  65. package configuration file.
  66. Find-module Packages
  67. --------------------
  68. A find module is a file with a set of rules for finding the required pieces of
  69. a dependency, primarily header files and libraries. Typically, a find module
  70. is needed when the upstream is not built with CMake, or is not CMake-aware
  71. enough to otherwise provide a package configuration file. Unlike a package configuration
  72. file, it is not shipped with upstream, but is used by downstream to find the
  73. files by guessing locations of files with platform-specific hints.
  74. Unlike the case of an upstream-provided package configuration file, no single point
  75. of reference identifies the package as being found, so the ``<Package>_FOUND``
  76. variable is not automatically set by the :command:`find_package` command. It
  77. can still be expected to be set by convention however and should be set by
  78. the author of the Find-module. Similarly there is no ``<Package>_DIR`` variable,
  79. but each of the artifacts such as library locations and header file locations
  80. provide a separate cache variable.
  81. See the :manual:`cmake-developer(7)` manual for more information about creating
  82. Find-module files.
  83. Package Layout
  84. ==============
  85. A config-file package consists of a `Package Configuration File`_ and
  86. optionally a `Package Version File`_ provided with the project distribution.
  87. Package Configuration File
  88. --------------------------
  89. Consider a project ``Foo`` that installs the following files::
  90. <prefix>/include/foo-1.2/foo.h
  91. <prefix>/lib/foo-1.2/libfoo.a
  92. It may also provide a CMake package configuration file::
  93. <prefix>/lib/cmake/foo-1.2/FooConfig.cmake
  94. with content defining :prop_tgt:`IMPORTED` targets, or defining variables, such
  95. as:
  96. .. code-block:: cmake
  97. # ...
  98. # (compute PREFIX relative to file location)
  99. # ...
  100. set(Foo_INCLUDE_DIRS ${PREFIX}/include/foo-1.2)
  101. set(Foo_LIBRARIES ${PREFIX}/lib/foo-1.2/libfoo.a)
  102. If another project wishes to use ``Foo`` it need only to locate the ``FooConfig.cmake``
  103. file and load it to get all the information it needs about package content
  104. locations. Since the package configuration file is provided by the package
  105. installation it already knows all the file locations.
  106. The :command:`find_package` command may be used to search for the package
  107. configuration file. This command constructs a set of installation prefixes
  108. and searches under each prefix in several locations. Given the name ``Foo``,
  109. it looks for a file called ``FooConfig.cmake`` or ``foo-config.cmake``.
  110. The full set of locations is specified in the :command:`find_package` command
  111. documentation. One place it looks is::
  112. <prefix>/lib/cmake/Foo*/
  113. where ``Foo*`` is a case-insensitive globbing expression. In our example the
  114. globbing expression will match ``<prefix>/lib/cmake/foo-1.2`` and the package
  115. configuration file will be found.
  116. Once found, a package configuration file is immediately loaded. It, together
  117. with a package version file, contains all the information the project needs to
  118. use the package.
  119. Package Version File
  120. --------------------
  121. When the :command:`find_package` command finds a candidate package configuration
  122. file it looks next to it for a version file. The version file is loaded to test
  123. whether the package version is an acceptable match for the version requested.
  124. If the version file claims compatibility the configuration file is accepted.
  125. Otherwise it is ignored.
  126. The name of the package version file must match that of the package configuration
  127. file but has either ``-version`` or ``Version`` appended to the name before
  128. the ``.cmake`` extension. For example, the files::
  129. <prefix>/lib/cmake/foo-1.3/foo-config.cmake
  130. <prefix>/lib/cmake/foo-1.3/foo-config-version.cmake
  131. and::
  132. <prefix>/lib/cmake/bar-4.2/BarConfig.cmake
  133. <prefix>/lib/cmake/bar-4.2/BarConfigVersion.cmake
  134. are each pairs of package configuration files and corresponding package version
  135. files.
  136. When the :command:`find_package` command loads a version file it first sets the
  137. following variables:
  138. ``PACKAGE_FIND_NAME``
  139. The <package> name
  140. ``PACKAGE_FIND_VERSION``
  141. Full requested version string
  142. ``PACKAGE_FIND_VERSION_MAJOR``
  143. Major version if requested, else 0
  144. ``PACKAGE_FIND_VERSION_MINOR``
  145. Minor version if requested, else 0
  146. ``PACKAGE_FIND_VERSION_PATCH``
  147. Patch version if requested, else 0
  148. ``PACKAGE_FIND_VERSION_TWEAK``
  149. Tweak version if requested, else 0
  150. ``PACKAGE_FIND_VERSION_COUNT``
  151. Number of version components, 0 to 4
  152. The version file must use these variables to check whether it is compatible or
  153. an exact match for the requested version and set the following variables with
  154. results:
  155. ``PACKAGE_VERSION``
  156. Full provided version string
  157. ``PACKAGE_VERSION_EXACT``
  158. True if version is exact match
  159. ``PACKAGE_VERSION_COMPATIBLE``
  160. True if version is compatible
  161. ``PACKAGE_VERSION_UNSUITABLE``
  162. True if unsuitable as any version
  163. Version files are loaded in a nested scope so they are free to set any variables
  164. they wish as part of their computation. The find_package command wipes out the
  165. scope when the version file has completed and it has checked the output
  166. variables. When the version file claims to be an acceptable match for the
  167. requested version the find_package command sets the following variables for
  168. use by the project:
  169. ``<package>_VERSION``
  170. Full provided version string
  171. ``<package>_VERSION_MAJOR``
  172. Major version if provided, else 0
  173. ``<package>_VERSION_MINOR``
  174. Minor version if provided, else 0
  175. ``<package>_VERSION_PATCH``
  176. Patch version if provided, else 0
  177. ``<package>_VERSION_TWEAK``
  178. Tweak version if provided, else 0
  179. ``<package>_VERSION_COUNT``
  180. Number of version components, 0 to 4
  181. The variables report the version of the package that was actually found.
  182. The ``<package>`` part of their name matches the argument given to the
  183. :command:`find_package` command.
  184. .. _`Creating Packages`:
  185. Creating Packages
  186. =================
  187. Usually, the upstream depends on CMake itself and can use some CMake facilities
  188. for creating the package files. Consider an upstream which provides a single
  189. shared library:
  190. .. code-block:: cmake
  191. project(UpstreamLib)
  192. set(CMAKE_INCLUDE_CURRENT_DIR ON)
  193. set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON)
  194. set(Upstream_VERSION 3.4.1)
  195. include(GenerateExportHeader)
  196. add_library(ClimbingStats SHARED climbingstats.cpp)
  197. generate_export_header(ClimbingStats)
  198. set_property(TARGET ClimbingStats PROPERTY VERSION ${Upstream_VERSION})
  199. set_property(TARGET ClimbingStats PROPERTY SOVERSION 3)
  200. set_property(TARGET ClimbingStats PROPERTY
  201. INTERFACE_ClimbingStats_MAJOR_VERSION 3)
  202. set_property(TARGET ClimbingStats APPEND PROPERTY
  203. COMPATIBLE_INTERFACE_STRING ClimbingStats_MAJOR_VERSION
  204. )
  205. install(TARGETS ClimbingStats EXPORT ClimbingStatsTargets
  206. LIBRARY DESTINATION lib
  207. ARCHIVE DESTINATION lib
  208. RUNTIME DESTINATION bin
  209. INCLUDES DESTINATION include
  210. )
  211. install(
  212. FILES
  213. climbingstats.h
  214. "${CMAKE_CURRENT_BINARY_DIR}/climbingstats_export.h"
  215. DESTINATION
  216. include
  217. COMPONENT
  218. Devel
  219. )
  220. include(CMakePackageConfigHelpers)
  221. write_basic_package_version_file(
  222. "${CMAKE_CURRENT_BINARY_DIR}/ClimbingStats/ClimbingStatsConfigVersion.cmake"
  223. VERSION ${Upstream_VERSION}
  224. COMPATIBILITY AnyNewerVersion
  225. )
  226. export(EXPORT ClimbingStatsTargets
  227. FILE "${CMAKE_CURRENT_BINARY_DIR}/ClimbingStats/ClimbingStatsTargets.cmake"
  228. NAMESPACE Upstream::
  229. )
  230. configure_file(cmake/ClimbingStatsConfig.cmake
  231. "${CMAKE_CURRENT_BINARY_DIR}/ClimbingStats/ClimbingStatsConfig.cmake"
  232. COPYONLY
  233. )
  234. set(ConfigPackageLocation lib/cmake/ClimbingStats)
  235. install(EXPORT ClimbingStatsTargets
  236. FILE
  237. ClimbingStatsTargets.cmake
  238. NAMESPACE
  239. Upstream::
  240. DESTINATION
  241. ${ConfigPackageLocation}
  242. )
  243. install(
  244. FILES
  245. cmake/ClimbingStatsConfig.cmake
  246. "${CMAKE_CURRENT_BINARY_DIR}/ClimbingStats/ClimbingStatsConfigVersion.cmake"
  247. DESTINATION
  248. ${ConfigPackageLocation}
  249. COMPONENT
  250. Devel
  251. )
  252. The :module:`CMakePackageConfigHelpers` module provides a macro for creating
  253. a simple ``ConfigVersion.cmake`` file. This file sets the version of the
  254. package. It is read by CMake when :command:`find_package` is called to
  255. determine the compatibility with the requested version, and to set some
  256. version-specific variables ``<Package>_VERSION``, ``<Package>_VERSION_MAJOR``,
  257. ``<Package>_VERSION_MINOR`` etc. The :command:`install(EXPORT)` command is
  258. used to export the targets in the ``ClimbingStatsTargets`` export-set, defined
  259. previously by the :command:`install(TARGETS)` command. This command generates
  260. the ``ClimbingStatsTargets.cmake`` file to contain :prop_tgt:`IMPORTED`
  261. targets, suitable for use by downsteams and arranges to install it to
  262. ``lib/cmake/ClimbingStats``. The generated ``ClimbingStatsConfigVersion.cmake``
  263. and a ``cmake/ClimbingStatsConfig.cmake`` are installed to the same location,
  264. completing the package.
  265. The generated :prop_tgt:`IMPORTED` targets have appropriate properties set
  266. to define their usage requirements, such as
  267. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`,
  268. :prop_tgt:`INTERFACE_COMPILE_DEFINITIONS` and other relevant built-in
  269. ``INTERFACE_`` properties. The ``INTERFACE`` variant of user-defined
  270. properties listed in :prop_tgt:`COMPATIBLE_INTERFACE_STRING` and
  271. other :ref:`Compatible Interface Properties` are also propagated to the
  272. generated :prop_tgt:`IMPORTED` targets. In the above case,
  273. ``ClimbingStats_MAJOR_VERSION`` is defined as a string which must be
  274. compatible among the dependencies of any depender. By setting this custom
  275. defined user property in this version and in the next version of
  276. ``ClimbingStats``, :manual:`cmake(1)` will issue a diagnostic if there is an
  277. attempt to use version 3 together with version 4. Packages can choose to
  278. employ such a pattern if different major versions of the package are designed
  279. to be incompatible.
  280. Note that it is not advisable to populate any properties which may contain
  281. paths, such as :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` and
  282. :prop_tgt:`INTERFACE_LINK_LIBRARIES`, with paths relevnt to dependencies.
  283. That would hard-code into installed packages the include directory or library
  284. paths for dependencies **as found on the machine the package was made on**.
  285. That is, code like this is incorrect for targets which will be used to
  286. generate config file packages:
  287. .. code-block:: cmake
  288. target_link_libraries(ClimbingStats INTERFACE
  289. ${Boost_LIBRARIES};${OtherDep_LIBRARIES}>
  290. )
  291. target_include_directories(ClimbingStats INTERFACE
  292. $<INSTALL_INTERFACE:${Boost_INCLUDE_DIRS};${OtherDep_INCLUDE_DIRS}>
  293. )
  294. Dependencies must provide their own :ref:`IMPORTED targets <Imported Targets>`
  295. which have their own :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` and
  296. :prop_tgt:`IMPORTED_LOCATION` populated appropriately. Those
  297. :ref:`IMPORTED targets <Imported Targets>` may then be
  298. used with the :command:`target_link_libraries` command for ``ClimbingStats``.
  299. That way, when a consumer uses the installed package, the
  300. consumer will run the appropriate :command:`find_package` command (via the
  301. find_dependency macro described below) to find
  302. the dependencies on their own machine and populate the
  303. :ref:`IMPORTED targets <Imported Targets>` with appropriate paths. Note that
  304. many modules currently shipped with CMake do not currently provide
  305. :ref:`IMPORTED targets <Imported Targets>`.
  306. A ``NAMESPACE`` with double-colons is specified when exporting the targets
  307. for installation. This convention of double-colons gives CMake a hint that
  308. the name is an :prop_tgt:`IMPORTED` target when it is used by downstreams
  309. with the :command:`target_link_libraries` command. This way, CMake can
  310. issue a diagnostic if the package providing it has not yet been found.
  311. In this case, when using :command:`install(TARGETS)` the ``INCLUDES DESTINATION``
  312. was specified. This causes the ``IMPORTED`` targets to have their
  313. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` populated with the ``include``
  314. directory in the :variable:`CMAKE_INSTALL_PREFIX`. When the ``IMPORTED``
  315. target is used by downsteam, it automatically consumes the entries from
  316. that property.
  317. In this case, the ``ClimbingStatsConfig.cmake`` file could be as simple as:
  318. .. code-block:: cmake
  319. include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStatsTargets.cmake")
  320. As this allows downstreams to use the ``IMPORTED`` targets. If any macros
  321. should be provided by the ``ClimbingStats`` package, they should
  322. be in a separate file which is installed to the same location as the
  323. ``ClimbingStatsConfig.cmake`` file, and included from there.
  324. Packages created by :command:`install(EXPORT)` are designed to be relocatable,
  325. using paths relative to the location of the package itself. When defining
  326. the interface of a target for ``EXPORT``, keep in mind that the include
  327. directories should be specified as relative paths which are relative to the
  328. :variable:`CMAKE_INSTALL_PREFIX`:
  329. .. code-block:: cmake
  330. target_include_directories(tgt INTERFACE
  331. # Wrong, not relocatable:
  332. $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include/TgtName>
  333. )
  334. target_include_directories(tgt INTERFACE
  335. # Ok, relocatable:
  336. $<INSTALL_INTERFACE:include/TgtName>
  337. )
  338. The ``$<INSTALL_PREFIX>``
  339. :manual:`generator expression <cmake-generator-expressions(7)>` may be used as
  340. a placeholder for the install prefix without resulting in a non-relocatable
  341. package. This is necessary if complex generator expressions are used:
  342. .. code-block:: cmake
  343. target_include_directories(tgt INTERFACE
  344. # Ok, relocatable:
  345. $<INSTALL_INTERFACE:$<$<CONFIG:Debug>:$<INSTALL_PREFIX>/include/TgtName>>
  346. )
  347. The :command:`export(EXPORT)` command creates an :prop_tgt:`IMPORTED` targets
  348. definition file which is specific to the build-tree, and is not relocatable.
  349. This can similiarly be used with a suitable package configuration file and
  350. package version file to define a package for the build tree which may be used
  351. without installation. Consumers of the build tree can simply ensure that the
  352. :variable:`CMAKE_PREFIX_PATH` contains the build directory, or set the
  353. ``ClimbingStats_DIR`` to ``<build_dir>/ClimbingStats`` in the cache.
  354. This can also be extended to cover dependencies:
  355. .. code-block:: cmake
  356. # ...
  357. add_library(ClimbingStats SHARED climbingstats.cpp)
  358. generate_export_header(ClimbingStats)
  359. find_package(Stats 2.6.4 REQUIRED)
  360. target_link_libraries(ClimbingStats PUBLIC Stats::Types)
  361. As the ``Stats::Types`` target is a ``PUBLIC`` dependency of ``ClimbingStats``,
  362. downsteams must also find the ``Stats`` package and link to the ``Stats::Types``
  363. library. The ``Stats`` package should be found in the ``ClimbingStatsConfig.cmake``
  364. file to ensure this. The ``find_dependency`` macro from the
  365. :module:`CMakeFindDependencyMacro` helps with this by propagating
  366. whether the package is ``REQUIRED``, or ``QUIET`` etc. All ``REQUIRED``
  367. dependencies of a package should be found in the ``Config.cmake`` file:
  368. .. code-block:: cmake
  369. include(CMakeFindDependencyMacro)
  370. find_dependency(Stats 2.6.4)
  371. include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStatsTargets.cmake")
  372. include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStatsMacros.cmake")
  373. The ``find_dependency`` macro also sets ``ClimbingStats_FOUND`` to ``False`` if
  374. the dependency is not found, along with a diagnostic that the ``ClimbingStats``
  375. package can not be used without the ``Stats`` package.
  376. If ``COMPONENTS`` are specified when the downstream uses :command:`find_package`,
  377. they are listed in the ``<Package>_FIND_COMPONENTS`` variable. If a particular
  378. component is non-optional, then the ``<Package>_FIND_REQUIRED_<comp>`` will
  379. be true. This can be tested with logic in the package configuration file:
  380. .. code-block:: cmake
  381. include(CMakeFindDependencyMacro)
  382. find_dependency(Stats 2.6.4)
  383. include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStatsTargets.cmake")
  384. include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStatsMacros.cmake")
  385. set(_supported_components Plot Table)
  386. foreach(_comp ${ClimbingStats_FIND_COMPONENTS})
  387. if (NOT ";${_supported_components};" MATCHES _comp)
  388. set(ClimbingStats_FOUND False)
  389. set(ClimbingStats_NOTFOUND_MESSAGE "Unsupported component: ${_comp}")
  390. endif()
  391. include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStats${_comp}Targets.cmake")
  392. endforeach()
  393. Here, the ``ClimbingStats_NOTFOUND_MESSAGE`` is set to a diagnosis that the package
  394. could not be found because an invalid component was specified. This message
  395. variable can be set for any case where the ``_FOUND`` variable is set to ``False``,
  396. and will be displayed to the user.
  397. .. _`Package Registry`:
  398. Package Registry
  399. ================
  400. CMake provides two central locations to register packages that have
  401. been built or installed anywhere on a system:
  402. * `User Package Registry`_
  403. * `System Package Registry`_
  404. The registries are especially useful to help projects find packages in
  405. non-standard install locations or directly in their own build trees.
  406. A project may populate either the user or system registry (using its own
  407. means, see below) to refer to its location.
  408. In either case the package should store at the registered location a
  409. `Package Configuration File`_ (``<package>Config.cmake``) and optionally a
  410. `Package Version File`_ (``<package>ConfigVersion.cmake``).
  411. The :command:`find_package` command searches the two package registries
  412. as two of the search steps specified in its documentation. If it has
  413. sufficient permissions it also removes stale package registry entries
  414. that refer to directories that do not exist or do not contain a matching
  415. package configuration file.
  416. .. _`User Package Registry`:
  417. User Package Registry
  418. ---------------------
  419. The User Package Registry is stored in a per-user location.
  420. The :command:`export(PACKAGE)` command may be used to register a project
  421. build tree in the user package registry. CMake currently provides no
  422. interface to add install trees to the user package registry. Installers
  423. must be manually taught to register their packages if desired.
  424. On Windows the user package registry is stored in the Windows registry
  425. under a key in ``HKEY_CURRENT_USER``.
  426. A ``<package>`` may appear under registry key::
  427. HKEY_CURRENT_USER\Software\Kitware\CMake\Packages\<package>
  428. as a ``REG_SZ`` value, with arbitrary name, that specifies the directory
  429. containing the package configuration file.
  430. On UNIX platforms the user package registry is stored in the user home
  431. directory under ``~/.cmake/packages``. A ``<package>`` may appear under
  432. the directory::
  433. ~/.cmake/packages/<package>
  434. as a file, with arbitrary name, whose content specifies the directory
  435. containing the package configuration file.
  436. .. _`System Package Registry`:
  437. System Package Registry
  438. -----------------------
  439. The System Package Registry is stored in a system-wide location.
  440. CMake currently provides no interface to add to the system package registry.
  441. Installers must be manually taught to register their packages if desired.
  442. On Windows the system package registry is stored in the Windows registry
  443. under a key in ``HKEY_LOCAL_MACHINE``. A ``<package>`` may appear under
  444. registry key::
  445. HKEY_LOCAL_MACHINE\Software\Kitware\CMake\Packages\<package>
  446. as a ``REG_SZ`` value, with arbitrary name, that specifies the directory
  447. containing the package configuration file.
  448. There is no system package registry on non-Windows platforms.
  449. .. _`Disabling the Package Registry`:
  450. Disabling the Package Registry
  451. ------------------------------
  452. In some cases using the Package Registries is not desirable. CMake
  453. allows to disable them using the following variables:
  454. * :variable:`CMAKE_EXPORT_NO_PACKAGE_REGISTRY` disables the
  455. :command:`export(PACKAGE)` command.
  456. * :variable:`CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY` disables the
  457. User Package Registry in all the :command:`find_package` calls.
  458. * :variable:`CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY` disables
  459. the System Package Registry in all the :command:`find_package` calls.
  460. Package Registry Example
  461. ------------------------
  462. A simple convention for naming package registry entries is to use content
  463. hashes. They are deterministic and unlikely to collide
  464. (:command:`export(PACKAGE)` uses this approach).
  465. The name of an entry referencing a specific directory is simply the content
  466. hash of the directory path itself.
  467. If a project arranges for package registry entries to exist, such as::
  468. > reg query HKCU\Software\Kitware\CMake\Packages\MyPackage
  469. HKEY_CURRENT_USER\Software\Kitware\CMake\Packages\MyPackage
  470. 45e7d55f13b87179bb12f907c8de6fc4 REG_SZ c:/Users/Me/Work/lib/cmake/MyPackage
  471. 7b4a9844f681c80ce93190d4e3185db9 REG_SZ c:/Users/Me/Work/MyPackage-build
  472. or::
  473. $ cat ~/.cmake/packages/MyPackage/7d1fb77e07ce59a81bed093bbee945bd
  474. /home/me/work/lib/cmake/MyPackage
  475. $ cat ~/.cmake/packages/MyPackage/f92c1db873a1937f3100706657c63e07
  476. /home/me/work/MyPackage-build
  477. then the ``CMakeLists.txt`` code:
  478. .. code-block:: cmake
  479. find_package(MyPackage)
  480. will search the registered locations for package configuration files
  481. (``MyPackageConfig.cmake``). The search order among package registry
  482. entries for a single package is unspecified and the entry names
  483. (hashes in this example) have no meaning. Registered locations may
  484. contain package version files (``MyPackageConfigVersion.cmake``) to
  485. tell :command:`find_package` whether a specific location is suitable
  486. for the version requested.
  487. Package Registry Ownership
  488. --------------------------
  489. Package registry entries are individually owned by the project installations
  490. that they reference. A package installer is responsible for adding its own
  491. entry and the corresponding uninstaller is responsible for removing it.
  492. The :command:`export(PACKAGE)` command populates the user package registry
  493. with the location of a project build tree. Build trees tend to be deleted by
  494. developers and have no "uninstall" event that could trigger removal of their
  495. entries. In order to keep the registries clean the :command:`find_package`
  496. command automatically removes stale entries it encounters if it has sufficient
  497. permissions. CMake provides no interface to remove an entry referencing an
  498. existing build tree once :command:`export(PACKAGE)` has been invoked.
  499. However, if the project removes its package configuration file from the build
  500. tree then the entry referencing the location will be considered stale.