find_package.rst 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. .. parsed-literal::
  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 config
  33. package 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. In this mode, CMake searches for a file called ``Find<PackageName>.cmake``,
  40. looking first in the locations listed in the :variable:`CMAKE_MODULE_PATH`,
  41. then among the :ref:`Find Modules` provided by the CMake installation.
  42. If the file is found, it is read and processed by CMake. It is responsible
  43. for finding the package, checking the version, and producing any needed
  44. messages. Some Find modules provide limited or no support for versioning;
  45. check the Find module's documentation.
  46. The ``Find<PackageName>.cmake`` file is not typically provided by the
  47. package itself. Rather, it is normally provided by something external to
  48. the package, such as the operating system, CMake itself, or even the project
  49. from which the ``find_package()`` command was called. Being externally
  50. provided, :ref:`Find Modules` tend to be heuristic in nature and are
  51. susceptible to becoming out-of-date. They typically search for certain
  52. libraries, files and other package artifacts.
  53. Module mode is only supported by the
  54. :ref:`basic command signature <Basic Signature>`.
  55. **Config mode**
  56. In this mode, CMake searches for a file called
  57. ``<lowercasePackageName>-config.cmake`` or ``<PackageName>Config.cmake``.
  58. It will also look for ``<lowercasePackageName>-config-version.cmake`` or
  59. ``<PackageName>ConfigVersion.cmake`` if version details were specified
  60. (see :ref:`version selection` for an explanation of how these separate
  61. version files are used).
  62. In config mode, the command can be given a list of names to search for
  63. as package names. The locations where CMake searches for the config and
  64. version files is considerably more complicated than for Module mode
  65. (see :ref:`search procedure`).
  66. The config and version files are typically installed as part of the
  67. package, so they tend to be more reliable than Find modules. They usually
  68. contain direct knowledge of the package contents, so no searching or
  69. heuristics are needed within the config or version files themselves.
  70. Config mode is supported by both the :ref:`basic <Basic Signature>` and
  71. :ref:`full <Full Signature>` command signatures.
  72. **FetchContent redirection mode**
  73. .. versionadded:: 3.24
  74. A call to ``find_package()`` can be redirected internally to a package
  75. provided by the :module:`FetchContent` module. To the caller, the behavior
  76. will appear similar to Config mode, except that the search logic is
  77. by-passed and the component information is not used. See
  78. :command:`FetchContent_Declare` and :command:`FetchContent_MakeAvailable`
  79. for further details.
  80. When not redirected to a package provided by :module:`FetchContent`, the
  81. command arguments determine whether Module or Config mode is used. When the
  82. `basic signature`_ is used, the command searches in Module mode first.
  83. If the package is not found, the search falls back to Config mode.
  84. A user may set the :variable:`CMAKE_FIND_PACKAGE_PREFER_CONFIG` variable
  85. to true to reverse the priority and direct CMake to search using Config mode
  86. first before falling back to Module mode. The basic signature can also be
  87. forced to use only Module mode with a ``MODULE`` keyword. If the
  88. `full signature`_ is used, the command only searches in Config mode.
  89. .. _`basic signature`:
  90. Basic Signature
  91. ^^^^^^^^^^^^^^^
  92. .. parsed-literal::
  93. find_package(<PackageName> [version] [EXACT] [QUIET] [MODULE]
  94. [REQUIRED] [[COMPONENTS] [components...]]
  95. [OPTIONAL_COMPONENTS components...]
  96. [REGISTRY_VIEW (64|32|64_32|32_64|HOST|TARGET|BOTH)]
  97. [GLOBAL]
  98. [NO_POLICY_SCOPE]
  99. [BYPASS_PROVIDER])
  100. The basic signature is supported by both Module and Config modes.
  101. The ``MODULE`` keyword implies that only Module mode can be used to find
  102. the package, with no fallback to Config mode.
  103. Regardless of the mode used, a ``<PackageName>_FOUND`` variable will be
  104. set to indicate whether the package was found. When the package is found,
  105. package-specific information may be provided through other variables and
  106. :ref:`Imported Targets` documented by the package itself. The
  107. ``QUIET`` option disables informational messages, including those indicating
  108. that the package cannot be found if it is not ``REQUIRED``. The ``REQUIRED``
  109. option stops processing with an error message if the package cannot be found.
  110. A package-specific list of required components may be listed after the
  111. ``COMPONENTS`` keyword. If any of these components are not able to be
  112. satisfied, the package overall is considered to be not found. If the
  113. ``REQUIRED`` option is also present, this is treated as a fatal error,
  114. otherwise execution still continues. As a form of shorthand, if the
  115. ``REQUIRED`` option is present, the ``COMPONENTS`` keyword can be omitted
  116. and the required components can be listed directly after ``REQUIRED``.
  117. Additional optional components may be listed after
  118. ``OPTIONAL_COMPONENTS``. If these cannot be satisfied, the package overall
  119. can still be considered found, as long as all required components are
  120. satisfied.
  121. The set of available components and their meaning are defined by the
  122. target package. Formally, it is up to the target package how to
  123. interpret the component information given to it, but it should follow
  124. the expectations stated above. For calls where no components are specified,
  125. there is no single expected behavior and target packages should clearly
  126. define what occurs in such cases. Common arrangements include assuming it
  127. should find all components, no components or some well-defined subset of the
  128. available components.
  129. .. versionadded:: 3.24
  130. The ``REGISTRY_VIEW`` keyword specifies which registry views should be
  131. queried. This keyword is only meaningful on ``Windows`` platforms and will
  132. be ignored on all others. Formally, it is up to the target package how to
  133. interpret the registry view information given to it.
  134. .. versionadded:: 3.24
  135. Specifying the ``GLOBAL`` keyword will promote all imported targets to
  136. a global scope in the importing project. Alternatively, this functionality
  137. can be enabled by setting the :variable:`CMAKE_FIND_PACKAGE_TARGETS_GLOBAL`
  138. variable.
  139. .. _FIND_PACKAGE_VERSION_FORMAT:
  140. The ``[version]`` argument requests a version with which the package found
  141. should be compatible. There are two possible forms in which it may be
  142. specified:
  143. * A single version with the format ``major[.minor[.patch[.tweak]]]``, where
  144. each component is a numeric value.
  145. * A version range with the format ``versionMin...[<]versionMax`` where
  146. ``versionMin`` and ``versionMax`` have the same format and constraints
  147. on components being integers as the single version. By default, both end
  148. points are included. By specifying ``<``, the upper end point will be
  149. excluded. Version ranges are only supported with CMake 3.19 or later.
  150. Note that it is not possible to extend the compatibility range specified
  151. by the package's version file. For example, if the package version file
  152. specifies compatibility within a minor version, it is not possible to
  153. extend the compatibility to several minor versions by specifying a
  154. version range.
  155. The ``EXACT`` option requests that the version be matched exactly. This option
  156. is incompatible with the specification of a version range.
  157. If no ``[version]`` and/or component list is given to a recursive invocation
  158. inside a find-module, the corresponding arguments are forwarded
  159. automatically from the outer call (including the ``EXACT`` flag for
  160. ``[version]``). Version support is currently provided only on a
  161. package-by-package basis (see the `Version Selection`_ section below).
  162. When a version range is specified but the package is only designed to expect
  163. a single version, the package will ignore the upper end point of the range and
  164. only take the single version at the lower end of the range into account.
  165. See the :command:`cmake_policy` command documentation for discussion
  166. of the ``NO_POLICY_SCOPE`` option.
  167. .. versionadded:: 3.24
  168. The ``BYPASS_PROVIDER`` keyword is only allowed when ``find_package()`` is
  169. being called by a :ref:`dependency provider <dependency_providers>`.
  170. It can be used by providers to call the built-in ``find_package()``
  171. implementation directly and prevent that call from being re-routed back to
  172. itself. Future versions of CMake may detect attempts to use this keyword
  173. from places other than a dependency provider and halt with a fatal error.
  174. .. _`full signature`:
  175. Full Signature
  176. ^^^^^^^^^^^^^^
  177. .. parsed-literal::
  178. find_package(<PackageName> [version] [EXACT] [QUIET]
  179. [REQUIRED] [[COMPONENTS] [components...]]
  180. [OPTIONAL_COMPONENTS components...]
  181. [CONFIG|NO_MODULE]
  182. [GLOBAL]
  183. [NO_POLICY_SCOPE]
  184. [BYPASS_PROVIDER]
  185. [NAMES name1 [name2 ...]]
  186. [CONFIGS config1 [config2 ...]]
  187. [HINTS path1 [path2 ... ]]
  188. [PATHS path1 [path2 ... ]]
  189. [REGISTRY_VIEW (64|32|64_32|32_64|HOST|TARGET|BOTH)]
  190. [PATH_SUFFIXES suffix1 [suffix2 ...]]
  191. [NO_DEFAULT_PATH]
  192. [NO_PACKAGE_ROOT_PATH]
  193. [NO_CMAKE_PATH]
  194. [NO_CMAKE_ENVIRONMENT_PATH]
  195. [NO_SYSTEM_ENVIRONMENT_PATH]
  196. [NO_CMAKE_PACKAGE_REGISTRY]
  197. [NO_CMAKE_BUILDS_PATH] # Deprecated; does nothing.
  198. [NO_CMAKE_SYSTEM_PATH]
  199. [NO_CMAKE_INSTALL_PREFIX]
  200. [NO_CMAKE_SYSTEM_PACKAGE_REGISTRY]
  201. [CMAKE_FIND_ROOT_PATH_BOTH |
  202. ONLY_CMAKE_FIND_ROOT_PATH |
  203. NO_CMAKE_FIND_ROOT_PATH])
  204. The ``CONFIG`` option, the synonymous ``NO_MODULE`` option, or the use
  205. of options not specified in the `basic signature`_ all enforce pure Config
  206. mode. In pure Config mode, the command skips Module mode search and
  207. proceeds at once with Config mode search.
  208. Config mode search attempts to locate a configuration file provided by the
  209. package to be found. A cache entry called ``<PackageName>_DIR`` is created to
  210. hold the directory containing the file. By default, the command searches for
  211. a package with the name ``<PackageName>``. If the ``NAMES`` option is given,
  212. the names following it are used instead of ``<PackageName>``. The names are
  213. also considered when determining whether to redirect the call to a package
  214. provided by :module:`FetchContent`.
  215. The command searches for a file called ``<PackageName>Config.cmake`` or
  216. ``<lowercasePackageName>-config.cmake`` for each name specified.
  217. A replacement set of possible configuration file names may be given
  218. using the ``CONFIGS`` option. The :ref:`search procedure` is specified below.
  219. Once found, any :ref:`version constraint <version selection>` is checked,
  220. and if satisfied, the configuration file is read and processed by CMake.
  221. Since the file is provided by the package it already knows the
  222. location of package contents. The full path to the configuration file
  223. is stored in the cmake variable ``<PackageName>_CONFIG``.
  224. All configuration files which have been considered by CMake while
  225. searching for the package with an appropriate version are stored in the
  226. ``<PackageName>_CONSIDERED_CONFIGS`` variable, and the associated versions
  227. in the ``<PackageName>_CONSIDERED_VERSIONS`` variable.
  228. If the package configuration file cannot be found CMake will generate
  229. an error describing the problem unless the ``QUIET`` argument is
  230. specified. If ``REQUIRED`` is specified and the package is not found a
  231. fatal error is generated and the configure step stops executing. If
  232. ``<PackageName>_DIR`` has been set to a directory not containing a
  233. configuration file CMake will ignore it and search from scratch.
  234. Package maintainers providing CMake package configuration files are
  235. encouraged to name and install them such that the :ref:`search procedure`
  236. outlined below will find them without requiring use of additional options.
  237. .. _`search procedure`:
  238. Config Mode Search Procedure
  239. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  240. .. note::
  241. When Config mode is used, this search procedure is applied regardless of
  242. whether the :ref:`full <full signature>` or :ref:`basic <basic signature>`
  243. signature was given.
  244. .. versionadded:: 3.24
  245. All calls to ``find_package()`` (even in Module mode) first look for a config
  246. package file in the :variable:`CMAKE_FIND_PACKAGE_REDIRECTS_DIR` directory.
  247. The :module:`FetchContent` module, or even the project itself, may write files
  248. to that location to redirect ``find_package()`` calls to content already
  249. provided by the project. If no config package file is found in that location,
  250. the search proceeds with the logic described below.
  251. CMake constructs a set of possible installation prefixes for the
  252. package. Under each prefix several directories are searched for a
  253. configuration file. The tables below show the directories searched.
  254. Each entry is meant for installation trees following Windows (``W``), UNIX
  255. (``U``), or Apple (``A``) conventions:
  256. ==================================================================== ==========
  257. Entry Convention
  258. ==================================================================== ==========
  259. ``<prefix>/<name>/cps/`` [#p2]_ W
  260. ``<prefix>/<name>/*/cps/`` [#p2]_ W
  261. ``<prefix>/cps/<name>/`` [#p2]_ W
  262. ``<prefix>/cps/<name>/*/`` [#p2]_ W
  263. ``<prefix>/cps/`` [#p2]_ W
  264. ``<prefix>/`` W
  265. ``<prefix>/(cmake|CMake)/`` W
  266. ``<prefix>/<name>*/`` W
  267. ``<prefix>/<name>*/(cmake|CMake)/`` W
  268. ``<prefix>/<name>*/(cmake|CMake)/<name>*/`` [#p1]_ W
  269. ``<prefix>/(lib/<arch>|lib*|share)/cps/<name>/`` [#p2]_ U
  270. ``<prefix>/(lib/<arch>|lib*|share)/cps/<name>/*/`` [#p2]_ U
  271. ``<prefix>/(lib/<arch>|lib*|share)/cps/`` [#p2]_ U
  272. ``<prefix>/(lib/<arch>|lib*|share)/cmake/<name>*/`` U
  273. ``<prefix>/(lib/<arch>|lib*|share)/<name>*/`` U
  274. ``<prefix>/(lib/<arch>|lib*|share)/<name>*/(cmake|CMake)/`` U
  275. ``<prefix>/<name>*/(lib/<arch>|lib*|share)/cmake/<name>*/`` W/U
  276. ``<prefix>/<name>*/(lib/<arch>|lib*|share)/<name>*/`` W/U
  277. ``<prefix>/<name>*/(lib/<arch>|lib*|share)/<name>*/(cmake|CMake)/`` W/U
  278. ==================================================================== ==========
  279. .. [#p1] .. versionadded:: 3.25
  280. .. [#p2] .. versionadded:: 3.32
  281. On systems supporting macOS :prop_tgt:`FRAMEWORK` and :prop_tgt:`BUNDLE`, the
  282. following directories are searched for Frameworks or Application Bundles
  283. containing a configuration file:
  284. =============================================================== ==========
  285. Entry Convention
  286. =============================================================== ==========
  287. ``<prefix>/<name>.framework/Versions/*/Resources/CPS/`` [#p3]_ A
  288. ``<prefix>/<name>.framework/Resources/CPS/`` [#p3]_ A
  289. ``<prefix>/<name>.framework/Resources/`` A
  290. ``<prefix>/<name>.framework/Resources/CMake/`` A
  291. ``<prefix>/<name>.framework/Versions/*/Resources/`` A
  292. ``<prefix>/<name>.framework/Versions/*/Resources/CMake/`` A
  293. ``<prefix>/<name>.app/Contents/Resources/CPS/`` [#p3]_ A
  294. ``<prefix>/<name>.app/Contents/Resources/`` A
  295. ``<prefix>/<name>.app/Contents/Resources/CMake/`` A
  296. =============================================================== ==========
  297. .. [#p3] .. versionadded:: 3.32
  298. When searching the above paths, ``find_package`` will only look for ``.cps``
  299. files in search paths which contain ``/cps/``, and will only look for
  300. ``.cmake`` files otherwise. (This only applies to the paths as specified and
  301. does not consider the contents of ``<prefix>`` or ``<name>``.)
  302. In all cases the ``<name>`` is treated as case-insensitive and corresponds
  303. to any of the names specified (``<PackageName>`` or names given by ``NAMES``).
  304. If at least one compiled language has been enabled, the architecture-specific
  305. ``lib/<arch>`` and ``lib*`` directories may be searched based on the compiler's
  306. target architecture, in the following order:
  307. ``lib/<arch>``
  308. Searched if the :variable:`CMAKE_LIBRARY_ARCHITECTURE` variable is set.
  309. ``lib64``
  310. Searched on 64 bit platforms (:variable:`CMAKE_SIZEOF_VOID_P` is 8) and the
  311. :prop_gbl:`FIND_LIBRARY_USE_LIB64_PATHS` property is set to ``TRUE``.
  312. ``lib32``
  313. Searched on 32 bit platforms (:variable:`CMAKE_SIZEOF_VOID_P` is 4) and the
  314. :prop_gbl:`FIND_LIBRARY_USE_LIB32_PATHS` property is set to ``TRUE``.
  315. ``libx32``
  316. Searched on platforms using the x32 ABI
  317. if the :prop_gbl:`FIND_LIBRARY_USE_LIBX32_PATHS` property is set to ``TRUE``.
  318. ``lib``
  319. Always searched.
  320. .. versionchanged:: 3.24
  321. On ``Windows`` platform, it is possible to include registry queries as part
  322. of the directories specified through ``HINTS`` and ``PATHS`` keywords, using
  323. a :ref:`dedicated syntax <Find Using Windows Registry>`. Such specifications
  324. will be ignored on all other platforms.
  325. .. versionadded:: 3.24
  326. ``REGISTRY_VIEW`` can be specified to manage ``Windows`` registry queries
  327. specified as part of ``PATHS`` and ``HINTS``.
  328. .. include:: FIND_XXX_REGISTRY_VIEW.txt
  329. If ``PATH_SUFFIXES`` is specified, the suffixes are appended to each
  330. (``W``) or (``U``) directory entry one-by-one.
  331. This set of directories is intended to work in cooperation with
  332. projects that provide configuration files in their installation trees.
  333. Directories above marked with (``W``) are intended for installations on
  334. Windows where the prefix may point at the top of an application's
  335. installation directory. Those marked with (``U``) are intended for
  336. installations on UNIX platforms where the prefix is shared by multiple
  337. packages. This is merely a convention, so all (``W``) and (``U``) directories
  338. are still searched on all platforms. Directories marked with (``A``) are
  339. intended for installations on Apple platforms. The
  340. :variable:`CMAKE_FIND_FRAMEWORK` and :variable:`CMAKE_FIND_APPBUNDLE`
  341. variables determine the order of preference.
  342. .. warning::
  343. Setting :variable:`CMAKE_FIND_FRAMEWORK` or :variable:`CMAKE_FIND_APPBUNDLE`
  344. to values other than ``FIRST`` (the default) will cause CMake searching for
  345. CPS files in an order that is different from the order specified by CPS.
  346. The set of installation prefixes is constructed using the following
  347. steps. If ``NO_DEFAULT_PATH`` is specified all ``NO_*`` options are
  348. enabled.
  349. 1. Search prefixes unique to the current ``<PackageName>`` being found.
  350. See policy :policy:`CMP0074`.
  351. .. versionadded:: 3.12
  352. Specifically, search prefixes specified by the following variables,
  353. in order:
  354. a. :variable:`<PackageName>_ROOT` CMake variable,
  355. where ``<PackageName>`` is the case-preserved package name.
  356. b. :variable:`<PACKAGENAME>_ROOT` CMake variable,
  357. where ``<PACKAGENAME>`` is the upper-cased package name.
  358. See policy :policy:`CMP0144`.
  359. .. versionadded:: 3.27
  360. c. :envvar:`<PackageName>_ROOT` environment variable,
  361. where ``<PackageName>`` is the case-preserved package name.
  362. d. :envvar:`<PACKAGENAME>_ROOT` environment variable,
  363. where ``<PACKAGENAME>`` is the upper-cased package name.
  364. See policy :policy:`CMP0144`.
  365. .. versionadded:: 3.27
  366. The package root variables are maintained as a stack so if
  367. called from within a find module, root paths from the parent's find
  368. module will also be searched after paths for the current package.
  369. This can be skipped if ``NO_PACKAGE_ROOT_PATH`` is passed or by setting
  370. the :variable:`CMAKE_FIND_USE_PACKAGE_ROOT_PATH` to ``FALSE``.
  371. 2. Search paths specified in cmake-specific cache variables. These
  372. are intended to be used on the command line with a :option:`-DVAR=VALUE <cmake -D>`.
  373. The values are interpreted as :ref:`semicolon-separated lists <CMake Language Lists>`.
  374. This can be skipped if ``NO_CMAKE_PATH`` is passed or by setting the
  375. :variable:`CMAKE_FIND_USE_CMAKE_PATH` to ``FALSE``:
  376. * :variable:`CMAKE_PREFIX_PATH`
  377. * :variable:`CMAKE_FRAMEWORK_PATH`
  378. * :variable:`CMAKE_APPBUNDLE_PATH`
  379. 3. Search paths specified in cmake-specific environment variables.
  380. These are intended to be set in the user's shell configuration,
  381. and therefore use the host's native path separator
  382. (``;`` on Windows and ``:`` on UNIX).
  383. This can be skipped if ``NO_CMAKE_ENVIRONMENT_PATH`` is passed or by setting
  384. the :variable:`CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH` to ``FALSE``:
  385. * ``<PackageName>_DIR``
  386. * :envvar:`CMAKE_PREFIX_PATH`
  387. * :envvar:`CMAKE_FRAMEWORK_PATH`
  388. * :envvar:`CMAKE_APPBUNDLE_PATH`
  389. 4. Search paths specified by the ``HINTS`` option. These should be paths
  390. computed by system introspection, such as a hint provided by the
  391. location of another item already found. Hard-coded guesses should
  392. be specified with the ``PATHS`` option.
  393. 5. Search the standard system environment variables. This can be
  394. skipped if ``NO_SYSTEM_ENVIRONMENT_PATH`` is passed or by setting the
  395. :variable:`CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH` to ``FALSE``. Path entries
  396. ending in ``/bin`` or ``/sbin`` are automatically converted to their
  397. parent directories:
  398. * ``PATH``
  399. 6. Search paths stored in the CMake :ref:`User Package Registry`.
  400. This can be skipped if ``NO_CMAKE_PACKAGE_REGISTRY`` is passed or by
  401. setting the variable :variable:`CMAKE_FIND_USE_PACKAGE_REGISTRY`
  402. to ``FALSE`` or the deprecated variable
  403. :variable:`CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY` to ``TRUE``.
  404. See the :manual:`cmake-packages(7)` manual for details on the user
  405. package registry.
  406. 7. Search cmake variables defined in the Platform files for the
  407. current system. The searching of :variable:`CMAKE_INSTALL_PREFIX` and
  408. :variable:`CMAKE_STAGING_PREFIX` can be
  409. skipped if ``NO_CMAKE_INSTALL_PREFIX`` is passed or by setting the
  410. :variable:`CMAKE_FIND_USE_INSTALL_PREFIX` to ``FALSE``. All these locations
  411. can be skipped if ``NO_CMAKE_SYSTEM_PATH`` is passed or by setting the
  412. :variable:`CMAKE_FIND_USE_CMAKE_SYSTEM_PATH` to ``FALSE``:
  413. * :variable:`CMAKE_SYSTEM_PREFIX_PATH`
  414. * :variable:`CMAKE_SYSTEM_FRAMEWORK_PATH`
  415. * :variable:`CMAKE_SYSTEM_APPBUNDLE_PATH`
  416. The platform paths that these variables contain are locations that
  417. typically include installed software. An example being ``/usr/local`` for
  418. UNIX based platforms.
  419. 8. Search paths stored in the CMake :ref:`System Package Registry`.
  420. This can be skipped if ``NO_CMAKE_SYSTEM_PACKAGE_REGISTRY`` is passed
  421. or by setting the :variable:`CMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY`
  422. variable to ``FALSE`` or the deprecated variable
  423. :variable:`CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY` to ``TRUE``.
  424. See the :manual:`cmake-packages(7)` manual for details on the system
  425. package registry.
  426. 9. Search paths specified by the ``PATHS`` option. These are typically
  427. hard-coded guesses.
  428. The :variable:`CMAKE_IGNORE_PATH`, :variable:`CMAKE_IGNORE_PREFIX_PATH`,
  429. :variable:`CMAKE_SYSTEM_IGNORE_PATH` and
  430. :variable:`CMAKE_SYSTEM_IGNORE_PREFIX_PATH` variables can also cause some
  431. of the above locations to be ignored.
  432. Paths are searched in the order described above. The first viable package
  433. configuration file found is used, even if a newer version of the package
  434. resides later in the list of search paths.
  435. For search paths which contain glob expressions (``*``), the order in which
  436. directories matching the glob are searched is unspecified unless the
  437. :variable:`CMAKE_FIND_PACKAGE_SORT_ORDER` variable is set. This variable,
  438. along with the :variable:`CMAKE_FIND_PACKAGE_SORT_DIRECTION` variable,
  439. determines the order in which CMake considers glob matches. For example, if
  440. the file system contains the package configuration files
  441. ::
  442. <prefix>/example-1.2/example-config.cmake
  443. <prefix>/example-1.10/example-config.cmake
  444. <prefix>/share/example-2.0/example-config.cmake
  445. it is unspecified (when the aforementioned variables are unset) whether
  446. ``find_package(example)`` will find ``example-1.2`` or ``example-1.10``
  447. (assuming that both are viable), but ``find_package`` will *not* find
  448. ``example-2.0``, because one of the other two will be found first.
  449. To control the order in which ``find_package`` searches directories that match
  450. a glob expression, use :variable:`CMAKE_FIND_PACKAGE_SORT_ORDER` and
  451. :variable:`CMAKE_FIND_PACKAGE_SORT_DIRECTION`.
  452. For instance, to cause the above example to select ``example-1.10``,
  453. one can set
  454. .. code-block:: cmake
  455. SET(CMAKE_FIND_PACKAGE_SORT_ORDER NATURAL)
  456. SET(CMAKE_FIND_PACKAGE_SORT_DIRECTION DEC)
  457. before calling ``find_package``.
  458. .. versionadded:: 3.16
  459. Added the ``CMAKE_FIND_USE_<CATEGORY>`` variables to globally disable
  460. various search locations.
  461. .. versionchanged:: 3.32
  462. The variables :variable:`CMAKE_FIND_PACKAGE_SORT_ORDER` and
  463. :variable:`CMAKE_FIND_PACKAGE_SORT_DIRECTION` now also control the order
  464. in which ``find_package`` searches directories matching the glob expression
  465. in the search paths ``<prefix>/<name>.framework/Versions/*/Resources/``
  466. and ``<prefix>/<name>.framework/Versions/*/Resources/CMake``. In previous
  467. versions of CMake, this order was unspecified.
  468. .. include:: FIND_XXX_ROOT.txt
  469. .. include:: FIND_XXX_ORDER.txt
  470. By default the value stored in the result variable will be the path at
  471. which the file is found. The :variable:`CMAKE_FIND_PACKAGE_RESOLVE_SYMLINKS`
  472. variable may be set to ``TRUE`` before calling ``find_package`` in order
  473. to resolve symbolic links and store the real path to the file.
  474. Every non-REQUIRED ``find_package`` call can be disabled or made REQUIRED:
  475. * Setting the :variable:`CMAKE_DISABLE_FIND_PACKAGE_<PackageName>` variable
  476. to ``TRUE`` disables the package. This also disables redirection to a
  477. package provided by :module:`FetchContent`.
  478. * Setting the :variable:`CMAKE_REQUIRE_FIND_PACKAGE_<PackageName>` variable
  479. to ``TRUE`` makes the package REQUIRED.
  480. Setting both variables to ``TRUE`` simultaneously is an error.
  481. .. _`version selection`:
  482. Config Mode Version Selection
  483. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  484. .. note::
  485. When Config mode is used, this version selection process is applied
  486. regardless of whether the :ref:`full <full signature>` or
  487. :ref:`basic <basic signature>` signature was given.
  488. When the ``[version]`` argument is given, Config mode will only find a
  489. version of the package that claims compatibility with the requested
  490. version (see :ref:`format specification <FIND_PACKAGE_VERSION_FORMAT>`). If the
  491. ``EXACT`` option is given, only a version of the package claiming an exact match
  492. of the requested version may be found. CMake does not establish any
  493. convention for the meaning of version numbers. Package version
  494. numbers are checked by "version" files provided by the packages themselves
  495. or by :module:`FetchContent`. For a candidate package configuration file
  496. ``<config-file>.cmake`` the corresponding version file is located next
  497. to it and named either ``<config-file>-version.cmake`` or
  498. ``<config-file>Version.cmake``. If no such version file is available
  499. then the configuration file is assumed to not be compatible with any
  500. requested version. A basic version file containing generic version
  501. matching code can be created using the
  502. :module:`CMakePackageConfigHelpers` module. When a version file
  503. is found it is loaded to check the requested version number. The
  504. version file is loaded in a nested scope in which the following
  505. variables have been defined:
  506. ``PACKAGE_FIND_NAME``
  507. The ``<PackageName>``
  508. ``PACKAGE_FIND_VERSION``
  509. Full requested version string
  510. ``PACKAGE_FIND_VERSION_MAJOR``
  511. Major version if requested, else 0
  512. ``PACKAGE_FIND_VERSION_MINOR``
  513. Minor version if requested, else 0
  514. ``PACKAGE_FIND_VERSION_PATCH``
  515. Patch version if requested, else 0
  516. ``PACKAGE_FIND_VERSION_TWEAK``
  517. Tweak version if requested, else 0
  518. ``PACKAGE_FIND_VERSION_COUNT``
  519. Number of version components, 0 to 4
  520. When a version range is specified, the above version variables will hold
  521. values based on the lower end of the version range. This is to preserve
  522. compatibility with packages that have not been implemented to expect version
  523. ranges. In addition, the version range will be described by the following
  524. variables:
  525. ``PACKAGE_FIND_VERSION_RANGE``
  526. Full requested version range string
  527. ``PACKAGE_FIND_VERSION_RANGE_MIN``
  528. This specifies whether the lower end point of the version range should be
  529. included or excluded. Currently, the only supported value for this variable
  530. is ``INCLUDE``.
  531. ``PACKAGE_FIND_VERSION_RANGE_MAX``
  532. This specifies whether the upper end point of the version range should be
  533. included or excluded. The supported values for this variable are
  534. ``INCLUDE`` and ``EXCLUDE``.
  535. ``PACKAGE_FIND_VERSION_MIN``
  536. Full requested version string of the lower end point of the range
  537. ``PACKAGE_FIND_VERSION_MIN_MAJOR``
  538. Major version of the lower end point if requested, else 0
  539. ``PACKAGE_FIND_VERSION_MIN_MINOR``
  540. Minor version of the lower end point if requested, else 0
  541. ``PACKAGE_FIND_VERSION_MIN_PATCH``
  542. Patch version of the lower end point if requested, else 0
  543. ``PACKAGE_FIND_VERSION_MIN_TWEAK``
  544. Tweak version of the lower end point if requested, else 0
  545. ``PACKAGE_FIND_VERSION_MIN_COUNT``
  546. Number of version components of the lower end point, 0 to 4
  547. ``PACKAGE_FIND_VERSION_MAX``
  548. Full requested version string of the upper end point of the range
  549. ``PACKAGE_FIND_VERSION_MAX_MAJOR``
  550. Major version of the upper end point if requested, else 0
  551. ``PACKAGE_FIND_VERSION_MAX_MINOR``
  552. Minor version of the upper end point if requested, else 0
  553. ``PACKAGE_FIND_VERSION_MAX_PATCH``
  554. Patch version of the upper end point if requested, else 0
  555. ``PACKAGE_FIND_VERSION_MAX_TWEAK``
  556. Tweak version of the upper end point if requested, else 0
  557. ``PACKAGE_FIND_VERSION_MAX_COUNT``
  558. Number of version components of the upper end point, 0 to 4
  559. Regardless of whether a single version or a version range is specified, the
  560. variable ``PACKAGE_FIND_VERSION_COMPLETE`` will be defined and will hold
  561. the full requested version string as specified.
  562. The version file checks whether it satisfies the requested version and
  563. sets these variables:
  564. ``PACKAGE_VERSION``
  565. Full provided version string
  566. ``PACKAGE_VERSION_EXACT``
  567. True if version is exact match
  568. ``PACKAGE_VERSION_COMPATIBLE``
  569. True if version is compatible
  570. ``PACKAGE_VERSION_UNSUITABLE``
  571. True if unsuitable as any version
  572. These variables are checked by the ``find_package`` command to determine
  573. whether the configuration file provides an acceptable version. They
  574. are not available after the ``find_package`` call returns. If the version
  575. is acceptable the following variables are set:
  576. ``<PackageName>_VERSION``
  577. Full provided version string
  578. ``<PackageName>_VERSION_MAJOR``
  579. Major version if provided, else 0
  580. ``<PackageName>_VERSION_MINOR``
  581. Minor version if provided, else 0
  582. ``<PackageName>_VERSION_PATCH``
  583. Patch version if provided, else 0
  584. ``<PackageName>_VERSION_TWEAK``
  585. Tweak version if provided, else 0
  586. ``<PackageName>_VERSION_COUNT``
  587. Number of version components, 0 to 4
  588. and the corresponding package configuration file is loaded.
  589. Package File Interface Variables
  590. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  591. When loading a find module or package configuration file ``find_package``
  592. defines variables to provide information about the call arguments (and
  593. restores their original state before returning):
  594. ``CMAKE_FIND_PACKAGE_NAME``
  595. The ``<PackageName>`` which is searched for
  596. ``<PackageName>_FIND_REQUIRED``
  597. True if ``REQUIRED`` option was given
  598. ``<PackageName>_FIND_QUIETLY``
  599. True if ``QUIET`` option was given
  600. ``<PackageName>_FIND_REGISTRY_VIEW``
  601. The requested view if ``REGISTRY_VIEW`` option was given
  602. ``<PackageName>_FIND_VERSION``
  603. Full requested version string
  604. ``<PackageName>_FIND_VERSION_MAJOR``
  605. Major version if requested, else 0
  606. ``<PackageName>_FIND_VERSION_MINOR``
  607. Minor version if requested, else 0
  608. ``<PackageName>_FIND_VERSION_PATCH``
  609. Patch version if requested, else 0
  610. ``<PackageName>_FIND_VERSION_TWEAK``
  611. Tweak version if requested, else 0
  612. ``<PackageName>_FIND_VERSION_COUNT``
  613. Number of version components, 0 to 4
  614. ``<PackageName>_FIND_VERSION_EXACT``
  615. True if ``EXACT`` option was given
  616. ``<PackageName>_FIND_COMPONENTS``
  617. List of specified components (required and optional)
  618. ``<PackageName>_FIND_REQUIRED_<c>``
  619. True if component ``<c>`` is required,
  620. false if component ``<c>`` is optional
  621. When a version range is specified, the above version variables will hold
  622. values based on the lower end of the version range. This is to preserve
  623. compatibility with packages that have not been implemented to expect version
  624. ranges. In addition, the version range will be described by the following
  625. variables:
  626. ``<PackageName>_FIND_VERSION_RANGE``
  627. Full requested version range string
  628. ``<PackageName>_FIND_VERSION_RANGE_MIN``
  629. This specifies whether the lower end point of the version range is
  630. included or excluded. Currently, ``INCLUDE`` is the only supported value.
  631. ``<PackageName>_FIND_VERSION_RANGE_MAX``
  632. This specifies whether the upper end point of the version range is
  633. included or excluded. The possible values for this variable are
  634. ``INCLUDE`` or ``EXCLUDE``.
  635. ``<PackageName>_FIND_VERSION_MIN``
  636. Full requested version string of the lower end point of the range
  637. ``<PackageName>_FIND_VERSION_MIN_MAJOR``
  638. Major version of the lower end point if requested, else 0
  639. ``<PackageName>_FIND_VERSION_MIN_MINOR``
  640. Minor version of the lower end point if requested, else 0
  641. ``<PackageName>_FIND_VERSION_MIN_PATCH``
  642. Patch version of the lower end point if requested, else 0
  643. ``<PackageName>_FIND_VERSION_MIN_TWEAK``
  644. Tweak version of the lower end point if requested, else 0
  645. ``<PackageName>_FIND_VERSION_MIN_COUNT``
  646. Number of version components of the lower end point, 0 to 4
  647. ``<PackageName>_FIND_VERSION_MAX``
  648. Full requested version string of the upper end point of the range
  649. ``<PackageName>_FIND_VERSION_MAX_MAJOR``
  650. Major version of the upper end point if requested, else 0
  651. ``<PackageName>_FIND_VERSION_MAX_MINOR``
  652. Minor version of the upper end point if requested, else 0
  653. ``<PackageName>_FIND_VERSION_MAX_PATCH``
  654. Patch version of the upper end point if requested, else 0
  655. ``<PackageName>_FIND_VERSION_MAX_TWEAK``
  656. Tweak version of the upper end point if requested, else 0
  657. ``<PackageName>_FIND_VERSION_MAX_COUNT``
  658. Number of version components of the upper end point, 0 to 4
  659. Regardless of whether a single version or a version range is specified, the
  660. variable ``<PackageName>_FIND_VERSION_COMPLETE`` will be defined and will hold
  661. the full requested version string as specified.
  662. In Module mode the loaded find module is responsible to honor the
  663. request detailed by these variables; see the find module for details.
  664. In Config mode ``find_package`` handles ``REQUIRED``, ``QUIET``, and
  665. ``[version]`` options automatically but leaves it to the package
  666. configuration file to handle components in a way that makes sense
  667. for the package. The package configuration file may set
  668. ``<PackageName>_FOUND`` to false to tell ``find_package`` that component
  669. requirements are not satisfied.