sphinx.rst 19 KB

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