cmake-packages.7.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. .. cmake-manual-description: CMake Packages Reference
  2. cmake-packages(7)
  3. *****************
  4. .. only:: html or latex
  5. .. contents::
  6. Introduction
  7. ============
  8. Packages provide dependency information to CMake based buildsystems. Packages
  9. are found with the :command:`find_package` command. The result of
  10. using ``find_package`` is either a set of :prop_tgt:`IMPORTED` targets, or
  11. a set of variables corresponding to build-relevant information.
  12. Using Packages
  13. ==============
  14. CMake provides direct support for two forms of packages,
  15. `Config-file Packages`_ and `Find-module Packages`_.
  16. Indirect support for ``pkg-config`` packages is also provided via
  17. the :module:`FindPkgConfig` module. In all cases, the basic form
  18. of :command:`find_package` calls is the same:
  19. .. code-block:: cmake
  20. find_package(Qt4 4.7.0 REQUIRED) # CMake provides a Qt4 find-module
  21. find_package(Qt5Core 5.1.0 REQUIRED) # Qt provides a Qt5 package config file.
  22. find_package(LibXml2 REQUIRED) # Use pkg-config via the LibXml2 find-module
  23. In cases where it is known that a package configuration file is provided by
  24. upstream, and only that should be used, the ``CONFIG`` keyword may be passed
  25. to :command:`find_package`:
  26. .. code-block:: cmake
  27. find_package(Qt5Core 5.1.0 CONFIG REQUIRED)
  28. find_package(Qt5Gui 5.1.0 CONFIG)
  29. Similarly, the ``MODULE`` keyword says to use only a find-module:
  30. .. code-block:: cmake
  31. find_package(Qt4 4.7.0 MODULE REQUIRED)
  32. Specifying the type of package explicitly improves the error message shown to
  33. the user if it is not found.
  34. Both types of packages also support specifying components of a package,
  35. either after the ``REQUIRED`` keyword:
  36. .. code-block:: cmake
  37. find_package(Qt5 5.1.0 CONFIG REQUIRED Widgets Xml Sql)
  38. or as a separate ``COMPONENTS`` list:
  39. .. code-block:: cmake
  40. find_package(Qt5 5.1.0 COMPONENTS Widgets Xml Sql)
  41. or as a separate ``OPTIONAL_COMPONENTS`` list:
  42. .. code-block:: cmake
  43. find_package(Qt5 5.1.0 COMPONENTS Widgets
  44. OPTIONAL_COMPONENTS Xml Sql
  45. )
  46. Handling of ``COMPONENTS`` and ``OPTIONAL_COMPONENTS`` is defined by the
  47. package.
  48. Config-file Packages
  49. --------------------
  50. A config-file package is a set of files provided by upstreams for downstreams
  51. to use. CMake searches in a number of locations for package configuration files, as
  52. described in the :command:`find_package` documentation. The most simple way for
  53. a CMake user to tell :manual:`cmake(1)` to search in a non-standard prefix for
  54. a package is to set the ``CMAKE_PREFIX_PATH`` cache variable.
  55. Config-file packages are provided by upstream vendors as part of development
  56. packages, that is, they belong with the header files and any other files
  57. provided to assist downsteams in using the package.
  58. A set of variables which provide package status information are also set
  59. automatically when using a config-file package. The ``<Package>_FOUND``
  60. variable is set to true or false, depending on whether the package was
  61. found. The ``<Package>_DIR`` cache variable is set to the location of the
  62. package configuration file.
  63. Find-module Packages
  64. --------------------
  65. A find module is a file with a set of rules for finding the required pieces of
  66. a dependency, primarily header files and libraries. Typically, a find module
  67. is needed when the upstream is not built with CMake, or is not CMake-aware
  68. enough to otherwise provide a package configuration file. Unlike a package configuration
  69. file, it is not shipped with upstream, but is used by downstream to find the
  70. files by guessing locations of files with platform-specific hints.
  71. Unlike the case of an upstream-provided package configuration file, no single point
  72. of reference identifies the package as being found, so the ``<Package>_FOUND``
  73. variable is not automatically set by the :command:`find_package` command. It
  74. can still be expected to be set by convention however and should be set by
  75. the author of the Find-module. Similarly there is no ``<Package>_DIR`` variable,
  76. but each of the artifacts such as library locations and header file locations
  77. provide a separate cache variable.
  78. See the :manual:`cmake-developer(7)` manual for more information about creating
  79. Find-module files.
  80. Package Layout
  81. ==============
  82. A config-file package consists of a `Package Configuration File`_ and
  83. optionally a `Package Version File`_ provided with the project distribution.
  84. Package Configuration File
  85. --------------------------
  86. Consider a project ``Foo`` that installs the following files::
  87. <prefix>/include/foo-1.2/foo.h
  88. <prefix>/lib/foo-1.2/libfoo.a
  89. It may also provide a CMake package configuration file::
  90. <prefix>/lib/cmake/foo-1.2/FooConfig.cmake
  91. with content defining :prop_tgt:`IMPORTED` targets, or defining variables, such
  92. as:
  93. .. code-block:: cmake
  94. # ...
  95. # (compute PREFIX relative to file location)
  96. # ...
  97. set(Foo_INCLUDE_DIRS ${PREFIX}/include/foo-1.2)
  98. set(Foo_LIBRARIES ${PREFIX}/lib/foo-1.2/libfoo.a)
  99. If another project wishes to use ``Foo`` it need only to locate the ``FooConfig.cmake``
  100. file and load it to get all the information it needs about package content
  101. locations. Since the package configuration file is provided by the package
  102. installation it already knows all the file locations.
  103. The :command:`find_package` command may be used to search for the package
  104. configuration file. This command constructs a set of installation prefixes
  105. and searches under each prefix in several locations. Given the name ``Foo``,
  106. it looks for a file called ``FooConfig.cmake`` or ``foo-config.cmake``.
  107. The full set of locations is specified in the :command:`find_package` command
  108. documentation. One place it looks is::
  109. <prefix>/lib/cmake/Foo*/
  110. where ``Foo*`` is a case-insensitive globbing expression. In our example the
  111. globbing expression will match ``<prefix>/lib/cmake/foo-1.2`` and the package
  112. configuration file will be found.
  113. Once found, a package configuration file is immediately loaded. It, together
  114. with a package version file, contains all the information the project needs to
  115. use the package.
  116. Package Version File
  117. --------------------
  118. When the :command:`find_package` command finds a candidate package configuration
  119. file it looks next to it for a version file. The version file is loaded to test
  120. whether the package version is an acceptable match for the version requested.
  121. If the version file claims compatibility the configuration file is accepted.
  122. Otherwise it is ignored.
  123. The name of the package version file must match that of the package configuration
  124. file but has either ``-version`` or ``Version`` appended to the name before
  125. the ``.cmake`` extension. For example, the files::
  126. <prefix>/lib/cmake/foo-1.3/foo-config.cmake
  127. <prefix>/lib/cmake/foo-1.3/foo-config-version.cmake
  128. and::
  129. <prefix>/lib/cmake/bar-4.2/BarConfig.cmake
  130. <prefix>/lib/cmake/bar-4.2/BarConfigVersion.cmake
  131. are each pairs of package configuration files and corresponding package version
  132. files.
  133. When the :command:`find_package` command loads a version file it first sets the
  134. following variables:
  135. ``PACKAGE_FIND_NAME``
  136. The <package> name
  137. ``PACKAGE_FIND_VERSION``
  138. Full requested version string
  139. ``PACKAGE_FIND_VERSION_MAJOR``
  140. Major version if requested, else 0
  141. ``PACKAGE_FIND_VERSION_MINOR``
  142. Minor version if requested, else 0
  143. ``PACKAGE_FIND_VERSION_PATCH``
  144. Patch version if requested, else 0
  145. ``PACKAGE_FIND_VERSION_TWEAK``
  146. Tweak version if requested, else 0
  147. ``PACKAGE_FIND_VERSION_COUNT``
  148. Number of version components, 0 to 4
  149. The version file must use these variables to check whether it is compatible or
  150. an exact match for the requested version and set the following variables with
  151. results:
  152. ``PACKAGE_VERSION``
  153. Full provided version string
  154. ``PACKAGE_VERSION_EXACT``
  155. True if version is exact match
  156. ``PACKAGE_VERSION_COMPATIBLE``
  157. True if version is compatible
  158. ``PACKAGE_VERSION_UNSUITABLE``
  159. True if unsuitable as any version
  160. Version files are loaded in a nested scope so they are free to set any variables
  161. they wish as part of their computation. The find_package command wipes out the
  162. scope when the version file has completed and it has checked the output
  163. variables. When the version file claims to be an acceptable match for the
  164. requested version the find_package command sets the following variables for
  165. use by the project:
  166. ``<package>_VERSION``
  167. Full provided version string
  168. ``<package>_VERSION_MAJOR``
  169. Major version if provided, else 0
  170. ``<package>_VERSION_MINOR``
  171. Minor version if provided, else 0
  172. ``<package>_VERSION_PATCH``
  173. Patch version if provided, else 0
  174. ``<package>_VERSION_TWEAK``
  175. Tweak version if provided, else 0
  176. ``<package>_VERSION_COUNT``
  177. Number of version components, 0 to 4
  178. The variables report the version of the package that was actually found.
  179. The ``<package>`` part of their name matches the argument given to the
  180. :command:`find_package` command.
  181. Creating Packages
  182. =================
  183. Usually, the upstream depends on CMake itself and can use some CMake facilities
  184. for creating the package files. Consider an upstream which provides a single
  185. shared library:
  186. .. code-block:: cmake
  187. project(UpstreamLib)
  188. set(CMAKE_INCLUDE_CURRENT_DIR ON)
  189. set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON)
  190. set(Upstream_VERSION 3.4.1)
  191. include(GenerateExportHeader)
  192. add_library(ClimbingStats SHARED climbingstats.cpp)
  193. generate_export_header(ClimbingStats)
  194. install(TARGETS ClimbingStats EXPORT ClimbingStatsTargets
  195. LIBRARY DESTINATION lib
  196. ARCHIVE DESTINATION lib
  197. RUNTIME DESTINATION bin
  198. INCLUDES DESTINATION include
  199. )
  200. install(
  201. FILES
  202. climbingstats.h
  203. "${CMAKE_CURRENT_BINARY_DIR}/climbingstats_export.h"
  204. DESTINATION
  205. include
  206. COMPONENT
  207. Devel
  208. )
  209. include(CMakePackageConfigHelpers)
  210. write_basic_package_version_file(
  211. "${CMAKE_CURRENT_BINARY_DIR}/ClimbingStats/ClimbingStatsConfigVersion.cmake"
  212. VERSION ${Upstream_VERSION}
  213. COMPATIBILITY AnyNewerVersion
  214. )
  215. export(EXPORT ClimbingStatsTargets
  216. FILE "${CMAKE_CURRENT_BINARY_DIR}/ClimbingStats/ClimbingStatsTargets.cmake"
  217. NAMESPACE Upstream::
  218. )
  219. configure_file(cmake/ClimbingStatsConfig.cmake
  220. "${CMAKE_CURRENT_BINARY_DIR}/ClimbingStats/ClimbingStatsConfig.cmake"
  221. COPY_ONLY
  222. )
  223. set(ConfigPackageLocation lib/cmake/ClimbingStats)
  224. install(EXPORT ClimbingStatsTargets
  225. FILE
  226. ClimbingStatsTargets.cmake
  227. NAMESPACE
  228. Upstream::
  229. DESTINATION
  230. ${ConfigPackageLocation}
  231. )
  232. install(
  233. FILES
  234. cmake/ClimbingStatsConfig.cmake
  235. "${CMAKE_CURRENT_BINARY_DIR}/ClimbingStats/ClimbingStatsConfigVersion.cmake"
  236. DESTINATION
  237. ${ConfigPackageLocation}
  238. COMPONENT
  239. Devel
  240. )
  241. The :module:`CMakePackageConfigHelpers` module provides a macro for creating
  242. a simple ``ConfigVersion.cmake`` file. This file sets the version of the
  243. package. It is read by CMake when :command:`find_package` is called to
  244. determine the compatibility with the requested version, and to set some
  245. version-specific variables ``<Package>_VERSION``, ``<Package>_VERSION_MAJOR``,
  246. ``<Package>_VERSION_MINOR`` etc. The :command:`install(EXPORT)` command is
  247. used to export the targets in the ``ClimbingStatsTargets`` export-set, defined
  248. previously by the :command:`install(TARGETS)` command. This command generates
  249. the ``ClimbingStatsTargets.cmake`` file to contain :prop_tgt:`IMPORTED`
  250. targets, suitable for use by downsteams and arranges to install it to
  251. ``lib/cmake/ClimbingStats``. The generated ``ClimbingStatsConfigVersion.cmake``
  252. and a ``cmake/ClimbingStatsConfig.cmake`` are installed to the same location,
  253. completing the package.
  254. A ``NAMESPACE`` with double-colons is specified when exporting the targets
  255. for installation. This convention of double-colons gives CMake a hint that
  256. the name is an :prop_tgt:`IMPORTED` target when it is used by downstreams
  257. with the :command:`target_link_libraries` command. This way, CMake can
  258. issue a diagnostic if the package providing it has not yet been found.
  259. In this case, when using :command:`install(TARGETS)` the ``INCLUDES DESTINATION``
  260. was specified. This causes the ``IMPORTED`` targets to have their
  261. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` populated with the ``include``
  262. directory in the :variable:`CMAKE_INSTALL_PREFIX`. When the ``IMPORTED``
  263. target is used by downsteam, it automatically consumes the entries from
  264. that property.
  265. In this case, the ``ClimbingStatsConfig.cmake`` file could be as simple as:
  266. .. code-block:: cmake
  267. include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStatsTargets.cmake")
  268. As this allows downstreams to use the ``IMPORTED`` targets. If any macros
  269. should be provided by the ``ClimbingStats`` package, they should
  270. be in a separate file which is installed to the same location as the
  271. ``ClimbingStatsConfig.cmake`` file, and included from there.
  272. The :command:`export(EXPORT)` command creates an :prop_tgt:`IMPORTED` targets
  273. definition file which is specific to the build-tree. This can similiarly be
  274. used with a suitable package configuration file and package version file to
  275. define a package for the build tree which may be used without installation.
  276. Consumers of the build tree can simply ensure that the
  277. :variable:`CMAKE_PREFIX_PATH` contains the build directory, or set the
  278. ``ClimbingStats_DIR`` to ``<build_dir>/ClimbingStats`` in the cache.
  279. This can also be extended to cover dependencies:
  280. .. code-block:: cmake
  281. # ...
  282. add_library(ClimbingStats SHARED climbingstats.cpp)
  283. generate_export_header(ClimbingStats)
  284. find_package(Stats 2.6.4 REQUIRED)
  285. target_link_libraries(ClimbingStats PUBLIC Stats::Types)
  286. As the ``Stats::Types`` target is a ``PUBLIC`` dependency of ``ClimbingStats``,
  287. downsteams must also find the ``Stats`` package and link to the ``Stats::Types``
  288. library. The ``Stats`` package should be found in the ``ClimbingStatsConfig.cmake``
  289. file to ensure this. The ``find_dependency`` macro from the
  290. :module:`CMakeFindDependencyMacro` helps with this by propagating
  291. whether the package is ``REQUIRED``, or ``QUIET`` etc. All ``REQUIRED``
  292. dependencies of a package should be found in the ``Config.cmake`` file:
  293. .. code-block:: cmake
  294. include(CMakeFindDependencyMacro)
  295. find_dependency(Stats 2.6.4)
  296. include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStatsTargets.cmake")
  297. include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStatsMacros.cmake")
  298. The ``find_dependency`` macro also sets ``ClimbingStats_FOUND`` to ``False`` if
  299. the dependency is not found, along with a diagnostic that the ``ClimbingStats``
  300. package can not be used without the ``Stats`` package.
  301. If ``COMPONENTS`` are specified when the downstream uses :command:`find_package`,
  302. they are listed in the ``<Package>_FIND_COMPONENTS`` variable. If a particular
  303. component is non-optional, then the ``<Package>_FIND_REQUIRED_<comp>`` will
  304. be true. This can be tested with logic in the package configuration file:
  305. .. code-block:: cmake
  306. include(CMakeFindDependencyMacro)
  307. find_dependency(Stats 2.6.4)
  308. include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStatsTargets.cmake")
  309. include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStatsMacros.cmake")
  310. set(_supported_components Plot Table)
  311. foreach(_comp ${ClimbingStats_FIND_COMPONENTS})
  312. if (NOT ";${_supported_components};" MATCHES _comp)
  313. set(ClimbingStats_FOUND False)
  314. set(ClimbingStats_NOTFOUND_MESSAGE "Specified unsupported component: ${_comp}")
  315. endif()
  316. include("${CMAKE_CURRENT_LIST_DIR}/ClimbingStats${_comp}Targets.cmake")
  317. endforeach()
  318. Here, the ``ClimbingStats_NOTFOUND_MESSAGE`` is set to a diagnosis that the package
  319. could not be found because an invalid component was specified. This message
  320. variable can be set for any case where the ``_FOUND`` variable is set to ``False``,
  321. and will be displayed to the user.