find_package.rst 17 KB

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