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