documentation.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. ``generator``
  97. A CMake native build system generator.
  98. See the `cmake(1)`_ command-line tool's ``-G`` option.
  99. ``manual``
  100. A CMake manual page, like the `cmake(1)`_ manual.
  101. ``module``
  102. A CMake module.
  103. See the `cmake-modules(7)`_ manual
  104. and the `include()`_ command.
  105. ``policy``
  106. A CMake policy.
  107. See the `cmake-policies(7)`_ manual
  108. and the `cmake_policy()`_ command.
  109. ``prop_cache, prop_dir, prop_gbl, prop_sf, prop_inst, prop_test, prop_tgt``
  110. A CMake cache, directory, global, source file, installed file, test,
  111. or target property, respectively. See the `cmake-properties(7)`_
  112. manual and the `set_property()`_ command.
  113. ``variable``
  114. A CMake language variable.
  115. See the `cmake-variables(7)`_ manual
  116. and the `set()`_ command.
  117. Documentation objects in the CMake Domain come from two sources.
  118. First, the CMake extension to Sphinx transforms every document named
  119. with the form ``Help/<type>/<file-name>.rst`` to a domain object with
  120. type ``<type>``. The object name is extracted from the document title,
  121. which is expected to be of the form::
  122. <object-name>
  123. -------------
  124. and to appear at or near the top of the ``.rst`` file before any other
  125. lines starting in a letter, digit, or ``<``. If no such title appears
  126. literally in the ``.rst`` file, the object name is the ``<file-name>``.
  127. If a title does appear, it is expected that ``<file-name>`` is equal
  128. to ``<object-name>`` with any ``<`` and ``>`` characters removed.
  129. Second, the CMake Domain provides directives to define objects inside
  130. other documents:
  131. .. code-block:: rst
  132. .. command:: <command-name>
  133. This indented block documents <command-name>.
  134. .. variable:: <variable-name>
  135. This indented block documents <variable-name>.
  136. Object types for which no directive is available must be defined using
  137. the first approach above.
  138. .. _`Sphinx Domain`: http://sphinx-doc.org/domains.html
  139. .. _`cmake(1)`: https://cmake.org/cmake/help/latest/manual/cmake.1.html
  140. .. _`cmake-modules(7)`: https://cmake.org/cmake/help/latest/manual/cmake-modules.7.html
  141. .. _`cmake-policies(7)`: https://cmake.org/cmake/help/latest/manual/cmake-policies.7.html
  142. .. _`cmake-properties(7)`: https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html
  143. .. _`cmake-variables(7)`: https://cmake.org/cmake/help/latest/manual/cmake-variables.7.html
  144. .. _`cmake_policy()`: https://cmake.org/cmake/help/latest/command/cmake_policy.html
  145. .. _`include()`: https://cmake.org/cmake/help/latest/command/include.html
  146. .. _`set()`: https://cmake.org/cmake/help/latest/command/set.html
  147. .. _`set_property()`: https://cmake.org/cmake/help/latest/command/set_property.html
  148. Cross-References
  149. ----------------
  150. Sphinx uses reStructuredText interpreted text roles to provide
  151. cross-reference syntax. The `CMake Domain`_ provides for each
  152. domain object type a role of the same name to cross-reference it.
  153. CMake Domain roles are inline markup of the forms::
  154. :type:`name`
  155. :type:`text <name>`
  156. where ``type`` is the domain object type and ``name`` is the
  157. domain object name. In the first form the link text will be
  158. ``name`` (or ``name()`` if the type is ``command``) and in
  159. the second form the link text will be the explicit ``text``.
  160. For example, the code:
  161. .. code-block:: rst
  162. * The :command:`list` command.
  163. * The :command:`list(APPEND)` sub-command.
  164. * The :command:`list() command <list>`.
  165. * The :command:`list(APPEND) sub-command <list>`.
  166. * The :variable:`CMAKE_VERSION` variable.
  167. * The :prop_tgt:`OUTPUT_NAME_<CONFIG>` target property.
  168. produces:
  169. * The `list()`_ command.
  170. * The `list(APPEND)`_ sub-command.
  171. * The `list() command`_.
  172. * The `list(APPEND) sub-command`_.
  173. * The `CMAKE_VERSION`_ variable.
  174. * The `OUTPUT_NAME_<CONFIG>`_ target property.
  175. Note that CMake Domain roles differ from Sphinx and reStructuredText
  176. convention in that the form ``a<b>``, without a space preceding ``<``,
  177. is interpreted as a name instead of link text with an explicit target.
  178. This is necessary because we use ``<placeholders>`` frequently in
  179. object names like ``OUTPUT_NAME_<CONFIG>``. The form ``a <b>``,
  180. with a space preceding ``<``, is still interpreted as a link text
  181. with an explicit target.
  182. .. _`list()`: https://cmake.org/cmake/help/latest/command/list.html
  183. .. _`list(APPEND)`: https://cmake.org/cmake/help/latest/command/list.html
  184. .. _`list(APPEND) sub-command`: https://cmake.org/cmake/help/latest/command/list.html
  185. .. _`list() command`: https://cmake.org/cmake/help/latest/command/list.html
  186. .. _`CMAKE_VERSION`: https://cmake.org/cmake/help/latest/variable/CMAKE_VERSION.html
  187. .. _`OUTPUT_NAME_<CONFIG>`: https://cmake.org/cmake/help/latest/prop_tgt/OUTPUT_NAME_CONFIG.html
  188. Style
  189. -----
  190. Style: Section Headers
  191. ^^^^^^^^^^^^^^^^^^^^^^
  192. When marking section titles, make the section decoration line as long as
  193. the title text. Use only a line below the title, not above. For
  194. example:
  195. .. code-block:: rst
  196. Title Text
  197. ----------
  198. Capitalize the first letter of each non-minor word in the title.
  199. The section header underline character hierarchy is
  200. * ``#``: Manual group (part) in the master document
  201. * ``*``: Manual (chapter) title
  202. * ``=``: Section within a manual
  203. * ``-``: Subsection or `CMake Domain`_ object document title
  204. * ``^``: Subsubsection or `CMake Domain`_ object document section
  205. * ``"``: Paragraph or `CMake Domain`_ object document subsection
  206. Style: Whitespace
  207. ^^^^^^^^^^^^^^^^^
  208. Use two spaces for indentation. Use two spaces between sentences in
  209. prose.
  210. Style: Line Length
  211. ^^^^^^^^^^^^^^^^^^
  212. Prefer to restrict the width of lines to 75-80 columns. This is not a
  213. hard restriction, but writing new paragraphs wrapped at 75 columns
  214. allows space for adding minor content without significant re-wrapping of
  215. content.
  216. Style: Prose
  217. ^^^^^^^^^^^^
  218. Use American English spellings in prose.
  219. Style: Starting Literal Blocks
  220. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  221. Prefer to mark the start of literal blocks with ``::`` at the end of
  222. the preceding paragraph. In cases where the following block gets
  223. a ``code-block`` marker, put a single ``:`` at the end of the preceding
  224. paragraph.
  225. Style: CMake Command Signatures
  226. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  227. Command signatures should be marked up as plain literal blocks, not as
  228. cmake ``code-blocks``.
  229. Signatures are separated from preceding content by a section header.
  230. That is, use:
  231. .. code-block:: rst
  232. ... preceding paragraph.
  233. Normal Libraries
  234. ^^^^^^^^^^^^^^^^
  235. ::
  236. add_library(<lib> ...)
  237. This signature is used for ...
  238. Signatures of commands should wrap optional parts with square brackets,
  239. and should mark list of optional arguments with an ellipsis (``...``).
  240. Elements of the signature which are specified by the user should be
  241. specified with angle brackets, and may be referred to in prose using
  242. ``inline-literal`` syntax.
  243. Style: Boolean Constants
  244. ^^^^^^^^^^^^^^^^^^^^^^^^
  245. Use "``OFF``" and "``ON``" for boolean values which can be modified by
  246. the user, such as ``POSITION_INDEPENDENT_CODE``. Such properties
  247. may be "enabled" and "disabled". Use "``True``" and "``False``" for
  248. inherent values which can't be modified after being set, such as the
  249. ``IMPORTED`` property of a build target.
  250. Style: Inline Literals
  251. ^^^^^^^^^^^^^^^^^^^^^^
  252. Mark up references to keywords in signatures, file names, and other
  253. technical terms with ``inline-literal`` syntax, for example:
  254. .. code-block:: rst
  255. If ``WIN32`` is used with :command:`add_executable`, the
  256. :prop_tgt:`WIN32_EXECUTABLE` target property is enabled. That command
  257. creates the file ``<name>.exe`` on Windows.
  258. Style: Cross-References
  259. ^^^^^^^^^^^^^^^^^^^^^^^
  260. Mark up linkable references as links, including repeats.
  261. An alternative, which is used by wikipedia
  262. (`<http://en.wikipedia.org/wiki/WP:REPEATLINK>`_),
  263. is to link to a reference only once per article. That style is not used
  264. in CMake documentation.
  265. Style: Referencing CMake Concepts
  266. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  267. If referring to a concept which corresponds to a property, and that
  268. concept is described in a high-level manual, prefer to link to the
  269. manual section instead of the property. For example:
  270. .. code-block:: rst
  271. This command creates an :ref:`Imported Target <Imported Targets>`.
  272. instead of:
  273. .. code-block:: rst
  274. This command creates an :prop_tgt:`IMPORTED` target.
  275. The latter should be used only when referring specifically to the
  276. property.
  277. References to manual sections are not automatically created by creating
  278. a section, but code such as:
  279. .. code-block:: rst
  280. .. _`Imported Targets`:
  281. creates a suitable anchor. Use an anchor name which matches the name
  282. of the corresponding section. Refer to the anchor using a
  283. cross-reference with specified text.
  284. Imported Targets need the ``IMPORTED`` term marked up with care in
  285. particular because the term may refer to a command keyword, a target
  286. property, or a concept.
  287. Where a property, command or variable is related conceptually to others,
  288. by for example, being related to the buildsystem description, generator
  289. expressions or Qt, each relevant property, command or variable should
  290. link to the primary manual, which provides high-level information. Only
  291. particular information relating to the command should be in the
  292. documentation of the command.
  293. Style: Referencing CMake Domain Objects
  294. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  295. When referring to `CMake Domain`_ objects such as properties, variables,
  296. commands etc, prefer to link to the target object and follow that with
  297. the type of object it is. For example:
  298. .. code-block:: rst
  299. Set the :prop_tgt:`AUTOMOC` target property to ``ON``.
  300. Instead of
  301. .. code-block:: rst
  302. Set the target property :prop_tgt:`AUTOMOC` to ``ON``.
  303. The ``policy`` directive is an exception, and the type us usually
  304. referred to before the link:
  305. .. code-block:: rst
  306. If policy :policy:`CMP0022` is set to ``NEW`` the behavior is ...
  307. However, markup self-references with ``inline-literal`` syntax.
  308. For example, within the ``add_executable`` command documentation, use
  309. .. code-block:: rst
  310. ``add_executable``
  311. not
  312. .. code-block:: rst
  313. :command:`add_executable`
  314. which is used elsewhere.
  315. Modules
  316. =======
  317. The ``Modules`` directory contains CMake-language ``.cmake`` module files.
  318. Module Documentation
  319. --------------------
  320. To document CMake module ``Modules/<module-name>.cmake``, modify
  321. ``Help/manual/cmake-modules.7.rst`` to reference the module in the
  322. ``toctree`` directive, in sorted order, as::
  323. /module/<module-name>
  324. Then add the module document file ``Help/module/<module-name>.rst``
  325. containing just the line::
  326. .. cmake-module:: ../../Modules/<module-name>.cmake
  327. The ``cmake-module`` directive will scan the module file to extract
  328. reStructuredText markup from comment blocks that start in ``.rst:``.
  329. At the top of ``Modules/<module-name>.cmake``, begin with the following
  330. license notice:
  331. .. code-block:: cmake
  332. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  333. # file Copyright.txt or https://cmake.org/licensing for details.
  334. After this notice, add a *BLANK* line. Then, add documentation using
  335. a `Line Comment`_ block of the form:
  336. .. code-block:: cmake
  337. #.rst:
  338. # <module-name>
  339. # -------------
  340. #
  341. # <reStructuredText documentation of module>
  342. or a `Bracket Comment`_ of the form:
  343. ::
  344. #[[.rst:
  345. <module-name>
  346. -------------
  347. <reStructuredText documentation of module>
  348. #]]
  349. Any number of ``=`` may be used in the opening and closing brackets
  350. as long as they match. Content on the line containing the closing
  351. bracket is excluded if and only if the line starts in ``#``.
  352. Additional such ``.rst:`` comments may appear anywhere in the module file.
  353. All such comments must start with ``#`` in the first column.
  354. For example, a ``Findxxx.cmake`` module may contain:
  355. ::
  356. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  357. # file Copyright.txt or https://cmake.org/licensing for details.
  358. #.rst:
  359. # FindXxx
  360. # -------
  361. #
  362. # This is a cool module.
  363. # This module does really cool stuff.
  364. # It can do even more than you think.
  365. #
  366. # It even needs two paragraphs to tell you about it.
  367. # And it defines the following variables:
  368. #
  369. # * VAR_COOL: this is great isn't it?
  370. # * VAR_REALLY_COOL: cool right?
  371. <code>
  372. #[========================================[.rst:
  373. .. command:: xxx_do_something
  374. This command does something for Xxx::
  375. xxx_do_something(some arguments)
  376. #]========================================]
  377. macro(xxx_do_something)
  378. <code>
  379. endmacro()
  380. Test the documentation formatting by running
  381. ``cmake --help-module <module-name>``, and also by enabling the
  382. ``SPHINX_HTML`` and ``SPHINX_MAN`` options to build the documentation.
  383. Edit the comments until generated documentation looks satisfactory. To
  384. have a .cmake file in this directory NOT show up in the modules
  385. documentation, simply leave out the ``Help/module/<module-name>.rst``
  386. file and the ``Help/manual/cmake-modules.7.rst`` toctree entry.
  387. .. _`Line Comment`: https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#line-comment
  388. .. _`Bracket Comment`: https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#bracket-comment