cmake-developer.7.rst 22 KB

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