1.. cmake-manual-description: CMake Packages Reference
2
3cmake-packages(7)
4*****************
5
6.. only:: html
7
8   .. contents::
9
10Introduction
11============
12
13Packages provide dependency information to CMake based buildsystems.  Packages
14are found with the :command:`find_package` command.  The result of
15using :command:`find_package` is either a set of :prop_tgt:`IMPORTED` targets, or
16a set of variables corresponding to build-relevant information.
17
18Using Packages
19==============
20
21CMake provides direct support for two forms of packages,
22`Config-file Packages`_ and `Find-module Packages`_.
23Indirect support for ``pkg-config`` packages is also provided via
24the :module:`FindPkgConfig` module.  In all cases, the basic form
25of :command:`find_package` calls is the same:
26
27.. code-block:: cmake
28
29  find_package(Qt4 4.7.0 REQUIRED) # CMake provides a Qt4 find-module
30  find_package(Qt5Core 5.1.0 REQUIRED) # Qt provides a Qt5 package config file.
31  find_package(LibXml2 REQUIRED) # Use pkg-config via the LibXml2 find-module
32
33In cases where it is known that a package configuration file is provided by
34upstream, and only that should be used, the ``CONFIG`` keyword may be passed
35to :command:`find_package`:
36
37.. code-block:: cmake
38
39  find_package(Qt5Core 5.1.0 CONFIG REQUIRED)
40  find_package(Qt5Gui 5.1.0 CONFIG)
41
42Similarly, the ``MODULE`` keyword says to use only a find-module:
43
44.. code-block:: cmake
45
46  find_package(Qt4 4.7.0 MODULE REQUIRED)
47
48Specifying the type of package explicitly improves the error message shown to
49the user if it is not found.
50
51Both types of packages also support specifying components of a package,
52either after the ``REQUIRED`` keyword:
53
54.. code-block:: cmake
55
56  find_package(Qt5 5.1.0 CONFIG REQUIRED Widgets Xml Sql)
57
58or as a separate ``COMPONENTS`` list:
59
60.. code-block:: cmake
61
62  find_package(Qt5 5.1.0 COMPONENTS Widgets Xml Sql)
63
64or as a separate ``OPTIONAL_COMPONENTS`` list:
65
66.. code-block:: cmake
67
68  find_package(Qt5 5.1.0 COMPONENTS Widgets
69                         OPTIONAL_COMPONENTS Xml Sql
70  )
71
72Handling of ``COMPONENTS`` and ``OPTIONAL_COMPONENTS`` is defined by the
73package.
74
75By setting the :variable:`CMAKE_DISABLE_FIND_PACKAGE_<PackageName>` variable to
76``TRUE``, the ``<PackageName>`` package will not be searched, and will always
77be ``NOTFOUND``. Likewise, setting the
78:variable:`CMAKE_REQUIRE_FIND_PACKAGE_<PackageName>` to ``TRUE`` will make the
79package REQUIRED.
80
81.. _`Config File Packages`:
82
83Config-file Packages
84--------------------
85
86A config-file package is a set of files provided by upstreams for downstreams
87to use. CMake searches in a number of locations for package configuration files, as
88described in the :command:`find_package` documentation.  The most simple way for
89a CMake user to tell :manual:`cmake(1)` to search in a non-standard prefix for
90a package is to set the ``CMAKE_PREFIX_PATH`` cache variable.
91
92Config-file packages are provided by upstream vendors as part of development
93packages, that is, they belong with the header files and any other files
94provided to assist downstreams in using the package.
95
96A set of variables which provide package status information are also set
97automatically when using a config-file package.  The ``<PackageName>_FOUND``
98variable is set to true or false, depending on whether the package was
99found.  The ``<PackageName>_DIR`` cache variable is set to the location of the
100package configuration file.
101
102Find-module Packages
103--------------------
104
105A find module is a file with a set of rules for finding the required pieces of
106a dependency, primarily header files and libraries.  Typically, a find module
107is needed when the upstream is not built with CMake, or is not CMake-aware
108enough to otherwise provide a package configuration file.  Unlike a package configuration
109file, it is not shipped with upstream, but is used by downstream to find the
110files by guessing locations of files with platform-specific hints.
111
112Unlike the case of an upstream-provided package configuration file, no single point
113of reference identifies the package as being found, so the ``<PackageName>_FOUND``
114variable is not automatically set by the :command:`find_package` command.  It
115can still be expected to be set by convention however and should be set by
116the author of the Find-module.  Similarly there is no ``<PackageName>_DIR`` variable,
117but each of the artifacts such as library locations and header file locations
118provide a separate cache variable.
119
120See the :manual:`cmake-developer(7)` manual for more information about creating
121Find-module files.
122
123Package Layout
124==============
125
126A config-file package consists of a `Package Configuration File`_ and
127optionally a `Package Version File`_ provided with the project distribution.
128
129Package Configuration File
130--------------------------
131
132Consider a project ``Foo`` that installs the following files::
133
134  <prefix>/include/foo-1.2/foo.h
135  <prefix>/lib/foo-1.2/libfoo.a
136
137It may also provide a CMake package configuration file::
138
139  <prefix>/lib/cmake/foo-1.2/FooConfig.cmake
140
141with content defining :prop_tgt:`IMPORTED` targets, or defining variables, such
142as:
143
144.. code-block:: cmake
145
146  # ...
147  # (compute PREFIX relative to file location)
148  # ...
149  set(Foo_INCLUDE_DIRS ${PREFIX}/include/foo-1.2)
150  set(Foo_LIBRARIES ${PREFIX}/lib/foo-1.2/libfoo.a)
151
152If another project wishes to use ``Foo`` it need only to locate the ``FooConfig.cmake``
153file and load it to get all the information it needs about package content
154locations.  Since the package configuration file is provided by the package
155installation it already knows all the file locations.
156
157The :command:`find_package` command may be used to search for the package
158configuration file.  This command constructs a set of installation prefixes
159and searches under each prefix in several locations.  Given the name ``Foo``,
160it looks for a file called ``FooConfig.cmake`` or ``foo-config.cmake``.
161The full set of locations is specified in the :command:`find_package` command
162documentation. One place it looks is::
163
164 <prefix>/lib/cmake/Foo*/
165
166where ``Foo*`` is a case-insensitive globbing expression.  In our example the
167globbing expression will match ``<prefix>/lib/cmake/foo-1.2`` and the package
168configuration file will be found.
169
170Once found, a package configuration file is immediately loaded.  It, together
171with a package version file, contains all the information the project needs to
172use the package.
173
174Package Version File
175--------------------
176
177When the :command:`find_package` command finds a candidate package configuration
178file it looks next to it for a version file. The version file is loaded to test
179whether the package version is an acceptable match for the version requested.
180If the version file claims compatibility the configuration file is accepted.
181Otherwise it is ignored.
182
183The name of the package version file must match that of the package configuration
184file but has either ``-version`` or ``Version`` appended to the name before
185the ``.cmake`` extension.  For example, the files::
186
187 <prefix>/lib/cmake/foo-1.3/foo-config.cmake
188 <prefix>/lib/cmake/foo-1.3/foo-config-version.cmake
189
190and::
191
192 <prefix>/lib/cmake/bar-4.2/BarConfig.cmake
193 <prefix>/lib/cmake/bar-4.2/BarConfigVersion.cmake
194
195are each pairs of package configuration files and corresponding package version
196files.
197
198When the :command:`find_package` command loads a version file it first sets the
199following variables:
200
201``PACKAGE_FIND_NAME``
202 The ``<PackageName>``
203
204``PACKAGE_FIND_VERSION``
205 Full requested version string
206
207``PACKAGE_FIND_VERSION_MAJOR``
208 Major version if requested, else 0
209
210``PACKAGE_FIND_VERSION_MINOR``
211 Minor version if requested, else 0
212
213``PACKAGE_FIND_VERSION_PATCH``
214 Patch version if requested, else 0
215
216``PACKAGE_FIND_VERSION_TWEAK``
217 Tweak version if requested, else 0
218
219``PACKAGE_FIND_VERSION_COUNT``
220 Number of version components, 0 to 4
221
222The version file must use these variables to check whether it is compatible or
223an exact match for the requested version and set the following variables with
224results:
225
226``PACKAGE_VERSION``
227 Full provided version string
228
229``PACKAGE_VERSION_EXACT``
230 True if version is exact match
231
232``PACKAGE_VERSION_COMPATIBLE``
233 True if version is compatible
234
235``PACKAGE_VERSION_UNSUITABLE``
236 True if unsuitable as any version
237
238Version files are loaded in a nested scope so they are free to set any variables
239they wish as part of their computation. The find_package command wipes out the
240scope when the version file has completed and it has checked the output
241variables. When the version file claims to be an acceptable match for the
242requested version the find_package command sets the following variables for
243use by the project:
244
245``<PackageName>_VERSION``
246 Full provided version string
247
248``<PackageName>_VERSION_MAJOR``
249 Major version if provided, else 0
250
251``<PackageName>_VERSION_MINOR``
252 Minor version if provided, else 0
253
254``<PackageName>_VERSION_PATCH``
255 Patch version if provided, else 0
256
257``<PackageName>_VERSION_TWEAK``
258 Tweak version if provided, else 0
259
260``<PackageName>_VERSION_COUNT``
261 Number of version components, 0 to 4
262
263The variables report the version of the package that was actually found.
264The ``<PackageName>`` part of their name matches the argument given to the
265:command:`find_package` command.
266
267.. _`Creating Packages`:
268
269Creating Packages
270=================
271
272Usually, the upstream depends on CMake itself and can use some CMake facilities
273for creating the package files. Consider an upstream which provides a single
274shared library:
275
276.. code-block:: cmake
277
278  project(UpstreamLib)
279
280  set(CMAKE_INCLUDE_CURRENT_DIR ON)
281  set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON)
282
283  set(Upstream_VERSION 3.4.1)
284
285  include(GenerateExportHeader)
286
287  add_library(ClimbingStats SHARED climbingstats.cpp)
288  generate_export_header(ClimbingStats)
289  set_property(TARGET ClimbingStats PROPERTY VERSION ${Upstream_VERSION})
290  set_property(TARGET ClimbingStats PROPERTY SOVERSION 3)
291  set_property(TARGET ClimbingStats PROPERTY
292    INTERFACE_ClimbingStats_MAJOR_VERSION 3)
293  set_property(TARGET ClimbingStats APPEND PROPERTY
294    COMPATIBLE_INTERFACE_STRING ClimbingStats_MAJOR_VERSION
295  )
296
297  install(TARGETS ClimbingStats EXPORT ClimbingStatsTargets
298    LIBRARY DESTINATION lib
299    ARCHIVE DESTINATION lib
300    RUNTIME DESTINATION bin
301    INCLUDES DESTINATION include
302  )
303  install(
304    FILES
305      climbingstats.h
306      "${CMAKE_CURRENT_BINARY_DIR}/climbingstats_export.h"
307    DESTINATION
308      include
309    COMPONENT
310      Devel
311  )
312
313  include(CMakePackageConfigHelpers)
314  write_basic_package_version_file(
315    "${CMAKE_CURRENT_BINARY_DIR}/ClimbingStats/ClimbingStatsConfigVersion.cmake"
316    VERSION ${Upstream_VERSION}
317    COMPATIBILITY AnyNewerVersion
318  )
319
320  export(EXPORT ClimbingStatsTargets
321    FILE "${CMAKE_CURRENT_BINARY_DIR}/ClimbingStats/ClimbingStatsTargets.cmake"
322    NAMESPACE Upstream::
323  )
324  configure_file(cmake/ClimbingStatsConfig.cmake
325    "${CMAKE_CURRENT_BINARY_DIR}/ClimbingStats/ClimbingStatsConfig.cmake"
326    COPYONLY
327  )
328
329  set(ConfigPackageLocation lib/cmake/ClimbingStats)
330  install(EXPORT ClimbingStatsTargets
331    FILE
332      ClimbingStatsTargets.cmake
333    NAMESPACE
334      Upstream::
335    DESTINATION
336      ${ConfigPackageLocation}
337  )
338  install(
339    FILES
340      cmake/ClimbingStatsConfig.cmake
341      "${CMAKE_CURRENT_BINARY_DIR}/ClimbingStats/ClimbingStatsConfigVersion.cmake"
342    DESTINATION
343      ${ConfigPackageLocation}
344    COMPONENT
345      Devel
346  )
347
348The :module:`CMakePackageConfigHelpers` module provides a macro for creating
349a simple ``ConfigVersion.cmake`` file.  This file sets the version of the
350package.  It is read by CMake when :command:`find_package` is called to
351determine the compatibility with the requested version, and to set some
352version-specific variables ``<PackageName>_VERSION``, ``<PackageName>_VERSION_MAJOR``,
353``<PackageName>_VERSION_MINOR`` etc.  The :command:`install(EXPORT)` command is
354used to export the targets in the ``ClimbingStatsTargets`` export-set, defined
355previously by the :command:`install(TARGETS)` command. This command generates
356the ``ClimbingStatsTargets.cmake`` file to contain :prop_tgt:`IMPORTED`
357targets, suitable for use by downstreams and arranges to install it to
358``lib/cmake/ClimbingStats``.  The generated ``ClimbingStatsConfigVersion.cmake``
359and a ``cmake/ClimbingStatsConfig.cmake`` are installed to the same location,
360completing the package.
361
362The generated :prop_tgt:`IMPORTED` targets have appropriate properties set
363to define their :ref:`usage requirements <Target Usage Requirements>`, such as
364:prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`,
365:prop_tgt:`INTERFACE_COMPILE_DEFINITIONS` and other relevant built-in
366``INTERFACE_`` properties.  The ``INTERFACE`` variant of user-defined
367properties listed in :prop_tgt:`COMPATIBLE_INTERFACE_STRING` and
368other :ref:`Compatible Interface Properties` are also propagated to the
369generated :prop_tgt:`IMPORTED` targets.  In the above case,
370``ClimbingStats_MAJOR_VERSION`` is defined as a string which must be
371compatible among the dependencies of any depender.  By setting this custom
372defined user property in this version and in the next version of
373``ClimbingStats``, :manual:`cmake(1)` will issue a diagnostic if there is an
374attempt to use version 3 together with version 4.  Packages can choose to
375employ such a pattern if different major versions of the package are designed
376to be incompatible.
377
378A ``NAMESPACE`` with double-colons is specified when exporting the targets
379for installation.  This convention of double-colons gives CMake a hint that
380the name is an :prop_tgt:`IMPORTED` target when it is used by downstreams
381with the :command:`target_link_libraries` command.  This way, CMake can
382issue a diagnostic if the package providing it has not yet been found.
383
384In this case, when using :command:`install(TARGETS)` the ``INCLUDES DESTINATION``
385was specified.  This causes the ``IMPORTED`` targets to have their
386:prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` populated with the ``include``
387directory in the :variable:`CMAKE_INSTALL_PREFIX`.  When the ``IMPORTED``
388target is used by downstream, it automatically consumes the entries from
389that property.
390
391Creating a Package Configuration File
392-------------------------------------
393
394In this case, the ``ClimbingStatsConfig.cmake`` file could be as simple as:
395
396.. code-block:: cmake
397
398  include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStatsTargets.cmake")
399
400As this allows downstreams to use the ``IMPORTED`` targets.  If any macros
401should be provided by the ``ClimbingStats`` package, they should
402be in a separate file which is installed to the same location as the
403``ClimbingStatsConfig.cmake`` file, and included from there.
404
405This can also be extended to cover dependencies:
406
407.. code-block:: cmake
408
409  # ...
410  add_library(ClimbingStats SHARED climbingstats.cpp)
411  generate_export_header(ClimbingStats)
412
413  find_package(Stats 2.6.4 REQUIRED)
414  target_link_libraries(ClimbingStats PUBLIC Stats::Types)
415
416As the ``Stats::Types`` target is a ``PUBLIC`` dependency of ``ClimbingStats``,
417downstreams must also find the ``Stats`` package and link to the ``Stats::Types``
418library.  The ``Stats`` package should be found in the ``ClimbingStatsConfig.cmake``
419file to ensure this.  The ``find_dependency`` macro from the
420:module:`CMakeFindDependencyMacro` helps with this by propagating
421whether the package is ``REQUIRED``, or ``QUIET`` etc.  All ``REQUIRED``
422dependencies of a package should be found in the ``Config.cmake`` file:
423
424.. code-block:: cmake
425
426  include(CMakeFindDependencyMacro)
427  find_dependency(Stats 2.6.4)
428
429  include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStatsTargets.cmake")
430  include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStatsMacros.cmake")
431
432The ``find_dependency`` macro also sets ``ClimbingStats_FOUND`` to ``False`` if
433the dependency is not found, along with a diagnostic that the ``ClimbingStats``
434package can not be used without the ``Stats`` package.
435
436If ``COMPONENTS`` are specified when the downstream uses :command:`find_package`,
437they are listed in the ``<PackageName>_FIND_COMPONENTS`` variable. If a particular
438component is non-optional, then the ``<PackageName>_FIND_REQUIRED_<comp>`` will
439be true. This can be tested with logic in the package configuration file:
440
441.. code-block:: cmake
442
443  include(CMakeFindDependencyMacro)
444  find_dependency(Stats 2.6.4)
445
446  include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStatsTargets.cmake")
447  include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStatsMacros.cmake")
448
449  set(_supported_components Plot Table)
450
451  foreach(_comp ${ClimbingStats_FIND_COMPONENTS})
452    if (NOT ";${_supported_components};" MATCHES ";${_comp};")
453      set(ClimbingStats_FOUND False)
454      set(ClimbingStats_NOT_FOUND_MESSAGE "Unsupported component: ${_comp}")
455    endif()
456    include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStats${_comp}Targets.cmake")
457  endforeach()
458
459Here, the ``ClimbingStats_NOT_FOUND_MESSAGE`` is set to a diagnosis that the package
460could not be found because an invalid component was specified.  This message
461variable can be set for any case where the ``_FOUND`` variable is set to ``False``,
462and will be displayed to the user.
463
464Creating a Package Configuration File for the Build Tree
465^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
466
467The :command:`export(EXPORT)` command creates an :prop_tgt:`IMPORTED` targets
468definition file which is specific to the build-tree, and is not relocatable.
469This can similarly be used with a suitable package configuration file and
470package version file to define a package for the build tree which may be used
471without installation.  Consumers of the build tree can simply ensure that the
472:variable:`CMAKE_PREFIX_PATH` contains the build directory, or set the
473``ClimbingStats_DIR`` to ``<build_dir>/ClimbingStats`` in the cache.
474
475.. _`Creating Relocatable Packages`:
476
477Creating Relocatable Packages
478-----------------------------
479
480A relocatable package must not reference absolute paths of files on
481the machine where the package is built that will not exist on the
482machines where the package may be installed.
483
484Packages created by :command:`install(EXPORT)` are designed to be relocatable,
485using paths relative to the location of the package itself.  When defining
486the interface of a target for ``EXPORT``, keep in mind that the include
487directories should be specified as relative paths which are relative to the
488:variable:`CMAKE_INSTALL_PREFIX`:
489
490.. code-block:: cmake
491
492  target_include_directories(tgt INTERFACE
493    # Wrong, not relocatable:
494    $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include/TgtName>
495  )
496
497  target_include_directories(tgt INTERFACE
498    # Ok, relocatable:
499    $<INSTALL_INTERFACE:include/TgtName>
500  )
501
502The ``$<INSTALL_PREFIX>``
503:manual:`generator expression <cmake-generator-expressions(7)>` may be used as
504a placeholder for the install prefix without resulting in a non-relocatable
505package.  This is necessary if complex generator expressions are used:
506
507.. code-block:: cmake
508
509  target_include_directories(tgt INTERFACE
510    # Ok, relocatable:
511    $<INSTALL_INTERFACE:$<$<CONFIG:Debug>:$<INSTALL_PREFIX>/include/TgtName>>
512  )
513
514This also applies to paths referencing external dependencies.
515It is not advisable to populate any properties which may contain
516paths, such as :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` and
517:prop_tgt:`INTERFACE_LINK_LIBRARIES`, with paths relevant to dependencies.
518For example, this code may not work well for a relocatable package:
519
520.. code-block:: cmake
521
522  target_link_libraries(ClimbingStats INTERFACE
523    ${Foo_LIBRARIES} ${Bar_LIBRARIES}
524    )
525  target_include_directories(ClimbingStats INTERFACE
526    "$<INSTALL_INTERFACE:${Foo_INCLUDE_DIRS};${Bar_INCLUDE_DIRS}>"
527    )
528
529The referenced variables may contain the absolute paths to libraries
530and include directories **as found on the machine the package was made on**.
531This would create a package with hard-coded paths to dependencies and not
532suitable for relocation.
533
534Ideally such dependencies should be used through their own
535:ref:`IMPORTED targets <Imported Targets>` that have their own
536:prop_tgt:`IMPORTED_LOCATION` and usage requirement properties
537such as :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` populated
538appropriately.  Those imported targets may then be used with
539the :command:`target_link_libraries` command for ``ClimbingStats``:
540
541.. code-block:: cmake
542
543  target_link_libraries(ClimbingStats INTERFACE Foo::Foo Bar::Bar)
544
545With this approach the package references its external dependencies
546only through the names of :ref:`IMPORTED targets <Imported Targets>`.
547When a consumer uses the installed package, the consumer will run the
548appropriate :command:`find_package` commands (via the ``find_dependency``
549macro described above) to find the dependencies and populate the
550imported targets with appropriate paths on their own machine.
551
552Unfortunately many :manual:`modules <cmake-modules(7)>` shipped with
553CMake do not yet provide :ref:`IMPORTED targets <Imported Targets>`
554because their development pre-dated this approach.  This may improve
555incrementally over time.  Workarounds to create relocatable packages
556using such modules include:
557
558* When building the package, specify each ``Foo_LIBRARY`` cache
559  entry as just a library name, e.g. ``-DFoo_LIBRARY=foo``.  This
560  tells the corresponding find module to populate the ``Foo_LIBRARIES``
561  with just ``foo`` to ask the linker to search for the library
562  instead of hard-coding a path.
563
564* Or, after installing the package content but before creating the
565  package installation binary for redistribution, manually replace
566  the absolute paths with placeholders for substitution by the
567  installation tool when the package is installed.
568
569.. _`Package Registry`:
570
571Package Registry
572================
573
574CMake provides two central locations to register packages that have
575been built or installed anywhere on a system:
576
577* `User Package Registry`_
578* `System Package Registry`_
579
580The registries are especially useful to help projects find packages in
581non-standard install locations or directly in their own build trees.
582A project may populate either the user or system registry (using its own
583means, see below) to refer to its location.
584In either case the package should store at the registered location a
585`Package Configuration File`_ (``<PackageName>Config.cmake``) and optionally a
586`Package Version File`_ (``<PackageName>ConfigVersion.cmake``).
587
588The :command:`find_package` command searches the two package registries
589as two of the search steps specified in its documentation.  If it has
590sufficient permissions it also removes stale package registry entries
591that refer to directories that do not exist or do not contain a matching
592package configuration file.
593
594.. _`User Package Registry`:
595
596User Package Registry
597---------------------
598
599The User Package Registry is stored in a per-user location.
600The :command:`export(PACKAGE)` command may be used to register a project
601build tree in the user package registry.  CMake currently provides no
602interface to add install trees to the user package registry.  Installers
603must be manually taught to register their packages if desired.
604
605On Windows the user package registry is stored in the Windows registry
606under a key in ``HKEY_CURRENT_USER``.
607
608A ``<PackageName>`` may appear under registry key::
609
610  HKEY_CURRENT_USER\Software\Kitware\CMake\Packages\<PackageName>
611
612as a ``REG_SZ`` value, with arbitrary name, that specifies the directory
613containing the package configuration file.
614
615On UNIX platforms the user package registry is stored in the user home
616directory under ``~/.cmake/packages``.  A ``<PackageName>`` may appear under
617the directory::
618
619  ~/.cmake/packages/<PackageName>
620
621as a file, with arbitrary name, whose content specifies the directory
622containing the package configuration file.
623
624.. _`System Package Registry`:
625
626System Package Registry
627-----------------------
628
629The System Package Registry is stored in a system-wide location.
630CMake currently provides no interface to add to the system package registry.
631Installers must be manually taught to register their packages if desired.
632
633On Windows the system package registry is stored in the Windows registry
634under a key in ``HKEY_LOCAL_MACHINE``.  A ``<PackageName>`` may appear under
635registry key::
636
637  HKEY_LOCAL_MACHINE\Software\Kitware\CMake\Packages\<PackageName>
638
639as a ``REG_SZ`` value, with arbitrary name, that specifies the directory
640containing the package configuration file.
641
642There is no system package registry on non-Windows platforms.
643
644.. _`Disabling the Package Registry`:
645
646Disabling the Package Registry
647------------------------------
648
649In some cases using the Package Registries is not desirable. CMake
650allows one to disable them using the following variables:
651
652* The :command:`export(PACKAGE)` command does not populate the user
653  package registry when :policy:`CMP0090` is set to ``NEW`` unless the
654  :variable:`CMAKE_EXPORT_PACKAGE_REGISTRY` variable explicitly enables it.
655  When :policy:`CMP0090` is *not* set to ``NEW`` then
656  :command:`export(PACKAGE)` populates the user package registry unless
657  the :variable:`CMAKE_EXPORT_NO_PACKAGE_REGISTRY` variable explicitly
658  disables it.
659* :variable:`CMAKE_FIND_USE_PACKAGE_REGISTRY` disables the
660  User Package Registry in all the :command:`find_package` calls when
661  set to ``FALSE``.
662* Deprecated :variable:`CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY` disables the
663  User Package Registry in all the :command:`find_package` calls when set
664  to ``TRUE``. This variable is ignored when
665  :variable:`CMAKE_FIND_USE_PACKAGE_REGISTRY` has been set.
666* :variable:`CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY` disables
667  the System Package Registry in all the :command:`find_package` calls.
668
669Package Registry Example
670------------------------
671
672A simple convention for naming package registry entries is to use content
673hashes.  They are deterministic and unlikely to collide
674(:command:`export(PACKAGE)` uses this approach).
675The name of an entry referencing a specific directory is simply the content
676hash of the directory path itself.
677
678If a project arranges for package registry entries to exist, such as::
679
680 > reg query HKCU\Software\Kitware\CMake\Packages\MyPackage
681 HKEY_CURRENT_USER\Software\Kitware\CMake\Packages\MyPackage
682  45e7d55f13b87179bb12f907c8de6fc4 REG_SZ c:/Users/Me/Work/lib/cmake/MyPackage
683  7b4a9844f681c80ce93190d4e3185db9 REG_SZ c:/Users/Me/Work/MyPackage-build
684
685or::
686
687 $ cat ~/.cmake/packages/MyPackage/7d1fb77e07ce59a81bed093bbee945bd
688 /home/me/work/lib/cmake/MyPackage
689 $ cat ~/.cmake/packages/MyPackage/f92c1db873a1937f3100706657c63e07
690 /home/me/work/MyPackage-build
691
692then the ``CMakeLists.txt`` code:
693
694.. code-block:: cmake
695
696  find_package(MyPackage)
697
698will search the registered locations for package configuration files
699(``MyPackageConfig.cmake``).  The search order among package registry
700entries for a single package is unspecified and the entry names
701(hashes in this example) have no meaning.  Registered locations may
702contain package version files (``MyPackageConfigVersion.cmake``) to
703tell :command:`find_package` whether a specific location is suitable
704for the version requested.
705
706Package Registry Ownership
707--------------------------
708
709Package registry entries are individually owned by the project installations
710that they reference.  A package installer is responsible for adding its own
711entry and the corresponding uninstaller is responsible for removing it.
712
713The :command:`export(PACKAGE)` command populates the user package registry
714with the location of a project build tree.  Build trees tend to be deleted by
715developers and have no "uninstall" event that could trigger removal of their
716entries.  In order to keep the registries clean the :command:`find_package`
717command automatically removes stale entries it encounters if it has sufficient
718permissions.  CMake provides no interface to remove an entry referencing an
719existing build tree once :command:`export(PACKAGE)` has been invoked.
720However, if the project removes its package configuration file from the build
721tree then the entry referencing the location will be considered stale.
722