3 =====================================
4 Using Sphinx for kernel documentation
5 =====================================
7 The Linux kernel uses `Sphinx`_ to generate pretty documentation from
8 `reStructuredText`_ files under ``Documentation``. To build the documentation in
9 HTML or PDF formats, use ``make htmldocs`` or ``make pdfdocs``. The generated
10 documentation is placed in ``Documentation/output``.
12 .. _Sphinx: http://www.sphinx-doc.org/
13 .. _reStructuredText: http://docutils.sourceforge.net/rst.html
15 The reStructuredText files may contain directives to include structured
16 documentation comments, or kernel-doc comments, from source files. Usually these
17 are used to describe the functions and types and design of the code. The
18 kernel-doc comments have some special structure and formatting, but beyond that
19 they are also treated as reStructuredText.
21 Finally, there are thousands of plain text documentation files scattered around
22 ``Documentation``. Some of these will likely be converted to reStructuredText
23 over time, but the bulk of them will remain in plain text.
30 The ReST markups currently used by the Documentation/ files are meant to be
31 built with ``Sphinx`` version 1.7 or higher.
33 There's a script that checks for the Sphinx requirements. Please see
34 :ref:`sphinx-pre-install` for further details.
36 Most distributions are shipped with Sphinx, but its toolchain is fragile,
37 and it is not uncommon that upgrading it or some other Python packages
38 on your machine would cause the documentation build to break.
40 A way to avoid that is to use a different version than the one shipped
41 with your distributions. In order to do so, it is recommended to install
42 Sphinx inside a virtual environment, using ``virtualenv-3``
43 or ``virtualenv``, depending on how your distribution packaged Python 3.
47 #) It is recommended to use the RTD theme for html output. Depending
48 on the Sphinx version, it should be installed separately,
49 with ``pip install sphinx_rtd_theme``.
51 In summary, if you want to install Sphinx version 2.4.4, you should do::
53 $ virtualenv sphinx_2.4.4
54 $ . sphinx_2.4.4/bin/activate
55 (sphinx_2.4.4) $ pip install -r Documentation/sphinx/requirements.txt
57 After running ``. sphinx_2.4.4/bin/activate``, the prompt will change,
58 in order to indicate that you're using the new environment. If you
59 open a new shell, you need to rerun this command to enter again at
60 the virtual environment before building the documentation.
65 The kernel documentation build system contains an extension that
66 handles images on both GraphViz and SVG formats (see
67 :ref:`sphinx_kfigure`).
69 For it to work, you need to install both GraphViz and ImageMagick
70 packages. If those packages are not installed, the build system will
71 still build the documentation, but won't include any images at the
77 Such builds are currently supported only with Sphinx versions 2.4 and higher.
79 For PDF and LaTeX output, you'll also need ``XeLaTeX`` version 3.14159265.
81 Depending on the distribution, you may also need to install a series of
82 ``texlive`` packages that provide the minimal set of functionalities
83 required for ``XeLaTeX`` to work.
85 Math Expressions in HTML
86 ------------------------
88 Some ReST pages contain math expressions. Due to the way Sphinx works,
89 those expressions are written using LaTeX notation.
90 There are two options for Sphinx to render math expressions in html output.
91 One is an extension called `imgmath`_ which converts math expressions into
92 images and embeds them in html pages.
93 The other is an extension called `mathjax`_ which delegates math rendering
94 to JavaScript capable web browsers.
95 The former was the only option for pre-6.1 kernel documentation and it
96 requires quite a few texlive packages including amsfonts and amsmath among
99 Since kernel release 6.1, html pages with math expressions can be built
100 without installing any texlive packages. See `Choice of Math Renderer`_ for
103 .. _imgmath: https://www.sphinx-doc.org/en/master/usage/extensions/math.html#module-sphinx.ext.imgmath
104 .. _mathjax: https://www.sphinx-doc.org/en/master/usage/extensions/math.html#module-sphinx.ext.mathjax
106 .. _sphinx-pre-install:
108 Checking for Sphinx dependencies
109 --------------------------------
111 There's a script that automatically check for Sphinx dependencies. If it can
112 recognize your distribution, it will also give a hint about the install
113 command line options for your distro::
115 $ ./scripts/sphinx-pre-install
116 Checking if the needed tools for Fedora release 26 (Twenty Six) are available
117 Warning: better to also install "texlive-luatex85".
120 sudo dnf install -y texlive-luatex85
121 /usr/bin/virtualenv sphinx_2.4.4
122 . sphinx_2.4.4/bin/activate
123 pip install -r Documentation/sphinx/requirements.txt
125 Can't build as 1 mandatory dependency is missing at ./scripts/sphinx-pre-install line 468.
127 By default, it checks all the requirements for both html and PDF, including
128 the requirements for images, math expressions and LaTeX build, and assumes
129 that a virtual Python environment will be used. The ones needed for html
130 builds are assumed to be mandatory; the others to be optional.
132 It supports two optional parameters:
135 Disable checks for PDF;
138 Use OS packaging for Sphinx instead of Python virtual environment.
144 The usual way to generate the documentation is to run ``make htmldocs`` or
145 ``make pdfdocs``. There are also other formats available: see the documentation
146 section of ``make help``. The generated documentation is placed in
147 format-specific subdirectories under ``Documentation/output``.
149 To generate documentation, Sphinx (``sphinx-build``) must obviously be
150 installed. For PDF output you'll also need ``XeLaTeX`` and ``convert(1)``
151 from ImageMagick (https://www.imagemagick.org).\ [#ink]_ All of these are
152 widely available and packaged in distributions.
154 To pass extra options to Sphinx, you can use the ``SPHINXOPTS`` make
155 variable. For example, use ``make SPHINXOPTS=-v htmldocs`` to get more verbose
158 It is also possible to pass an extra DOCS_CSS overlay file, in order to customize
159 the html layout, by using the ``DOCS_CSS`` make variable.
161 By default, the "Alabaster" theme is used to build the HTML documentation;
162 this theme is bundled with Sphinx and need not be installed separately.
163 The Sphinx theme can be overridden by using the ``DOCS_THEME`` make variable.
165 There is another make variable ``SPHINXDIRS``, which is useful when test
166 building a subset of documentation. For example, you can build documents
167 under ``Documentation/doc-guide`` by running
168 ``make SPHINXDIRS=doc-guide htmldocs``.
169 The documentation section of ``make help`` will show you the list of
170 subdirectories you can specify.
172 To remove the generated documentation, run ``make cleandocs``.
174 .. [#ink] Having ``inkscape(1)`` from Inkscape (https://inkscape.org)
175 as well would improve the quality of images embedded in PDF
176 documents, especially for kernel releases 5.18 and later.
178 Choice of Math Renderer
179 -----------------------
181 Since kernel release 6.1, mathjax works as a fallback math renderer for
182 html output.\ [#sph1_8]_
184 Math renderer is chosen depending on available commands as shown below:
186 .. table:: Math Renderer Choices for HTML
188 ============= ================= ============
189 Math renderer Required commands Image format
190 ============= ================= ============
191 imgmath latex, dvipng PNG (raster)
193 ============= ================= ============
195 The choice can be overridden by setting an environment variable
196 ``SPHINX_IMGMATH`` as shown below:
198 .. table:: Effect of Setting ``SPHINX_IMGMATH``
200 ====================== ========
202 ====================== ========
203 ``SPHINX_IMGMATH=yes`` imgmath
204 ``SPHINX_IMGMATH=no`` mathjax
205 ====================== ========
207 .. [#sph1_8] Fallback of math renderer requires Sphinx >=1.8.
210 Writing Documentation
211 =====================
213 Adding new documentation can be as simple as:
215 1. Add a new ``.rst`` file somewhere under ``Documentation``.
216 2. Refer to it from the Sphinx main `TOC tree`_ in ``Documentation/index.rst``.
218 .. _TOC tree: http://www.sphinx-doc.org/en/stable/markup/toctree.html
220 This is usually good enough for simple documentation (like the one you're
221 reading right now), but for larger documents it may be advisable to create a
222 subdirectory (or use an existing one). For example, the graphics subsystem
223 documentation is under ``Documentation/gpu``, split to several ``.rst`` files,
224 and has a separate ``index.rst`` (with a ``toctree`` of its own) referenced from
227 See the documentation for `Sphinx`_ and `reStructuredText`_ on what you can do
228 with them. In particular, the Sphinx `reStructuredText Primer`_ is a good place
229 to get started with reStructuredText. There are also some `Sphinx specific
232 .. _reStructuredText Primer: http://www.sphinx-doc.org/en/stable/rest.html
233 .. _Sphinx specific markup constructs: http://www.sphinx-doc.org/en/stable/markup/index.html
235 Specific guidelines for the kernel documentation
236 ------------------------------------------------
238 Here are some specific guidelines for the kernel documentation:
240 * Please don't go overboard with reStructuredText markup. Keep it
241 simple. For the most part the documentation should be plain text with
242 just enough consistency in formatting that it can be converted to
245 * Please keep the formatting changes minimal when converting existing
246 documentation to reStructuredText.
248 * Also update the content, not just the formatting, when converting
251 * Please stick to this order of heading adornments:
253 1. ``=`` with overline for document title::
259 2. ``=`` for chapters::
264 3. ``-`` for sections::
269 4. ``~`` for subsections::
274 Although RST doesn't mandate a specific order ("Rather than imposing a fixed
275 number and order of section title adornment styles, the order enforced will be
276 the order as encountered."), having the higher levels the same overall makes
277 it easier to follow the documents.
279 * For inserting fixed width text blocks (for code examples, use case
280 examples, etc.), use ``::`` for anything that doesn't really benefit
281 from syntax highlighting, especially short snippets. Use
282 ``.. code-block:: <language>`` for longer code blocks that benefit
283 from highlighting. For a short snippet of code embedded in the text, use \`\`.
289 The **Sphinx C Domain** (name c) is suited for documentation of C API. E.g. a
294 .. c:function:: int ioctl( int fd, int request )
296 The C domain of the kernel-doc has some additional features. E.g. you can
297 *rename* the reference name of a function with a common name like ``open`` or
302 .. c:function:: int ioctl( int fd, int request )
303 :name: VIDIOC_LOG_STATUS
305 The func-name (e.g. ioctl) remains in the output but the ref-name changed from
306 ``ioctl`` to ``VIDIOC_LOG_STATUS``. The index entry for this function is also
307 changed to ``VIDIOC_LOG_STATUS``.
309 Please note that there is no need to use ``c:func:`` to generate cross
310 references to function documentation. Due to some Sphinx extension magic,
311 the documentation build system will automatically turn a reference to
312 ``function()`` into a cross reference if an index entry for the given
313 function name exists. If you see ``c:func:`` use in a kernel document,
314 please feel free to remove it.
320 The list-table formats can be useful for tables that are not easily laid
321 out in the usual Sphinx ASCII-art formats. These formats are nearly
322 impossible for readers of the plain-text documents to understand, though,
323 and should be avoided in the absence of a strong justification for their
326 The ``flat-table`` is a double-stage list similar to the ``list-table`` with
327 some additional features:
329 * column-span: with the role ``cspan`` a cell can be extended through
332 * row-span: with the role ``rspan`` a cell can be extended through
335 * auto span rightmost cell of a table row over the missing cells on the right
336 side of that table-row. With Option ``:fill-cells:`` this behavior can
337 changed from *auto span* to *auto fill*, which automatically inserts (empty)
338 cells instead of spanning the last cell.
342 * ``:header-rows:`` [int] count of header rows
343 * ``:stub-columns:`` [int] count of stub columns
344 * ``:widths:`` [[int] [int] ... ] widths of columns
345 * ``:fill-cells:`` instead of auto-spanning missing cells, insert missing cells
349 * ``:cspan:`` [int] additional columns (*morecols*)
350 * ``:rspan:`` [int] additional rows (*morerows*)
352 The example below shows how to use this markup. The first level of the staged
353 list is the *table-row*. In the *table-row* there is only one markup allowed,
354 the list of the cells in this *table-row*. Exceptions are *comments* ( ``..`` )
355 and *targets* (e.g. a ref to ``:ref:`last row <last row>``` / :ref:`last row
360 .. flat-table:: table title
370 - field 1.2 with autospan
374 - :rspan:`1` :cspan:`1` field 2.2 - 3.3
382 .. flat-table:: table title
392 - field 1.2 with autospan
396 - :rspan:`1` :cspan:`1` field 2.2 - 3.3
405 Cross-referencing from one documentation page to another can be done simply by
406 writing the path to the document file, no special syntax required. The path can
407 be either absolute or relative. For absolute paths, start it with
408 "Documentation/". For example, to cross-reference to this page, all the
409 following are valid options, depending on the current document's directory (note
410 that the ``.rst`` extension is required)::
412 See Documentation/doc-guide/sphinx.rst. This always works.
413 Take a look at sphinx.rst, which is at this same directory.
414 Read ../sphinx.rst, which is one directory above.
416 If you want the link to have a different rendered text other than the document's
417 title, you need to use Sphinx's ``doc`` role. For example::
419 See :doc:`my custom link text for document sphinx <sphinx>`.
421 For most use cases, the former is preferred, as it is cleaner and more suited
422 for people reading the source files. If you come across a ``:doc:`` usage that
423 isn't adding any value, please feel free to convert it to just the document
426 For information on cross-referencing to kernel-doc functions or types, see
427 Documentation/doc-guide/kernel-doc.rst.
434 If you want to add an image, you should use the ``kernel-figure`` and
435 ``kernel-image`` directives. E.g. to insert a figure with a scalable
436 image format, use SVG (:ref:`svg_image_example`)::
438 .. kernel-figure:: svg_image.svg
439 :alt: simple SVG image
443 .. _svg_image_example:
445 .. kernel-figure:: svg_image.svg
446 :alt: simple SVG image
450 The kernel figure (and image) directive supports **DOT** formatted files, see
452 * DOT: http://graphviz.org/pdf/dotguide.pdf
453 * Graphviz: http://www.graphviz.org/content/dot-language
455 A simple example (:ref:`hello_dot_file`)::
457 .. kernel-figure:: hello.dot
460 DOT's hello world example
464 .. kernel-figure:: hello.dot
467 DOT's hello world example
469 Embedded *render* markups (or languages) like Graphviz's **DOT** are provided by the
470 ``kernel-render`` directives.::
472 .. kernel-render:: DOT
474 :caption: Embedded **DOT** (Graphviz) code
480 How this will be rendered depends on the installed tools. If Graphviz is
481 installed, you will see a vector image. If not, the raw markup is inserted as
482 *literal-block* (:ref:`hello_dot_render`).
484 .. _hello_dot_render:
486 .. kernel-render:: DOT
488 :caption: Embedded **DOT** (Graphviz) code
494 The *render* directive has all the options known from the *figure* directive,
495 plus option ``caption``. If ``caption`` has a value, a *figure* node is
496 inserted. If not, an *image* node is inserted. A ``caption`` is also needed, if
497 you want to refer to it (:ref:`hello_svg_render`).
501 .. kernel-render:: SVG
502 :caption: Embedded **SVG** markup
505 <?xml version="1.0" encoding="UTF-8"?>
506 <svg xmlns="http://www.w3.org/2000/svg" version="1.1" ...>
510 .. _hello_svg_render:
512 .. kernel-render:: SVG
513 :caption: Embedded **SVG** markup
516 <?xml version="1.0" encoding="UTF-8"?>
517 <svg xmlns="http://www.w3.org/2000/svg"
518 version="1.1" baseProfile="full" width="70px" height="40px" viewBox="0 0 700 400">
519 <line x1="180" y1="370" x2="500" y2="50" stroke="black" stroke-width="15px"/>
520 <polygon points="585 0 525 25 585 50" transform="rotate(135 525 25)"/>