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