cmake-packages.7.rst 27 KB

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