find_package.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. find_package
  2. ------------
  3. .. only:: html
  4. .. contents::
  5. Find an external project, and load its settings.
  6. .. _`basic signature`:
  7. Basic Signature and Module Mode
  8. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  9. .. code-block:: cmake
  10. find_package(<PackageName> [version] [EXACT] [QUIET] [MODULE]
  11. [REQUIRED] [[COMPONENTS] [components...]]
  12. [OPTIONAL_COMPONENTS components...]
  13. [NO_POLICY_SCOPE])
  14. Finds and loads settings from an external project. ``<PackageName>_FOUND``
  15. will be set to indicate whether the package was found. When the
  16. package is found package-specific information is provided through
  17. variables and :ref:`Imported Targets` documented by the package itself. The
  18. ``QUIET`` option disables informational messages, including those indicating
  19. that the package cannot be found if it is not ``REQUIRED``. The ``REQUIRED``
  20. option stops processing with an error message if the package cannot be found.
  21. A package-specific list of required components may be listed after the
  22. ``COMPONENTS`` option (or after the ``REQUIRED`` option if present).
  23. Additional optional components may be listed after
  24. ``OPTIONAL_COMPONENTS``. Available components and their influence on
  25. whether a package is considered to be found are defined by the target
  26. package.
  27. The ``[version]`` argument requests a version with which the package found
  28. should be compatible (format is ``major[.minor[.patch[.tweak]]]``). The
  29. ``EXACT`` option requests that the version be matched exactly. If no
  30. ``[version]`` and/or component list is given to a recursive invocation
  31. inside a find-module, the corresponding arguments are forwarded
  32. automatically from the outer call (including the ``EXACT`` flag for
  33. ``[version]``). Version support is currently provided only on a
  34. package-by-package basis (see the `Version Selection`_ section below).
  35. See the :command:`cmake_policy` command documentation for discussion
  36. of the ``NO_POLICY_SCOPE`` option.
  37. The command has two modes by which it searches for packages: "Module"
  38. mode and "Config" mode. The above signature selects Module mode.
  39. If no module is found the command falls back to Config mode, described
  40. below. This fall back is disabled if the ``MODULE`` option is given.
  41. In Module mode, CMake searches for a file called ``Find<PackageName>.cmake``.
  42. The file is first searched in the :variable:`CMAKE_MODULE_PATH`,
  43. then among the :ref:`Find Modules` provided by the CMake installation.
  44. If the file is found, it is read and processed by CMake. It is responsible
  45. for finding the package, checking the version, and producing any needed
  46. messages. Some find-modules provide limited or no support for versioning;
  47. check the module documentation.
  48. If the ``MODULE`` option is not specfied in the above signature,
  49. CMake first searches for the package using Module mode. Then, if the
  50. package is not found, it searches again using Config mode. A user
  51. may set the variable :variable:`CMAKE_FIND_PACKAGE_PREFER_CONFIG` to
  52. ``TRUE`` to direct CMake first search using Config mode before falling
  53. back to Module mode.
  54. Full Signature and Config Mode
  55. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  56. User code should generally look for packages using the above `basic
  57. signature`_. The remainder of this command documentation specifies the
  58. full command signature and details of the search process. Project
  59. maintainers wishing to provide a package to be found by this command
  60. are encouraged to read on.
  61. The complete Config mode command signature is
  62. .. code-block:: cmake
  63. find_package(<PackageName> [version] [EXACT] [QUIET]
  64. [REQUIRED] [[COMPONENTS] [components...]]
  65. [OPTIONAL_COMPONENTS components...]
  66. [CONFIG|NO_MODULE]
  67. [NO_POLICY_SCOPE]
  68. [NAMES name1 [name2 ...]]
  69. [CONFIGS config1 [config2 ...]]
  70. [HINTS path1 [path2 ... ]]
  71. [PATHS path1 [path2 ... ]]
  72. [PATH_SUFFIXES suffix1 [suffix2 ...]]
  73. [NO_DEFAULT_PATH]
  74. [NO_PACKAGE_ROOT_PATH]
  75. [NO_CMAKE_PATH]
  76. [NO_CMAKE_ENVIRONMENT_PATH]
  77. [NO_SYSTEM_ENVIRONMENT_PATH]
  78. [NO_CMAKE_PACKAGE_REGISTRY]
  79. [NO_CMAKE_BUILDS_PATH] # Deprecated; does nothing.
  80. [NO_CMAKE_SYSTEM_PATH]
  81. [NO_CMAKE_SYSTEM_PACKAGE_REGISTRY]
  82. [CMAKE_FIND_ROOT_PATH_BOTH |
  83. ONLY_CMAKE_FIND_ROOT_PATH |
  84. NO_CMAKE_FIND_ROOT_PATH])
  85. The ``CONFIG`` option, the synonymous ``NO_MODULE`` option, or the use
  86. of options not specified in the `basic signature`_ all enforce pure Config
  87. mode. In pure Config mode, the command skips Module mode search and
  88. proceeds at once with Config mode search.
  89. Config mode search attempts to locate a configuration file provided by the
  90. package to be found. A cache entry called ``<PackageName>_DIR`` is created to
  91. hold the directory containing the file. By default the command
  92. searches for a package with the name ``<PackageName>``. If the ``NAMES`` option
  93. is given the names following it are used instead of ``<PackageName>``.
  94. The command searches for a file called ``<PackageName>Config.cmake`` or
  95. ``<lower-case-package-name>-config.cmake`` for each name specified.
  96. A replacement set of possible configuration file names may be given
  97. using the ``CONFIGS`` option. The search procedure is specified below.
  98. Once found, the configuration file is read and processed by CMake.
  99. Since the file is provided by the package it already knows the
  100. location of package contents. The full path to the configuration file
  101. is stored in the cmake variable ``<PackageName>_CONFIG``.
  102. All configuration files which have been considered by CMake while
  103. searching for an installation of the package with an appropriate
  104. version are stored in the cmake variable ``<PackageName>_CONSIDERED_CONFIGS``,
  105. the associated versions in ``<PackageName>_CONSIDERED_VERSIONS``.
  106. If the package configuration file cannot be found CMake will generate
  107. an error describing the problem unless the ``QUIET`` argument is
  108. specified. If ``REQUIRED`` is specified and the package is not found a
  109. fatal error is generated and the configure step stops executing. If
  110. ``<PackageName>_DIR`` has been set to a directory not containing a
  111. configuration file CMake will ignore it and search from scratch.
  112. Package maintainers providing CMake package configuration files are
  113. encouraged to name and install them such that the `Search Procedure`_
  114. outlined below will find them without requiring use of additional options.
  115. Version Selection
  116. ^^^^^^^^^^^^^^^^^
  117. When the ``[version]`` argument is given Config mode will only find a
  118. version of the package that claims compatibility with the requested
  119. version (format is ``major[.minor[.patch[.tweak]]]``). If the ``EXACT``
  120. option is given only a version of the package claiming an exact match
  121. of the requested version may be found. CMake does not establish any
  122. convention for the meaning of version numbers. Package version
  123. numbers are checked by "version" files provided by the packages
  124. themselves. For a candidate package configuration file
  125. ``<config-file>.cmake`` the corresponding version file is located next
  126. to it and named either ``<config-file>-version.cmake`` or
  127. ``<config-file>Version.cmake``. If no such version file is available
  128. then the configuration file is assumed to not be compatible with any
  129. requested version. A basic version file containing generic version
  130. matching code can be created using the
  131. :module:`CMakePackageConfigHelpers` module. When a version file
  132. is found it is loaded to check the requested version number. The
  133. version file is loaded in a nested scope in which the following
  134. variables have been defined:
  135. ``PACKAGE_FIND_NAME``
  136. the ``<PackageName>``
  137. ``PACKAGE_FIND_VERSION``
  138. full requested version string
  139. ``PACKAGE_FIND_VERSION_MAJOR``
  140. major version if requested, else 0
  141. ``PACKAGE_FIND_VERSION_MINOR``
  142. minor version if requested, else 0
  143. ``PACKAGE_FIND_VERSION_PATCH``
  144. patch version if requested, else 0
  145. ``PACKAGE_FIND_VERSION_TWEAK``
  146. tweak version if requested, else 0
  147. ``PACKAGE_FIND_VERSION_COUNT``
  148. number of version components, 0 to 4
  149. The version file checks whether it satisfies the requested version and
  150. sets these variables:
  151. ``PACKAGE_VERSION``
  152. full provided version string
  153. ``PACKAGE_VERSION_EXACT``
  154. true if version is exact match
  155. ``PACKAGE_VERSION_COMPATIBLE``
  156. true if version is compatible
  157. ``PACKAGE_VERSION_UNSUITABLE``
  158. true if unsuitable as any version
  159. These variables are checked by the ``find_package`` command to determine
  160. whether the configuration file provides an acceptable version. They
  161. are not available after the ``find_package`` call returns. If the version
  162. is acceptable the following variables are set:
  163. ``<PackageName>_VERSION``
  164. full provided version string
  165. ``<PackageName>_VERSION_MAJOR``
  166. major version if provided, else 0
  167. ``<PackageName>_VERSION_MINOR``
  168. minor version if provided, else 0
  169. ``<PackageName>_VERSION_PATCH``
  170. patch version if provided, else 0
  171. ``<PackageName>_VERSION_TWEAK``
  172. tweak version if provided, else 0
  173. ``<PackageName>_VERSION_COUNT``
  174. number of version components, 0 to 4
  175. and the corresponding package configuration file is loaded.
  176. When multiple package configuration files are available whose version files
  177. claim compatibility with the version requested it is unspecified which
  178. one is chosen: unless the variable :variable:`CMAKE_FIND_PACKAGE_SORT_ORDER`
  179. is set no attempt is made to choose a highest or closest version number.
  180. To control the order in which ``find_package`` checks for compatibility use
  181. the two variables :variable:`CMAKE_FIND_PACKAGE_SORT_ORDER` and
  182. :variable:`CMAKE_FIND_PACKAGE_SORT_DIRECTION`.
  183. For instance in order to select the highest version one can set
  184. .. code-block:: cmake
  185. SET(CMAKE_FIND_PACKAGE_SORT_ORDER NATURAL)
  186. SET(CMAKE_FIND_PACKAGE_SORT_DIRECTION DEC)
  187. before calling ``find_package``.
  188. Search Procedure
  189. ^^^^^^^^^^^^^^^^
  190. CMake constructs a set of possible installation prefixes for the
  191. package. Under each prefix several directories are searched for a
  192. configuration file. The tables below show the directories searched.
  193. Each entry is meant for installation trees following Windows (``W``), UNIX
  194. (``U``), or Apple (``A``) conventions::
  195. <prefix>/ (W)
  196. <prefix>/(cmake|CMake)/ (W)
  197. <prefix>/<name>*/ (W)
  198. <prefix>/<name>*/(cmake|CMake)/ (W)
  199. <prefix>/(lib/<arch>|lib*|share)/cmake/<name>*/ (U)
  200. <prefix>/(lib/<arch>|lib*|share)/<name>*/ (U)
  201. <prefix>/(lib/<arch>|lib*|share)/<name>*/(cmake|CMake)/ (U)
  202. <prefix>/<name>*/(lib/<arch>|lib*|share)/cmake/<name>*/ (W/U)
  203. <prefix>/<name>*/(lib/<arch>|lib*|share)/<name>*/ (W/U)
  204. <prefix>/<name>*/(lib/<arch>|lib*|share)/<name>*/(cmake|CMake)/ (W/U)
  205. On systems supporting macOS :prop_tgt:`FRAMEWORK` and :prop_tgt:`BUNDLE`, the
  206. following directories are searched for Frameworks or Application Bundles
  207. containing a configuration file::
  208. <prefix>/<name>.framework/Resources/ (A)
  209. <prefix>/<name>.framework/Resources/CMake/ (A)
  210. <prefix>/<name>.framework/Versions/*/Resources/ (A)
  211. <prefix>/<name>.framework/Versions/*/Resources/CMake/ (A)
  212. <prefix>/<name>.app/Contents/Resources/ (A)
  213. <prefix>/<name>.app/Contents/Resources/CMake/ (A)
  214. In all cases the ``<name>`` is treated as case-insensitive and corresponds
  215. to any of the names specified (``<PackageName>`` or names given by ``NAMES``).
  216. Paths with ``lib/<arch>`` are enabled if the
  217. :variable:`CMAKE_LIBRARY_ARCHITECTURE` variable is set. ``lib*`` includes one
  218. or more of the values ``lib64``, ``lib32``, ``libx32`` or ``lib`` (searched in
  219. that order).
  220. * Paths with ``lib64`` are searched on 64 bit platforms if the
  221. :prop_gbl:`FIND_LIBRARY_USE_LIB64_PATHS` property is set to ``TRUE``.
  222. * Paths with ``lib32`` are searched on 32 bit platforms if the
  223. :prop_gbl:`FIND_LIBRARY_USE_LIB32_PATHS` property is set to ``TRUE``.
  224. * Paths with ``libx32`` are searched on platforms using the x32 ABI
  225. if the :prop_gbl:`FIND_LIBRARY_USE_LIBX32_PATHS` property is set to ``TRUE``.
  226. * The ``lib`` path is always searched.
  227. If ``PATH_SUFFIXES`` is specified, the suffixes are appended to each
  228. (``W``) or (``U``) directory entry one-by-one.
  229. This set of directories is intended to work in cooperation with
  230. projects that provide configuration files in their installation trees.
  231. Directories above marked with (``W``) are intended for installations on
  232. Windows where the prefix may point at the top of an application's
  233. installation directory. Those marked with (``U``) are intended for
  234. installations on UNIX platforms where the prefix is shared by multiple
  235. packages. This is merely a convention, so all (``W``) and (``U``) directories
  236. are still searched on all platforms. Directories marked with (``A``) are
  237. intended for installations on Apple platforms. The
  238. :variable:`CMAKE_FIND_FRAMEWORK` and :variable:`CMAKE_FIND_APPBUNDLE`
  239. variables determine the order of preference.
  240. The set of installation prefixes is constructed using the following
  241. steps. If ``NO_DEFAULT_PATH`` is specified all ``NO_*`` options are
  242. enabled.
  243. 1. Search paths specified in the :variable:`<PackageName>_ROOT` CMake
  244. variable and the :envvar:`<PackageName>_ROOT` environment variable,
  245. where ``<PackageName>`` is the package to be found.
  246. The package root variables are maintained as a stack so if
  247. called from within a find module, root paths from the parent's find
  248. module will also be searched after paths for the current package.
  249. This can be skipped if ``NO_PACKAGE_ROOT_PATH`` is passed or by setting
  250. the :variable:`CMAKE_FIND_USE_PACKAGE_ROOT_PATH` to ``FALSE``.
  251. See policy :policy:`CMP0074`.
  252. 2. Search paths specified in cmake-specific cache variables. These
  253. are intended to be used on the command line with a ``-DVAR=value``.
  254. The values are interpreted as :ref:`semicolon-separated lists <CMake Language Lists>`.
  255. This can be skipped if ``NO_CMAKE_PATH`` is passed or by setting the
  256. :variable:`CMAKE_FIND_USE_CMAKE_PATH` to ``FALSE``::
  257. CMAKE_PREFIX_PATH
  258. CMAKE_FRAMEWORK_PATH
  259. CMAKE_APPBUNDLE_PATH
  260. 3. Search paths specified in cmake-specific environment variables.
  261. These are intended to be set in the user's shell configuration,
  262. and therefore use the host's native path separator
  263. (``;`` on Windows and ``:`` on UNIX).
  264. This can be skipped if ``NO_CMAKE_ENVIRONMENT_PATH`` is passed or by setting
  265. the :variable:`CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH` to ``FALSE``::
  266. <PackageName>_DIR
  267. CMAKE_PREFIX_PATH
  268. CMAKE_FRAMEWORK_PATH
  269. CMAKE_APPBUNDLE_PATH
  270. 4. Search paths specified by the ``HINTS`` option. These should be paths
  271. computed by system introspection, such as a hint provided by the
  272. location of another item already found. Hard-coded guesses should
  273. be specified with the ``PATHS`` option.
  274. 5. Search the standard system environment variables. This can be
  275. skipped if ``NO_SYSTEM_ENVIRONMENT_PATH`` is passed or by setting the
  276. :variable:`CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH` to ``FALSE``. Path entries
  277. ending in ``/bin`` or ``/sbin`` are automatically converted to their
  278. parent directories::
  279. PATH
  280. 6. Search paths stored in the CMake :ref:`User Package Registry`.
  281. This can be skipped if ``NO_CMAKE_PACKAGE_REGISTRY`` is passed or by
  282. setting the variable :variable:`CMAKE_FIND_USE_PACKAGE_REGISTRY`
  283. to ``FALSE`` or the deprecated variable
  284. :variable:`CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY` to ``TRUE``.
  285. See the :manual:`cmake-packages(7)` manual for details on the user
  286. package registry.
  287. 7. Search cmake variables defined in the Platform files for the
  288. current system. This can be skipped if ``NO_CMAKE_SYSTEM_PATH`` is
  289. passed or by setting the :variable:`CMAKE_FIND_USE_CMAKE_SYSTEM_PATH`
  290. to ``FALSE``::
  291. CMAKE_SYSTEM_PREFIX_PATH
  292. CMAKE_SYSTEM_FRAMEWORK_PATH
  293. CMAKE_SYSTEM_APPBUNDLE_PATH
  294. 8. Search paths stored in the CMake :ref:`System Package Registry`.
  295. This can be skipped if ``NO_CMAKE_SYSTEM_PACKAGE_REGISTRY`` is passed
  296. or by setting the :variable:`CMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY`
  297. variable to ``FALSE`` or the deprecated variable
  298. :variable:`CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY` to ``TRUE``.
  299. See the :manual:`cmake-packages(7)` manual for details on the system
  300. package registry.
  301. 9. Search paths specified by the ``PATHS`` option. These are typically
  302. hard-coded guesses.
  303. .. |FIND_XXX| replace:: find_package
  304. .. |FIND_ARGS_XXX| replace:: <PackageName>
  305. .. |CMAKE_FIND_ROOT_PATH_MODE_XXX| replace::
  306. :variable:`CMAKE_FIND_ROOT_PATH_MODE_PACKAGE`
  307. .. include:: FIND_XXX_ROOT.txt
  308. .. include:: FIND_XXX_ORDER.txt
  309. By default the value stored in the result variable will be the path at
  310. which the file is found. The :variable:`CMAKE_FIND_PACKAGE_RESOLVE_SYMLINKS`
  311. variable may be set to ``TRUE`` before calling ``find_package`` in order
  312. to resolve symbolic links and store the real path to the file.
  313. Every non-REQUIRED ``find_package`` call can be disabled by setting the
  314. :variable:`CMAKE_DISABLE_FIND_PACKAGE_<PackageName>` variable to ``TRUE``.
  315. Package File Interface Variables
  316. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  317. When loading a find module or package configuration file ``find_package``
  318. defines variables to provide information about the call arguments (and
  319. restores their original state before returning):
  320. ``CMAKE_FIND_PACKAGE_NAME``
  321. the ``<PackageName>`` which is searched for
  322. ``<PackageName>_FIND_REQUIRED``
  323. true if ``REQUIRED`` option was given
  324. ``<PackageName>_FIND_QUIETLY``
  325. true if ``QUIET`` option was given
  326. ``<PackageName>_FIND_VERSION``
  327. full requested version string
  328. ``<PackageName>_FIND_VERSION_MAJOR``
  329. major version if requested, else 0
  330. ``<PackageName>_FIND_VERSION_MINOR``
  331. minor version if requested, else 0
  332. ``<PackageName>_FIND_VERSION_PATCH``
  333. patch version if requested, else 0
  334. ``<PackageName>_FIND_VERSION_TWEAK``
  335. tweak version if requested, else 0
  336. ``<PackageName>_FIND_VERSION_COUNT``
  337. number of version components, 0 to 4
  338. ``<PackageName>_FIND_VERSION_EXACT``
  339. true if ``EXACT`` option was given
  340. ``<PackageName>_FIND_COMPONENTS``
  341. list of requested components
  342. ``<PackageName>_FIND_REQUIRED_<c>``
  343. true if component ``<c>`` is required,
  344. false if component ``<c>`` is optional
  345. In Module mode the loaded find module is responsible to honor the
  346. request detailed by these variables; see the find module for details.
  347. In Config mode ``find_package`` handles ``REQUIRED``, ``QUIET``, and
  348. ``[version]`` options automatically but leaves it to the package
  349. configuration file to handle components in a way that makes sense
  350. for the package. The package configuration file may set
  351. ``<PackageName>_FOUND`` to false to tell ``find_package`` that component
  352. requirements are not satisfied.