find_package.rst 33 KB

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