find_package.rst 17 KB

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