find_package.rst 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. find_package
  2. ------------
  3. .. |FIND_XXX| replace:: find_package
  4. .. |FIND_ARGS_XXX| replace:: <PackageName>
  5. .. |FIND_XXX_REGISTRY_VIEW_DEFAULT| replace:: ``TARGET``
  6. .. |CMAKE_FIND_ROOT_PATH_MODE_XXX| replace::
  7. :variable:`CMAKE_FIND_ROOT_PATH_MODE_PACKAGE`
  8. .. only:: html
  9. .. contents::
  10. .. note:: The :guide:`Using Dependencies Guide` provides a high-level
  11. introduction to this general topic. It provides a broader overview of
  12. where the ``find_package()`` command fits into the bigger picture,
  13. including its relationship to the :module:`FetchContent` module.
  14. The guide is recommended pre-reading before moving on to the details below.
  15. Find a package (usually provided by something external to the project),
  16. and load its package-specific details. Calls to this command can also
  17. be intercepted by :ref:`dependency providers <dependency_providers>`.
  18. Typical Usage
  19. ^^^^^^^^^^^^^
  20. Most calls to ``find_package()`` typically have the following form:
  21. .. code-block:: cmake
  22. find_package(<PackageName> [<version>] [REQUIRED] [COMPONENTS <components>...])
  23. The ``<PackageName>`` is the only mandatory argument. The ``<version>`` is
  24. often omitted, and ``REQUIRED`` should be given if the project cannot be
  25. configured successfully without the package. Some more complicated packages
  26. support components which can be selected with the ``COMPONENTS`` keyword, but
  27. most packages don't have that level of complexity.
  28. The above is a reduced form of the `basic signature`_. Where possible,
  29. projects should find packages using this form. This reduces complexity and
  30. maximizes the ways in which the package can be found or provided.
  31. Understanding the `basic signature`_ should be enough for general usage of
  32. ``find_package()``. Project maintainers who intend to provide a package
  33. configuration file should understand the bigger picture, as explained in
  34. :ref:`Full Signature` and all subsequent sections on this page.
  35. Search Modes
  36. ^^^^^^^^^^^^
  37. The command has a few modes by which it searches for packages:
  38. .. _`Module mode`:
  39. **Module mode**
  40. In this mode, CMake searches for a file called ``Find<PackageName>.cmake``,
  41. looking first in the locations listed in the :variable:`CMAKE_MODULE_PATH`,
  42. then among the :ref:`Find Modules` provided by the CMake installation.
  43. If the file is found, it is read and processed by CMake. It is responsible
  44. for finding the package, checking the version, and producing any needed
  45. messages. Some Find modules provide limited or no support for versioning;
  46. check the Find module's documentation.
  47. The ``Find<PackageName>.cmake`` file is not typically provided by the
  48. package itself. Rather, it is normally provided by something external to
  49. the package, such as the operating system, CMake itself, or even the project
  50. from which the ``find_package()`` command was called. Being externally
  51. provided, :ref:`Find Modules` tend to be heuristic in nature and are
  52. susceptible to becoming out-of-date. They typically search for certain
  53. libraries, files and other package artifacts.
  54. Module mode is only supported by the
  55. :ref:`basic command signature <Basic Signature>`.
  56. .. _`Config mode`:
  57. **Config mode**
  58. In this mode, CMake searches for a file called
  59. ``<lowercasePackageName>-config.cmake`` or ``<PackageName>Config.cmake``.
  60. It will also look for ``<lowercasePackageName>-config-version.cmake`` or
  61. ``<PackageName>ConfigVersion.cmake`` if version details were specified
  62. (see :ref:`version selection` for an explanation of how these separate
  63. version files are used).
  64. .. note::
  65. If the experimental ``CMAKE_EXPERIMENTAL_FIND_CPS_PACKAGES`` is enabled,
  66. files named ``<PackageName>.cps`` and ``<lowercasePackageName>.cps`` are
  67. also considered. These files provide package information according to the
  68. |CPS|_ (CPS), which is more portable than CMake script. Aside from any
  69. explicitly noted exceptions, any references to "config files", "config
  70. mode", "package configuration files", and so forth refer equally to both
  71. CPS and CMake-script files. This functionality is a work in progress, and
  72. some features may be missing.
  73. Search is implemented in a manner that will tend to prefer |CPS| files
  74. over CMake-script config files in most cases. Specifying ``CONFIGS``
  75. suppresses consideration of CPS files.
  76. In config mode, the command can be given a list of names to search for
  77. as package names. The locations where CMake searches for the config and
  78. version files is considerably more complicated than for Module mode
  79. (see :ref:`search procedure`).
  80. The config and version files are typically installed as part of the
  81. package, so they tend to be more reliable than Find modules. They usually
  82. contain direct knowledge of the package contents, so no searching or
  83. heuristics are needed within the config or version files themselves.
  84. Config mode is supported by both the :ref:`basic <Basic Signature>` and
  85. :ref:`full <Full Signature>` command signatures.
  86. **FetchContent redirection mode**
  87. .. versionadded:: 3.24
  88. A call to ``find_package()`` can be redirected internally to a package
  89. provided by the :module:`FetchContent` module. To the caller, the behavior
  90. will appear similar to Config mode, except that the search logic is
  91. by-passed and the component information is not used. See
  92. :command:`FetchContent_Declare` and :command:`FetchContent_MakeAvailable`
  93. for further details.
  94. When not redirected to a package provided by :module:`FetchContent`, the
  95. command arguments determine whether Module or Config mode is used. When the
  96. `basic signature`_ is used, the command searches in Module mode first.
  97. If the package is not found, the search falls back to Config mode.
  98. A user may set the :variable:`CMAKE_FIND_PACKAGE_PREFER_CONFIG` variable
  99. to true to reverse the priority and direct CMake to search using Config mode
  100. first before falling back to Module mode. The basic signature can also be
  101. forced to use only Module mode with a ``MODULE`` keyword. If the
  102. `full signature`_ is used, the command only searches in Config mode.
  103. .. _`basic signature`:
  104. Basic Signature
  105. ^^^^^^^^^^^^^^^
  106. .. code-block:: cmake
  107. find_package(<PackageName> [version] [EXACT] [QUIET] [MODULE]
  108. [REQUIRED|OPTIONAL] [[COMPONENTS] [components...]]
  109. [OPTIONAL_COMPONENTS components...]
  110. [REGISTRY_VIEW (64|32|64_32|32_64|HOST|TARGET|BOTH)]
  111. [GLOBAL]
  112. [NO_POLICY_SCOPE]
  113. [BYPASS_PROVIDER])
  114. The basic signature is supported by both Module and Config modes.
  115. The ``MODULE`` keyword implies that only Module mode can be used to find
  116. the package, with no fallback to Config mode.
  117. Regardless of the mode used, a ``<PackageName>_FOUND`` variable will be
  118. set to indicate whether the package was found. When the package is found,
  119. package-specific information may be provided through other variables and
  120. :ref:`Imported Targets` documented by the package itself. The
  121. ``QUIET`` option disables informational messages, including those indicating
  122. that the package cannot be found if it is not ``REQUIRED``. The ``REQUIRED``
  123. option stops processing with an error message if the package cannot be found.
  124. A package-specific list of required components may be listed after the
  125. ``COMPONENTS`` keyword. If any of these components are not able to be
  126. satisfied, the package overall is considered to be not found. If the
  127. ``REQUIRED`` option is also present, this is treated as a fatal error,
  128. otherwise execution still continues. As a form of shorthand, if the
  129. ``REQUIRED`` option is present, the ``COMPONENTS`` keyword can be omitted
  130. and the required components can be listed directly after ``REQUIRED``.
  131. The :variable:`CMAKE_FIND_REQUIRED` variable can be enabled to make this call
  132. ``REQUIRED`` by default. This behavior can be overridden by providing the
  133. ``OPTIONAL`` keyword. As with the ``REQUIRED`` option, a list of components
  134. can be listed directly after ``OPTIONAL``, which is equivalent to listing
  135. them after the ``COMPONENTS`` keyword. When the ``OPTIONAL`` keyword is given,
  136. the warning output when a package is not found is suppressed.
  137. Additional optional components may be listed after ``OPTIONAL_COMPONENTS``.
  138. If these cannot be satisfied, the package overall can still be considered
  139. found, as long as all required components are satisfied.
  140. The set of available components and their meaning are defined by the
  141. target package:
  142. * For CMake-script package configuration files, it is formally up to the target
  143. package how to interpret the component information given to it, but it should
  144. follow the expectations stated above. For calls where no components are
  145. specified, there is no single expected behavior and target packages should
  146. clearly define what occurs in such cases. Common arrangements include
  147. assuming it should find all components, no components or some well-defined
  148. subset of the available components.
  149. * |CPS| packages consist of a root configuration file and zero or more
  150. appendices, each of which provide components and may have dependencies.
  151. CMake always attempts to load the root configuration file. Appendices are
  152. only loaded if their dependencies can be satisfied, and if they either
  153. provide requested components, or if no components were requested. If the
  154. dependencies of an appendix providing a required component cannot be
  155. satisfied, the package is considered not found. Otherwise, that appendix
  156. is ignored.
  157. .. versionadded:: 3.24
  158. The ``REGISTRY_VIEW`` keyword specifies which registry views should be
  159. queried. This keyword is only meaningful on ``Windows`` platforms and will
  160. be ignored on all others. Formally, it is up to the target package how to
  161. interpret the registry view information given to it.
  162. .. versionadded:: 3.24
  163. Specifying the ``GLOBAL`` keyword will promote all imported targets to
  164. a global scope in the importing project. Alternatively, this functionality
  165. can be enabled by setting the :variable:`CMAKE_FIND_PACKAGE_TARGETS_GLOBAL`
  166. variable.
  167. .. _FIND_PACKAGE_VERSION_FORMAT:
  168. The ``[version]`` argument requests a version with which the package found
  169. should be compatible. There are two possible forms in which it may be
  170. specified:
  171. * A single version with the format ``major[.minor[.patch[.tweak]]]``, where
  172. each component is a numeric value.
  173. * A version range with the format ``versionMin...[<]versionMax`` where
  174. ``versionMin`` and ``versionMax`` have the same format and constraints on
  175. components being integers as the single version. By default, both end points
  176. are included. By specifying ``<``, the upper end point will be excluded.
  177. Version ranges are only supported with CMake 3.19 or later.
  178. .. note::
  179. With the exception of CPS packages, version support is currently provided
  180. only on a package-by-package basis. When a version range is specified but
  181. the package is only designed to expect a single version, the package will
  182. ignore the upper end point of the range and only take the single version at
  183. the lower end of the range into account. Non-CPS packages that do support
  184. version ranges do so in a manner that is determined by the individual
  185. package. See the `Version Selection`_ section below for details and
  186. important caveats.
  187. The ``EXACT`` option requests that the version be matched exactly. This option
  188. is incompatible with the specification of a version range.
  189. If no ``[version]`` and/or component list is given to a recursive invocation
  190. inside a find-module, the corresponding arguments are forwarded
  191. automatically from the outer call (including the ``EXACT`` flag for
  192. ``[version]``).
  193. See the :command:`cmake_policy` command documentation for discussion
  194. of the ``NO_POLICY_SCOPE`` option.
  195. .. versionadded:: 3.24
  196. The ``BYPASS_PROVIDER`` keyword is only allowed when ``find_package()`` is
  197. being called by a :ref:`dependency provider <dependency_providers>`.
  198. It can be used by providers to call the built-in ``find_package()``
  199. implementation directly and prevent that call from being re-routed back to
  200. itself. Future versions of CMake may detect attempts to use this keyword
  201. from places other than a dependency provider and halt with a fatal error.
  202. .. _`full signature`:
  203. Full Signature
  204. ^^^^^^^^^^^^^^
  205. .. code-block:: cmake
  206. find_package(<PackageName> [version] [EXACT] [QUIET]
  207. [REQUIRED|OPTIONAL] [[COMPONENTS] [components...]]
  208. [OPTIONAL_COMPONENTS components...]
  209. [CONFIG|NO_MODULE]
  210. [GLOBAL]
  211. [NO_POLICY_SCOPE]
  212. [BYPASS_PROVIDER]
  213. [NAMES name1 [name2 ...]]
  214. [CONFIGS config1 [config2 ...]]
  215. [HINTS path1 [path2 ...]]
  216. [PATHS path1 [path2 ...]]
  217. [REGISTRY_VIEW (64|32|64_32|32_64|HOST|TARGET|BOTH)]
  218. [PATH_SUFFIXES suffix1 [suffix2 ...]]
  219. [NO_DEFAULT_PATH]
  220. [NO_PACKAGE_ROOT_PATH]
  221. [NO_CMAKE_PATH]
  222. [NO_CMAKE_ENVIRONMENT_PATH]
  223. [NO_SYSTEM_ENVIRONMENT_PATH]
  224. [NO_CMAKE_PACKAGE_REGISTRY]
  225. [NO_CMAKE_BUILDS_PATH] # Deprecated; does nothing.
  226. [NO_CMAKE_SYSTEM_PATH]
  227. [NO_CMAKE_INSTALL_PREFIX]
  228. [NO_CMAKE_SYSTEM_PACKAGE_REGISTRY]
  229. [CMAKE_FIND_ROOT_PATH_BOTH |
  230. ONLY_CMAKE_FIND_ROOT_PATH |
  231. NO_CMAKE_FIND_ROOT_PATH])
  232. The ``CONFIG`` option, the synonymous ``NO_MODULE`` option, or the use
  233. of options not specified in the `basic signature`_ all enforce pure Config
  234. mode. In pure Config mode, the command skips Module mode search and
  235. proceeds at once with Config mode search.
  236. Config mode search attempts to locate a configuration file provided by the
  237. package to be found. A cache entry called ``<PackageName>_DIR`` is created to
  238. hold the directory containing the file. By default, the command searches for
  239. a package with the name ``<PackageName>``. If the ``NAMES`` option is given,
  240. the names following it are used instead of ``<PackageName>``. The names are
  241. also considered when determining whether to redirect the call to a package
  242. provided by :module:`FetchContent`.
  243. The command searches for a file called ``<PackageName>Config.cmake`` or
  244. ``<lowercasePackageName>-config.cmake`` for each name specified.
  245. A replacement set of possible configuration file names may be given
  246. using the ``CONFIGS`` option. The :ref:`search procedure` is specified below.
  247. Once found, any :ref:`version constraint <version selection>` is checked,
  248. and if satisfied, the configuration file is read and processed by CMake.
  249. Since the file is provided by the package it already knows the
  250. location of package contents. The full path to the configuration file
  251. is stored in the CMake variable ``<PackageName>_CONFIG``.
  252. .. note::
  253. If the experimental ``CMAKE_EXPERIMENTAL_FIND_CPS_PACKAGES`` is enabled,
  254. files named ``<PackageName>.cps`` and ``<lowercasePackageName>.cps`` are
  255. also considered, unless ``CONFIGS`` is given.
  256. All configuration files which have been considered by CMake while
  257. searching for the package with an appropriate version are stored in the
  258. ``<PackageName>_CONSIDERED_CONFIGS`` variable, and the associated versions
  259. in the ``<PackageName>_CONSIDERED_VERSIONS`` variable.
  260. If the package configuration file cannot be found, CMake will generate
  261. an error describing the problem unless the ``QUIET`` argument is
  262. specified. If ``REQUIRED`` is specified and the package is not found, a
  263. fatal error is generated and the configure step stops executing. If
  264. ``<PackageName>_DIR`` has been set to a directory not containing a
  265. configuration file, CMake will ignore it and search from scratch.
  266. Package maintainers providing package configuration files are encouraged to
  267. name and install them such that the :ref:`search procedure` outlined below
  268. will find them without requiring use of additional options.
  269. .. _`search procedure`:
  270. Config Mode Search Procedure
  271. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  272. .. note::
  273. When Config mode is used, this search procedure is applied regardless of
  274. whether the :ref:`full <full signature>` or :ref:`basic <basic signature>`
  275. signature was given.
  276. .. versionadded:: 3.24
  277. All calls to ``find_package()`` (even in Module mode) first look for a config
  278. package file in the :variable:`CMAKE_FIND_PACKAGE_REDIRECTS_DIR` directory.
  279. The :module:`FetchContent` module, or even the project itself, may write files
  280. to that location to redirect ``find_package()`` calls to content already
  281. provided by the project. If no config package file is found in that location,
  282. the search proceeds with the logic described below.
  283. CMake constructs a set of possible installation prefixes for the
  284. package. Under each prefix several directories are searched for a
  285. configuration file. The tables below show the directories searched.
  286. Each entry is meant for installation trees following Windows (``W``), UNIX
  287. (``U``), or Apple (``A``) conventions:
  288. ==================================================================== ==========
  289. Entry Convention
  290. ==================================================================== ==========
  291. ``<prefix>/<name>/cps/`` [#p2]_ W
  292. ``<prefix>/<name>/*/cps/`` [#p2]_ W
  293. ``<prefix>/cps/<name>/`` [#p2]_ W
  294. ``<prefix>/cps/<name>/*/`` [#p2]_ W
  295. ``<prefix>/cps/`` [#p2]_ W
  296. ``<prefix>/`` W
  297. ``<prefix>/(cmake|CMake)/`` W
  298. ``<prefix>/<name>*/`` W
  299. ``<prefix>/<name>*/(cmake|CMake)/`` W
  300. ``<prefix>/<name>*/(cmake|CMake)/<name>*/`` [#p1]_ W
  301. ``<prefix>/(lib/<arch>|lib*|share)/cps/<name>/`` [#p2]_ U
  302. ``<prefix>/(lib/<arch>|lib*|share)/cps/<name>/*/`` [#p2]_ U
  303. ``<prefix>/(lib/<arch>|lib*|share)/cps/`` [#p2]_ U
  304. ``<prefix>/(lib/<arch>|lib*|share)/cmake/<name>*/`` U
  305. ``<prefix>/(lib/<arch>|lib*|share)/<name>*/`` U
  306. ``<prefix>/(lib/<arch>|lib*|share)/<name>*/(cmake|CMake)/`` U
  307. ``<prefix>/<name>*/(lib/<arch>|lib*|share)/cmake/<name>*/`` W/U
  308. ``<prefix>/<name>*/(lib/<arch>|lib*|share)/<name>*/`` W/U
  309. ``<prefix>/<name>*/(lib/<arch>|lib*|share)/<name>*/(cmake|CMake)/`` W/U
  310. ==================================================================== ==========
  311. .. [#p1] .. versionadded:: 3.25
  312. .. [#p2] .. versionadded:: 4.0
  313. On systems supporting macOS :prop_tgt:`FRAMEWORK` and :prop_tgt:`BUNDLE`, the
  314. following directories are searched for Frameworks or Application Bundles
  315. containing a configuration file:
  316. =============================================================== ==========
  317. Entry Convention
  318. =============================================================== ==========
  319. ``<prefix>/<name>.framework/Versions/*/Resources/CPS/`` [#p3]_ A
  320. ``<prefix>/<name>.framework/Resources/CPS/`` [#p3]_ A
  321. ``<prefix>/<name>.framework/Resources/`` A
  322. ``<prefix>/<name>.framework/Resources/CMake/`` A
  323. ``<prefix>/<name>.framework/Versions/*/Resources/`` A
  324. ``<prefix>/<name>.framework/Versions/*/Resources/CMake/`` A
  325. ``<prefix>/<name>.app/Contents/Resources/CPS/`` [#p3]_ A
  326. ``<prefix>/<name>.app/Contents/Resources/`` A
  327. ``<prefix>/<name>.app/Contents/Resources/CMake/`` A
  328. =============================================================== ==========
  329. .. [#p3] .. versionadded:: 4.0
  330. When searching the above paths, ``find_package`` will only look for ``.cps``
  331. files in search paths which contain ``/cps/``, and will only look for
  332. ``.cmake`` files otherwise. (This only applies to the paths as specified and
  333. does not consider the contents of ``<prefix>`` or ``<name>``.)
  334. In all cases the ``<name>`` is treated as case-insensitive and corresponds
  335. to any of the names specified (``<PackageName>`` or names given by ``NAMES``).
  336. If at least one compiled language has been enabled, the architecture-specific
  337. ``lib/<arch>`` and ``lib*`` directories may be searched based on the compiler's
  338. target architecture, in the following order:
  339. ``lib/<arch>``
  340. Searched if the :variable:`CMAKE_LIBRARY_ARCHITECTURE` variable is set.
  341. ``lib64``
  342. Searched on 64 bit platforms (:variable:`CMAKE_SIZEOF_VOID_P` is 8) and the
  343. :prop_gbl:`FIND_LIBRARY_USE_LIB64_PATHS` property is set to ``TRUE``.
  344. ``lib32``
  345. Searched on 32 bit platforms (:variable:`CMAKE_SIZEOF_VOID_P` is 4) and the
  346. :prop_gbl:`FIND_LIBRARY_USE_LIB32_PATHS` property is set to ``TRUE``.
  347. ``libx32``
  348. Searched on platforms using the x32 ABI
  349. if the :prop_gbl:`FIND_LIBRARY_USE_LIBX32_PATHS` property is set to ``TRUE``.
  350. ``lib``
  351. Always searched.
  352. .. versionchanged:: 3.24
  353. On ``Windows`` platform, it is possible to include registry queries as part
  354. of the directories specified through ``HINTS`` and ``PATHS`` keywords, using
  355. a :ref:`dedicated syntax <Find Using Windows Registry>`. Such specifications
  356. will be ignored on all other platforms.
  357. .. versionadded:: 3.24
  358. ``REGISTRY_VIEW`` can be specified to manage ``Windows`` registry queries
  359. specified as part of ``PATHS`` and ``HINTS``.
  360. .. include:: include/FIND_XXX_REGISTRY_VIEW.rst
  361. If ``PATH_SUFFIXES`` is specified, the suffixes are appended to each
  362. (``W``) or (``U``) directory entry one-by-one.
  363. This set of directories is intended to work in cooperation with
  364. projects that provide configuration files in their installation trees.
  365. Directories above marked with (``W``) are intended for installations on
  366. Windows where the prefix may point at the top of an application's
  367. installation directory. Those marked with (``U``) are intended for
  368. installations on UNIX platforms where the prefix is shared by multiple
  369. packages. This is merely a convention, so all (``W``) and (``U``) directories
  370. are still searched on all platforms. Directories marked with (``A``) are
  371. intended for installations on Apple platforms. The
  372. :variable:`CMAKE_FIND_FRAMEWORK` and :variable:`CMAKE_FIND_APPBUNDLE`
  373. variables determine the order of preference.
  374. .. warning::
  375. Setting :variable:`CMAKE_FIND_FRAMEWORK` or :variable:`CMAKE_FIND_APPBUNDLE`
  376. to values other than ``FIRST`` (the default) will cause CMake to search for
  377. |CPS| files in an order that is different from the order set forth in the
  378. specification.
  379. The set of installation prefixes is constructed using the following
  380. steps. If ``NO_DEFAULT_PATH`` is specified all ``NO_*`` options are
  381. enabled.
  382. 1. Search prefixes unique to the current ``<PackageName>`` being found.
  383. See policy :policy:`CMP0074`.
  384. .. versionadded:: 3.12
  385. Specifically, search prefixes specified by the following variables,
  386. in order:
  387. a. :variable:`<PackageName>_ROOT` CMake variable,
  388. where ``<PackageName>`` is the case-preserved package name.
  389. b. :variable:`<PACKAGENAME>_ROOT` CMake variable,
  390. where ``<PACKAGENAME>`` is the upper-cased package name.
  391. See policy :policy:`CMP0144`.
  392. .. versionadded:: 3.27
  393. c. :envvar:`<PackageName>_ROOT` environment variable,
  394. where ``<PackageName>`` is the case-preserved package name.
  395. d. :envvar:`<PACKAGENAME>_ROOT` environment variable,
  396. where ``<PACKAGENAME>`` is the upper-cased package name.
  397. See policy :policy:`CMP0144`.
  398. .. versionadded:: 3.27
  399. The package root variables are maintained as a stack so if
  400. called from within a find module, root paths from the parent's find
  401. module will also be searched after paths for the current package.
  402. This can be skipped if ``NO_PACKAGE_ROOT_PATH`` is passed or by setting
  403. the :variable:`CMAKE_FIND_USE_PACKAGE_ROOT_PATH` to ``FALSE``.
  404. 2. Search paths specified in CMake-specific cache variables. These
  405. are intended to be used on the command line with a :option:`-DVAR=VALUE <cmake -D>`.
  406. The values are interpreted as :ref:`semicolon-separated lists <CMake Language Lists>`.
  407. This can be skipped if ``NO_CMAKE_PATH`` is passed or by setting the
  408. :variable:`CMAKE_FIND_USE_CMAKE_PATH` to ``FALSE``:
  409. * :variable:`CMAKE_PREFIX_PATH`
  410. * :variable:`CMAKE_FRAMEWORK_PATH`
  411. * :variable:`CMAKE_APPBUNDLE_PATH`
  412. 3. Search paths specified in CMake-specific environment variables.
  413. These are intended to be set in the user's shell configuration,
  414. and therefore use the host's native path separator
  415. (``;`` on Windows and ``:`` on UNIX).
  416. This can be skipped if ``NO_CMAKE_ENVIRONMENT_PATH`` is passed or by setting
  417. the :variable:`CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH` to ``FALSE``:
  418. * ``<PackageName>_DIR``
  419. * :envvar:`CMAKE_PREFIX_PATH`
  420. * :envvar:`CMAKE_FRAMEWORK_PATH`
  421. * :envvar:`CMAKE_APPBUNDLE_PATH`
  422. 4. Search paths specified by the ``HINTS`` option. These should be paths
  423. computed by system introspection, such as a hint provided by the
  424. location of another item already found. Hard-coded guesses should
  425. be specified with the ``PATHS`` option.
  426. 5. Search the standard system environment variables. This can be
  427. skipped if ``NO_SYSTEM_ENVIRONMENT_PATH`` is passed or by setting the
  428. :variable:`CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH` to ``FALSE``. Path entries
  429. ending in ``/bin`` or ``/sbin`` are automatically converted to their
  430. parent directories:
  431. * ``PATH``
  432. 6. Search paths stored in the CMake :ref:`User Package Registry`.
  433. This can be skipped if ``NO_CMAKE_PACKAGE_REGISTRY`` is passed or by
  434. setting the variable :variable:`CMAKE_FIND_USE_PACKAGE_REGISTRY`
  435. to ``FALSE`` or the deprecated variable
  436. :variable:`CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY` to ``TRUE``.
  437. See the :manual:`cmake-packages(7)` manual for details on the user
  438. package registry.
  439. 7. Search CMake variables defined in the Platform files for the
  440. current system. The searching of :variable:`CMAKE_INSTALL_PREFIX` and
  441. :variable:`CMAKE_STAGING_PREFIX` can be
  442. skipped if ``NO_CMAKE_INSTALL_PREFIX`` is passed or by setting the
  443. :variable:`CMAKE_FIND_USE_INSTALL_PREFIX` to ``FALSE``. All these locations
  444. can be skipped if ``NO_CMAKE_SYSTEM_PATH`` is passed or by setting the
  445. :variable:`CMAKE_FIND_USE_CMAKE_SYSTEM_PATH` to ``FALSE``:
  446. * :variable:`CMAKE_SYSTEM_PREFIX_PATH`
  447. * :variable:`CMAKE_SYSTEM_FRAMEWORK_PATH`
  448. * :variable:`CMAKE_SYSTEM_APPBUNDLE_PATH`
  449. The platform paths that these variables contain are locations that
  450. typically include installed software. An example being ``/usr/local`` for
  451. UNIX based platforms.
  452. 8. Search paths stored in the CMake :ref:`System Package Registry`.
  453. This can be skipped if ``NO_CMAKE_SYSTEM_PACKAGE_REGISTRY`` is passed
  454. or by setting the :variable:`CMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY`
  455. variable to ``FALSE`` or the deprecated variable
  456. :variable:`CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY` to ``TRUE``.
  457. See the :manual:`cmake-packages(7)` manual for details on the system
  458. package registry.
  459. 9. Search paths specified by the ``PATHS`` option. These are typically
  460. hard-coded guesses.
  461. The :variable:`CMAKE_IGNORE_PATH`, :variable:`CMAKE_IGNORE_PREFIX_PATH`,
  462. :variable:`CMAKE_SYSTEM_IGNORE_PATH` and
  463. :variable:`CMAKE_SYSTEM_IGNORE_PREFIX_PATH` variables can also cause some
  464. of the above locations to be ignored.
  465. Paths are searched in the order described above. The first viable package
  466. configuration file found is used, even if a newer version of the package
  467. resides later in the list of search paths.
  468. For search paths which contain glob expressions (``*``), directories matching
  469. the glob are searched in natural, descending order by default. This behavior
  470. can be overridden by setting variables :variable:`CMAKE_FIND_PACKAGE_SORT_ORDER`
  471. and :variable:`CMAKE_FIND_PACKAGE_SORT_DIRECTION` accordingly. Those variables
  472. determine the order in which CMake considers glob matches. For example, if the
  473. file system contains the package configuration files
  474. ::
  475. <prefix>/example-1.2/example-config.cmake
  476. <prefix>/example-1.10/example-config.cmake
  477. <prefix>/share/example-2.0/example-config.cmake
  478. then ``find_package(example)`` will (when the aforementioned variables are
  479. unset) pick ``example-1.10`` (assuming both ``example-1.2`` and ``example-1.10``
  480. are viable). Note however that ``find_package`` will *not* find ``example-2.0``,
  481. because one of the other two will be found first.
  482. To control the order in which ``find_package`` searches directories that match
  483. a glob expression, use :variable:`CMAKE_FIND_PACKAGE_SORT_ORDER` and
  484. :variable:`CMAKE_FIND_PACKAGE_SORT_DIRECTION`.
  485. For instance, to cause the above example to select ``example-1.2``,
  486. one can set
  487. .. code-block:: cmake
  488. set(CMAKE_FIND_PACKAGE_SORT_ORDER NATURAL)
  489. set(CMAKE_FIND_PACKAGE_SORT_DIRECTION ASC)
  490. before calling ``find_package``.
  491. .. versionadded:: 3.16
  492. Added the ``CMAKE_FIND_USE_<CATEGORY>`` variables to globally disable
  493. various search locations.
  494. .. versionchanged:: 4.0
  495. The variables :variable:`CMAKE_FIND_PACKAGE_SORT_ORDER` and
  496. :variable:`CMAKE_FIND_PACKAGE_SORT_DIRECTION` now also control the order
  497. in which ``find_package`` searches directories matching the glob expression
  498. in the search paths ``<prefix>/<name>.framework/Versions/*/Resources/``
  499. and ``<prefix>/<name>.framework/Versions/*/Resources/CMake``. In previous
  500. versions of CMake, this order was unspecified.
  501. .. versionchanged:: 4.2
  502. When encountering multiple viable matches, ``find_package`` now picks the
  503. one with the most recent version by default. In previous versions of CMake,
  504. the result was unspecified. Accordingly, the default of
  505. :variable:`CMAKE_FIND_PACKAGE_SORT_ORDER` has changed from ``NONE`` to
  506. ``NATURAL`` and :variable:`CMAKE_FIND_PACKAGE_SORT_DIRECTION`
  507. now defaults to ``DEC`` (descending) instead of ``ASC`` (ascending).
  508. .. include:: include/FIND_XXX_ROOT.rst
  509. .. include:: include/FIND_XXX_ORDER.rst
  510. By default the value stored in the result variable will be the path at
  511. which the file is found. The :variable:`CMAKE_FIND_PACKAGE_RESOLVE_SYMLINKS`
  512. variable may be set to ``TRUE`` before calling ``find_package`` in order
  513. to resolve symbolic links and store the real path to the file.
  514. Every non-REQUIRED ``find_package`` call can be disabled or made REQUIRED:
  515. * Setting the :variable:`CMAKE_DISABLE_FIND_PACKAGE_<PackageName>` variable
  516. to ``TRUE`` disables the package. This also disables redirection to a
  517. package provided by :module:`FetchContent`.
  518. * Setting the :variable:`CMAKE_REQUIRE_FIND_PACKAGE_<PackageName>` variable
  519. to ``TRUE`` makes the package REQUIRED.
  520. Setting both variables to ``TRUE`` simultaneously is an error.
  521. The :variable:`CMAKE_REQUIRE_FIND_PACKAGE_<PackageName>` variable takes priority
  522. over the ``OPTIONAL`` keyword in determining whether a package is required.
  523. .. _`version selection`:
  524. Config Mode Version Selection
  525. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  526. .. note::
  527. When Config mode is used, this version selection process is applied
  528. regardless of whether the :ref:`full <full signature>` or
  529. :ref:`basic <basic signature>` signature was given.
  530. When the ``[version]`` argument is given, Config mode will only find a
  531. version of the package that claims compatibility with the requested
  532. version (see :ref:`format specification <FIND_PACKAGE_VERSION_FORMAT>`). If
  533. the ``EXACT`` option is given, only a version of the package claiming an exact
  534. match of the requested version may be found. CMake does not establish any
  535. convention for the meaning of version numbers.
  536. .. _`cmake script version selection`:
  537. CMake-script
  538. """"""""""""
  539. For CMake-script package configuration files, package version numbers are
  540. checked by "version" files provided by the packages themselves or by
  541. :module:`FetchContent`. For a candidate package configuration file
  542. ``<config-file>.cmake`` the corresponding version file is located next
  543. to it and named either ``<config-file>-version.cmake`` or
  544. ``<config-file>Version.cmake``. If no such version file is available
  545. then the configuration file is assumed to not be compatible with any
  546. requested version. A basic version file containing generic version
  547. matching code can be created using the
  548. :module:`CMakePackageConfigHelpers` module. When a version file
  549. is found it is loaded to check the requested version number. The
  550. version file is loaded in a nested scope in which the following
  551. variables have been defined:
  552. ``PACKAGE_FIND_NAME``
  553. The ``<PackageName>``
  554. ``PACKAGE_FIND_VERSION``
  555. Full requested version string
  556. ``PACKAGE_FIND_VERSION_MAJOR``
  557. Major version if requested, else 0
  558. ``PACKAGE_FIND_VERSION_MINOR``
  559. Minor version if requested, else 0
  560. ``PACKAGE_FIND_VERSION_PATCH``
  561. Patch version if requested, else 0
  562. ``PACKAGE_FIND_VERSION_TWEAK``
  563. Tweak version if requested, else 0
  564. ``PACKAGE_FIND_VERSION_COUNT``
  565. Number of version components, 0 to 4
  566. When a version range is specified, the above version variables will hold
  567. values based on the lower end of the version range. This is to preserve
  568. compatibility with packages that have not been implemented to expect version
  569. ranges. In addition, the version range will be described by the following
  570. variables:
  571. ``PACKAGE_FIND_VERSION_RANGE``
  572. Full requested version range string
  573. ``PACKAGE_FIND_VERSION_RANGE_MIN``
  574. This specifies whether the lower end point of the version range should be
  575. included or excluded. Currently, the only supported value for this variable
  576. is ``INCLUDE``.
  577. ``PACKAGE_FIND_VERSION_RANGE_MAX``
  578. This specifies whether the upper end point of the version range should be
  579. included or excluded. The supported values for this variable are
  580. ``INCLUDE`` and ``EXCLUDE``.
  581. ``PACKAGE_FIND_VERSION_MIN``
  582. Full requested version string of the lower end point of the range
  583. ``PACKAGE_FIND_VERSION_MIN_MAJOR``
  584. Major version of the lower end point if requested, else 0
  585. ``PACKAGE_FIND_VERSION_MIN_MINOR``
  586. Minor version of the lower end point if requested, else 0
  587. ``PACKAGE_FIND_VERSION_MIN_PATCH``
  588. Patch version of the lower end point if requested, else 0
  589. ``PACKAGE_FIND_VERSION_MIN_TWEAK``
  590. Tweak version of the lower end point if requested, else 0
  591. ``PACKAGE_FIND_VERSION_MIN_COUNT``
  592. Number of version components of the lower end point, 0 to 4
  593. ``PACKAGE_FIND_VERSION_MAX``
  594. Full requested version string of the upper end point of the range
  595. ``PACKAGE_FIND_VERSION_MAX_MAJOR``
  596. Major version of the upper end point if requested, else 0
  597. ``PACKAGE_FIND_VERSION_MAX_MINOR``
  598. Minor version of the upper end point if requested, else 0
  599. ``PACKAGE_FIND_VERSION_MAX_PATCH``
  600. Patch version of the upper end point if requested, else 0
  601. ``PACKAGE_FIND_VERSION_MAX_TWEAK``
  602. Tweak version of the upper end point if requested, else 0
  603. ``PACKAGE_FIND_VERSION_MAX_COUNT``
  604. Number of version components of the upper end point, 0 to 4
  605. Regardless of whether a single version or a version range is specified, the
  606. variable ``PACKAGE_FIND_VERSION_COMPLETE`` will be defined and will hold
  607. the full requested version string as specified.
  608. The version file checks whether it satisfies the requested version and
  609. sets these variables:
  610. ``PACKAGE_VERSION``
  611. Full provided version string
  612. ``PACKAGE_VERSION_EXACT``
  613. True if version is exact match
  614. ``PACKAGE_VERSION_COMPATIBLE``
  615. True if version is compatible
  616. ``PACKAGE_VERSION_UNSUITABLE``
  617. True if unsuitable as any version
  618. These variables are checked by the ``find_package`` command to determine
  619. whether the configuration file provides an acceptable version. They
  620. are not available after the ``find_package`` call returns. If the version
  621. is acceptable, the following variables are set:
  622. ``<PackageName>_VERSION``
  623. Full provided version string
  624. ``<PackageName>_VERSION_MAJOR``
  625. Major version if provided, else 0
  626. ``<PackageName>_VERSION_MINOR``
  627. Minor version if provided, else 0
  628. ``<PackageName>_VERSION_PATCH``
  629. Patch version if provided, else 0
  630. ``<PackageName>_VERSION_TWEAK``
  631. Tweak version if provided, else 0
  632. ``<PackageName>_VERSION_COUNT``
  633. Number of version components, 0 to 4
  634. and the corresponding package configuration file is loaded.
  635. .. note::
  636. While the exact behavior of version matching is determined by the individual
  637. package, many packages use :command:`write_basic_package_version_file` to
  638. supply this logic. The version check scripts this produces have some notable
  639. caveats with respect to version ranges:
  640. * The upper end of a version range acts as a hard limit on what versions will
  641. be accepted. Thus, while a request for version ``1.4.0`` might be
  642. satisfied by a package whose version is ``1.6.0`` and which advertises
  643. 'same major version' compatibility, the same package will be rejected if
  644. the requested version range is ``1.4.0...1.5.0``.
  645. * Both ends of the version range must match the package's advertised
  646. compatibility level. For example, if a package advertises 'same major and
  647. minor version' compatibility, requesting the version range
  648. ``1.4.0...<1.5.5`` or ``1.4.0...1.5.0`` will result in that package being
  649. rejected, even if the package version is ``1.4.1``.
  650. As a result, it is not possible to use a version range to extend the range
  651. of compatible package versions that will be accepted.
  652. .. _`cps version selection`:
  653. |CPS|
  654. """""
  655. For |CPS| package configuration files, package version numbers are checked by
  656. CMake according to the set of recognized version schemas. At present, the
  657. following schemas are recognized:
  658. ``simple``
  659. Version numbers are a tuple of integers followed by an optional trailing
  660. segment which is ignored with respect to version comparisons.
  661. ``custom``
  662. The mechanism for interpreting version numbers is unspecified. The version
  663. strings must match exactly for the package to be accepted.
  664. Refer to |cps-version_schema|_ for a more detailed explanation of each schema
  665. and how comparisons for each are performed. Note that the specification may
  666. include schemas that are not supported by CMake.
  667. In addition to the package's ``version``, CPS allows packages to optionally
  668. specify a |cps-compat_version|_, which is the oldest version for which the
  669. package provides compatibility. That is, the package warrants that a consumer
  670. expecting the ``compat_version`` should be able to use the package, even if the
  671. package's actual version is newer. If not specified, the ``compat_version``
  672. is implicitly equal to the package version, i.e. no backwards compatibility is
  673. provided.
  674. .. TODO Rework the preceding paragraph when COMPAT_VERSION has broader support
  675. in CMake.
  676. When a package uses a recognized schema, CMake will determine the package's
  677. acceptability according to the following rules:
  678. * If ``EXACT`` was specified, or if the package does not supply a
  679. ``compat_version``, the package's ``version`` must equal the requested
  680. version.
  681. * Otherwise:
  682. * The package's ``version`` must be greater than or equal to the requested
  683. (minimum) version, and
  684. * the package's ``compat_version`` must be less than or equal to the
  685. requested (minimum) version, and
  686. * if a requested maximum version was given, it must be greater than (or equal
  687. to, depending on whether the maximum version is specified as inclusive or
  688. exclusive) the package's ``version``.
  689. .. note::
  690. This implementation of range matching was chosen in order to most closely
  691. match the behavior of :command:`write_basic_package_version_file`, albeit
  692. without the case where an overly broad range matches nothing.
  693. For packages using the ``simple`` version schema, if the version is acceptable,
  694. the following variables are set:
  695. ``<PackageName>_VERSION``
  696. Full provided version string
  697. ``<PackageName>_VERSION_MAJOR``
  698. Major version if provided, else 0
  699. ``<PackageName>_VERSION_MINOR``
  700. Minor version if provided, else 0
  701. ``<PackageName>_VERSION_PATCH``
  702. Patch version if provided, else 0
  703. ``<PackageName>_VERSION_TWEAK``
  704. Tweak version if provided, else 0
  705. ``<PackageName>_VERSION_COUNT``
  706. Number of version components, non-negative
  707. Package File Interface Variables
  708. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  709. When loading a find module or CMake-script package configuration file,
  710. ``find_package`` defines variables to provide information about the call
  711. arguments (and restores their original state before returning):
  712. ``CMAKE_FIND_PACKAGE_NAME``
  713. The ``<PackageName>`` which is searched for
  714. ``<PackageName>_FIND_REQUIRED``
  715. True if ``REQUIRED`` option was given
  716. ``<PackageName>_FIND_QUIETLY``
  717. True if ``QUIET`` option was given
  718. ``<PackageName>_FIND_REGISTRY_VIEW``
  719. The requested view if ``REGISTRY_VIEW`` option was given
  720. ``<PackageName>_FIND_VERSION``
  721. Full requested version string
  722. ``<PackageName>_FIND_VERSION_MAJOR``
  723. Major version if requested, else 0
  724. ``<PackageName>_FIND_VERSION_MINOR``
  725. Minor version if requested, else 0
  726. ``<PackageName>_FIND_VERSION_PATCH``
  727. Patch version if requested, else 0
  728. ``<PackageName>_FIND_VERSION_TWEAK``
  729. Tweak version if requested, else 0
  730. ``<PackageName>_FIND_VERSION_COUNT``
  731. Number of version components, 0 to 4
  732. ``<PackageName>_FIND_VERSION_EXACT``
  733. True if ``EXACT`` option was given
  734. ``<PackageName>_FIND_COMPONENTS``
  735. List of specified components (required and optional)
  736. ``<PackageName>_FIND_REQUIRED_<c>``
  737. True if component ``<c>`` is required,
  738. false if component ``<c>`` is optional
  739. When a version range is specified, the above version variables will hold
  740. values based on the lower end of the version range. This is to preserve
  741. compatibility with packages that have not been implemented to expect version
  742. ranges. In addition, the version range will be described by the following
  743. variables:
  744. ``<PackageName>_FIND_VERSION_RANGE``
  745. Full requested version range string
  746. ``<PackageName>_FIND_VERSION_RANGE_MIN``
  747. This specifies whether the lower end point of the version range is
  748. included or excluded. Currently, ``INCLUDE`` is the only supported value.
  749. ``<PackageName>_FIND_VERSION_RANGE_MAX``
  750. This specifies whether the upper end point of the version range is
  751. included or excluded. The possible values for this variable are
  752. ``INCLUDE`` or ``EXCLUDE``.
  753. ``<PackageName>_FIND_VERSION_MIN``
  754. Full requested version string of the lower end point of the range
  755. ``<PackageName>_FIND_VERSION_MIN_MAJOR``
  756. Major version of the lower end point if requested, else 0
  757. ``<PackageName>_FIND_VERSION_MIN_MINOR``
  758. Minor version of the lower end point if requested, else 0
  759. ``<PackageName>_FIND_VERSION_MIN_PATCH``
  760. Patch version of the lower end point if requested, else 0
  761. ``<PackageName>_FIND_VERSION_MIN_TWEAK``
  762. Tweak version of the lower end point if requested, else 0
  763. ``<PackageName>_FIND_VERSION_MIN_COUNT``
  764. Number of version components of the lower end point, 0 to 4
  765. ``<PackageName>_FIND_VERSION_MAX``
  766. Full requested version string of the upper end point of the range
  767. ``<PackageName>_FIND_VERSION_MAX_MAJOR``
  768. Major version of the upper end point if requested, else 0
  769. ``<PackageName>_FIND_VERSION_MAX_MINOR``
  770. Minor version of the upper end point if requested, else 0
  771. ``<PackageName>_FIND_VERSION_MAX_PATCH``
  772. Patch version of the upper end point if requested, else 0
  773. ``<PackageName>_FIND_VERSION_MAX_TWEAK``
  774. Tweak version of the upper end point if requested, else 0
  775. ``<PackageName>_FIND_VERSION_MAX_COUNT``
  776. Number of version components of the upper end point, 0 to 4
  777. Regardless of whether a single version or a version range is specified, the
  778. variable ``<PackageName>_FIND_VERSION_COMPLETE`` will be defined and will hold
  779. the full requested version string as specified.
  780. In Module mode the loaded find module is responsible to honor the
  781. request detailed by these variables; see the find module for details.
  782. In Config mode ``find_package`` handles ``REQUIRED``, ``QUIET``, and
  783. ``[version]`` options automatically but leaves it to the package
  784. configuration file to handle components in a way that makes sense
  785. for the package. The package configuration file may set
  786. ``<PackageName>_FOUND`` to false to tell ``find_package`` that component
  787. requirements are not satisfied.
  788. .. _CPS: https://cps-org.github.io/cps/
  789. .. |CPS| replace:: Common Package Specification
  790. .. _cps-compat_version: https://cps-org.github.io/cps/schema.html#compat-version
  791. .. |cps-compat_version| replace:: ``compat_version``
  792. .. _cps-version_schema: https://cps-org.github.io/cps/schema.html#version-schema
  793. .. |cps-version_schema| replace:: ``version_schema``
  794. CPS Transitive Requirements
  795. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  796. A |CPS| package description consists of one or more components which may in
  797. turn depend on other components either internal or external to the package.
  798. When external components are required, the providing package is noted as
  799. a package-level requirement of the package. Additionally, the set of required
  800. components is typically noted in said external package requirement.
  801. Where a CMake-script package description would use the
  802. :command:`find_dependency` command to handle transitive dependencies, CMake
  803. handles transitive dependencies for CPS itself using an internally nested
  804. ``find_package`` call. This call can resolve CPS package dependencies via
  805. *either* another CPS package, or via a CMake-script package. The manner in
  806. which the CPS component dependencies are handled is subject to some caveats.
  807. When the candidate for resolving a transitive dependency is another CPS
  808. package, things are simple; ``COMPONENTS`` and CPS "components" are directly
  809. comparable (and are effectively synonymous with CMake "imported targets").
  810. CMake-script packages, however, are encouraged to (and often do) check that
  811. required components were found, whether or not the package describes separate
  812. components. Additionally, even those that do describe components typically do
  813. not have the same correlation to imported targets that is normal for CPS. As
  814. a result, passing the set of required components declared by a CPS package to
  815. ``COMPONENTS`` would result in spurious failures to resolve dependencies.
  816. To address this, if a candidate for resolving a CPS transitive dependency is a
  817. CMake-script package, CMake passes the required components as declared by the
  818. consuming CPS package as ``OPTIONAL_COMPONENTS`` and performs a separate,
  819. internal check that the candidate package supplied the required imported
  820. targets. Those targets must be named ``<PackageName>::<ComponentName>``, in
  821. conformance with CPS convention, or the check will consider the package not
  822. found.