cmake-developer.7.rst 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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 modifying the CMake
  9. source tree itself, and by those authoring externally-maintained modules.
  10. See https://cmake.org/get-involved/ to get involved in development of
  11. CMake upstream.
  12. Help
  13. ====
  14. The ``Help`` directory contains CMake help manual source files.
  15. They are written using the `reStructuredText`_ markup syntax and
  16. processed by `Sphinx`_ to generate the CMake help manuals.
  17. .. _`reStructuredText`: http://docutils.sourceforge.net/docs/ref/rst/introduction.html
  18. .. _`Sphinx`: http://sphinx-doc.org
  19. Markup Constructs
  20. -----------------
  21. In addition to using Sphinx to generate the CMake help manuals, we
  22. also use a C++-implemented document processor to print documents for
  23. the ``--help-*`` command-line help options. It supports a subset of
  24. reStructuredText markup. When authoring or modifying documents,
  25. please verify that the command-line help looks good in addition to the
  26. Sphinx-generated html and man pages.
  27. The command-line help processor supports the following constructs
  28. defined by reStructuredText, Sphinx, and a CMake extension to Sphinx.
  29. ..
  30. Note: This list must be kept consistent with the cmRST implementation.
  31. CMake Domain directives
  32. Directives defined in the `CMake Domain`_ for defining CMake
  33. documentation objects are printed in command-line help output as
  34. if the lines were normal paragraph text with interpretation.
  35. CMake Domain interpreted text roles
  36. Interpreted text roles defined in the `CMake Domain`_ for
  37. cross-referencing CMake documentation objects are replaced by their
  38. link text in command-line help output. Other roles are printed
  39. literally and not processed.
  40. ``code-block`` directive
  41. Add a literal code block without interpretation. The command-line
  42. help processor prints the block content without the leading directive
  43. line and with common indentation replaced by one space.
  44. ``include`` directive
  45. Include another document source file. The command-line help
  46. processor prints the included document inline with the referencing
  47. document.
  48. literal block after ``::``
  49. A paragraph ending in ``::`` followed by a blank line treats
  50. the following indented block as literal text without interpretation.
  51. The command-line help processor prints the ``::`` literally and
  52. prints the block content with common indentation replaced by one
  53. space.
  54. ``note`` directive
  55. Call out a side note. The command-line help processor prints the
  56. block content as if the lines were normal paragraph text with
  57. interpretation.
  58. ``parsed-literal`` directive
  59. Add a literal block with markup interpretation. The command-line
  60. help processor prints the block content without the leading
  61. directive line and with common indentation replaced by one space.
  62. ``productionlist`` directive
  63. Render context-free grammar productions. The command-line help
  64. processor prints the block content as if the lines were normal
  65. paragraph text with interpretation.
  66. ``replace`` directive
  67. Define a ``|substitution|`` replacement.
  68. The command-line help processor requires a substitution replacement
  69. to be defined before it is referenced.
  70. ``|substitution|`` reference
  71. Reference a substitution replacement previously defined by
  72. the ``replace`` directive. The command-line help processor
  73. performs the substitution and replaces all newlines in the
  74. replacement text with spaces.
  75. ``toctree`` directive
  76. Include other document sources in the Table-of-Contents
  77. document tree. The command-line help processor prints
  78. the referenced documents inline as part of the referencing
  79. document.
  80. Inline markup constructs not listed above are printed literally in the
  81. command-line help output. We prefer to use inline markup constructs that
  82. look correct in source form, so avoid use of \\-escapes in favor of inline
  83. literals when possible.
  84. Explicit markup blocks not matching directives listed above are removed from
  85. command-line help output. Do not use them, except for plain ``..`` comments
  86. that are removed by Sphinx too.
  87. Note that nested indentation of blocks is not recognized by the
  88. command-line help processor. Therefore:
  89. * Explicit markup blocks are recognized only when not indented
  90. inside other blocks.
  91. * Literal blocks after paragraphs ending in ``::`` but not
  92. at the top indentation level may consume all indented lines
  93. following them.
  94. Try to avoid these cases in practice.
  95. CMake Domain
  96. ------------
  97. CMake adds a `Sphinx Domain`_ called ``cmake``, also called the
  98. "CMake Domain". It defines several "object" types for CMake
  99. documentation:
  100. ``command``
  101. A CMake language command.
  102. ``generator``
  103. A CMake native build system generator.
  104. See the :manual:`cmake(1)` command-line tool's ``-G`` option.
  105. ``manual``
  106. A CMake manual page, like this :manual:`cmake-developer(7)` manual.
  107. ``module``
  108. A CMake module.
  109. See the :manual:`cmake-modules(7)` manual
  110. and the :command:`include` command.
  111. ``policy``
  112. A CMake policy.
  113. See the :manual:`cmake-policies(7)` manual
  114. and the :command:`cmake_policy` command.
  115. ``prop_cache, prop_dir, prop_gbl, prop_sf, prop_inst, prop_test, prop_tgt``
  116. A CMake cache, directory, global, source file, installed file, test,
  117. or target property, respectively. See the :manual:`cmake-properties(7)`
  118. manual and the :command:`set_property` command.
  119. ``variable``
  120. A CMake language variable.
  121. See the :manual:`cmake-variables(7)` manual
  122. and the :command:`set` command.
  123. Documentation objects in the CMake Domain come from two sources.
  124. First, the CMake extension to Sphinx transforms every document named
  125. with the form ``Help/<type>/<file-name>.rst`` to a domain object with
  126. type ``<type>``. The object name is extracted from the document title,
  127. which is expected to be of the form::
  128. <object-name>
  129. -------------
  130. and to appear at or near the top of the ``.rst`` file before any other
  131. lines starting in a letter, digit, or ``<``. If no such title appears
  132. literally in the ``.rst`` file, the object name is the ``<file-name>``.
  133. If a title does appear, it is expected that ``<file-name>`` is equal
  134. to ``<object-name>`` with any ``<`` and ``>`` characters removed.
  135. Second, the CMake Domain provides directives to define objects inside
  136. other documents:
  137. .. code-block:: rst
  138. .. command:: <command-name>
  139. This indented block documents <command-name>.
  140. .. variable:: <variable-name>
  141. This indented block documents <variable-name>.
  142. Object types for which no directive is available must be defined using
  143. the first approach above.
  144. .. _`Sphinx Domain`: http://sphinx-doc.org/domains.html
  145. Cross-References
  146. ----------------
  147. Sphinx uses reStructuredText interpreted text roles to provide
  148. cross-reference syntax. The `CMake Domain`_ provides for each
  149. domain object type a role of the same name to cross-reference it.
  150. CMake Domain roles are inline markup of the forms::
  151. :type:`name`
  152. :type:`text <name>`
  153. where ``type`` is the domain object type and ``name`` is the
  154. domain object name. In the first form the link text will be
  155. ``name`` (or ``name()`` if the type is ``command``) and in
  156. the second form the link text will be the explicit ``text``.
  157. For example, the code:
  158. .. code-block:: rst
  159. * The :command:`list` command.
  160. * The :command:`list(APPEND)` sub-command.
  161. * The :command:`list() command <list>`.
  162. * The :command:`list(APPEND) sub-command <list>`.
  163. * The :variable:`CMAKE_VERSION` variable.
  164. * The :prop_tgt:`OUTPUT_NAME_<CONFIG>` target property.
  165. produces:
  166. * The :command:`list` command.
  167. * The :command:`list(APPEND)` sub-command.
  168. * The :command:`list() command <list>`.
  169. * The :command:`list(APPEND) sub-command <list>`.
  170. * The :variable:`CMAKE_VERSION` variable.
  171. * The :prop_tgt:`OUTPUT_NAME_<CONFIG>` target property.
  172. Note that CMake Domain roles differ from Sphinx and reStructuredText
  173. convention in that the form ``a<b>``, without a space preceding ``<``,
  174. is interpreted as a name instead of link text with an explicit target.
  175. This is necessary because we use ``<placeholders>`` frequently in
  176. object names like ``OUTPUT_NAME_<CONFIG>``. The form ``a <b>``,
  177. with a space preceding ``<``, is still interpreted as a link text
  178. with an explicit target.
  179. Style
  180. -----
  181. Style: Section Headers
  182. ^^^^^^^^^^^^^^^^^^^^^^
  183. When marking section titles, make the section decoration line as long as
  184. the title text. Use only a line below the title, not above. For
  185. example:
  186. .. code-block:: rst
  187. Title Text
  188. ----------
  189. Capitalize the first letter of each non-minor word in the title.
  190. The section header underline character hierarchy is
  191. * ``#``: Manual group (part) in the master document
  192. * ``*``: Manual (chapter) title
  193. * ``=``: Section within a manual
  194. * ``-``: Subsection or `CMake Domain`_ object document title
  195. * ``^``: Subsubsection or `CMake Domain`_ object document section
  196. * ``"``: Paragraph or `CMake Domain`_ object document subsection
  197. Style: Whitespace
  198. ^^^^^^^^^^^^^^^^^
  199. Use two spaces for indentation. Use two spaces between sentences in
  200. prose.
  201. Style: Line Length
  202. ^^^^^^^^^^^^^^^^^^
  203. Prefer to restrict the width of lines to 75-80 columns. This is not a
  204. hard restriction, but writing new paragraphs wrapped at 75 columns
  205. allows space for adding minor content without significant re-wrapping of
  206. content.
  207. Style: Prose
  208. ^^^^^^^^^^^^
  209. Use American English spellings in prose.
  210. Style: Starting Literal Blocks
  211. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  212. Prefer to mark the start of literal blocks with ``::`` at the end of
  213. the preceding paragraph. In cases where the following block gets
  214. a ``code-block`` marker, put a single ``:`` at the end of the preceding
  215. paragraph.
  216. Style: CMake Command Signatures
  217. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  218. Command signatures should be marked up as plain literal blocks, not as
  219. cmake ``code-blocks``.
  220. Signatures are separated from preceding content by a section header.
  221. That is, use:
  222. .. code-block:: rst
  223. ... preceding paragraph.
  224. Normal Libraries
  225. ^^^^^^^^^^^^^^^^
  226. ::
  227. add_library(<lib> ...)
  228. This signature is used for ...
  229. Signatures of commands should wrap optional parts with square brackets,
  230. and should mark list of optional arguments with an ellipsis (``...``).
  231. Elements of the signature which are specified by the user should be
  232. specified with angle brackets, and may be referred to in prose using
  233. ``inline-literal`` syntax.
  234. Style: Boolean Constants
  235. ^^^^^^^^^^^^^^^^^^^^^^^^
  236. Use "``OFF``" and "``ON``" for boolean values which can be modified by
  237. the user, such as :prop_tgt:`POSITION_INDEPENDENT_CODE`. Such properties
  238. may be "enabled" and "disabled". Use "``True``" and "``False``" for
  239. inherent values which can't be modified after being set, such as the
  240. :prop_tgt:`IMPORTED` property of a build target.
  241. Style: Inline Literals
  242. ^^^^^^^^^^^^^^^^^^^^^^
  243. Mark up references to keywords in signatures, file names, and other
  244. technical terms with ``inline-literal`` syntax, for example:
  245. .. code-block:: rst
  246. If ``WIN32`` is used with :command:`add_executable`, the
  247. :prop_tgt:`WIN32_EXECUTABLE` target property is enabled. That command
  248. creates the file ``<name>.exe`` on Windows.
  249. Style: Cross-References
  250. ^^^^^^^^^^^^^^^^^^^^^^^
  251. Mark up linkable references as links, including repeats.
  252. An alternative, which is used by wikipedia
  253. (`<http://en.wikipedia.org/wiki/WP:REPEATLINK>`_),
  254. is to link to a reference only once per article. That style is not used
  255. in CMake documentation.
  256. Style: Referencing CMake Concepts
  257. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  258. If referring to a concept which corresponds to a property, and that
  259. concept is described in a high-level manual, prefer to link to the
  260. manual section instead of the property. For example:
  261. .. code-block:: rst
  262. This command creates an :ref:`Imported Target <Imported Targets>`.
  263. instead of:
  264. .. code-block:: rst
  265. This command creates an :prop_tgt:`IMPORTED` target.
  266. The latter should be used only when referring specifically to the
  267. property.
  268. References to manual sections are not automatically created by creating
  269. a section, but code such as:
  270. .. code-block:: rst
  271. .. _`Imported Targets`:
  272. creates a suitable anchor. Use an anchor name which matches the name
  273. of the corresponding section. Refer to the anchor using a
  274. cross-reference with specified text.
  275. Imported Targets need the ``IMPORTED`` term marked up with care in
  276. particular because the term may refer to a command keyword
  277. (``IMPORTED``), a target property (:prop_tgt:`IMPORTED`), or a
  278. concept (:ref:`Imported Targets`).
  279. Where a property, command or variable is related conceptually to others,
  280. by for example, being related to the buildsystem description, generator
  281. expressions or Qt, each relevant property, command or variable should
  282. link to the primary manual, which provides high-level information. Only
  283. particular information relating to the command should be in the
  284. documentation of the command.
  285. Style: Referencing CMake Domain Objects
  286. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  287. When referring to `CMake Domain`_ objects such as properties, variables,
  288. commands etc, prefer to link to the target object and follow that with
  289. the type of object it is. For example:
  290. .. code-block:: rst
  291. Set the :prop_tgt:`AUTOMOC` target property to ``ON``.
  292. Instead of
  293. .. code-block:: rst
  294. Set the target property :prop_tgt:`AUTOMOC` to ``ON``.
  295. The ``policy`` directive is an exception, and the type us usually
  296. referred to before the link:
  297. .. code-block:: rst
  298. If policy :policy:`CMP0022` is set to ``NEW`` the behavior is ...
  299. However, markup self-references with ``inline-literal`` syntax.
  300. For example, within the :command:`add_executable` command
  301. documentation, use
  302. .. code-block:: rst
  303. ``add_executable``
  304. not
  305. .. code-block:: rst
  306. :command:`add_executable`
  307. which is used elsewhere.
  308. Modules
  309. =======
  310. The ``Modules`` directory contains CMake-language ``.cmake`` module files.
  311. Module Documentation
  312. --------------------
  313. To document CMake module ``Modules/<module-name>.cmake``, modify
  314. ``Help/manual/cmake-modules.7.rst`` to reference the module in the
  315. ``toctree`` directive, in sorted order, as::
  316. /module/<module-name>
  317. Then add the module document file ``Help/module/<module-name>.rst``
  318. containing just the line::
  319. .. cmake-module:: ../../Modules/<module-name>.cmake
  320. The ``cmake-module`` directive will scan the module file to extract
  321. reStructuredText markup from comment blocks that start in ``.rst:``.
  322. At the top of ``Modules/<module-name>.cmake``, begin with the following
  323. license notice:
  324. .. code-block:: cmake
  325. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  326. # file Copyright.txt or https://cmake.org/licensing for details.
  327. After this notice, add a *BLANK* line. Then, add documentation using
  328. a :ref:`Line Comment` block of the form:
  329. .. code-block:: cmake
  330. #.rst:
  331. # <module-name>
  332. # -------------
  333. #
  334. # <reStructuredText documentation of module>
  335. or a :ref:`Bracket Comment` of the form:
  336. ::
  337. #[[.rst:
  338. <module-name>
  339. -------------
  340. <reStructuredText documentation of module>
  341. #]]
  342. Any number of ``=`` may be used in the opening and closing brackets
  343. as long as they match. Content on the line containing the closing
  344. bracket is excluded if and only if the line starts in ``#``.
  345. Additional such ``.rst:`` comments may appear anywhere in the module file.
  346. All such comments must start with ``#`` in the first column.
  347. For example, a ``Modules/Findxxx.cmake`` module may contain:
  348. ::
  349. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  350. # file Copyright.txt or https://cmake.org/licensing for details.
  351. #.rst:
  352. # FindXxx
  353. # -------
  354. #
  355. # This is a cool module.
  356. # This module does really cool stuff.
  357. # It can do even more than you think.
  358. #
  359. # It even needs two paragraphs to tell you about it.
  360. # And it defines the following variables:
  361. #
  362. # * VAR_COOL: this is great isn't it?
  363. # * VAR_REALLY_COOL: cool right?
  364. <code>
  365. #[========================================[.rst:
  366. .. command:: xxx_do_something
  367. This command does something for Xxx::
  368. xxx_do_something(some arguments)
  369. #]========================================]
  370. macro(xxx_do_something)
  371. <code>
  372. endmacro()
  373. Test the documentation formatting by running
  374. ``cmake --help-module <module-name>``, and also by enabling the
  375. ``SPHINX_HTML`` and ``SPHINX_MAN`` options to build the documentation.
  376. Edit the comments until generated documentation looks satisfactory. To
  377. have a .cmake file in this directory NOT show up in the modules
  378. documentation, simply leave out the ``Help/module/<module-name>.rst``
  379. file and the ``Help/manual/cmake-modules.7.rst`` toctree entry.
  380. .. _`Find Modules`:
  381. Find Modules
  382. ------------
  383. A "find module" is a ``Modules/Find<PackageName>.cmake`` file to be loaded
  384. by the :command:`find_package` command when invoked for ``<PackageName>``.
  385. The primary task of a find module is to determine whether a package
  386. exists on the system, set the ``<PackageName>_FOUND`` variable to reflect
  387. this and provide any variables, macros and imported targets required to
  388. use the package. A find module is useful in cases where an upstream
  389. library does not provide a
  390. :ref:`config file package <Config File Packages>`.
  391. The traditional approach is to use variables for everything, including
  392. libraries and executables: see the `Standard Variable Names`_ section
  393. below. This is what most of the existing find modules provided by CMake
  394. do.
  395. The more modern approach is to behave as much like
  396. :ref:`config file packages <Config File Packages>` files as possible, by
  397. providing :ref:`imported target <Imported targets>`. This has the advantage
  398. of propagating :ref:`Target Usage Requirements` to consumers.
  399. In either case (or even when providing both variables and imported
  400. targets), find modules should provide backwards compatibility with old
  401. versions that had the same name.
  402. A FindFoo.cmake module will typically be loaded by the command::
  403. find_package(Foo [major[.minor[.patch[.tweak]]]]
  404. [EXACT] [QUIET] [REQUIRED]
  405. [[COMPONENTS] [components...]]
  406. [OPTIONAL_COMPONENTS components...]
  407. [NO_POLICY_SCOPE])
  408. See the :command:`find_package` documentation for details on what
  409. variables are set for the find module. Most of these are dealt with by
  410. using :module:`FindPackageHandleStandardArgs`.
  411. Briefly, the module should only locate versions of the package
  412. compatible with the requested version, as described by the
  413. ``Foo_FIND_VERSION`` family of variables. If ``Foo_FIND_QUIETLY`` is
  414. set to true, it should avoid printing messages, including anything
  415. complaining about the package not being found. If ``Foo_FIND_REQUIRED``
  416. is set to true, the module should issue a ``FATAL_ERROR`` if the package
  417. cannot be found. If neither are set to true, it should print a
  418. non-fatal message if it cannot find the package.
  419. Packages that find multiple semi-independent parts (like bundles of
  420. libraries) should search for the components listed in
  421. ``Foo_FIND_COMPONENTS`` if it is set , and only set ``Foo_FOUND`` to
  422. true if for each searched-for component ``<c>`` that was not found,
  423. ``Foo_FIND_REQUIRED_<c>`` is not set to true. The ``HANDLE_COMPONENTS``
  424. argument of ``find_package_handle_standard_args()`` can be used to
  425. implement this.
  426. If ``Foo_FIND_COMPONENTS`` is not set, which modules are searched for
  427. and required is up to the find module, but should be documented.
  428. For internal implementation, it is a generally accepted convention that
  429. variables starting with underscore are for temporary use only.
  430. Like all modules, find modules should be properly documented. To add a
  431. module to the CMake documentation, follow the steps in the `Module
  432. Documentation`_ section above.
  433. .. _`CMake Developer Standard Variable Names`:
  434. Standard Variable Names
  435. ^^^^^^^^^^^^^^^^^^^^^^^
  436. For a ``FindXxx.cmake`` module that takes the approach of setting
  437. variables (either instead of or in addition to creating imported
  438. targets), the following variable names should be used to keep things
  439. consistent between find modules. Note that all variables start with
  440. ``Xxx_`` to make sure they do not interfere with other find modules; the
  441. same consideration applies to macros, functions and imported targets.
  442. ``Xxx_INCLUDE_DIRS``
  443. The final set of include directories listed in one variable for use by
  444. client code. This should not be a cache entry.
  445. ``Xxx_LIBRARIES``
  446. The libraries to link against to use Xxx. These should include full
  447. paths. This should not be a cache entry.
  448. ``Xxx_DEFINITIONS``
  449. Definitions to use when compiling code that uses Xxx. This really
  450. shouldn't include options such as ``-DHAS_JPEG`` that a client
  451. source-code file uses to decide whether to ``#include <jpeg.h>``
  452. ``Xxx_EXECUTABLE``
  453. Where to find the Xxx tool.
  454. ``Xxx_Yyy_EXECUTABLE``
  455. Where to find the Yyy tool that comes with Xxx.
  456. ``Xxx_LIBRARY_DIRS``
  457. Optionally, the final set of library directories listed in one
  458. variable for use by client code. This should not be a cache entry.
  459. ``Xxx_ROOT_DIR``
  460. Where to find the base directory of Xxx.
  461. ``Xxx_VERSION_Yy``
  462. Expect Version Yy if true. Make sure at most one of these is ever true.
  463. ``Xxx_WRAP_Yy``
  464. If False, do not try to use the relevant CMake wrapping command.
  465. ``Xxx_Yy_FOUND``
  466. If False, optional Yy part of Xxx system is not available.
  467. ``Xxx_FOUND``
  468. Set to false, or undefined, if we haven't found, or don't want to use
  469. Xxx.
  470. ``Xxx_NOT_FOUND_MESSAGE``
  471. Should be set by config-files in the case that it has set
  472. ``Xxx_FOUND`` to FALSE. The contained message will be printed by the
  473. :command:`find_package` command and by
  474. ``find_package_handle_standard_args()`` to inform the user about the
  475. problem.
  476. ``Xxx_RUNTIME_LIBRARY_DIRS``
  477. Optionally, the runtime library search path for use when running an
  478. executable linked to shared libraries. The list should be used by
  479. user code to create the ``PATH`` on windows or ``LD_LIBRARY_PATH`` on
  480. UNIX. This should not be a cache entry.
  481. ``Xxx_VERSION``
  482. The full version string of the package found, if any. Note that many
  483. existing modules provide ``Xxx_VERSION_STRING`` instead.
  484. ``Xxx_VERSION_MAJOR``
  485. The major version of the package found, if any.
  486. ``Xxx_VERSION_MINOR``
  487. The minor version of the package found, if any.
  488. ``Xxx_VERSION_PATCH``
  489. The patch version of the package found, if any.
  490. The following names should not usually be used in CMakeLists.txt files, but
  491. are typically cache variables for users to edit and control the
  492. behaviour of find modules (like entering the path to a library manually)
  493. ``Xxx_LIBRARY``
  494. The path of the Xxx library (as used with :command:`find_library`, for
  495. example).
  496. ``Xxx_Yy_LIBRARY``
  497. The path of the Yy library that is part of the Xxx system. It may or
  498. may not be required to use Xxx.
  499. ``Xxx_INCLUDE_DIR``
  500. Where to find headers for using the Xxx library.
  501. ``Xxx_Yy_INCLUDE_DIR``
  502. Where to find headers for using the Yy library of the Xxx system.
  503. To prevent users being overwhelmed with settings to configure, try to
  504. keep as many options as possible out of the cache, leaving at least one
  505. option which can be used to disable use of the module, or locate a
  506. not-found library (e.g. ``Xxx_ROOT_DIR``). For the same reason, mark
  507. most cache options as advanced. For packages which provide both debug
  508. and release binaries, it is common to create cache variables with a
  509. ``_LIBRARY_<CONFIG>`` suffix, such as ``Foo_LIBRARY_RELEASE`` and
  510. ``Foo_LIBRARY_DEBUG``.
  511. While these are the standard variable names, you should provide
  512. backwards compatibility for any old names that were actually in use.
  513. Make sure you comment them as deprecated, so that no-one starts using
  514. them.
  515. A Sample Find Module
  516. ^^^^^^^^^^^^^^^^^^^^
  517. We will describe how to create a simple find module for a library
  518. ``Foo``.
  519. The first thing that is needed is a license notice.
  520. .. code-block:: cmake
  521. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  522. # file Copyright.txt or https://cmake.org/licensing for details.
  523. Next we need module documentation. CMake's documentation system requires you
  524. to follow the license notice with a blank line and then with a documentation
  525. marker and the name of the module. You should follow this with a simple
  526. statement of what the module does.
  527. .. code-block:: cmake
  528. #.rst:
  529. # FindFoo
  530. # -------
  531. #
  532. # Finds the Foo library
  533. #
  534. More description may be required for some packages. If there are
  535. caveats or other details users of the module should be aware of, you can
  536. add further paragraphs below this. Then you need to document what
  537. variables and imported targets are set by the module, such as
  538. .. code-block:: cmake
  539. # This will define the following variables::
  540. #
  541. # Foo_FOUND - True if the system has the Foo library
  542. # Foo_VERSION - The version of the Foo library which was found
  543. #
  544. # and the following imported targets::
  545. #
  546. # Foo::Foo - The Foo library
  547. If the package provides any macros, they should be listed here, but can
  548. be documented where they are defined. See the `Module
  549. Documentation`_ section above for more details.
  550. Now the actual libraries and so on have to be found. The code here will
  551. obviously vary from module to module (dealing with that, after all, is the
  552. point of find modules), but there tends to be a common pattern for libraries.
  553. First, we try to use ``pkg-config`` to find the library. Note that we
  554. cannot rely on this, as it may not be available, but it provides a good
  555. starting point.
  556. .. code-block:: cmake
  557. find_package(PkgConfig)
  558. pkg_check_modules(PC_Foo QUIET Foo)
  559. This should define some variables starting ``PC_Foo_`` that contain the
  560. information from the ``Foo.pc`` file.
  561. Now we need to find the libraries and include files; we use the
  562. information from ``pkg-config`` to provide hints to CMake about where to
  563. look.
  564. .. code-block:: cmake
  565. find_path(Foo_INCLUDE_DIR
  566. NAMES foo.h
  567. PATHS ${PC_Foo_INCLUDE_DIRS}
  568. PATH_SUFFIXES Foo
  569. )
  570. find_library(Foo_LIBRARY
  571. NAMES foo
  572. PATHS ${PC_Foo_LIBRARY_DIRS}
  573. )
  574. If you have a good way of getting the version (from a header file, for
  575. example), you can use that information to set ``Foo_VERSION`` (although
  576. note that find modules have traditionally used ``Foo_VERSION_STRING``,
  577. so you may want to set both). Otherwise, attempt to use the information
  578. from ``pkg-config``
  579. .. code-block:: cmake
  580. set(Foo_VERSION ${PC_Foo_VERSION})
  581. Now we can use :module:`FindPackageHandleStandardArgs` to do most of the
  582. rest of the work for us
  583. .. code-block:: cmake
  584. include(FindPackageHandleStandardArgs)
  585. find_package_handle_standard_args(Foo
  586. FOUND_VAR Foo_FOUND
  587. REQUIRED_VARS
  588. Foo_LIBRARY
  589. Foo_INCLUDE_DIR
  590. VERSION_VAR Foo_VERSION
  591. )
  592. This will check that the ``REQUIRED_VARS`` contain values (that do not
  593. end in ``-NOTFOUND``) and set ``Foo_FOUND`` appropriately. It will also
  594. cache those values. If ``Foo_VERSION`` is set, and a required version
  595. was passed to :command:`find_package`, it will check the requested version
  596. against the one in ``Foo_VERSION``. It will also print messages as
  597. appropriate; note that if the package was found, it will print the
  598. contents of the first required variable to indicate where it was found.
  599. At this point, we have to provide a way for users of the find module to
  600. link to the library or libraries that were found. There are two
  601. approaches, as discussed in the `Find Modules`_ section above. The
  602. traditional variable approach looks like
  603. .. code-block:: cmake
  604. if(Foo_FOUND)
  605. set(Foo_LIBRARIES ${Foo_LIBRARY})
  606. set(Foo_INCLUDE_DIRS ${Foo_INCLUDE_DIR})
  607. set(Foo_DEFINITIONS ${PC_Foo_CFLAGS_OTHER})
  608. endif()
  609. If more than one library was found, all of them should be included in
  610. these variables (see the `Standard Variable Names`_ section for more
  611. information).
  612. When providing imported targets, these should be namespaced (hence the
  613. ``Foo::`` prefix); CMake will recognize that values passed to
  614. :command:`target_link_libraries` that contain ``::`` in their name are
  615. supposed to be imported targets (rather than just library names), and
  616. will produce appropriate diagnostic messages if that target does not
  617. exist (see policy :policy:`CMP0028`).
  618. .. code-block:: cmake
  619. if(Foo_FOUND AND NOT TARGET Foo::Foo)
  620. add_library(Foo::Foo UNKNOWN IMPORTED)
  621. set_target_properties(Foo::Foo PROPERTIES
  622. IMPORTED_LOCATION "${Foo_LIBRARY}"
  623. INTERFACE_COMPILE_OPTIONS "${PC_Foo_CFLAGS_OTHER}"
  624. INTERFACE_INCLUDE_DIRECTORIES "${Foo_INCLUDE_DIR}"
  625. )
  626. endif()
  627. One thing to note about this is that the ``INTERFACE_INCLUDE_DIRECTORIES`` and
  628. similar properties should only contain information about the target itself, and
  629. not any of its dependencies. Instead, those dependencies should also be
  630. targets, and CMake should be told that they are dependencies of this target.
  631. CMake will then combine all the necessary information automatically.
  632. The type of the :prop_tgt:`IMPORTED` target created in the
  633. :command:`add_library` command can always be specified as ``UNKNOWN``
  634. type. This simplifies the code in cases where static or shared variants may
  635. be found, and CMake will determine the type by inspecting the files.
  636. If the library is available with multiple configurations, the
  637. :prop_tgt:`IMPORTED_CONFIGURATIONS` target property should also be
  638. populated:
  639. .. code-block:: cmake
  640. if(Foo_FOUND)
  641. if (NOT TARGET Foo::Foo)
  642. add_library(Foo::Foo UNKNOWN IMPORTED)
  643. endif()
  644. if (Foo_LIBRARY_RELEASE)
  645. set_property(TARGET Foo::Foo APPEND PROPERTY
  646. IMPORTED_CONFIGURATIONS RELEASE
  647. )
  648. set_target_properties(Foo::Foo PROPERTIES
  649. IMPORTED_LOCATION_RELEASE "${Foo_LIBRARY_RELEASE}"
  650. )
  651. endif()
  652. if (Foo_LIBRARY_DEBUG)
  653. set_property(TARGET Foo::Foo APPEND PROPERTY
  654. IMPORTED_CONFIGURATIONS DEBUG
  655. )
  656. set_target_properties(Foo::Foo PROPERTIES
  657. IMPORTED_LOCATION_DEBUG "${Foo_LIBRARY_DEBUG}"
  658. )
  659. endif()
  660. set_target_properties(Foo::Foo PROPERTIES
  661. INTERFACE_COMPILE_OPTIONS "${PC_Foo_CFLAGS_OTHER}"
  662. INTERFACE_INCLUDE_DIRECTORIES "${Foo_INCLUDE_DIR}"
  663. )
  664. endif()
  665. The ``RELEASE`` variant should be listed first in the property
  666. so that the variant is chosen if the user uses a configuration which is
  667. not an exact match for any listed ``IMPORTED_CONFIGURATIONS``.
  668. Most of the cache variables should be hidden in the ``ccmake`` interface unless
  669. the user explicitly asks to edit them.
  670. .. code-block:: cmake
  671. mark_as_advanced(
  672. Foo_INCLUDE_DIR
  673. Foo_LIBRARY
  674. )
  675. If this module replaces an older version, you should set compatibility variables
  676. to cause the least disruption possible.
  677. .. code-block:: cmake
  678. # compatibility variables
  679. set(Foo_VERSION_STRING ${Foo_VERSION})