cmake-developer.7.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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
  149. ``Foo``.
  150. The first thing that is needed is a license notice.
  151. .. code-block:: cmake
  152. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  153. # file Copyright.txt or https://cmake.org/licensing for details.
  154. Next we need module documentation. CMake's documentation system requires you
  155. to follow the license notice with a blank line and then with a documentation
  156. marker and the name of the module. You should follow this with a simple
  157. statement of what the module does.
  158. .. code-block:: cmake
  159. #.rst:
  160. # FindFoo
  161. # -------
  162. #
  163. # Finds the Foo library
  164. #
  165. More description may be required for some packages. If there are
  166. caveats or other details users of the module should be aware of, you can
  167. add further paragraphs below this. Then you need to document what
  168. variables and imported targets are set by the module, such as
  169. .. code-block:: cmake
  170. # This will define the following variables::
  171. #
  172. # Foo_FOUND - True if the system has the Foo library
  173. # Foo_VERSION - The version of the Foo library which was found
  174. #
  175. # and the following imported targets::
  176. #
  177. # Foo::Foo - The Foo library
  178. If the package provides any macros, they should be listed here, but can
  179. be documented where they are defined.
  180. Now the actual libraries and so on have to be found. The code here will
  181. obviously vary from module to module (dealing with that, after all, is the
  182. point of find modules), but there tends to be a common pattern for libraries.
  183. First, we try to use ``pkg-config`` to find the library. Note that we
  184. cannot rely on this, as it may not be available, but it provides a good
  185. starting point.
  186. .. code-block:: cmake
  187. find_package(PkgConfig)
  188. pkg_check_modules(PC_Foo QUIET Foo)
  189. This should define some variables starting ``PC_Foo_`` that contain the
  190. information from the ``Foo.pc`` file.
  191. Now we need to find the libraries and include files; we use the
  192. information from ``pkg-config`` to provide hints to CMake about where to
  193. look.
  194. .. code-block:: cmake
  195. find_path(Foo_INCLUDE_DIR
  196. NAMES foo.h
  197. PATHS ${PC_Foo_INCLUDE_DIRS}
  198. PATH_SUFFIXES Foo
  199. )
  200. find_library(Foo_LIBRARY
  201. NAMES foo
  202. PATHS ${PC_Foo_LIBRARY_DIRS}
  203. )
  204. If you have a good way of getting the version (from a header file, for
  205. example), you can use that information to set ``Foo_VERSION`` (although
  206. note that find modules have traditionally used ``Foo_VERSION_STRING``,
  207. so you may want to set both). Otherwise, attempt to use the information
  208. from ``pkg-config``
  209. .. code-block:: cmake
  210. set(Foo_VERSION ${PC_Foo_VERSION})
  211. Now we can use :module:`FindPackageHandleStandardArgs` to do most of the
  212. rest of the work for us
  213. .. code-block:: cmake
  214. include(FindPackageHandleStandardArgs)
  215. find_package_handle_standard_args(Foo
  216. FOUND_VAR Foo_FOUND
  217. REQUIRED_VARS
  218. Foo_LIBRARY
  219. Foo_INCLUDE_DIR
  220. VERSION_VAR Foo_VERSION
  221. )
  222. This will check that the ``REQUIRED_VARS`` contain values (that do not
  223. end in ``-NOTFOUND``) and set ``Foo_FOUND`` appropriately. It will also
  224. cache those values. If ``Foo_VERSION`` is set, and a required version
  225. was passed to :command:`find_package`, it will check the requested version
  226. against the one in ``Foo_VERSION``. It will also print messages as
  227. appropriate; note that if the package was found, it will print the
  228. contents of the first required variable to indicate where it was found.
  229. At this point, we have to provide a way for users of the find module to
  230. link to the library or libraries that were found. There are two
  231. approaches, as discussed in the `Find Modules`_ section above. The
  232. traditional variable approach looks like
  233. .. code-block:: cmake
  234. if(Foo_FOUND)
  235. set(Foo_LIBRARIES ${Foo_LIBRARY})
  236. set(Foo_INCLUDE_DIRS ${Foo_INCLUDE_DIR})
  237. set(Foo_DEFINITIONS ${PC_Foo_CFLAGS_OTHER})
  238. endif()
  239. If more than one library was found, all of them should be included in
  240. these variables (see the `Standard Variable Names`_ section for more
  241. information).
  242. When providing imported targets, these should be namespaced (hence the
  243. ``Foo::`` prefix); CMake will recognize that values passed to
  244. :command:`target_link_libraries` that contain ``::`` in their name are
  245. supposed to be imported targets (rather than just library names), and
  246. will produce appropriate diagnostic messages if that target does not
  247. exist (see policy :policy:`CMP0028`).
  248. .. code-block:: cmake
  249. if(Foo_FOUND AND NOT TARGET Foo::Foo)
  250. add_library(Foo::Foo UNKNOWN IMPORTED)
  251. set_target_properties(Foo::Foo PROPERTIES
  252. IMPORTED_LOCATION "${Foo_LIBRARY}"
  253. INTERFACE_COMPILE_OPTIONS "${PC_Foo_CFLAGS_OTHER}"
  254. INTERFACE_INCLUDE_DIRECTORIES "${Foo_INCLUDE_DIR}"
  255. )
  256. endif()
  257. One thing to note about this is that the ``INTERFACE_INCLUDE_DIRECTORIES`` and
  258. similar properties should only contain information about the target itself, and
  259. not any of its dependencies. Instead, those dependencies should also be
  260. targets, and CMake should be told that they are dependencies of this target.
  261. CMake will then combine all the necessary information automatically.
  262. The type of the :prop_tgt:`IMPORTED` target created in the
  263. :command:`add_library` command can always be specified as ``UNKNOWN``
  264. type. This simplifies the code in cases where static or shared variants may
  265. be found, and CMake will determine the type by inspecting the files.
  266. If the library is available with multiple configurations, the
  267. :prop_tgt:`IMPORTED_CONFIGURATIONS` target property should also be
  268. populated:
  269. .. code-block:: cmake
  270. if(Foo_FOUND)
  271. if (NOT TARGET Foo::Foo)
  272. add_library(Foo::Foo UNKNOWN IMPORTED)
  273. endif()
  274. if (Foo_LIBRARY_RELEASE)
  275. set_property(TARGET Foo::Foo APPEND PROPERTY
  276. IMPORTED_CONFIGURATIONS RELEASE
  277. )
  278. set_target_properties(Foo::Foo PROPERTIES
  279. IMPORTED_LOCATION_RELEASE "${Foo_LIBRARY_RELEASE}"
  280. )
  281. endif()
  282. if (Foo_LIBRARY_DEBUG)
  283. set_property(TARGET Foo::Foo APPEND PROPERTY
  284. IMPORTED_CONFIGURATIONS DEBUG
  285. )
  286. set_target_properties(Foo::Foo PROPERTIES
  287. IMPORTED_LOCATION_DEBUG "${Foo_LIBRARY_DEBUG}"
  288. )
  289. endif()
  290. set_target_properties(Foo::Foo PROPERTIES
  291. INTERFACE_COMPILE_OPTIONS "${PC_Foo_CFLAGS_OTHER}"
  292. INTERFACE_INCLUDE_DIRECTORIES "${Foo_INCLUDE_DIR}"
  293. )
  294. endif()
  295. The ``RELEASE`` variant should be listed first in the property
  296. so that the variant is chosen if the user uses a configuration which is
  297. not an exact match for any listed ``IMPORTED_CONFIGURATIONS``.
  298. Most of the cache variables should be hidden in the ``ccmake`` interface unless
  299. the user explicitly asks to edit them.
  300. .. code-block:: cmake
  301. mark_as_advanced(
  302. Foo_INCLUDE_DIR
  303. Foo_LIBRARY
  304. )
  305. If this module replaces an older version, you should set compatibility variables
  306. to cause the least disruption possible.
  307. .. code-block:: cmake
  308. # compatibility variables
  309. set(Foo_VERSION_STRING ${Foo_VERSION})