find_package.rst 32 KB

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