find_package.rst 17 KB

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