blob: bb36f18ae9ac3e274c859ca8cc69518e283a5185 [file] [log] [blame]
Federico Vagaf77af632018-11-21 01:35:19 +01001.. _sphinxdoc:
2
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -02003Introduction
4============
5
6The Linux kernel uses `Sphinx`_ to generate pretty documentation from
7`reStructuredText`_ files under ``Documentation``. To build the documentation in
8HTML or PDF formats, use ``make htmldocs`` or ``make pdfdocs``. The generated
9documentation is placed in ``Documentation/output``.
10
11.. _Sphinx: http://www.sphinx-doc.org/
12.. _reStructuredText: http://docutils.sourceforge.net/rst.html
13
14The reStructuredText files may contain directives to include structured
15documentation comments, or kernel-doc comments, from source files. Usually these
16are used to describe the functions and types and design of the code. The
17kernel-doc comments have some special structure and formatting, but beyond that
18they are also treated as reStructuredText.
19
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -020020Finally, there are thousands of plain text documentation files scattered around
21``Documentation``. Some of these will likely be converted to reStructuredText
22over time, but the bulk of them will remain in plain text.
23
Mauro Carvalho Chehabb8b07b52017-07-14 13:41:17 -030024.. _sphinx_install:
25
26Sphinx Install
27==============
28
29The ReST markups currently used by the Documentation/ files are meant to be
Akira Yokosawa6d6a8d62021-11-10 18:16:48 +090030built with ``Sphinx`` version 1.7 or higher.
Mauro Carvalho Chehabb8b07b52017-07-14 13:41:17 -030031
Federico Vagac1ec85f2018-06-06 00:48:58 +020032There's a script that checks for the Sphinx requirements. Please see
Mauro Carvalho Chehab868366a2017-07-17 18:46:42 -030033:ref:`sphinx-pre-install` for further details.
34
Mauro Carvalho Chehab58ef4a42017-07-14 13:41:18 -030035Most distributions are shipped with Sphinx, but its toolchain is fragile,
36and it is not uncommon that upgrading it or some other Python packages
37on your machine would cause the documentation build to break.
38
Joel Nider4d014602019-01-14 09:14:59 +020039A way to avoid that is to use a different version than the one shipped
40with your distributions. In order to do so, it is recommended to install
Mauro Carvalho Chehab58ef4a42017-07-14 13:41:18 -030041Sphinx inside a virtual environment, using ``virtualenv-3``
42or ``virtualenv``, depending on how your distribution packaged Python 3.
43
44.. note::
45
Mauro Carvalho Chehab58ef4a42017-07-14 13:41:18 -030046 #) It is recommended to use the RTD theme for html output. Depending
Randy Dunlap0be15112020-12-28 15:12:12 -080047 on the Sphinx version, it should be installed separately,
Mauro Carvalho Chehab58ef4a42017-07-14 13:41:18 -030048 with ``pip install sphinx_rtd_theme``.
49
Randy Dunlap0be15112020-12-28 15:12:12 -080050 #) Some ReST pages contain math expressions. Due to the way Sphinx works,
Mauro Carvalho Chehab868366a2017-07-17 18:46:42 -030051 those expressions are written using LaTeX notation. It needs texlive
Randy Dunlap0be15112020-12-28 15:12:12 -080052 installed with amsfonts and amsmath in order to evaluate them.
Mauro Carvalho Chehab868366a2017-07-17 18:46:42 -030053
Akira Yokosawa6d6a8d62021-11-10 18:16:48 +090054In summary, if you want to install Sphinx version 2.4.4, you should do::
Mauro Carvalho Chehab58ef4a42017-07-14 13:41:18 -030055
Akira Yokosawa6d6a8d62021-11-10 18:16:48 +090056 $ virtualenv sphinx_2.4.4
57 $ . sphinx_2.4.4/bin/activate
58 (sphinx_2.4.4) $ pip install -r Documentation/sphinx/requirements.txt
Mauro Carvalho Chehab58ef4a42017-07-14 13:41:18 -030059
Akira Yokosawa6d6a8d62021-11-10 18:16:48 +090060After running ``. sphinx_2.4.4/bin/activate``, the prompt will change,
Mauro Carvalho Chehab58ef4a42017-07-14 13:41:18 -030061in order to indicate that you're using the new environment. If you
62open a new shell, you need to rerun this command to enter again at
63the virtual environment before building the documentation.
64
Mauro Carvalho Chehabd43e5ae2017-07-14 13:41:20 -030065Image output
66------------
67
68The kernel documentation build system contains an extension that
69handles images on both GraphViz and SVG formats (see
70:ref:`sphinx_kfigure`).
71
72For it to work, you need to install both GraphViz and ImageMagick
73packages. If those packages are not installed, the build system will
74still build the documentation, but won't include any images at the
75output.
76
Mauro Carvalho Chehab6e322a12017-07-14 13:41:21 -030077PDF and LaTeX builds
78--------------------
79
Akira Yokosawa6d6a8d62021-11-10 18:16:48 +090080Such builds are currently supported only with Sphinx versions 2.4 and higher.
Mauro Carvalho Chehab6e322a12017-07-14 13:41:21 -030081
82For PDF and LaTeX output, you'll also need ``XeLaTeX`` version 3.14159265.
83
84Depending on the distribution, you may also need to install a series of
85``texlive`` packages that provide the minimal set of functionalities
Mauro Carvalho Chehab868366a2017-07-17 18:46:42 -030086required for ``XeLaTeX`` to work.
87
88.. _sphinx-pre-install:
89
90Checking for Sphinx dependencies
91--------------------------------
92
93There's a script that automatically check for Sphinx dependencies. If it can
94recognize your distribution, it will also give a hint about the install
95command line options for your distro::
96
97 $ ./scripts/sphinx-pre-install
98 Checking if the needed tools for Fedora release 26 (Twenty Six) are available
99 Warning: better to also install "texlive-luatex85".
100 You should run:
101
102 sudo dnf install -y texlive-luatex85
Akira Yokosawa6d6a8d62021-11-10 18:16:48 +0900103 /usr/bin/virtualenv sphinx_2.4.4
104 . sphinx_2.4.4/bin/activate
Mauro Carvalho Chehab868366a2017-07-17 18:46:42 -0300105 pip install -r Documentation/sphinx/requirements.txt
106
107 Can't build as 1 mandatory dependency is missing at ./scripts/sphinx-pre-install line 468.
108
109By default, it checks all the requirements for both html and PDF, including
110the requirements for images, math expressions and LaTeX build, and assumes
111that a virtual Python environment will be used. The ones needed for html
112builds are assumed to be mandatory; the others to be optional.
113
114It supports two optional parameters:
115
116``--no-pdf``
117 Disable checks for PDF;
118
119``--no-virtualenv``
120 Use OS packaging for Sphinx instead of Python virtual environment.
121
Mauro Carvalho Chehab6e322a12017-07-14 13:41:21 -0300122
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200123Sphinx Build
124============
125
126The usual way to generate the documentation is to run ``make htmldocs`` or
Randy Dunlap0be15112020-12-28 15:12:12 -0800127``make pdfdocs``. There are also other formats available: see the documentation
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200128section of ``make help``. The generated documentation is placed in
129format-specific subdirectories under ``Documentation/output``.
130
131To generate documentation, Sphinx (``sphinx-build``) must obviously be
132installed. For prettier HTML output, the Read the Docs Sphinx theme
Markus Heiserdb6ccf22017-03-06 14:09:27 +0100133(``sphinx_rtd_theme``) is used if available. For PDF output you'll also need
134``XeLaTeX`` and ``convert(1)`` from ImageMagick (https://www.imagemagick.org).
135All of these are widely available and packaged in distributions.
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200136
137To pass extra options to Sphinx, you can use the ``SPHINXOPTS`` make
138variable. For example, use ``make SPHINXOPTS=-v htmldocs`` to get more verbose
139output.
140
Mauro Carvalho Chehab135707d2021-12-07 10:53:00 +0100141It is also possible to pass an extra DOCS_CSS overlay file, in order to customize
142the html layout, by using the ``DOCS_CSS`` make variable.
143
Mauro Carvalho Chehabfca72162021-12-07 10:52:59 +0100144By default, the build will try to use the Read the Docs sphinx theme:
145
146 https://github.com/readthedocs/sphinx_rtd_theme
147
148If the theme is not available, it will fall-back to the classic one.
149
Mauro Carvalho Chehab0e805b12021-12-11 09:57:00 +0100150The Sphinx theme can be overridden by using the ``DOCS_THEME`` make variable.
Mauro Carvalho Chehabfca72162021-12-07 10:52:59 +0100151
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200152To remove the generated documentation, run ``make cleandocs``.
153
154Writing Documentation
155=====================
156
157Adding new documentation can be as simple as:
158
1591. Add a new ``.rst`` file somewhere under ``Documentation``.
1602. Refer to it from the Sphinx main `TOC tree`_ in ``Documentation/index.rst``.
161
162.. _TOC tree: http://www.sphinx-doc.org/en/stable/markup/toctree.html
163
164This is usually good enough for simple documentation (like the one you're
165reading right now), but for larger documents it may be advisable to create a
166subdirectory (or use an existing one). For example, the graphics subsystem
167documentation is under ``Documentation/gpu``, split to several ``.rst`` files,
168and has a separate ``index.rst`` (with a ``toctree`` of its own) referenced from
169the main index.
170
171See the documentation for `Sphinx`_ and `reStructuredText`_ on what you can do
172with them. In particular, the Sphinx `reStructuredText Primer`_ is a good place
173to get started with reStructuredText. There are also some `Sphinx specific
174markup constructs`_.
175
176.. _reStructuredText Primer: http://www.sphinx-doc.org/en/stable/rest.html
177.. _Sphinx specific markup constructs: http://www.sphinx-doc.org/en/stable/markup/index.html
178
179Specific guidelines for the kernel documentation
180------------------------------------------------
181
182Here are some specific guidelines for the kernel documentation:
183
Daniel Vetterc3c53602017-03-02 16:16:33 +0100184* Please don't go overboard with reStructuredText markup. Keep it
185 simple. For the most part the documentation should be plain text with
186 just enough consistency in formatting that it can be converted to
187 other formats.
188
189* Please keep the formatting changes minimal when converting existing
190 documentation to reStructuredText.
191
192* Also update the content, not just the formatting, when converting
193 documentation.
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200194
195* Please stick to this order of heading adornments:
196
197 1. ``=`` with overline for document title::
198
199 ==============
200 Document title
201 ==============
202
203 2. ``=`` for chapters::
204
205 Chapters
206 ========
207
208 3. ``-`` for sections::
209
210 Section
211 -------
212
213 4. ``~`` for subsections::
214
215 Subsection
216 ~~~~~~~~~~
217
218 Although RST doesn't mandate a specific order ("Rather than imposing a fixed
219 number and order of section title adornment styles, the order enforced will be
220 the order as encountered."), having the higher levels the same overall makes
221 it easier to follow the documents.
222
Daniel Vetterc3c53602017-03-02 16:16:33 +0100223* For inserting fixed width text blocks (for code examples, use case
224 examples, etc.), use ``::`` for anything that doesn't really benefit
225 from syntax highlighting, especially short snippets. Use
226 ``.. code-block:: <language>`` for longer code blocks that benefit
André Almeida83e8b972019-06-11 17:03:16 -0300227 from highlighting. For a short snippet of code embedded in the text, use \`\`.
Daniel Vetterc3c53602017-03-02 16:16:33 +0100228
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200229
230the C domain
231------------
232
Mauro Carvalho Chehab29fd35b2017-07-14 13:41:19 -0300233The **Sphinx C Domain** (name c) is suited for documentation of C API. E.g. a
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200234function prototype:
235
236.. code-block:: rst
237
238 .. c:function:: int ioctl( int fd, int request )
239
240The C domain of the kernel-doc has some additional features. E.g. you can
241*rename* the reference name of a function with a common name like ``open`` or
242``ioctl``:
243
244.. code-block:: rst
245
246 .. c:function:: int ioctl( int fd, int request )
247 :name: VIDIOC_LOG_STATUS
248
249The func-name (e.g. ioctl) remains in the output but the ref-name changed from
250``ioctl`` to ``VIDIOC_LOG_STATUS``. The index entry for this function is also
Jonathan Corbetd9d7c0c2019-06-26 11:20:21 -0600251changed to ``VIDIOC_LOG_STATUS``.
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200252
Jonathan Corbetd9d7c0c2019-06-26 11:20:21 -0600253Please note that there is no need to use ``c:func:`` to generate cross
254references to function documentation. Due to some Sphinx extension magic,
255the documentation build system will automatically turn a reference to
256``function()`` into a cross reference if an index entry for the given
257function name exists. If you see ``c:func:`` use in a kernel document,
258please feel free to remove it.
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200259
260
261list tables
262-----------
263
Jonathan Corbetdb67eb72022-01-03 15:33:47 -0700264The list-table formats can be useful for tables that are not easily laid
265out in the usual Sphinx ASCII-art formats. These formats are nearly
266impossible for readers of the plain-text documents to understand, though,
267and should be avoided in the absence of a strong justification for their
268use.
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200269
270The ``flat-table`` is a double-stage list similar to the ``list-table`` with
271some additional features:
272
273* column-span: with the role ``cspan`` a cell can be extended through
274 additional columns
275
276* row-span: with the role ``rspan`` a cell can be extended through
277 additional rows
278
279* auto span rightmost cell of a table row over the missing cells on the right
280 side of that table-row. With Option ``:fill-cells:`` this behavior can
281 changed from *auto span* to *auto fill*, which automatically inserts (empty)
282 cells instead of spanning the last cell.
283
284options:
285
286* ``:header-rows:`` [int] count of header rows
287* ``:stub-columns:`` [int] count of stub columns
288* ``:widths:`` [[int] [int] ... ] widths of columns
289* ``:fill-cells:`` instead of auto-spanning missing cells, insert missing cells
290
291roles:
292
293* ``:cspan:`` [int] additional columns (*morecols*)
294* ``:rspan:`` [int] additional rows (*morerows*)
295
296The example below shows how to use this markup. The first level of the staged
297list is the *table-row*. In the *table-row* there is only one markup allowed,
298the list of the cells in this *table-row*. Exceptions are *comments* ( ``..`` )
299and *targets* (e.g. a ref to ``:ref:`last row <last row>``` / :ref:`last row
300<last row>`).
301
302.. code-block:: rst
303
304 .. flat-table:: table title
305 :widths: 2 1 1 3
306
307 * - head col 1
308 - head col 2
309 - head col 3
310 - head col 4
311
Randy Dunlap0be15112020-12-28 15:12:12 -0800312 * - row 1
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200313 - field 1.1
314 - field 1.2 with autospan
315
Randy Dunlap0be15112020-12-28 15:12:12 -0800316 * - row 2
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200317 - field 2.1
318 - :rspan:`1` :cspan:`1` field 2.2 - 3.3
319
320 * .. _`last row`:
321
Randy Dunlap0be15112020-12-28 15:12:12 -0800322 - row 3
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200323
324Rendered as:
325
326 .. flat-table:: table title
327 :widths: 2 1 1 3
328
329 * - head col 1
330 - head col 2
331 - head col 3
332 - head col 4
333
Randy Dunlap0be15112020-12-28 15:12:12 -0800334 * - row 1
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200335 - field 1.1
336 - field 1.2 with autospan
337
Randy Dunlap0be15112020-12-28 15:12:12 -0800338 * - row 2
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200339 - field 2.1
340 - :rspan:`1` :cspan:`1` field 2.2 - 3.3
341
342 * .. _`last row`:
343
Randy Dunlap0be15112020-12-28 15:12:12 -0800344 - row 3
Markus Heiserdb6ccf22017-03-06 14:09:27 +0100345
Nícolas F. R. A. Pradoc170f2e2020-09-11 13:34:46 +0000346Cross-referencing
347-----------------
348
Nícolas F. R. A. Prado1e013ff2021-01-28 01:01:36 +0000349Cross-referencing from one documentation page to another can be done simply by
350writing the path to the document file, no special syntax required. The path can
351be either absolute or relative. For absolute paths, start it with
352"Documentation/". For example, to cross-reference to this page, all the
353following are valid options, depending on the current document's directory (note
354that the ``.rst`` extension is required)::
Nícolas F. R. A. Pradoc170f2e2020-09-11 13:34:46 +0000355
Nícolas F. R. A. Prado1e013ff2021-01-28 01:01:36 +0000356 See Documentation/doc-guide/sphinx.rst. This always works.
357 Take a look at sphinx.rst, which is at this same directory.
358 Read ../sphinx.rst, which is one directory above.
Nícolas F. R. A. Pradoc170f2e2020-09-11 13:34:46 +0000359
Nícolas F. R. A. Prado1e013ff2021-01-28 01:01:36 +0000360If you want the link to have a different rendered text other than the document's
361title, you need to use Sphinx's ``doc`` role. For example::
Nícolas F. R. A. Pradoc170f2e2020-09-11 13:34:46 +0000362
Nícolas F. R. A. Prado1e013ff2021-01-28 01:01:36 +0000363 See :doc:`my custom link text for document sphinx <sphinx>`.
364
365For most use cases, the former is preferred, as it is cleaner and more suited
366for people reading the source files. If you come across a ``:doc:`` usage that
367isn't adding any value, please feel free to convert it to just the document
368path.
Nícolas F. R. A. Pradoc170f2e2020-09-11 13:34:46 +0000369
370For information on cross-referencing to kernel-doc functions or types, see
371Documentation/doc-guide/kernel-doc.rst.
372
Mauro Carvalho Chehabd43e5ae2017-07-14 13:41:20 -0300373.. _sphinx_kfigure:
Markus Heiserdb6ccf22017-03-06 14:09:27 +0100374
375Figures & Images
376================
377
378If you want to add an image, you should use the ``kernel-figure`` and
379``kernel-image`` directives. E.g. to insert a figure with a scalable
Randy Dunlap0be15112020-12-28 15:12:12 -0800380image format, use SVG (:ref:`svg_image_example`)::
Markus Heiserdb6ccf22017-03-06 14:09:27 +0100381
382 .. kernel-figure:: svg_image.svg
383 :alt: simple SVG image
384
385 SVG image example
386
387.. _svg_image_example:
388
389.. kernel-figure:: svg_image.svg
390 :alt: simple SVG image
391
392 SVG image example
393
Randy Dunlap0be15112020-12-28 15:12:12 -0800394The kernel figure (and image) directive supports **DOT** formatted files, see
Markus Heiserdb6ccf22017-03-06 14:09:27 +0100395
396* DOT: http://graphviz.org/pdf/dotguide.pdf
397* Graphviz: http://www.graphviz.org/content/dot-language
398
399A simple example (:ref:`hello_dot_file`)::
400
401 .. kernel-figure:: hello.dot
402 :alt: hello world
403
404 DOT's hello world example
405
406.. _hello_dot_file:
407
408.. kernel-figure:: hello.dot
409 :alt: hello world
410
411 DOT's hello world example
412
Randy Dunlap0be15112020-12-28 15:12:12 -0800413Embedded *render* markups (or languages) like Graphviz's **DOT** are provided by the
Markus Heiserdb6ccf22017-03-06 14:09:27 +0100414``kernel-render`` directives.::
415
416 .. kernel-render:: DOT
417 :alt: foobar digraph
418 :caption: Embedded **DOT** (Graphviz) code
419
420 digraph foo {
421 "bar" -> "baz";
422 }
423
424How this will be rendered depends on the installed tools. If Graphviz is
Randy Dunlap0be15112020-12-28 15:12:12 -0800425installed, you will see a vector image. If not, the raw markup is inserted as
Markus Heiserdb6ccf22017-03-06 14:09:27 +0100426*literal-block* (:ref:`hello_dot_render`).
427
428.. _hello_dot_render:
429
430.. kernel-render:: DOT
431 :alt: foobar digraph
432 :caption: Embedded **DOT** (Graphviz) code
433
434 digraph foo {
435 "bar" -> "baz";
436 }
437
438The *render* directive has all the options known from the *figure* directive,
439plus option ``caption``. If ``caption`` has a value, a *figure* node is
Randy Dunlap0be15112020-12-28 15:12:12 -0800440inserted. If not, an *image* node is inserted. A ``caption`` is also needed, if
441you want to refer to it (:ref:`hello_svg_render`).
Markus Heiserdb6ccf22017-03-06 14:09:27 +0100442
443Embedded **SVG**::
444
445 .. kernel-render:: SVG
446 :caption: Embedded **SVG** markup
447 :alt: so-nw-arrow
448
449 <?xml version="1.0" encoding="UTF-8"?>
450 <svg xmlns="http://www.w3.org/2000/svg" version="1.1" ...>
451 ...
452 </svg>
453
454.. _hello_svg_render:
455
456.. kernel-render:: SVG
457 :caption: Embedded **SVG** markup
458 :alt: so-nw-arrow
459
460 <?xml version="1.0" encoding="UTF-8"?>
461 <svg xmlns="http://www.w3.org/2000/svg"
462 version="1.1" baseProfile="full" width="70px" height="40px" viewBox="0 0 700 400">
463 <line x1="180" y1="370" x2="500" y2="50" stroke="black" stroke-width="15px"/>
464 <polygon points="585 0 525 25 585 50" transform="rotate(135 525 25)"/>
465 </svg>