find_package.rst 15 KB

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