find_package.rst 31 KB

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