find_package.rst 39 KB

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