find_package.rst 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. If at least one compiled language has been enabled, the architecture-specific
  271. ``lib/<arch>`` and ``lib*`` directories may be searched based on the compiler's
  272. target architecture, in the following order:
  273. ``lib/<arch>``
  274. Searched if the :variable:`CMAKE_LIBRARY_ARCHITECTURE` variable is set.
  275. ``lib64``
  276. Searched on 64 bit platforms (:variable:`CMAKE_SIZEOF_VOID_P` is 8) and the
  277. :prop_gbl:`FIND_LIBRARY_USE_LIB64_PATHS` property is set to ``TRUE``.
  278. ``lib32``
  279. Searched on 32 bit platforms (:variable:`CMAKE_SIZEOF_VOID_P` is 4) and the
  280. :prop_gbl:`FIND_LIBRARY_USE_LIB32_PATHS` property is set to ``TRUE``.
  281. ``libx32``
  282. Searched on platforms using the x32 ABI
  283. if the :prop_gbl:`FIND_LIBRARY_USE_LIBX32_PATHS` property is set to ``TRUE``.
  284. ``lib``
  285. Always searched.
  286. .. versionchanged:: 3.24
  287. On ``Windows`` platform, it is possible to include registry queries as part
  288. of the directories specified through ``HINTS`` and ``PATHS`` keywords, using
  289. a :ref:`dedicated syntax <Find Using Windows Registry>`. Such specifications
  290. will be ignored on all other platforms.
  291. .. versionadded:: 3.24
  292. ``REGISTRY_VIEW`` can be specified to manage ``Windows`` registry queries
  293. specified as part of ``PATHS`` and ``HINTS``.
  294. .. include:: FIND_XXX_REGISTRY_VIEW.txt
  295. If ``PATH_SUFFIXES`` is specified, the suffixes are appended to each
  296. (``W``) or (``U``) directory entry one-by-one.
  297. This set of directories is intended to work in cooperation with
  298. projects that provide configuration files in their installation trees.
  299. Directories above marked with (``W``) are intended for installations on
  300. Windows where the prefix may point at the top of an application's
  301. installation directory. Those marked with (``U``) are intended for
  302. installations on UNIX platforms where the prefix is shared by multiple
  303. packages. This is merely a convention, so all (``W``) and (``U``) directories
  304. are still searched on all platforms. Directories marked with (``A``) are
  305. intended for installations on Apple platforms. The
  306. :variable:`CMAKE_FIND_FRAMEWORK` and :variable:`CMAKE_FIND_APPBUNDLE`
  307. variables determine the order of preference.
  308. The set of installation prefixes is constructed using the following
  309. steps. If ``NO_DEFAULT_PATH`` is specified all ``NO_*`` options are
  310. enabled.
  311. 1. Search prefixes unique to the current ``<PackageName>`` being found.
  312. See policy :policy:`CMP0074`.
  313. .. versionadded:: 3.12
  314. Specifically, search prefixes specified by the following variables,
  315. in order:
  316. a. :variable:`<PackageName>_ROOT` CMake variable,
  317. where ``<PackageName>`` is the case-preserved package name.
  318. b. :variable:`<PACKAGENAME>_ROOT` CMake variable,
  319. where ``<PACKAGENAME>`` is the upper-cased package name.
  320. See policy :policy:`CMP0144`.
  321. .. versionadded:: 3.27
  322. c. :envvar:`<PackageName>_ROOT` environment variable,
  323. where ``<PackageName>`` is the case-preserved package name.
  324. d. :envvar:`<PACKAGENAME>_ROOT` environment variable,
  325. where ``<PACKAGENAME>`` is the upper-cased package name.
  326. See policy :policy:`CMP0144`.
  327. .. versionadded:: 3.27
  328. The package root variables are maintained as a stack so if
  329. called from within a find module, root paths from the parent's find
  330. module will also be searched after paths for the current package.
  331. This can be skipped if ``NO_PACKAGE_ROOT_PATH`` is passed or by setting
  332. the :variable:`CMAKE_FIND_USE_PACKAGE_ROOT_PATH` to ``FALSE``.
  333. 2. Search paths specified in cmake-specific cache variables. These
  334. are intended to be used on the command line with a :option:`-DVAR=VALUE <cmake -D>`.
  335. The values are interpreted as :ref:`semicolon-separated lists <CMake Language Lists>`.
  336. This can be skipped if ``NO_CMAKE_PATH`` is passed or by setting the
  337. :variable:`CMAKE_FIND_USE_CMAKE_PATH` to ``FALSE``:
  338. * :variable:`CMAKE_PREFIX_PATH`
  339. * :variable:`CMAKE_FRAMEWORK_PATH`
  340. * :variable:`CMAKE_APPBUNDLE_PATH`
  341. 3. Search paths specified in cmake-specific environment variables.
  342. These are intended to be set in the user's shell configuration,
  343. and therefore use the host's native path separator
  344. (``;`` on Windows and ``:`` on UNIX).
  345. This can be skipped if ``NO_CMAKE_ENVIRONMENT_PATH`` is passed or by setting
  346. the :variable:`CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH` to ``FALSE``:
  347. * ``<PackageName>_DIR``
  348. * :envvar:`CMAKE_PREFIX_PATH`
  349. * :envvar:`CMAKE_FRAMEWORK_PATH`
  350. * :envvar:`CMAKE_APPBUNDLE_PATH`
  351. 4. Search paths specified by the ``HINTS`` option. These should be paths
  352. computed by system introspection, such as a hint provided by the
  353. location of another item already found. Hard-coded guesses should
  354. be specified with the ``PATHS`` option.
  355. 5. Search the standard system environment variables. This can be
  356. skipped if ``NO_SYSTEM_ENVIRONMENT_PATH`` is passed or by setting the
  357. :variable:`CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH` to ``FALSE``. Path entries
  358. ending in ``/bin`` or ``/sbin`` are automatically converted to their
  359. parent directories:
  360. * ``PATH``
  361. 6. Search paths stored in the CMake :ref:`User Package Registry`.
  362. This can be skipped if ``NO_CMAKE_PACKAGE_REGISTRY`` is passed or by
  363. setting the variable :variable:`CMAKE_FIND_USE_PACKAGE_REGISTRY`
  364. to ``FALSE`` or the deprecated variable
  365. :variable:`CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY` to ``TRUE``.
  366. See the :manual:`cmake-packages(7)` manual for details on the user
  367. package registry.
  368. 7. Search cmake variables defined in the Platform files for the
  369. current system. The searching of :variable:`CMAKE_INSTALL_PREFIX` and
  370. :variable:`CMAKE_STAGING_PREFIX` can be
  371. skipped if ``NO_CMAKE_INSTALL_PREFIX`` is passed or by setting the
  372. :variable:`CMAKE_FIND_USE_INSTALL_PREFIX` to ``FALSE``. All these locations
  373. can be skipped if ``NO_CMAKE_SYSTEM_PATH`` is passed or by setting the
  374. :variable:`CMAKE_FIND_USE_CMAKE_SYSTEM_PATH` to ``FALSE``:
  375. * :variable:`CMAKE_SYSTEM_PREFIX_PATH`
  376. * :variable:`CMAKE_SYSTEM_FRAMEWORK_PATH`
  377. * :variable:`CMAKE_SYSTEM_APPBUNDLE_PATH`
  378. The platform paths that these variables contain are locations that
  379. typically include installed software. An example being ``/usr/local`` for
  380. UNIX based platforms.
  381. 8. Search paths stored in the CMake :ref:`System Package Registry`.
  382. This can be skipped if ``NO_CMAKE_SYSTEM_PACKAGE_REGISTRY`` is passed
  383. or by setting the :variable:`CMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY`
  384. variable to ``FALSE`` or the deprecated variable
  385. :variable:`CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY` to ``TRUE``.
  386. See the :manual:`cmake-packages(7)` manual for details on the system
  387. package registry.
  388. 9. Search paths specified by the ``PATHS`` option. These are typically
  389. hard-coded guesses.
  390. The :variable:`CMAKE_IGNORE_PATH`, :variable:`CMAKE_IGNORE_PREFIX_PATH`,
  391. :variable:`CMAKE_SYSTEM_IGNORE_PATH` and
  392. :variable:`CMAKE_SYSTEM_IGNORE_PREFIX_PATH` variables can also cause some
  393. of the above locations to be ignored.
  394. .. versionadded:: 3.16
  395. Added the ``CMAKE_FIND_USE_<CATEGORY>`` variables to globally disable
  396. various search locations.
  397. .. include:: FIND_XXX_ROOT.txt
  398. .. include:: FIND_XXX_ORDER.txt
  399. By default the value stored in the result variable will be the path at
  400. which the file is found. The :variable:`CMAKE_FIND_PACKAGE_RESOLVE_SYMLINKS`
  401. variable may be set to ``TRUE`` before calling ``find_package`` in order
  402. to resolve symbolic links and store the real path to the file.
  403. Every non-REQUIRED ``find_package`` call can be disabled or made REQUIRED:
  404. * Setting the :variable:`CMAKE_DISABLE_FIND_PACKAGE_<PackageName>` variable
  405. to ``TRUE`` disables the package. This also disables redirection to a
  406. package provided by :module:`FetchContent`.
  407. * Setting the :variable:`CMAKE_REQUIRE_FIND_PACKAGE_<PackageName>` variable
  408. to ``TRUE`` makes the package REQUIRED.
  409. Setting both variables to ``TRUE`` simultaneously is an error.
  410. .. _`version selection`:
  411. Config Mode Version Selection
  412. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  413. .. note::
  414. When Config mode is used, this version selection process is applied
  415. regardless of whether the :ref:`full <full signature>` or
  416. :ref:`basic <basic signature>` signature was given.
  417. When the ``[version]`` argument is given, Config mode will only find a
  418. version of the package that claims compatibility with the requested
  419. version (see :ref:`format specification <FIND_PACKAGE_VERSION_FORMAT>`). If the
  420. ``EXACT`` option is given, only a version of the package claiming an exact match
  421. of the requested version may be found. CMake does not establish any
  422. convention for the meaning of version numbers. Package version
  423. numbers are checked by "version" files provided by the packages themselves
  424. or by :module:`FetchContent`. For a candidate package configuration file
  425. ``<config-file>.cmake`` the corresponding version file is located next
  426. to it and named either ``<config-file>-version.cmake`` or
  427. ``<config-file>Version.cmake``. If no such version file is available
  428. then the configuration file is assumed to not be compatible with any
  429. requested version. A basic version file containing generic version
  430. matching code can be created using the
  431. :module:`CMakePackageConfigHelpers` module. When a version file
  432. is found it is loaded to check the requested version number. The
  433. version file is loaded in a nested scope in which the following
  434. variables have been defined:
  435. ``PACKAGE_FIND_NAME``
  436. The ``<PackageName>``
  437. ``PACKAGE_FIND_VERSION``
  438. Full requested version string
  439. ``PACKAGE_FIND_VERSION_MAJOR``
  440. Major version if requested, else 0
  441. ``PACKAGE_FIND_VERSION_MINOR``
  442. Minor version if requested, else 0
  443. ``PACKAGE_FIND_VERSION_PATCH``
  444. Patch version if requested, else 0
  445. ``PACKAGE_FIND_VERSION_TWEAK``
  446. Tweak version if requested, else 0
  447. ``PACKAGE_FIND_VERSION_COUNT``
  448. Number of version components, 0 to 4
  449. When a version range is specified, the above version variables will hold
  450. values based on the lower end of the version range. This is to preserve
  451. compatibility with packages that have not been implemented to expect version
  452. ranges. In addition, the version range will be described by the following
  453. variables:
  454. ``PACKAGE_FIND_VERSION_RANGE``
  455. Full requested version range string
  456. ``PACKAGE_FIND_VERSION_RANGE_MIN``
  457. This specifies whether the lower end point of the version range should be
  458. included or excluded. Currently, the only supported value for this variable
  459. is ``INCLUDE``.
  460. ``PACKAGE_FIND_VERSION_RANGE_MAX``
  461. This specifies whether the upper end point of the version range should be
  462. included or excluded. The supported values for this variable are
  463. ``INCLUDE`` and ``EXCLUDE``.
  464. ``PACKAGE_FIND_VERSION_MIN``
  465. Full requested version string of the lower end point of the range
  466. ``PACKAGE_FIND_VERSION_MIN_MAJOR``
  467. Major version of the lower end point if requested, else 0
  468. ``PACKAGE_FIND_VERSION_MIN_MINOR``
  469. Minor version of the lower end point if requested, else 0
  470. ``PACKAGE_FIND_VERSION_MIN_PATCH``
  471. Patch version of the lower end point if requested, else 0
  472. ``PACKAGE_FIND_VERSION_MIN_TWEAK``
  473. Tweak version of the lower end point if requested, else 0
  474. ``PACKAGE_FIND_VERSION_MIN_COUNT``
  475. Number of version components of the lower end point, 0 to 4
  476. ``PACKAGE_FIND_VERSION_MAX``
  477. Full requested version string of the upper end point of the range
  478. ``PACKAGE_FIND_VERSION_MAX_MAJOR``
  479. Major version of the upper end point if requested, else 0
  480. ``PACKAGE_FIND_VERSION_MAX_MINOR``
  481. Minor version of the upper end point if requested, else 0
  482. ``PACKAGE_FIND_VERSION_MAX_PATCH``
  483. Patch version of the upper end point if requested, else 0
  484. ``PACKAGE_FIND_VERSION_MAX_TWEAK``
  485. Tweak version of the upper end point if requested, else 0
  486. ``PACKAGE_FIND_VERSION_MAX_COUNT``
  487. Number of version components of the upper end point, 0 to 4
  488. Regardless of whether a single version or a version range is specified, the
  489. variable ``PACKAGE_FIND_VERSION_COMPLETE`` will be defined and will hold
  490. the full requested version string as specified.
  491. The version file checks whether it satisfies the requested version and
  492. sets these variables:
  493. ``PACKAGE_VERSION``
  494. Full provided version string
  495. ``PACKAGE_VERSION_EXACT``
  496. True if version is exact match
  497. ``PACKAGE_VERSION_COMPATIBLE``
  498. True if version is compatible
  499. ``PACKAGE_VERSION_UNSUITABLE``
  500. True if unsuitable as any version
  501. These variables are checked by the ``find_package`` command to determine
  502. whether the configuration file provides an acceptable version. They
  503. are not available after the ``find_package`` call returns. If the version
  504. is acceptable the following variables are set:
  505. ``<PackageName>_VERSION``
  506. Full provided version string
  507. ``<PackageName>_VERSION_MAJOR``
  508. Major version if provided, else 0
  509. ``<PackageName>_VERSION_MINOR``
  510. Minor version if provided, else 0
  511. ``<PackageName>_VERSION_PATCH``
  512. Patch version if provided, else 0
  513. ``<PackageName>_VERSION_TWEAK``
  514. Tweak version if provided, else 0
  515. ``<PackageName>_VERSION_COUNT``
  516. Number of version components, 0 to 4
  517. and the corresponding package configuration file is loaded.
  518. When multiple package configuration files are available whose version files
  519. claim compatibility with the version requested it is unspecified which
  520. one is chosen: unless the variable :variable:`CMAKE_FIND_PACKAGE_SORT_ORDER`
  521. is set no attempt is made to choose a highest or closest version number.
  522. To control the order in which ``find_package`` checks for compatibility use
  523. the two variables :variable:`CMAKE_FIND_PACKAGE_SORT_ORDER` and
  524. :variable:`CMAKE_FIND_PACKAGE_SORT_DIRECTION`.
  525. For instance in order to select the highest version one can set
  526. .. code-block:: cmake
  527. SET(CMAKE_FIND_PACKAGE_SORT_ORDER NATURAL)
  528. SET(CMAKE_FIND_PACKAGE_SORT_DIRECTION DEC)
  529. before calling ``find_package``.
  530. Package File Interface Variables
  531. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  532. When loading a find module or package configuration file ``find_package``
  533. defines variables to provide information about the call arguments (and
  534. restores their original state before returning):
  535. ``CMAKE_FIND_PACKAGE_NAME``
  536. The ``<PackageName>`` which is searched for
  537. ``<PackageName>_FIND_REQUIRED``
  538. True if ``REQUIRED`` option was given
  539. ``<PackageName>_FIND_QUIETLY``
  540. True if ``QUIET`` option was given
  541. ``<PackageName>_FIND_REGISTRY_VIEW``
  542. The requested view if ``REGISTRY_VIEW`` option was given
  543. ``<PackageName>_FIND_VERSION``
  544. Full requested version string
  545. ``<PackageName>_FIND_VERSION_MAJOR``
  546. Major version if requested, else 0
  547. ``<PackageName>_FIND_VERSION_MINOR``
  548. Minor version if requested, else 0
  549. ``<PackageName>_FIND_VERSION_PATCH``
  550. Patch version if requested, else 0
  551. ``<PackageName>_FIND_VERSION_TWEAK``
  552. Tweak version if requested, else 0
  553. ``<PackageName>_FIND_VERSION_COUNT``
  554. Number of version components, 0 to 4
  555. ``<PackageName>_FIND_VERSION_EXACT``
  556. True if ``EXACT`` option was given
  557. ``<PackageName>_FIND_COMPONENTS``
  558. List of specified components (required and optional)
  559. ``<PackageName>_FIND_REQUIRED_<c>``
  560. True if component ``<c>`` is required,
  561. false if component ``<c>`` is optional
  562. When a version range is specified, the above version variables will hold
  563. values based on the lower end of the version range. This is to preserve
  564. compatibility with packages that have not been implemented to expect version
  565. ranges. In addition, the version range will be described by the following
  566. variables:
  567. ``<PackageName>_FIND_VERSION_RANGE``
  568. Full requested version range string
  569. ``<PackageName>_FIND_VERSION_RANGE_MIN``
  570. This specifies whether the lower end point of the version range is
  571. included or excluded. Currently, ``INCLUDE`` is the only supported value.
  572. ``<PackageName>_FIND_VERSION_RANGE_MAX``
  573. This specifies whether the upper end point of the version range is
  574. included or excluded. The possible values for this variable are
  575. ``INCLUDE`` or ``EXCLUDE``.
  576. ``<PackageName>_FIND_VERSION_MIN``
  577. Full requested version string of the lower end point of the range
  578. ``<PackageName>_FIND_VERSION_MIN_MAJOR``
  579. Major version of the lower end point if requested, else 0
  580. ``<PackageName>_FIND_VERSION_MIN_MINOR``
  581. Minor version of the lower end point if requested, else 0
  582. ``<PackageName>_FIND_VERSION_MIN_PATCH``
  583. Patch version of the lower end point if requested, else 0
  584. ``<PackageName>_FIND_VERSION_MIN_TWEAK``
  585. Tweak version of the lower end point if requested, else 0
  586. ``<PackageName>_FIND_VERSION_MIN_COUNT``
  587. Number of version components of the lower end point, 0 to 4
  588. ``<PackageName>_FIND_VERSION_MAX``
  589. Full requested version string of the upper end point of the range
  590. ``<PackageName>_FIND_VERSION_MAX_MAJOR``
  591. Major version of the upper end point if requested, else 0
  592. ``<PackageName>_FIND_VERSION_MAX_MINOR``
  593. Minor version of the upper end point if requested, else 0
  594. ``<PackageName>_FIND_VERSION_MAX_PATCH``
  595. Patch version of the upper end point if requested, else 0
  596. ``<PackageName>_FIND_VERSION_MAX_TWEAK``
  597. Tweak version of the upper end point if requested, else 0
  598. ``<PackageName>_FIND_VERSION_MAX_COUNT``
  599. Number of version components of the upper end point, 0 to 4
  600. Regardless of whether a single version or a version range is specified, the
  601. variable ``<PackageName>_FIND_VERSION_COMPLETE`` will be defined and will hold
  602. the full requested version string as specified.
  603. In Module mode the loaded find module is responsible to honor the
  604. request detailed by these variables; see the find module for details.
  605. In Config mode ``find_package`` handles ``REQUIRED``, ``QUIET``, and
  606. ``[version]`` options automatically but leaves it to the package
  607. configuration file to handle components in a way that makes sense
  608. for the package. The package configuration file may set
  609. ``<PackageName>_FOUND`` to false to tell ``find_package`` that component
  610. requirements are not satisfied.