cmake-developer.7.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. .. cmake-manual-description: CMake Developer Reference
  2. cmake-developer(7)
  3. ******************
  4. .. only:: html
  5. .. contents::
  6. Introduction
  7. ============
  8. This manual is intended for reference by developers working with
  9. :manual:`cmake-language(7)` code, whether writing their own modules,
  10. authoring their own build systems, or working on CMake itself.
  11. See https://cmake.org/get-involved/ to get involved in development of
  12. CMake upstream. It includes links to contribution instructions, which
  13. in turn link to developer guides for CMake itself.
  14. .. _`Find Modules`:
  15. Find Modules
  16. ============
  17. A "find module" is a ``Find<PackageName>.cmake`` file to be loaded
  18. by the :command:`find_package` command when invoked for ``<PackageName>``.
  19. The primary task of a find module is to determine whether a package
  20. exists on the system, set the ``<PackageName>_FOUND`` variable to reflect
  21. this and provide any variables, macros and imported targets required to
  22. use the package. A find module is useful in cases where an upstream
  23. library does not provide a
  24. :ref:`config file package <Config File Packages>`.
  25. The traditional approach is to use variables for everything, including
  26. libraries and executables: see the `Standard Variable Names`_ section
  27. below. This is what most of the existing find modules provided by CMake
  28. do.
  29. The more modern approach is to behave as much like
  30. :ref:`config file packages <Config File Packages>` files as possible, by
  31. providing :ref:`imported target <Imported targets>`. This has the advantage
  32. of propagating :ref:`Target Usage Requirements` to consumers.
  33. In either case (or even when providing both variables and imported
  34. targets), find modules should provide backwards compatibility with old
  35. versions that had the same name.
  36. A FindFoo.cmake module will typically be loaded by the command::
  37. find_package(Foo [major[.minor[.patch[.tweak]]]]
  38. [EXACT] [QUIET] [REQUIRED]
  39. [[COMPONENTS] [components...]]
  40. [OPTIONAL_COMPONENTS components...]
  41. [NO_POLICY_SCOPE])
  42. See the :command:`find_package` documentation for details on what
  43. variables are set for the find module. Most of these are dealt with by
  44. using :module:`FindPackageHandleStandardArgs`.
  45. Briefly, the module should only locate versions of the package
  46. compatible with the requested version, as described by the
  47. ``Foo_FIND_VERSION`` family of variables. If ``Foo_FIND_QUIETLY`` is
  48. set to true, it should avoid printing messages, including anything
  49. complaining about the package not being found. If ``Foo_FIND_REQUIRED``
  50. is set to true, the module should issue a ``FATAL_ERROR`` if the package
  51. cannot be found. If neither are set to true, it should print a
  52. non-fatal message if it cannot find the package.
  53. Packages that find multiple semi-independent parts (like bundles of
  54. libraries) should search for the components listed in
  55. ``Foo_FIND_COMPONENTS`` if it is set , and only set ``Foo_FOUND`` to
  56. true if for each searched-for component ``<c>`` that was not found,
  57. ``Foo_FIND_REQUIRED_<c>`` is not set to true. The ``HANDLE_COMPONENTS``
  58. argument of ``find_package_handle_standard_args()`` can be used to
  59. implement this.
  60. If ``Foo_FIND_COMPONENTS`` is not set, which modules are searched for
  61. and required is up to the find module, but should be documented.
  62. For internal implementation, it is a generally accepted convention that
  63. variables starting with underscore are for temporary use only.
  64. .. _`CMake Developer Standard Variable Names`:
  65. Standard Variable Names
  66. -----------------------
  67. For a ``FindXxx.cmake`` module that takes the approach of setting
  68. variables (either instead of or in addition to creating imported
  69. targets), the following variable names should be used to keep things
  70. consistent between find modules. Note that all variables start with
  71. ``Xxx_`` to make sure they do not interfere with other find modules; the
  72. same consideration applies to macros, functions and imported targets.
  73. ``Xxx_INCLUDE_DIRS``
  74. The final set of include directories listed in one variable for use by
  75. client code. This should not be a cache entry.
  76. ``Xxx_LIBRARIES``
  77. The libraries to link against to use Xxx. These should include full
  78. paths. This should not be a cache entry.
  79. ``Xxx_DEFINITIONS``
  80. Definitions to use when compiling code that uses Xxx. This really
  81. shouldn't include options such as ``-DHAS_JPEG`` that a client
  82. source-code file uses to decide whether to ``#include <jpeg.h>``
  83. ``Xxx_EXECUTABLE``
  84. Where to find the Xxx tool.
  85. ``Xxx_Yyy_EXECUTABLE``
  86. Where to find the Yyy tool that comes with Xxx.
  87. ``Xxx_LIBRARY_DIRS``
  88. Optionally, the final set of library directories listed in one
  89. variable for use by client code. This should not be a cache entry.
  90. ``Xxx_ROOT_DIR``
  91. Where to find the base directory of Xxx.
  92. ``Xxx_VERSION_Yy``
  93. Expect Version Yy if true. Make sure at most one of these is ever true.
  94. ``Xxx_WRAP_Yy``
  95. If False, do not try to use the relevant CMake wrapping command.
  96. ``Xxx_Yy_FOUND``
  97. If False, optional Yy part of Xxx system is not available.
  98. ``Xxx_FOUND``
  99. Set to false, or undefined, if we haven't found, or don't want to use
  100. Xxx.
  101. ``Xxx_NOT_FOUND_MESSAGE``
  102. Should be set by config-files in the case that it has set
  103. ``Xxx_FOUND`` to FALSE. The contained message will be printed by the
  104. :command:`find_package` command and by
  105. ``find_package_handle_standard_args()`` to inform the user about the
  106. problem.
  107. ``Xxx_RUNTIME_LIBRARY_DIRS``
  108. Optionally, the runtime library search path for use when running an
  109. executable linked to shared libraries. The list should be used by
  110. user code to create the ``PATH`` on windows or ``LD_LIBRARY_PATH`` on
  111. UNIX. This should not be a cache entry.
  112. ``Xxx_VERSION``
  113. The full version string of the package found, if any. Note that many
  114. existing modules provide ``Xxx_VERSION_STRING`` instead.
  115. ``Xxx_VERSION_MAJOR``
  116. The major version of the package found, if any.
  117. ``Xxx_VERSION_MINOR``
  118. The minor version of the package found, if any.
  119. ``Xxx_VERSION_PATCH``
  120. The patch version of the package found, if any.
  121. The following names should not usually be used in CMakeLists.txt files, but
  122. are typically cache variables for users to edit and control the
  123. behaviour of find modules (like entering the path to a library manually)
  124. ``Xxx_LIBRARY``
  125. The path of the Xxx library (as used with :command:`find_library`, for
  126. example).
  127. ``Xxx_Yy_LIBRARY``
  128. The path of the Yy library that is part of the Xxx system. It may or
  129. may not be required to use Xxx.
  130. ``Xxx_INCLUDE_DIR``
  131. Where to find headers for using the Xxx library.
  132. ``Xxx_Yy_INCLUDE_DIR``
  133. Where to find headers for using the Yy library of the Xxx system.
  134. To prevent users being overwhelmed with settings to configure, try to
  135. keep as many options as possible out of the cache, leaving at least one
  136. option which can be used to disable use of the module, or locate a
  137. not-found library (e.g. ``Xxx_ROOT_DIR``). For the same reason, mark
  138. most cache options as advanced. For packages which provide both debug
  139. and release binaries, it is common to create cache variables with a
  140. ``_LIBRARY_<CONFIG>`` suffix, such as ``Foo_LIBRARY_RELEASE`` and
  141. ``Foo_LIBRARY_DEBUG``.
  142. While these are the standard variable names, you should provide
  143. backwards compatibility for any old names that were actually in use.
  144. Make sure you comment them as deprecated, so that no-one starts using
  145. them.
  146. A Sample Find Module
  147. --------------------
  148. We will describe how to create a simple find module for a library ``Foo``.
  149. The top of the module should begin with a license notice, followed by
  150. a blank line, and then followed by a :ref:`Bracket Comment`. The comment
  151. should begin with ``.rst:`` to indicate that the rest of its content is
  152. reStructuredText-format documentation. For example:
  153. ::
  154. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  155. # file Copyright.txt or https://cmake.org/licensing for details.
  156. #[=======================================================================[.rst:
  157. FindFoo
  158. -------
  159. Finds the Foo library.
  160. Imported Targets
  161. ^^^^^^^^^^^^^^^^
  162. This module provides the following imported targets, if found:
  163. ``Foo::Foo``
  164. The Foo library
  165. Result Variables
  166. ^^^^^^^^^^^^^^^^
  167. This will define the following variables:
  168. ``Foo_FOUND``
  169. True if the system has the Foo library.
  170. ``Foo_VERSION``
  171. The version of the Foo library which was found.
  172. ``Foo_INCLUDE_DIRS``
  173. Include directories needed to use Foo.
  174. ``Foo_LIBRARIES``
  175. Libraries needed to link to Foo.
  176. Cache Variables
  177. ^^^^^^^^^^^^^^^
  178. The following cache variables may also be set:
  179. ``Foo_INCLUDE_DIR``
  180. The directory containing ``foo.h``.
  181. ``Foo_LIBRARY``
  182. The path to the Foo library.
  183. #]=======================================================================]
  184. The module documentation consists of:
  185. * An underlined heading specifying the module name.
  186. * A simple description of what the module finds.
  187. More description may be required for some packages. If there are
  188. caveats or other details users of the module should be aware of,
  189. specify them here.
  190. * A section listing imported targets provided by the module, if any.
  191. * A section listing result variables provided by the module.
  192. * Optionally a section listing cache variables used by the module, if any.
  193. If the package provides any macros or functions, they should be listed in
  194. an additional section, but can be documented by additional ``.rst:``
  195. comment blocks immediately above where those macros or functions are defined.
  196. The find module implementation may begin below the documentation block.
  197. Now the actual libraries and so on have to be found. The code here will
  198. obviously vary from module to module (dealing with that, after all, is the
  199. point of find modules), but there tends to be a common pattern for libraries.
  200. First, we try to use ``pkg-config`` to find the library. Note that we
  201. cannot rely on this, as it may not be available, but it provides a good
  202. starting point.
  203. .. code-block:: cmake
  204. find_package(PkgConfig)
  205. pkg_check_modules(PC_Foo QUIET Foo)
  206. This should define some variables starting ``PC_Foo_`` that contain the
  207. information from the ``Foo.pc`` file.
  208. Now we need to find the libraries and include files; we use the
  209. information from ``pkg-config`` to provide hints to CMake about where to
  210. look.
  211. .. code-block:: cmake
  212. find_path(Foo_INCLUDE_DIR
  213. NAMES foo.h
  214. PATHS ${PC_Foo_INCLUDE_DIRS}
  215. PATH_SUFFIXES Foo
  216. )
  217. find_library(Foo_LIBRARY
  218. NAMES foo
  219. PATHS ${PC_Foo_LIBRARY_DIRS}
  220. )
  221. If you have a good way of getting the version (from a header file, for
  222. example), you can use that information to set ``Foo_VERSION`` (although
  223. note that find modules have traditionally used ``Foo_VERSION_STRING``,
  224. so you may want to set both). Otherwise, attempt to use the information
  225. from ``pkg-config``
  226. .. code-block:: cmake
  227. set(Foo_VERSION ${PC_Foo_VERSION})
  228. Now we can use :module:`FindPackageHandleStandardArgs` to do most of the
  229. rest of the work for us
  230. .. code-block:: cmake
  231. include(FindPackageHandleStandardArgs)
  232. find_package_handle_standard_args(Foo
  233. FOUND_VAR Foo_FOUND
  234. REQUIRED_VARS
  235. Foo_LIBRARY
  236. Foo_INCLUDE_DIR
  237. VERSION_VAR Foo_VERSION
  238. )
  239. This will check that the ``REQUIRED_VARS`` contain values (that do not
  240. end in ``-NOTFOUND``) and set ``Foo_FOUND`` appropriately. It will also
  241. cache those values. If ``Foo_VERSION`` is set, and a required version
  242. was passed to :command:`find_package`, it will check the requested version
  243. against the one in ``Foo_VERSION``. It will also print messages as
  244. appropriate; note that if the package was found, it will print the
  245. contents of the first required variable to indicate where it was found.
  246. At this point, we have to provide a way for users of the find module to
  247. link to the library or libraries that were found. There are two
  248. approaches, as discussed in the `Find Modules`_ section above. The
  249. traditional variable approach looks like
  250. .. code-block:: cmake
  251. if(Foo_FOUND)
  252. set(Foo_LIBRARIES ${Foo_LIBRARY})
  253. set(Foo_INCLUDE_DIRS ${Foo_INCLUDE_DIR})
  254. set(Foo_DEFINITIONS ${PC_Foo_CFLAGS_OTHER})
  255. endif()
  256. If more than one library was found, all of them should be included in
  257. these variables (see the `Standard Variable Names`_ section for more
  258. information).
  259. When providing imported targets, these should be namespaced (hence the
  260. ``Foo::`` prefix); CMake will recognize that values passed to
  261. :command:`target_link_libraries` that contain ``::`` in their name are
  262. supposed to be imported targets (rather than just library names), and
  263. will produce appropriate diagnostic messages if that target does not
  264. exist (see policy :policy:`CMP0028`).
  265. .. code-block:: cmake
  266. if(Foo_FOUND AND NOT TARGET Foo::Foo)
  267. add_library(Foo::Foo UNKNOWN IMPORTED)
  268. set_target_properties(Foo::Foo PROPERTIES
  269. IMPORTED_LOCATION "${Foo_LIBRARY}"
  270. INTERFACE_COMPILE_OPTIONS "${PC_Foo_CFLAGS_OTHER}"
  271. INTERFACE_INCLUDE_DIRECTORIES "${Foo_INCLUDE_DIR}"
  272. )
  273. endif()
  274. One thing to note about this is that the ``INTERFACE_INCLUDE_DIRECTORIES`` and
  275. similar properties should only contain information about the target itself, and
  276. not any of its dependencies. Instead, those dependencies should also be
  277. targets, and CMake should be told that they are dependencies of this target.
  278. CMake will then combine all the necessary information automatically.
  279. The type of the :prop_tgt:`IMPORTED` target created in the
  280. :command:`add_library` command can always be specified as ``UNKNOWN``
  281. type. This simplifies the code in cases where static or shared variants may
  282. be found, and CMake will determine the type by inspecting the files.
  283. If the library is available with multiple configurations, the
  284. :prop_tgt:`IMPORTED_CONFIGURATIONS` target property should also be
  285. populated:
  286. .. code-block:: cmake
  287. if(Foo_FOUND)
  288. if (NOT TARGET Foo::Foo)
  289. add_library(Foo::Foo UNKNOWN IMPORTED)
  290. endif()
  291. if (Foo_LIBRARY_RELEASE)
  292. set_property(TARGET Foo::Foo APPEND PROPERTY
  293. IMPORTED_CONFIGURATIONS RELEASE
  294. )
  295. set_target_properties(Foo::Foo PROPERTIES
  296. IMPORTED_LOCATION_RELEASE "${Foo_LIBRARY_RELEASE}"
  297. )
  298. endif()
  299. if (Foo_LIBRARY_DEBUG)
  300. set_property(TARGET Foo::Foo APPEND PROPERTY
  301. IMPORTED_CONFIGURATIONS DEBUG
  302. )
  303. set_target_properties(Foo::Foo PROPERTIES
  304. IMPORTED_LOCATION_DEBUG "${Foo_LIBRARY_DEBUG}"
  305. )
  306. endif()
  307. set_target_properties(Foo::Foo PROPERTIES
  308. INTERFACE_COMPILE_OPTIONS "${PC_Foo_CFLAGS_OTHER}"
  309. INTERFACE_INCLUDE_DIRECTORIES "${Foo_INCLUDE_DIR}"
  310. )
  311. endif()
  312. The ``RELEASE`` variant should be listed first in the property
  313. so that the variant is chosen if the user uses a configuration which is
  314. not an exact match for any listed ``IMPORTED_CONFIGURATIONS``.
  315. Most of the cache variables should be hidden in the ``ccmake`` interface unless
  316. the user explicitly asks to edit them.
  317. .. code-block:: cmake
  318. mark_as_advanced(
  319. Foo_INCLUDE_DIR
  320. Foo_LIBRARY
  321. )
  322. If this module replaces an older version, you should set compatibility variables
  323. to cause the least disruption possible.
  324. .. code-block:: cmake
  325. # compatibility variables
  326. set(Foo_VERSION_STRING ${Foo_VERSION})