Introduction¶
The Linux kernel usesSphinx to generate pretty documentation fromreStructuredText files underDocumentation. To build the documentation inHTML or PDF formats, usemakehtmldocs ormakepdfdocs. The generateddocumentation is placed inDocumentation/output.
The reStructuredText files may contain directives to include structureddocumentation comments, or kernel-doc comments, from source files. Usually theseare used to describe the functions and types and design of the code. Thekernel-doc comments have some special structure and formatting, but beyond thatthey are also treated as reStructuredText.
Finally, there are thousands of plain text documentation files scattered aroundDocumentation. Some of these will likely be converted to reStructuredTextover time, but the bulk of them will remain in plain text.
Sphinx Install¶
The ReST markups currently used by the Documentation/ files are meant to bebuilt withSphinx version 1.3 or higher.
There’s a script that checks for the Sphinx requirements. Please seeChecking for Sphinx dependencies for further details.
Most distributions are shipped with Sphinx, but its toolchain is fragile,and it is not uncommon that upgrading it or some other Python packageson your machine would cause the documentation build to break.
A way to avoid that is to use a different version than the one shippedwith your distributions. In order to do so, it is recommended to installSphinx inside a virtual environment, usingvirtualenv-3orvirtualenv, depending on how your distribution packaged Python 3.
Note
- Sphinx versions below 1.5 don’t work properly with Python’sdocutils version 0.13.1 or higher. So, if you’re willing to usethose versions, you should run
pipinstall'docutils==0.12'. - It is recommended to use the RTD theme for html output. Dependingon the Sphinx version, it should be installed in separate,with
pipinstallsphinx_rtd_theme. - Some ReST pages contain math expressions. Due to the way Sphinx work,those expressions are written using LaTeX notation. It needs texliveinstalled with amdfonts and amsmath in order to evaluate them.
In summary, if you want to install Sphinx version 1.7.9, you should do:
$ virtualenv sphinx_1.7.9$ . sphinx_1.7.9/bin/activate(sphinx_1.7.9) $ pip install -r Documentation/sphinx/requirements.txt
After running.sphinx_1.7.9/bin/activate, the prompt will change,in order to indicate that you’re using the new environment. If youopen a new shell, you need to rerun this command to enter again atthe virtual environment before building the documentation.
Image output¶
The kernel documentation build system contains an extension thathandles images on both GraphViz and SVG formats (seeFigures & Images).
For it to work, you need to install both GraphViz and ImageMagickpackages. If those packages are not installed, the build system willstill build the documentation, but won’t include any images at theoutput.
PDF and LaTeX builds¶
Such builds are currently supported only with Sphinx versions 1.4 and higher.
For PDF and LaTeX output, you’ll also needXeLaTeX version 3.14159265.
Depending on the distribution, you may also need to install a series oftexlive packages that provide the minimal set of functionalitiesrequired forXeLaTeX to work.
Checking for Sphinx dependencies¶
There’s a script that automatically check for Sphinx dependencies. If it canrecognize your distribution, it will also give a hint about the installcommand line options for your distro:
$ ./scripts/sphinx-pre-installChecking if the needed tools for Fedora release 26 (Twenty Six) are availableWarning: better to also install "texlive-luatex85".You should run: sudo dnf install -y texlive-luatex85 /usr/bin/virtualenv sphinx_1.7.9 . sphinx_1.7.9/bin/activate pip install -r Documentation/sphinx/requirements.txtCan't build as 1 mandatory dependency is missing at ./scripts/sphinx-pre-install line 468.
By default, it checks all the requirements for both html and PDF, includingthe requirements for images, math expressions and LaTeX build, and assumesthat a virtual Python environment will be used. The ones needed for htmlbuilds are assumed to be mandatory; the others to be optional.
It supports two optional parameters:
--no-pdf- Disable checks for PDF;
--no-virtualenv- Use OS packaging for Sphinx instead of Python virtual environment.
Sphinx Build¶
The usual way to generate the documentation is to runmakehtmldocs ormakepdfdocs. There are also other formats available, see the documentationsection ofmakehelp. The generated documentation is placed informat-specific subdirectories underDocumentation/output.
To generate documentation, Sphinx (sphinx-build) must obviously beinstalled. For prettier HTML output, the Read the Docs Sphinx theme(sphinx_rtd_theme) is used if available. For PDF output you’ll also needXeLaTeX andconvert(1) from ImageMagick (https://www.imagemagick.org).All of these are widely available and packaged in distributions.
To pass extra options to Sphinx, you can use theSPHINXOPTS makevariable. For example, usemakeSPHINXOPTS=-vhtmldocs to get more verboseoutput.
To remove the generated documentation, runmakecleandocs.
Writing Documentation¶
Adding new documentation can be as simple as:
- Add a new
.rstfile somewhere underDocumentation. - Refer to it from the Sphinx mainTOC tree in
Documentation/index.rst.
This is usually good enough for simple documentation (like the one you’rereading right now), but for larger documents it may be advisable to create asubdirectory (or use an existing one). For example, the graphics subsystemdocumentation is underDocumentation/gpu, split to several.rst files,and has a separateindex.rst (with atoctree of its own) referenced fromthe main index.
See the documentation forSphinx andreStructuredText on what you can dowith them. In particular, the SphinxreStructuredText Primer is a good placeto get started with reStructuredText. There are also someSphinx specificmarkup constructs.
Specific guidelines for the kernel documentation¶
Here are some specific guidelines for the kernel documentation:
Please don’t go overboard with reStructuredText markup. Keep itsimple. For the most part the documentation should be plain text withjust enough consistency in formatting that it can be converted toother formats.
Please keep the formatting changes minimal when converting existingdocumentation to reStructuredText.
Also update the content, not just the formatting, when convertingdocumentation.
Please stick to this order of heading adornments:
=with overline for document title:==============Document title==============
=for chapters:Chapters========
-for sections:Section-------
~for subsections:Subsection~~~~~~~~~~
Although RST doesn’t mandate a specific order (“Rather than imposing a fixednumber and order of section title adornment styles, the order enforced will bethe order as encountered.”), having the higher levels the same overall makesit easier to follow the documents.
For inserting fixed width text blocks (for code examples, use caseexamples, etc.), use
::for anything that doesn’t really benefitfrom syntax highlighting, especially short snippets. Use..code-block::<language>for longer code blocks that benefitfrom highlighting. For a short snippet of code embedded in the text, use ``.
the C domain¶
TheSphinx C Domain (name c) is suited for documentation of C API. E.g. afunction prototype:
..c:function:: int ioctl( int fd, int request )
The C domain of the kernel-doc has some additional features. E.g. you canrename the reference name of a function with a common name likeopen orioctl:
..c:function:: int ioctl( int fd, int request ):name:VIDIOC_LOG_STATUS
The func-name (e.g. ioctl) remains in the output but the ref-name changed fromioctl toVIDIOC_LOG_STATUS. The index entry for this function is alsochanged toVIDIOC_LOG_STATUS.
Please note that there is no need to usec:func: to generate crossreferences to function documentation. Due to some Sphinx extension magic,the documentation build system will automatically turn a reference tofunction() into a cross reference if an index entry for the givenfunction name exists. If you seec:func: use in a kernel document,please feel free to remove it.
list tables¶
We recommend the use oflist table formats. Thelist table formats aredouble-stage lists. Compared to the ASCII-art they might not be ascomfortable forreaders of the text files. Their advantage is that they are easy tocreate or modify and that the diff of a modification is much more meaningful,because it is limited to the modified content.
Theflat-table is a double-stage list similar to thelist-table withsome additional features:
- column-span: with the role
cspana cell can be extended throughadditional columns - row-span: with the role
rspana cell can be extended throughadditional rows - auto span rightmost cell of a table row over the missing cells on the rightside of that table-row. With Option
:fill-cells:this behavior canchanged fromauto span toauto fill, which automatically inserts (empty)cells instead of spanning the last cell.
options:
:header-rows:[int] count of header rows:stub-columns:[int] count of stub columns:widths:[[int] [int] … ] widths of columns:fill-cells:instead of auto-spanning missing cells, insert missing cells
roles:
:cspan:[int] additional columns (morecols):rspan:[int] additional rows (morerows)
The example below shows how to use this markup. The first level of the stagedlist is thetable-row. In thetable-row there is only one markup allowed,the list of the cells in thistable-row. Exceptions arecomments (.. )andtargets (e.g. a ref to:ref:`lastrow<lastrow>` /last row).
..flat-table:: table title:widths:2 1 1 3* - head col 1- head col 2- head col 3- head col 4* - column 1- field 1.1- field 1.2 with autospan* - column 2- field 2.1-:rspan:`1`:cspan:`1` field 2.2 - 3.3* .. _`last row`:- column 3
Rendered as:
table title¶ head col 1 head col 2 head col 3 head col 4 column 1 field 1.1 field 1.2 with autospan column 2 field 2.1 field 2.2 - 3.3 column 3
Figures & Images¶
If you want to add an image, you should use thekernel-figure andkernel-image directives. E.g. to insert a figure with a scalableimage format use SVG (SVG image example):
.. kernel-figure:: svg_image.svg :alt: simple SVG image SVG image example
SVG image example
The kernel figure (and image) directive supportDOT formated files, see
A simple example (DOT’s hello world example):
.. kernel-figure:: hello.dot :alt: hello world DOT's hello world example
DOT’s hello world example
Embedrender markups (or languages) like Graphviz’sDOT is provided by thekernel-render directives.:
.. kernel-render:: DOT :alt: foobar digraph :caption: Embedded **DOT** (Graphviz) code digraph foo { "bar" -> "baz"; }How this will be rendered depends on the installed tools. If Graphviz isinstalled, you will see an vector image. If not the raw markup is inserted asliteral-block (Embedded DOT (Graphviz) code).
EmbeddedDOT (Graphviz) code
Therender directive has all the options known from thefigure directive,plus optioncaption. Ifcaption has a value, afigure node isinserted. If not, aimage node is inserted. Acaption is also needed, ifyou want to refer it (Embedded SVG markup).
EmbeddedSVG:
.. kernel-render:: SVG :caption: Embedded **SVG** markup :alt: so-nw-arrow <?xml version="1.0" encoding="UTF-8"?> <svg xmlns="http://www.w3.org/2000/svg" version="1.1" ...> ... </svg>
EmbeddedSVG markup