find_package.rst 32 KB

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