find_package.rst 31 KB

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