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