sphinx.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. .. _sphinxdoc:
  2. =====================================
  3. Using Sphinx for kernel documentation
  4. =====================================
  5. The Linux kernel uses `Sphinx`_ to generate pretty documentation from
  6. `reStructuredText`_ files under ``Documentation``. To build the documentation in
  7. HTML or PDF formats, use ``make htmldocs`` or ``make pdfdocs``. The generated
  8. documentation is placed in ``Documentation/output``.
  9. .. _Sphinx: http://www.sphinx-doc.org/
  10. .. _reStructuredText: http://docutils.sourceforge.net/rst.html
  11. The reStructuredText files may contain directives to include structured
  12. documentation comments, or kernel-doc comments, from source files. Usually these
  13. are used to describe the functions and types and design of the code. The
  14. kernel-doc comments have some special structure and formatting, but beyond that
  15. they are also treated as reStructuredText.
  16. Finally, there are thousands of plain text documentation files scattered around
  17. ``Documentation``. Some of these will likely be converted to reStructuredText
  18. over time, but the bulk of them will remain in plain text.
  19. .. _sphinx_install:
  20. Sphinx Install
  21. ==============
  22. The ReST markups currently used by the Documentation/ files are meant to be
  23. built with ``Sphinx`` version 2.4.4 or higher.
  24. There's a script that checks for the Sphinx requirements. Please see
  25. :ref:`sphinx-pre-install` for further details.
  26. Most distributions are shipped with Sphinx, but its toolchain is fragile,
  27. and it is not uncommon that upgrading it or some other Python packages
  28. on your machine would cause the documentation build to break.
  29. A way to avoid that is to use a different version than the one shipped
  30. with your distributions. In order to do so, it is recommended to install
  31. Sphinx inside a virtual environment, using ``virtualenv-3``
  32. or ``virtualenv``, depending on how your distribution packaged Python 3.
  33. .. note::
  34. #) It is recommended to use the RTD theme for html output. Depending
  35. on the Sphinx version, it should be installed separately,
  36. with ``pip install sphinx_rtd_theme``.
  37. In summary, if you want to install the latest version of Sphinx, you
  38. should do::
  39. $ virtualenv sphinx_latest
  40. $ . sphinx_latest/bin/activate
  41. (sphinx_latest) $ pip install -r Documentation/sphinx/requirements.txt
  42. After running ``. sphinx_latest/bin/activate``, the prompt will change,
  43. in order to indicate that you're using the new environment. If you
  44. open a new shell, you need to rerun this command to enter again at
  45. the virtual environment before building the documentation.
  46. Image output
  47. ------------
  48. The kernel documentation build system contains an extension that
  49. handles images in both GraphViz and SVG formats (see :ref:`sphinx_kfigure`).
  50. For it to work, you need to install both GraphViz and ImageMagick
  51. packages. If those packages are not installed, the build system will
  52. still build the documentation, but won't include any images at the
  53. output.
  54. PDF and LaTeX builds
  55. --------------------
  56. Such builds are currently supported only with Sphinx versions 2.4 and higher.
  57. For PDF and LaTeX output, you'll also need ``XeLaTeX`` version 3.14159265.
  58. Depending on the distribution, you may also need to install a series of
  59. ``texlive`` packages that provide the minimal set of functionalities
  60. required for ``XeLaTeX`` to work.
  61. Math Expressions in HTML
  62. ------------------------
  63. Some ReST pages contain math expressions. Due to the way Sphinx works,
  64. those expressions are written using LaTeX notation.
  65. There are two options for Sphinx to render math expressions in html output.
  66. One is an extension called `imgmath`_ which converts math expressions into
  67. images and embeds them in html pages.
  68. The other is an extension called `mathjax`_ which delegates math rendering
  69. to JavaScript capable web browsers.
  70. The former was the only option for pre-6.1 kernel documentation and it
  71. requires quite a few texlive packages including amsfonts and amsmath among
  72. others.
  73. Since kernel release 6.1, html pages with math expressions can be built
  74. without installing any texlive packages. See `Choice of Math Renderer`_ for
  75. further info.
  76. .. _imgmath: https://www.sphinx-doc.org/en/master/usage/extensions/math.html#module-sphinx.ext.imgmath
  77. .. _mathjax: https://www.sphinx-doc.org/en/master/usage/extensions/math.html#module-sphinx.ext.mathjax
  78. .. _sphinx-pre-install:
  79. Checking for Sphinx dependencies
  80. --------------------------------
  81. There's a script that automatically checks for Sphinx dependencies. If it can
  82. recognize your distribution, it will also give a hint about the install
  83. command line options for your distro::
  84. $ ./scripts/sphinx-pre-install
  85. Checking if the needed tools for Fedora release 26 (Twenty Six) are available
  86. Warning: better to also install "texlive-luatex85".
  87. You should run:
  88. sudo dnf install -y texlive-luatex85
  89. /usr/bin/virtualenv sphinx_2.4.4
  90. . sphinx_2.4.4/bin/activate
  91. pip install -r Documentation/sphinx/requirements.txt
  92. Can't build as 1 mandatory dependency is missing at ./scripts/sphinx-pre-install line 468.
  93. By default, it checks all the requirements for both html and PDF, including
  94. the requirements for images, math expressions and LaTeX build, and assumes
  95. that a virtual Python environment will be used. The ones needed for html
  96. builds are assumed to be mandatory; the others to be optional.
  97. It supports two optional parameters:
  98. ``--no-pdf``
  99. Disable checks for PDF;
  100. ``--no-virtualenv``
  101. Use OS packaging for Sphinx instead of Python virtual environment.
  102. Sphinx Build
  103. ============
  104. The usual way to generate the documentation is to run ``make htmldocs`` or
  105. ``make pdfdocs``. There are also other formats available: see the documentation
  106. section of ``make help``. The generated documentation is placed in
  107. format-specific subdirectories under ``Documentation/output``.
  108. To generate documentation, Sphinx (``sphinx-build``) must obviously be
  109. installed. For PDF output you'll also need ``XeLaTeX`` and ``convert(1)``
  110. from ImageMagick (https://www.imagemagick.org).\ [#ink]_ All of these are
  111. widely available and packaged in distributions.
  112. To pass extra options to Sphinx, you can use the ``SPHINXOPTS`` make
  113. variable. For example, use ``make SPHINXOPTS=-v htmldocs`` to get more verbose
  114. output.
  115. It is also possible to pass an extra DOCS_CSS overlay file, in order to customize
  116. the html layout, by using the ``DOCS_CSS`` make variable.
  117. By default, the "Alabaster" theme is used to build the HTML documentation;
  118. this theme is bundled with Sphinx and need not be installed separately.
  119. The Sphinx theme can be overridden by using the ``DOCS_THEME`` make variable.
  120. There is another make variable ``SPHINXDIRS``, which is useful when test
  121. building a subset of documentation. For example, you can build documents
  122. under ``Documentation/doc-guide`` by running
  123. ``make SPHINXDIRS=doc-guide htmldocs``.
  124. The documentation section of ``make help`` will show you the list of
  125. subdirectories you can specify.
  126. To remove the generated documentation, run ``make cleandocs``.
  127. .. [#ink] Having ``inkscape(1)`` from Inkscape (https://inkscape.org)
  128. as well would improve the quality of images embedded in PDF
  129. documents, especially for kernel releases 5.18 and later.
  130. Choice of Math Renderer
  131. -----------------------
  132. Since kernel release 6.1, mathjax works as a fallback math renderer for
  133. html output.\ [#sph1_8]_
  134. Math renderer is chosen depending on available commands as shown below:
  135. .. table:: Math Renderer Choices for HTML
  136. ============= ================= ============
  137. Math renderer Required commands Image format
  138. ============= ================= ============
  139. imgmath latex, dvipng PNG (raster)
  140. mathjax
  141. ============= ================= ============
  142. The choice can be overridden by setting an environment variable
  143. ``SPHINX_IMGMATH`` as shown below:
  144. .. table:: Effect of Setting ``SPHINX_IMGMATH``
  145. ====================== ========
  146. Setting Renderer
  147. ====================== ========
  148. ``SPHINX_IMGMATH=yes`` imgmath
  149. ``SPHINX_IMGMATH=no`` mathjax
  150. ====================== ========
  151. .. [#sph1_8] Fallback of math renderer requires Sphinx >=1.8.
  152. Writing Documentation
  153. =====================
  154. Adding new documentation can be as simple as:
  155. 1. Add a new ``.rst`` file somewhere under ``Documentation``.
  156. 2. Refer to it from the Sphinx main `TOC tree`_ in ``Documentation/index.rst``.
  157. .. _TOC tree: http://www.sphinx-doc.org/en/stable/markup/toctree.html
  158. This is usually good enough for simple documentation (like the one you're
  159. reading right now), but for larger documents it may be advisable to create a
  160. subdirectory (or use an existing one). For example, the graphics subsystem
  161. documentation is under ``Documentation/gpu``, split to several ``.rst`` files,
  162. and has a separate ``index.rst`` (with a ``toctree`` of its own) referenced from
  163. the main index.
  164. See the documentation for `Sphinx`_ and `reStructuredText`_ on what you can do
  165. with them. In particular, the Sphinx `reStructuredText Primer`_ is a good place
  166. to get started with reStructuredText. There are also some `Sphinx specific
  167. markup constructs`_.
  168. .. _reStructuredText Primer: http://www.sphinx-doc.org/en/stable/rest.html
  169. .. _Sphinx specific markup constructs: http://www.sphinx-doc.org/en/stable/markup/index.html
  170. Specific guidelines for the kernel documentation
  171. ------------------------------------------------
  172. Here are some specific guidelines for the kernel documentation:
  173. * Please don't go overboard with reStructuredText markup. Keep it
  174. simple. For the most part the documentation should be plain text with
  175. just enough consistency in formatting that it can be converted to
  176. other formats.
  177. * Please keep the formatting changes minimal when converting existing
  178. documentation to reStructuredText.
  179. * Also update the content, not just the formatting, when converting
  180. documentation.
  181. * Please stick to this order of heading adornments:
  182. 1. ``=`` with overline for document title::
  183. ==============
  184. Document title
  185. ==============
  186. 2. ``=`` for chapters::
  187. Chapters
  188. ========
  189. 3. ``-`` for sections::
  190. Section
  191. -------
  192. 4. ``~`` for subsections::
  193. Subsection
  194. ~~~~~~~~~~
  195. Although RST doesn't mandate a specific order ("Rather than imposing a fixed
  196. number and order of section title adornment styles, the order enforced will be
  197. the order as encountered."), having the higher levels the same overall makes
  198. it easier to follow the documents.
  199. * For inserting fixed width text blocks (for code examples, use case
  200. examples, etc.), use ``::`` for anything that doesn't really benefit
  201. from syntax highlighting, especially short snippets. Use
  202. ``.. code-block:: <language>`` for longer code blocks that benefit
  203. from highlighting. For a short snippet of code embedded in the text, use \`\`.
  204. The C domain
  205. ------------
  206. The **Sphinx C Domain** (name c) is suited for documentation of C API. E.g. a
  207. function prototype:
  208. .. code-block:: rst
  209. .. c:function:: int ioctl( int fd, int request )
  210. The C domain of the kernel-doc has some additional features. E.g. you can
  211. *rename* the reference name of a function with a common name like ``open`` or
  212. ``ioctl``:
  213. .. code-block:: rst
  214. .. c:function:: int ioctl( int fd, int request )
  215. :name: VIDIOC_LOG_STATUS
  216. The func-name (e.g. ioctl) remains in the output but the ref-name changed from
  217. ``ioctl`` to ``VIDIOC_LOG_STATUS``. The index entry for this function is also
  218. changed to ``VIDIOC_LOG_STATUS``.
  219. Please note that there is no need to use ``c:func:`` to generate cross
  220. references to function documentation. Due to some Sphinx extension magic,
  221. the documentation build system will automatically turn a reference to
  222. ``function()`` into a cross reference if an index entry for the given
  223. function name exists. If you see ``c:func:`` use in a kernel document,
  224. please feel free to remove it.
  225. Tables
  226. ------
  227. ReStructuredText provides several options for table syntax. Kernel style for
  228. tables is to prefer *simple table* syntax or *grid table* syntax. See the
  229. `reStructuredText user reference for table syntax`_ for more details.
  230. .. _reStructuredText user reference for table syntax:
  231. https://docutils.sourceforge.io/docs/user/rst/quickref.html#tables
  232. list tables
  233. ~~~~~~~~~~~
  234. The list-table formats can be useful for tables that are not easily laid
  235. out in the usual Sphinx ASCII-art formats. These formats are nearly
  236. impossible for readers of the plain-text documents to understand, though,
  237. and should be avoided in the absence of a strong justification for their
  238. use.
  239. The ``flat-table`` is a double-stage list similar to the ``list-table`` with
  240. some additional features:
  241. * column-span: with the role ``cspan`` a cell can be extended through
  242. additional columns
  243. * row-span: with the role ``rspan`` a cell can be extended through
  244. additional rows
  245. * auto span rightmost cell of a table row over the missing cells on the right
  246. side of that table-row. With Option ``:fill-cells:`` this behavior can
  247. changed from *auto span* to *auto fill*, which automatically inserts (empty)
  248. cells instead of spanning the last cell.
  249. options:
  250. * ``:header-rows:`` [int] count of header rows
  251. * ``:stub-columns:`` [int] count of stub columns
  252. * ``:widths:`` [[int] [int] ... ] widths of columns
  253. * ``:fill-cells:`` instead of auto-spanning missing cells, insert missing cells
  254. roles:
  255. * ``:cspan:`` [int] additional columns (*morecols*)
  256. * ``:rspan:`` [int] additional rows (*morerows*)
  257. The example below shows how to use this markup. The first level of the staged
  258. list is the *table-row*. In the *table-row* there is only one markup allowed,
  259. the list of the cells in this *table-row*. Exceptions are *comments* ( ``..`` )
  260. and *targets* (e.g. a ref to ``:ref:`last row <last row>``` / :ref:`last row
  261. <last row>`).
  262. .. code-block:: rst
  263. .. flat-table:: table title
  264. :widths: 2 1 1 3
  265. * - head col 1
  266. - head col 2
  267. - head col 3
  268. - head col 4
  269. * - row 1
  270. - field 1.1
  271. - field 1.2 with autospan
  272. * - row 2
  273. - field 2.1
  274. - :rspan:`1` :cspan:`1` field 2.2 - 3.3
  275. * .. _`last row`:
  276. - row 3
  277. Rendered as:
  278. .. flat-table:: table title
  279. :widths: 2 1 1 3
  280. * - head col 1
  281. - head col 2
  282. - head col 3
  283. - head col 4
  284. * - row 1
  285. - field 1.1
  286. - field 1.2 with autospan
  287. * - row 2
  288. - field 2.1
  289. - :rspan:`1` :cspan:`1` field 2.2 - 3.3
  290. * .. _`last row`:
  291. - row 3
  292. Cross-referencing
  293. -----------------
  294. Cross-referencing from one documentation page to another can be done simply by
  295. writing the path to the document file, no special syntax required. The path can
  296. be either absolute or relative. For absolute paths, start it with
  297. "Documentation/". For example, to cross-reference to this page, all the
  298. following are valid options, depending on the current document's directory (note
  299. that the ``.rst`` extension is required)::
  300. See Documentation/doc-guide/sphinx.rst. This always works.
  301. Take a look at sphinx.rst, which is at this same directory.
  302. Read ../sphinx.rst, which is one directory above.
  303. If you want the link to have a different rendered text other than the document's
  304. title, you need to use Sphinx's ``doc`` role. For example::
  305. See :doc:`my custom link text for document sphinx <sphinx>`.
  306. For most use cases, the former is preferred, as it is cleaner and more suited
  307. for people reading the source files. If you come across a ``:doc:`` usage that
  308. isn't adding any value, please feel free to convert it to just the document
  309. path.
  310. For information on cross-referencing to kernel-doc functions or types, see
  311. Documentation/doc-guide/kernel-doc.rst.
  312. Referencing commits
  313. ~~~~~~~~~~~~~~~~~~~
  314. References to git commits are automatically hyperlinked given that they are
  315. written in one of these formats::
  316. commit 72bf4f1767f0
  317. commit 72bf4f1767f0 ("net: do not leave an empty skb in write queue")
  318. .. _sphinx_kfigure:
  319. Figures & Images
  320. ================
  321. If you want to add an image, you should use the ``kernel-figure`` and
  322. ``kernel-image`` directives. E.g. to insert a figure with a scalable
  323. image format, use SVG (:ref:`svg_image_example`)::
  324. .. kernel-figure:: svg_image.svg
  325. :alt: simple SVG image
  326. SVG image example
  327. .. _svg_image_example:
  328. .. kernel-figure:: svg_image.svg
  329. :alt: simple SVG image
  330. SVG image example
  331. The kernel figure (and image) directive supports **DOT** formatted files, see
  332. * DOT: http://graphviz.org/pdf/dotguide.pdf
  333. * Graphviz: http://www.graphviz.org/content/dot-language
  334. A simple example (:ref:`hello_dot_file`)::
  335. .. kernel-figure:: hello.dot
  336. :alt: hello world
  337. DOT's hello world example
  338. .. _hello_dot_file:
  339. .. kernel-figure:: hello.dot
  340. :alt: hello world
  341. DOT's hello world example
  342. Embedded *render* markups (or languages) like Graphviz's **DOT** are provided by the
  343. ``kernel-render`` directives.::
  344. .. kernel-render:: DOT
  345. :alt: foobar digraph
  346. :caption: Embedded **DOT** (Graphviz) code
  347. digraph foo {
  348. "bar" -> "baz";
  349. }
  350. How this will be rendered depends on the installed tools. If Graphviz is
  351. installed, you will see a vector image. If not, the raw markup is inserted as
  352. *literal-block* (:ref:`hello_dot_render`).
  353. .. _hello_dot_render:
  354. .. kernel-render:: DOT
  355. :alt: foobar digraph
  356. :caption: Embedded **DOT** (Graphviz) code
  357. digraph foo {
  358. "bar" -> "baz";
  359. }
  360. The *render* directive has all the options known from the *figure* directive,
  361. plus option ``caption``. If ``caption`` has a value, a *figure* node is
  362. inserted. If not, an *image* node is inserted. A ``caption`` is also needed, if
  363. you want to refer to it (:ref:`hello_svg_render`).
  364. Embedded **SVG**::
  365. .. kernel-render:: SVG
  366. :caption: Embedded **SVG** markup
  367. :alt: so-nw-arrow
  368. <?xml version="1.0" encoding="UTF-8"?>
  369. <svg xmlns="http://www.w3.org/2000/svg" version="1.1" ...>
  370. ...
  371. </svg>
  372. .. _hello_svg_render:
  373. .. kernel-render:: SVG
  374. :caption: Embedded **SVG** markup
  375. :alt: so-nw-arrow
  376. <?xml version="1.0" encoding="UTF-8"?>
  377. <svg xmlns="http://www.w3.org/2000/svg"
  378. version="1.1" baseProfile="full" width="70px" height="40px" viewBox="0 0 700 400">
  379. <line x1="180" y1="370" x2="500" y2="50" stroke="black" stroke-width="15px"/>
  380. <polygon points="585 0 525 25 585 50" transform="rotate(135 525 25)"/>
  381. </svg>