documentation.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. CMake Documentation Guide
  2. *************************
  3. The following is a guide to the CMake documentation source for developers.
  4. See documentation on `CMake Development`_ for more information.
  5. .. _`CMake Development`: README.rst
  6. Help
  7. ====
  8. The ``Help`` directory contains CMake help manual source files.
  9. They are written using the `reStructuredText`_ markup syntax and
  10. processed by `Sphinx`_ to generate the CMake help manuals.
  11. .. _`reStructuredText`: http://docutils.sourceforge.net/docs/ref/rst/introduction.html
  12. .. _`Sphinx`: http://sphinx-doc.org
  13. Markup Constructs
  14. -----------------
  15. In addition to using Sphinx to generate the CMake help manuals, we
  16. also use a C++-implemented document processor to print documents for
  17. the ``--help-*`` command-line help options. It supports a subset of
  18. reStructuredText markup. When authoring or modifying documents,
  19. please verify that the command-line help looks good in addition to the
  20. Sphinx-generated html and man pages.
  21. The command-line help processor supports the following constructs
  22. defined by reStructuredText, Sphinx, and a CMake extension to Sphinx.
  23. ..
  24. Note: This list must be kept consistent with the cmRST implementation.
  25. CMake Domain directives
  26. Directives defined in the `CMake Domain`_ for defining CMake
  27. documentation objects are printed in command-line help output as
  28. if the lines were normal paragraph text with interpretation.
  29. CMake Domain interpreted text roles
  30. Interpreted text roles defined in the `CMake Domain`_ for
  31. cross-referencing CMake documentation objects are replaced by their
  32. link text in command-line help output. Other roles are printed
  33. literally and not processed.
  34. ``code-block`` directive
  35. Add a literal code block without interpretation. The command-line
  36. help processor prints the block content without the leading directive
  37. line and with common indentation replaced by one space.
  38. ``include`` directive
  39. Include another document source file. The command-line help
  40. processor prints the included document inline with the referencing
  41. document.
  42. literal block after ``::``
  43. A paragraph ending in ``::`` followed by a blank line treats
  44. the following indented block as literal text without interpretation.
  45. The command-line help processor prints the ``::`` literally and
  46. prints the block content with common indentation replaced by one
  47. space.
  48. ``note`` directive
  49. Call out a side note. The command-line help processor prints the
  50. block content as if the lines were normal paragraph text with
  51. interpretation.
  52. ``parsed-literal`` directive
  53. Add a literal block with markup interpretation. The command-line
  54. help processor prints the block content without the leading
  55. directive line and with common indentation replaced by one space.
  56. ``productionlist`` directive
  57. Render context-free grammar productions. The command-line help
  58. processor prints the block content as if the lines were normal
  59. paragraph text with interpretation.
  60. ``replace`` directive
  61. Define a ``|substitution|`` replacement.
  62. The command-line help processor requires a substitution replacement
  63. to be defined before it is referenced.
  64. ``|substitution|`` reference
  65. Reference a substitution replacement previously defined by
  66. the ``replace`` directive. The command-line help processor
  67. performs the substitution and replaces all newlines in the
  68. replacement text with spaces.
  69. ``toctree`` directive
  70. Include other document sources in the Table-of-Contents
  71. document tree. The command-line help processor prints
  72. the referenced documents inline as part of the referencing
  73. document.
  74. Inline markup constructs not listed above are printed literally in the
  75. command-line help output. We prefer to use inline markup constructs that
  76. look correct in source form, so avoid use of \\-escapes in favor of inline
  77. literals when possible.
  78. Explicit markup blocks not matching directives listed above are removed from
  79. command-line help output. Do not use them, except for plain ``..`` comments
  80. that are removed by Sphinx too.
  81. Note that nested indentation of blocks is not recognized by the
  82. command-line help processor. Therefore:
  83. * Explicit markup blocks are recognized only when not indented
  84. inside other blocks.
  85. * Literal blocks after paragraphs ending in ``::`` but not
  86. at the top indentation level may consume all indented lines
  87. following them.
  88. Try to avoid these cases in practice.
  89. CMake Domain
  90. ------------
  91. CMake adds a `Sphinx Domain`_ called ``cmake``, also called the
  92. "CMake Domain". It defines several "object" types for CMake
  93. documentation:
  94. ``command``
  95. A CMake language command.
  96. ``cpack_gen``
  97. A CPack package generator.
  98. See the `cpack(1)`_ command-line tool's ``-G`` option.
  99. ``envvar``
  100. An environment variable.
  101. See the `cmake-env-variables(7)`_ manual
  102. and the `set()`_ command.
  103. ``generator``
  104. A CMake native build system generator.
  105. See the `cmake(1)`_ command-line tool's ``-G`` option.
  106. ``genex``
  107. A CMake generator expression.
  108. See the `cmake-generator-expressions(7)`_ manual.
  109. ``manual``
  110. A CMake manual page, like the `cmake(1)`_ manual.
  111. ``module``
  112. A CMake module.
  113. See the `cmake-modules(7)`_ manual
  114. and the `include()`_ command.
  115. ``policy``
  116. A CMake policy.
  117. See the `cmake-policies(7)`_ manual
  118. and the `cmake_policy()`_ command.
  119. ``prop_cache, prop_dir, prop_gbl, prop_sf, prop_inst, prop_test, prop_tgt``
  120. A CMake cache, directory, global, source file, installed file, test,
  121. or target property, respectively. See the `cmake-properties(7)`_
  122. manual and the `set_property()`_ command.
  123. ``variable``
  124. A CMake language variable.
  125. See the `cmake-variables(7)`_ manual
  126. and the `set()`_ command.
  127. Documentation objects in the CMake Domain come from two sources.
  128. First, the CMake extension to Sphinx transforms every document named
  129. with the form ``Help/<type>/<file-name>.rst`` to a domain object with
  130. type ``<type>``. The object name is extracted from the document title,
  131. which is expected to be of the form::
  132. <object-name>
  133. -------------
  134. and to appear at or near the top of the ``.rst`` file before any other
  135. lines starting in a letter, digit, ``<``, or ``$``. If no such title appears
  136. literally in the ``.rst`` file, the object name is the ``<file-name>``.
  137. If a title does appear, it is expected that ``<file-name>`` is equal
  138. to ``<object-name>`` with any ``<`` and ``>`` characters removed,
  139. or in the case of a ``$<genex-name>`` or ``$<genex-name:...>``, the
  140. ``genex-name``.
  141. Second, the CMake Domain provides directives to define objects inside
  142. other documents:
  143. .. code-block:: rst
  144. .. command:: <command-name>
  145. This indented block documents <command-name>.
  146. .. envvar:: <envvar-name>
  147. This indented block documents <envvar-name>.
  148. .. genex:: <genex-name>
  149. This indented block documents <genex-name>.
  150. .. variable:: <variable-name>
  151. This indented block documents <variable-name>.
  152. Object types for which no directive is available must be defined using
  153. the first approach above.
  154. .. _`Sphinx Domain`: http://sphinx-doc.org/domains.html
  155. .. _`cmake(1)`: https://cmake.org/cmake/help/latest/manual/cmake.1.html
  156. .. _`cmake-env-variables(7)`: https://cmake.org/cmake/help/latest/manual/cmake-env-variables.7.html
  157. .. _`cmake-generator-expressions(7)`: https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html
  158. .. _`cmake-modules(7)`: https://cmake.org/cmake/help/latest/manual/cmake-modules.7.html
  159. .. _`cmake-policies(7)`: https://cmake.org/cmake/help/latest/manual/cmake-policies.7.html
  160. .. _`cmake-properties(7)`: https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html
  161. .. _`cmake-variables(7)`: https://cmake.org/cmake/help/latest/manual/cmake-variables.7.html
  162. .. _`cmake_policy()`: https://cmake.org/cmake/help/latest/command/cmake_policy.html
  163. .. _`cpack(1)`: https://cmake.org/cmake/help/latest/manual/cpack.1.html
  164. .. _`include()`: https://cmake.org/cmake/help/latest/command/include.html
  165. .. _`set()`: https://cmake.org/cmake/help/latest/command/set.html
  166. .. _`set_property()`: https://cmake.org/cmake/help/latest/command/set_property.html
  167. Cross-References
  168. ----------------
  169. Sphinx uses reStructuredText interpreted text roles to provide
  170. cross-reference syntax. The `CMake Domain`_ provides for each
  171. domain object type a role of the same name to cross-reference it.
  172. CMake Domain roles are inline markup of the forms::
  173. :type:`name`
  174. :type:`text <name>`
  175. where ``type`` is the domain object type and ``name`` is the
  176. domain object name. In the first form the link text will be
  177. ``name`` (or ``name()`` if the type is ``command``) and in
  178. the second form the link text will be the explicit ``text``.
  179. For example, the code:
  180. .. code-block:: rst
  181. * The :command:`list` command.
  182. * The :command:`list(APPEND)` sub-command.
  183. * The :command:`list() command <list>`.
  184. * The :command:`list(APPEND) sub-command <list>`.
  185. * The :variable:`CMAKE_VERSION` variable.
  186. * The :prop_tgt:`OUTPUT_NAME_<CONFIG>` target property.
  187. produces:
  188. * The `list()`_ command.
  189. * The `list(APPEND)`_ sub-command.
  190. * The `list() command`_.
  191. * The `list(APPEND) sub-command`_.
  192. * The `CMAKE_VERSION`_ variable.
  193. * The `OUTPUT_NAME_<CONFIG>`_ target property.
  194. Note that CMake Domain roles differ from Sphinx and reStructuredText
  195. convention in that the form ``a<b>``, without a space preceding ``<``,
  196. is interpreted as a name instead of link text with an explicit target.
  197. This is necessary because we use ``<placeholders>`` frequently in
  198. object names like ``OUTPUT_NAME_<CONFIG>``. The form ``a <b>``,
  199. with a space preceding ``<``, is still interpreted as a link text
  200. with an explicit target.
  201. .. _`list()`: https://cmake.org/cmake/help/latest/command/list.html
  202. .. _`list(APPEND)`: https://cmake.org/cmake/help/latest/command/list.html
  203. .. _`list(APPEND) sub-command`: https://cmake.org/cmake/help/latest/command/list.html
  204. .. _`list() command`: https://cmake.org/cmake/help/latest/command/list.html
  205. .. _`CMAKE_VERSION`: https://cmake.org/cmake/help/latest/variable/CMAKE_VERSION.html
  206. .. _`OUTPUT_NAME_<CONFIG>`: https://cmake.org/cmake/help/latest/prop_tgt/OUTPUT_NAME_CONFIG.html
  207. Style
  208. -----
  209. Style: Section Headers
  210. ^^^^^^^^^^^^^^^^^^^^^^
  211. When marking section titles, make the section decoration line as long as
  212. the title text. Use only a line below the title, not above. For
  213. example:
  214. .. code-block:: rst
  215. Title Text
  216. ----------
  217. Capitalize the first letter of each non-minor word in the title.
  218. The section header underline character hierarchy is
  219. * ``#``: Manual group (part) in the master document
  220. * ``*``: Manual (chapter) title
  221. * ``=``: Section within a manual
  222. * ``-``: Subsection or `CMake Domain`_ object document title
  223. * ``^``: Subsubsection or `CMake Domain`_ object document section
  224. * ``"``: Paragraph or `CMake Domain`_ object document subsection
  225. Style: Whitespace
  226. ^^^^^^^^^^^^^^^^^
  227. Use two spaces for indentation. Use two spaces between sentences in
  228. prose.
  229. Style: Line Length
  230. ^^^^^^^^^^^^^^^^^^
  231. Prefer to restrict the width of lines to 75-80 columns. This is not a
  232. hard restriction, but writing new paragraphs wrapped at 75 columns
  233. allows space for adding minor content without significant re-wrapping of
  234. content.
  235. Style: Prose
  236. ^^^^^^^^^^^^
  237. Use American English spellings in prose.
  238. Style: Starting Literal Blocks
  239. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  240. Prefer to mark the start of literal blocks with ``::`` at the end of
  241. the preceding paragraph. In cases where the following block gets
  242. a ``code-block`` marker, put a single ``:`` at the end of the preceding
  243. paragraph.
  244. Style: CMake Command Signatures
  245. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  246. Command signatures should be marked up as plain literal blocks, not as
  247. cmake ``code-blocks``.
  248. Signatures are separated from preceding content by a section header.
  249. That is, use:
  250. .. code-block:: rst
  251. ... preceding paragraph.
  252. Normal Libraries
  253. ^^^^^^^^^^^^^^^^
  254. ::
  255. add_library(<lib> ...)
  256. This signature is used for ...
  257. Signatures of commands should wrap optional parts with square brackets,
  258. and should mark list of optional arguments with an ellipsis (``...``).
  259. Elements of the signature which are specified by the user should be
  260. specified with angle brackets, and may be referred to in prose using
  261. ``inline-literal`` syntax.
  262. Style: Boolean Constants
  263. ^^^^^^^^^^^^^^^^^^^^^^^^
  264. Use "``OFF``" and "``ON``" for boolean values which can be modified by
  265. the user, such as ``POSITION_INDEPENDENT_CODE``. Such properties
  266. may be "enabled" and "disabled". Use "``True``" and "``False``" for
  267. inherent values which can't be modified after being set, such as the
  268. ``IMPORTED`` property of a build target.
  269. Style: Inline Literals
  270. ^^^^^^^^^^^^^^^^^^^^^^
  271. Mark up references to keywords in signatures, file names, and other
  272. technical terms with ``inline-literal`` syntax, for example:
  273. .. code-block:: rst
  274. If ``WIN32`` is used with :command:`add_executable`, the
  275. :prop_tgt:`WIN32_EXECUTABLE` target property is enabled. That command
  276. creates the file ``<name>.exe`` on Windows.
  277. Style: Cross-References
  278. ^^^^^^^^^^^^^^^^^^^^^^^
  279. Mark up linkable references as links, including repeats.
  280. An alternative, which is used by wikipedia
  281. (`<http://en.wikipedia.org/wiki/WP:REPEATLINK>`_),
  282. is to link to a reference only once per article. That style is not used
  283. in CMake documentation.
  284. Style: Referencing CMake Concepts
  285. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  286. If referring to a concept which corresponds to a property, and that
  287. concept is described in a high-level manual, prefer to link to the
  288. manual section instead of the property. For example:
  289. .. code-block:: rst
  290. This command creates an :ref:`Imported Target <Imported Targets>`.
  291. instead of:
  292. .. code-block:: rst
  293. This command creates an :prop_tgt:`IMPORTED` target.
  294. The latter should be used only when referring specifically to the
  295. property.
  296. References to manual sections are not automatically created by creating
  297. a section, but code such as:
  298. .. code-block:: rst
  299. .. _`Imported Targets`:
  300. creates a suitable anchor. Use an anchor name which matches the name
  301. of the corresponding section. Refer to the anchor using a
  302. cross-reference with specified text.
  303. Imported Targets need the ``IMPORTED`` term marked up with care in
  304. particular because the term may refer to a command keyword, a target
  305. property, or a concept.
  306. Where a property, command or variable is related conceptually to others,
  307. by for example, being related to the buildsystem description, generator
  308. expressions or Qt, each relevant property, command or variable should
  309. link to the primary manual, which provides high-level information. Only
  310. particular information relating to the command should be in the
  311. documentation of the command.
  312. Style: Referencing CMake Domain Objects
  313. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  314. When referring to `CMake Domain`_ objects such as properties, variables,
  315. commands etc, prefer to link to the target object and follow that with
  316. the type of object it is. For example:
  317. .. code-block:: rst
  318. Set the :prop_tgt:`AUTOMOC` target property to ``ON``.
  319. Instead of
  320. .. code-block:: rst
  321. Set the target property :prop_tgt:`AUTOMOC` to ``ON``.
  322. The ``policy`` directive is an exception, and the type us usually
  323. referred to before the link:
  324. .. code-block:: rst
  325. If policy :policy:`CMP0022` is set to ``NEW`` the behavior is ...
  326. However, markup self-references with ``inline-literal`` syntax.
  327. For example, within the ``add_executable`` command documentation, use
  328. .. code-block:: rst
  329. ``add_executable``
  330. not
  331. .. code-block:: rst
  332. :command:`add_executable`
  333. which is used elsewhere.
  334. Modules
  335. =======
  336. The ``Modules`` directory contains CMake-language ``.cmake`` module files.
  337. Module Documentation
  338. --------------------
  339. To document CMake module ``Modules/<module-name>.cmake``, modify
  340. ``Help/manual/cmake-modules.7.rst`` to reference the module in the
  341. ``toctree`` directive, in sorted order, as::
  342. /module/<module-name>
  343. Then add the module document file ``Help/module/<module-name>.rst``
  344. containing just the line::
  345. .. cmake-module:: ../../Modules/<module-name>.cmake
  346. The ``cmake-module`` directive will scan the module file to extract
  347. reStructuredText markup from comment blocks that start in ``.rst:``.
  348. At the top of ``Modules/<module-name>.cmake``, begin with the following
  349. license notice:
  350. ::
  351. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  352. # file Copyright.txt or https://cmake.org/licensing for details.
  353. After this notice, add a *BLANK* line. Then, add documentation using
  354. a `Bracket Comment`_ of the form:
  355. ::
  356. #[=======================================================================[.rst:
  357. <module-name>
  358. -------------
  359. <reStructuredText documentation of module>
  360. #]=======================================================================]
  361. Any number of ``=`` may be used in the opening and closing brackets
  362. as long as they match. Content on the line containing the closing
  363. bracket is excluded if and only if the line starts in ``#``.
  364. Additional such ``.rst:`` comments may appear anywhere in the module file.
  365. All such comments must start with ``#`` in the first column.
  366. For example, a ``Findxxx.cmake`` module may contain:
  367. ::
  368. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  369. # file Copyright.txt or https://cmake.org/licensing for details.
  370. #[=======================================================================[.rst:
  371. FindXxx
  372. -------
  373. This is a cool module.
  374. This module does really cool stuff.
  375. It can do even more than you think.
  376. It even needs two paragraphs to tell you about it.
  377. And it defines the following variables:
  378. ``VAR_COOL``
  379. this is great isn't it?
  380. ``VAR_REALLY_COOL``
  381. cool right?
  382. #]=======================================================================]
  383. <code>
  384. #[=======================================================================[.rst:
  385. .. command:: xxx_do_something
  386. This command does something for Xxx::
  387. xxx_do_something(some arguments)
  388. #]=======================================================================]
  389. macro(xxx_do_something)
  390. <code>
  391. endmacro()
  392. Test the documentation formatting by running
  393. ``cmake --help-module <module-name>``, and also by enabling the
  394. ``SPHINX_HTML`` and ``SPHINX_MAN`` options to build the documentation.
  395. Edit the comments until generated documentation looks satisfactory. To
  396. have a .cmake file in this directory NOT show up in the modules
  397. documentation, simply leave out the ``Help/module/<module-name>.rst``
  398. file and the ``Help/manual/cmake-modules.7.rst`` toctree entry.
  399. .. _`Bracket Comment`: https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#bracket-comment