cmake-developer.7.rst 19 KB

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