find_package.rst 18 KB

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