Back to home page

OSCL-LXR

 
 

    


0001 .. _sphinxdoc:
0002 
0003 =====================================
0004 Using Sphinx for kernel documentation
0005 =====================================
0006 
0007 The Linux kernel uses `Sphinx`_ to generate pretty documentation from
0008 `reStructuredText`_ files under ``Documentation``. To build the documentation in
0009 HTML or PDF formats, use ``make htmldocs`` or ``make pdfdocs``. The generated
0010 documentation is placed in ``Documentation/output``.
0011 
0012 .. _Sphinx: http://www.sphinx-doc.org/
0013 .. _reStructuredText: http://docutils.sourceforge.net/rst.html
0014 
0015 The reStructuredText files may contain directives to include structured
0016 documentation comments, or kernel-doc comments, from source files. Usually these
0017 are used to describe the functions and types and design of the code. The
0018 kernel-doc comments have some special structure and formatting, but beyond that
0019 they are also treated as reStructuredText.
0020 
0021 Finally, there are thousands of plain text documentation files scattered around
0022 ``Documentation``. Some of these will likely be converted to reStructuredText
0023 over time, but the bulk of them will remain in plain text.
0024 
0025 .. _sphinx_install:
0026 
0027 Sphinx Install
0028 ==============
0029 
0030 The ReST markups currently used by the Documentation/ files are meant to be
0031 built with ``Sphinx`` version 1.7 or higher.
0032 
0033 There's a script that checks for the Sphinx requirements. Please see
0034 :ref:`sphinx-pre-install` for further details.
0035 
0036 Most distributions are shipped with Sphinx, but its toolchain is fragile,
0037 and it is not uncommon that upgrading it or some other Python packages
0038 on your machine would cause the documentation build to break.
0039 
0040 A way to avoid that is to use a different version than the one shipped
0041 with your distributions. In order to do so, it is recommended to install
0042 Sphinx inside a virtual environment, using ``virtualenv-3``
0043 or ``virtualenv``, depending on how your distribution packaged Python 3.
0044 
0045 .. note::
0046 
0047    #) It is recommended to use the RTD theme for html output. Depending
0048       on the Sphinx version, it should be installed separately,
0049       with ``pip install sphinx_rtd_theme``.
0050 
0051    #) Some ReST pages contain math expressions. Due to the way Sphinx works,
0052       those expressions are written using LaTeX notation. It needs texlive
0053       installed with amsfonts and amsmath in order to evaluate them.
0054 
0055 In summary, if you want to install Sphinx version 2.4.4, you should do::
0056 
0057        $ virtualenv sphinx_2.4.4
0058        $ . sphinx_2.4.4/bin/activate
0059        (sphinx_2.4.4) $ pip install -r Documentation/sphinx/requirements.txt
0060 
0061 After running ``. sphinx_2.4.4/bin/activate``, the prompt will change,
0062 in order to indicate that you're using the new environment. If you
0063 open a new shell, you need to rerun this command to enter again at
0064 the virtual environment before building the documentation.
0065 
0066 Image output
0067 ------------
0068 
0069 The kernel documentation build system contains an extension that
0070 handles images on both GraphViz and SVG formats (see
0071 :ref:`sphinx_kfigure`).
0072 
0073 For it to work, you need to install both GraphViz and ImageMagick
0074 packages. If those packages are not installed, the build system will
0075 still build the documentation, but won't include any images at the
0076 output.
0077 
0078 PDF and LaTeX builds
0079 --------------------
0080 
0081 Such builds are currently supported only with Sphinx versions 2.4 and higher.
0082 
0083 For PDF and LaTeX output, you'll also need ``XeLaTeX`` version 3.14159265.
0084 
0085 Depending on the distribution, you may also need to install a series of
0086 ``texlive`` packages that provide the minimal set of functionalities
0087 required for ``XeLaTeX`` to work.
0088 
0089 .. _sphinx-pre-install:
0090 
0091 Checking for Sphinx dependencies
0092 --------------------------------
0093 
0094 There's a script that automatically check for Sphinx dependencies. If it can
0095 recognize your distribution, it will also give a hint about the install
0096 command line options for your distro::
0097 
0098         $ ./scripts/sphinx-pre-install
0099         Checking if the needed tools for Fedora release 26 (Twenty Six) are available
0100         Warning: better to also install "texlive-luatex85".
0101         You should run:
0102 
0103                 sudo dnf install -y texlive-luatex85
0104                 /usr/bin/virtualenv sphinx_2.4.4
0105                 . sphinx_2.4.4/bin/activate
0106                 pip install -r Documentation/sphinx/requirements.txt
0107 
0108         Can't build as 1 mandatory dependency is missing at ./scripts/sphinx-pre-install line 468.
0109 
0110 By default, it checks all the requirements for both html and PDF, including
0111 the requirements for images, math expressions and LaTeX build, and assumes
0112 that a virtual Python environment will be used. The ones needed for html
0113 builds are assumed to be mandatory; the others to be optional.
0114 
0115 It supports two optional parameters:
0116 
0117 ``--no-pdf``
0118         Disable checks for PDF;
0119 
0120 ``--no-virtualenv``
0121         Use OS packaging for Sphinx instead of Python virtual environment.
0122 
0123 
0124 Sphinx Build
0125 ============
0126 
0127 The usual way to generate the documentation is to run ``make htmldocs`` or
0128 ``make pdfdocs``. There are also other formats available: see the documentation
0129 section of ``make help``. The generated documentation is placed in
0130 format-specific subdirectories under ``Documentation/output``.
0131 
0132 To generate documentation, Sphinx (``sphinx-build``) must obviously be
0133 installed. For prettier HTML output, the Read the Docs Sphinx theme
0134 (``sphinx_rtd_theme``) is used if available. For PDF output you'll also need
0135 ``XeLaTeX`` and ``convert(1)`` from ImageMagick
0136 (https://www.imagemagick.org).\ [#ink]_
0137 All of these are widely available and packaged in distributions.
0138 
0139 To pass extra options to Sphinx, you can use the ``SPHINXOPTS`` make
0140 variable. For example, use ``make SPHINXOPTS=-v htmldocs`` to get more verbose
0141 output.
0142 
0143 It is also possible to pass an extra DOCS_CSS overlay file, in order to customize
0144 the html layout, by using the ``DOCS_CSS`` make variable.
0145 
0146 By default, the build will try to use the Read the Docs sphinx theme:
0147 
0148     https://github.com/readthedocs/sphinx_rtd_theme
0149 
0150 If the theme is not available, it will fall-back to the classic one.
0151 
0152 The Sphinx theme can be overridden by using the ``DOCS_THEME`` make variable.
0153 
0154 There is another make variable ``SPHINXDIRS``, which is useful when test
0155 building a subset of documentation.  For example, you can build documents
0156 under ``Documentation/doc-guide`` by running
0157 ``make SPHINXDIRS=doc-guide htmldocs``.
0158 The documentation section of ``make help`` will show you the list of
0159 subdirectories you can specify.
0160 
0161 To remove the generated documentation, run ``make cleandocs``.
0162 
0163 .. [#ink] Having ``inkscape(1)`` from Inkscape (https://inkscape.org)
0164           as well would improve the quality of images embedded in PDF
0165           documents, especially for kernel releases 5.18 and later.
0166 
0167 Writing Documentation
0168 =====================
0169 
0170 Adding new documentation can be as simple as:
0171 
0172 1. Add a new ``.rst`` file somewhere under ``Documentation``.
0173 2. Refer to it from the Sphinx main `TOC tree`_ in ``Documentation/index.rst``.
0174 
0175 .. _TOC tree: http://www.sphinx-doc.org/en/stable/markup/toctree.html
0176 
0177 This is usually good enough for simple documentation (like the one you're
0178 reading right now), but for larger documents it may be advisable to create a
0179 subdirectory (or use an existing one). For example, the graphics subsystem
0180 documentation is under ``Documentation/gpu``, split to several ``.rst`` files,
0181 and has a separate ``index.rst`` (with a ``toctree`` of its own) referenced from
0182 the main index.
0183 
0184 See the documentation for `Sphinx`_ and `reStructuredText`_ on what you can do
0185 with them. In particular, the Sphinx `reStructuredText Primer`_ is a good place
0186 to get started with reStructuredText. There are also some `Sphinx specific
0187 markup constructs`_.
0188 
0189 .. _reStructuredText Primer: http://www.sphinx-doc.org/en/stable/rest.html
0190 .. _Sphinx specific markup constructs: http://www.sphinx-doc.org/en/stable/markup/index.html
0191 
0192 Specific guidelines for the kernel documentation
0193 ------------------------------------------------
0194 
0195 Here are some specific guidelines for the kernel documentation:
0196 
0197 * Please don't go overboard with reStructuredText markup. Keep it
0198   simple. For the most part the documentation should be plain text with
0199   just enough consistency in formatting that it can be converted to
0200   other formats.
0201 
0202 * Please keep the formatting changes minimal when converting existing
0203   documentation to reStructuredText.
0204 
0205 * Also update the content, not just the formatting, when converting
0206   documentation.
0207 
0208 * Please stick to this order of heading adornments:
0209 
0210   1. ``=`` with overline for document title::
0211 
0212        ==============
0213        Document title
0214        ==============
0215 
0216   2. ``=`` for chapters::
0217 
0218        Chapters
0219        ========
0220 
0221   3. ``-`` for sections::
0222 
0223        Section
0224        -------
0225 
0226   4. ``~`` for subsections::
0227 
0228        Subsection
0229        ~~~~~~~~~~
0230 
0231   Although RST doesn't mandate a specific order ("Rather than imposing a fixed
0232   number and order of section title adornment styles, the order enforced will be
0233   the order as encountered."), having the higher levels the same overall makes
0234   it easier to follow the documents.
0235 
0236 * For inserting fixed width text blocks (for code examples, use case
0237   examples, etc.), use ``::`` for anything that doesn't really benefit
0238   from syntax highlighting, especially short snippets. Use
0239   ``.. code-block:: <language>`` for longer code blocks that benefit
0240   from highlighting. For a short snippet of code embedded in the text, use \`\`.
0241 
0242 
0243 the C domain
0244 ------------
0245 
0246 The **Sphinx C Domain** (name c) is suited for documentation of C API. E.g. a
0247 function prototype:
0248 
0249 .. code-block:: rst
0250 
0251     .. c:function:: int ioctl( int fd, int request )
0252 
0253 The C domain of the kernel-doc has some additional features. E.g. you can
0254 *rename* the reference name of a function with a common name like ``open`` or
0255 ``ioctl``:
0256 
0257 .. code-block:: rst
0258 
0259      .. c:function:: int ioctl( int fd, int request )
0260         :name: VIDIOC_LOG_STATUS
0261 
0262 The func-name (e.g. ioctl) remains in the output but the ref-name changed from
0263 ``ioctl`` to ``VIDIOC_LOG_STATUS``. The index entry for this function is also
0264 changed to ``VIDIOC_LOG_STATUS``.
0265 
0266 Please note that there is no need to use ``c:func:`` to generate cross
0267 references to function documentation.  Due to some Sphinx extension magic,
0268 the documentation build system will automatically turn a reference to
0269 ``function()`` into a cross reference if an index entry for the given
0270 function name exists.  If you see ``c:func:`` use in a kernel document,
0271 please feel free to remove it.
0272 
0273 
0274 list tables
0275 -----------
0276 
0277 The list-table formats can be useful for tables that are not easily laid
0278 out in the usual Sphinx ASCII-art formats.  These formats are nearly
0279 impossible for readers of the plain-text documents to understand, though,
0280 and should be avoided in the absence of a strong justification for their
0281 use.
0282 
0283 The ``flat-table`` is a double-stage list similar to the ``list-table`` with
0284 some additional features:
0285 
0286 * column-span: with the role ``cspan`` a cell can be extended through
0287   additional columns
0288 
0289 * row-span: with the role ``rspan`` a cell can be extended through
0290   additional rows
0291 
0292 * auto span rightmost cell of a table row over the missing cells on the right
0293   side of that table-row.  With Option ``:fill-cells:`` this behavior can
0294   changed from *auto span* to *auto fill*, which automatically inserts (empty)
0295   cells instead of spanning the last cell.
0296 
0297 options:
0298 
0299 * ``:header-rows:``   [int] count of header rows
0300 * ``:stub-columns:``  [int] count of stub columns
0301 * ``:widths:``        [[int] [int] ... ] widths of columns
0302 * ``:fill-cells:``    instead of auto-spanning missing cells, insert missing cells
0303 
0304 roles:
0305 
0306 * ``:cspan:`` [int] additional columns (*morecols*)
0307 * ``:rspan:`` [int] additional rows (*morerows*)
0308 
0309 The example below shows how to use this markup.  The first level of the staged
0310 list is the *table-row*. In the *table-row* there is only one markup allowed,
0311 the list of the cells in this *table-row*. Exceptions are *comments* ( ``..`` )
0312 and *targets* (e.g. a ref to ``:ref:`last row <last row>``` / :ref:`last row
0313 <last row>`).
0314 
0315 .. code-block:: rst
0316 
0317    .. flat-table:: table title
0318       :widths: 2 1 1 3
0319 
0320       * - head col 1
0321         - head col 2
0322         - head col 3
0323         - head col 4
0324 
0325       * - row 1
0326         - field 1.1
0327         - field 1.2 with autospan
0328 
0329       * - row 2
0330         - field 2.1
0331         - :rspan:`1` :cspan:`1` field 2.2 - 3.3
0332 
0333       * .. _`last row`:
0334 
0335         - row 3
0336 
0337 Rendered as:
0338 
0339    .. flat-table:: table title
0340       :widths: 2 1 1 3
0341 
0342       * - head col 1
0343         - head col 2
0344         - head col 3
0345         - head col 4
0346 
0347       * - row 1
0348         - field 1.1
0349         - field 1.2 with autospan
0350 
0351       * - row 2
0352         - field 2.1
0353         - :rspan:`1` :cspan:`1` field 2.2 - 3.3
0354 
0355       * .. _`last row`:
0356 
0357         - row 3
0358 
0359 Cross-referencing
0360 -----------------
0361 
0362 Cross-referencing from one documentation page to another can be done simply by
0363 writing the path to the document file, no special syntax required. The path can
0364 be either absolute or relative. For absolute paths, start it with
0365 "Documentation/". For example, to cross-reference to this page, all the
0366 following are valid options, depending on the current document's directory (note
0367 that the ``.rst`` extension is required)::
0368 
0369     See Documentation/doc-guide/sphinx.rst. This always works.
0370     Take a look at sphinx.rst, which is at this same directory.
0371     Read ../sphinx.rst, which is one directory above.
0372 
0373 If you want the link to have a different rendered text other than the document's
0374 title, you need to use Sphinx's ``doc`` role. For example::
0375 
0376     See :doc:`my custom link text for document sphinx <sphinx>`.
0377 
0378 For most use cases, the former is preferred, as it is cleaner and more suited
0379 for people reading the source files. If you come across a ``:doc:`` usage that
0380 isn't adding any value, please feel free to convert it to just the document
0381 path.
0382 
0383 For information on cross-referencing to kernel-doc functions or types, see
0384 Documentation/doc-guide/kernel-doc.rst.
0385 
0386 .. _sphinx_kfigure:
0387 
0388 Figures & Images
0389 ================
0390 
0391 If you want to add an image, you should use the ``kernel-figure`` and
0392 ``kernel-image`` directives. E.g. to insert a figure with a scalable
0393 image format, use SVG (:ref:`svg_image_example`)::
0394 
0395     .. kernel-figure::  svg_image.svg
0396        :alt:    simple SVG image
0397 
0398        SVG image example
0399 
0400 .. _svg_image_example:
0401 
0402 .. kernel-figure::  svg_image.svg
0403    :alt:    simple SVG image
0404 
0405    SVG image example
0406 
0407 The kernel figure (and image) directive supports **DOT** formatted files, see
0408 
0409 * DOT: http://graphviz.org/pdf/dotguide.pdf
0410 * Graphviz: http://www.graphviz.org/content/dot-language
0411 
0412 A simple example (:ref:`hello_dot_file`)::
0413 
0414   .. kernel-figure::  hello.dot
0415      :alt:    hello world
0416 
0417      DOT's hello world example
0418 
0419 .. _hello_dot_file:
0420 
0421 .. kernel-figure::  hello.dot
0422    :alt:    hello world
0423 
0424    DOT's hello world example
0425 
0426 Embedded *render* markups (or languages) like Graphviz's **DOT** are provided by the
0427 ``kernel-render`` directives.::
0428 
0429   .. kernel-render:: DOT
0430      :alt: foobar digraph
0431      :caption: Embedded **DOT** (Graphviz) code
0432 
0433      digraph foo {
0434       "bar" -> "baz";
0435      }
0436 
0437 How this will be rendered depends on the installed tools. If Graphviz is
0438 installed, you will see a vector image. If not, the raw markup is inserted as
0439 *literal-block* (:ref:`hello_dot_render`).
0440 
0441 .. _hello_dot_render:
0442 
0443 .. kernel-render:: DOT
0444    :alt: foobar digraph
0445    :caption: Embedded **DOT** (Graphviz) code
0446 
0447    digraph foo {
0448       "bar" -> "baz";
0449    }
0450 
0451 The *render* directive has all the options known from the *figure* directive,
0452 plus option ``caption``.  If ``caption`` has a value, a *figure* node is
0453 inserted. If not, an *image* node is inserted. A ``caption`` is also needed, if
0454 you want to refer to it (:ref:`hello_svg_render`).
0455 
0456 Embedded **SVG**::
0457 
0458   .. kernel-render:: SVG
0459      :caption: Embedded **SVG** markup
0460      :alt: so-nw-arrow
0461 
0462      <?xml version="1.0" encoding="UTF-8"?>
0463      <svg xmlns="http://www.w3.org/2000/svg" version="1.1" ...>
0464         ...
0465      </svg>
0466 
0467 .. _hello_svg_render:
0468 
0469 .. kernel-render:: SVG
0470    :caption: Embedded **SVG** markup
0471    :alt: so-nw-arrow
0472 
0473    <?xml version="1.0" encoding="UTF-8"?>
0474    <svg xmlns="http://www.w3.org/2000/svg"
0475      version="1.1" baseProfile="full" width="70px" height="40px" viewBox="0 0 700 400">
0476    <line x1="180" y1="370" x2="500" y2="50" stroke="black" stroke-width="15px"/>
0477    <polygon points="585 0 525 25 585 50" transform="rotate(135 525 25)"/>
0478    </svg>