find_package.rst 17 KB

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