1.. _sphinxdoc: 2 3Introduction 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 20Finally, 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 24.. _sphinx_install: 25 26Sphinx Install 27============== 28 29The ReST markups currently used by the Documentation/ files are meant to be 30built with ``Sphinx`` version 1.7 or higher. 31 32There's a script that checks for the Sphinx requirements. Please see 33:ref:`sphinx-pre-install` for further details. 34 35Most 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 39A 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 41Sphinx inside a virtual environment, using ``virtualenv-3`` 42or ``virtualenv``, depending on how your distribution packaged Python 3. 43 44.. note:: 45 46 #) It is recommended to use the RTD theme for html output. Depending 47 on the Sphinx version, it should be installed separately, 48 with ``pip install sphinx_rtd_theme``. 49 50 #) Some ReST pages contain math expressions. Due to the way Sphinx works, 51 those expressions are written using LaTeX notation. It needs texlive 52 installed with amsfonts and amsmath in order to evaluate them. 53 54In summary, if you want to install Sphinx version 2.4.4, you should do:: 55 56 $ virtualenv sphinx_2.4.4 57 $ . sphinx_2.4.4/bin/activate 58 (sphinx_2.4.4) $ pip install -r Documentation/sphinx/requirements.txt 59 60After running ``. sphinx_2.4.4/bin/activate``, the prompt will change, 61in 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 65Image 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 77PDF and LaTeX builds 78-------------------- 79 80Such builds are currently supported only with Sphinx versions 2.4 and higher. 81 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 86required 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 103 /usr/bin/virtualenv sphinx_2.4.4 104 . sphinx_2.4.4/bin/activate 105 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 122 123Sphinx Build 124============ 125 126The usual way to generate the documentation is to run ``make htmldocs`` or 127``make pdfdocs``. There are also other formats available: see the documentation 128section 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 133(``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. 136 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 141To remove the generated documentation, run ``make cleandocs``. 142 143Writing Documentation 144===================== 145 146Adding new documentation can be as simple as: 147 1481. Add a new ``.rst`` file somewhere under ``Documentation``. 1492. Refer to it from the Sphinx main `TOC tree`_ in ``Documentation/index.rst``. 150 151.. _TOC tree: http://www.sphinx-doc.org/en/stable/markup/toctree.html 152 153This is usually good enough for simple documentation (like the one you're 154reading right now), but for larger documents it may be advisable to create a 155subdirectory (or use an existing one). For example, the graphics subsystem 156documentation is under ``Documentation/gpu``, split to several ``.rst`` files, 157and has a separate ``index.rst`` (with a ``toctree`` of its own) referenced from 158the main index. 159 160See the documentation for `Sphinx`_ and `reStructuredText`_ on what you can do 161with them. In particular, the Sphinx `reStructuredText Primer`_ is a good place 162to get started with reStructuredText. There are also some `Sphinx specific 163markup constructs`_. 164 165.. _reStructuredText Primer: http://www.sphinx-doc.org/en/stable/rest.html 166.. _Sphinx specific markup constructs: http://www.sphinx-doc.org/en/stable/markup/index.html 167 168Specific guidelines for the kernel documentation 169------------------------------------------------ 170 171Here are some specific guidelines for the kernel documentation: 172 173* Please don't go overboard with reStructuredText markup. Keep it 174 simple. For the most part the documentation should be plain text with 175 just enough consistency in formatting that it can be converted to 176 other formats. 177 178* Please keep the formatting changes minimal when converting existing 179 documentation to reStructuredText. 180 181* Also update the content, not just the formatting, when converting 182 documentation. 183 184* Please stick to this order of heading adornments: 185 186 1. ``=`` with overline for document title:: 187 188 ============== 189 Document title 190 ============== 191 192 2. ``=`` for chapters:: 193 194 Chapters 195 ======== 196 197 3. ``-`` for sections:: 198 199 Section 200 ------- 201 202 4. ``~`` for subsections:: 203 204 Subsection 205 ~~~~~~~~~~ 206 207 Although RST doesn't mandate a specific order ("Rather than imposing a fixed 208 number and order of section title adornment styles, the order enforced will be 209 the order as encountered."), having the higher levels the same overall makes 210 it easier to follow the documents. 211 212* For inserting fixed width text blocks (for code examples, use case 213 examples, etc.), use ``::`` for anything that doesn't really benefit 214 from syntax highlighting, especially short snippets. Use 215 ``.. code-block:: <language>`` for longer code blocks that benefit 216 from highlighting. For a short snippet of code embedded in the text, use \`\`. 217 218 219the C domain 220------------ 221 222The **Sphinx C Domain** (name c) is suited for documentation of C API. E.g. a 223function prototype: 224 225.. code-block:: rst 226 227 .. c:function:: int ioctl( int fd, int request ) 228 229The C domain of the kernel-doc has some additional features. E.g. you can 230*rename* the reference name of a function with a common name like ``open`` or 231``ioctl``: 232 233.. code-block:: rst 234 235 .. c:function:: int ioctl( int fd, int request ) 236 :name: VIDIOC_LOG_STATUS 237 238The func-name (e.g. ioctl) remains in the output but the ref-name changed from 239``ioctl`` to ``VIDIOC_LOG_STATUS``. The index entry for this function is also 240changed to ``VIDIOC_LOG_STATUS``. 241 242Please note that there is no need to use ``c:func:`` to generate cross 243references to function documentation. Due to some Sphinx extension magic, 244the documentation build system will automatically turn a reference to 245``function()`` into a cross reference if an index entry for the given 246function name exists. If you see ``c:func:`` use in a kernel document, 247please feel free to remove it. 248 249 250list tables 251----------- 252 253We recommend the use of *list table* formats. The *list table* formats are 254double-stage lists. Compared to the ASCII-art they might not be as 255comfortable for 256readers of the text files. Their advantage is that they are easy to 257create or modify and that the diff of a modification is much more meaningful, 258because it is limited to the modified content. 259 260The ``flat-table`` is a double-stage list similar to the ``list-table`` with 261some additional features: 262 263* column-span: with the role ``cspan`` a cell can be extended through 264 additional columns 265 266* row-span: with the role ``rspan`` a cell can be extended through 267 additional rows 268 269* auto span rightmost cell of a table row over the missing cells on the right 270 side of that table-row. With Option ``:fill-cells:`` this behavior can 271 changed from *auto span* to *auto fill*, which automatically inserts (empty) 272 cells instead of spanning the last cell. 273 274options: 275 276* ``:header-rows:`` [int] count of header rows 277* ``:stub-columns:`` [int] count of stub columns 278* ``:widths:`` [[int] [int] ... ] widths of columns 279* ``:fill-cells:`` instead of auto-spanning missing cells, insert missing cells 280 281roles: 282 283* ``:cspan:`` [int] additional columns (*morecols*) 284* ``:rspan:`` [int] additional rows (*morerows*) 285 286The example below shows how to use this markup. The first level of the staged 287list is the *table-row*. In the *table-row* there is only one markup allowed, 288the list of the cells in this *table-row*. Exceptions are *comments* ( ``..`` ) 289and *targets* (e.g. a ref to ``:ref:`last row <last row>``` / :ref:`last row 290<last row>`). 291 292.. code-block:: rst 293 294 .. flat-table:: table title 295 :widths: 2 1 1 3 296 297 * - head col 1 298 - head col 2 299 - head col 3 300 - head col 4 301 302 * - row 1 303 - field 1.1 304 - field 1.2 with autospan 305 306 * - row 2 307 - field 2.1 308 - :rspan:`1` :cspan:`1` field 2.2 - 3.3 309 310 * .. _`last row`: 311 312 - row 3 313 314Rendered as: 315 316 .. flat-table:: table title 317 :widths: 2 1 1 3 318 319 * - head col 1 320 - head col 2 321 - head col 3 322 - head col 4 323 324 * - row 1 325 - field 1.1 326 - field 1.2 with autospan 327 328 * - row 2 329 - field 2.1 330 - :rspan:`1` :cspan:`1` field 2.2 - 3.3 331 332 * .. _`last row`: 333 334 - row 3 335 336Cross-referencing 337----------------- 338 339Cross-referencing from one documentation page to another can be done simply by 340writing the path to the document file, no special syntax required. The path can 341be either absolute or relative. For absolute paths, start it with 342"Documentation/". For example, to cross-reference to this page, all the 343following are valid options, depending on the current document's directory (note 344that the ``.rst`` extension is required):: 345 346 See Documentation/doc-guide/sphinx.rst. This always works. 347 Take a look at sphinx.rst, which is at this same directory. 348 Read ../sphinx.rst, which is one directory above. 349 350If you want the link to have a different rendered text other than the document's 351title, you need to use Sphinx's ``doc`` role. For example:: 352 353 See :doc:`my custom link text for document sphinx <sphinx>`. 354 355For most use cases, the former is preferred, as it is cleaner and more suited 356for people reading the source files. If you come across a ``:doc:`` usage that 357isn't adding any value, please feel free to convert it to just the document 358path. 359 360For information on cross-referencing to kernel-doc functions or types, see 361Documentation/doc-guide/kernel-doc.rst. 362 363.. _sphinx_kfigure: 364 365Figures & Images 366================ 367 368If you want to add an image, you should use the ``kernel-figure`` and 369``kernel-image`` directives. E.g. to insert a figure with a scalable 370image format, use SVG (:ref:`svg_image_example`):: 371 372 .. kernel-figure:: svg_image.svg 373 :alt: simple SVG image 374 375 SVG image example 376 377.. _svg_image_example: 378 379.. kernel-figure:: svg_image.svg 380 :alt: simple SVG image 381 382 SVG image example 383 384The kernel figure (and image) directive supports **DOT** formatted files, see 385 386* DOT: http://graphviz.org/pdf/dotguide.pdf 387* Graphviz: http://www.graphviz.org/content/dot-language 388 389A simple example (:ref:`hello_dot_file`):: 390 391 .. kernel-figure:: hello.dot 392 :alt: hello world 393 394 DOT's hello world example 395 396.. _hello_dot_file: 397 398.. kernel-figure:: hello.dot 399 :alt: hello world 400 401 DOT's hello world example 402 403Embedded *render* markups (or languages) like Graphviz's **DOT** are provided by the 404``kernel-render`` directives.:: 405 406 .. kernel-render:: DOT 407 :alt: foobar digraph 408 :caption: Embedded **DOT** (Graphviz) code 409 410 digraph foo { 411 "bar" -> "baz"; 412 } 413 414How this will be rendered depends on the installed tools. If Graphviz is 415installed, you will see a vector image. If not, the raw markup is inserted as 416*literal-block* (:ref:`hello_dot_render`). 417 418.. _hello_dot_render: 419 420.. kernel-render:: DOT 421 :alt: foobar digraph 422 :caption: Embedded **DOT** (Graphviz) code 423 424 digraph foo { 425 "bar" -> "baz"; 426 } 427 428The *render* directive has all the options known from the *figure* directive, 429plus option ``caption``. If ``caption`` has a value, a *figure* node is 430inserted. If not, an *image* node is inserted. A ``caption`` is also needed, if 431you want to refer to it (:ref:`hello_svg_render`). 432 433Embedded **SVG**:: 434 435 .. kernel-render:: SVG 436 :caption: Embedded **SVG** markup 437 :alt: so-nw-arrow 438 439 <?xml version="1.0" encoding="UTF-8"?> 440 <svg xmlns="http://www.w3.org/2000/svg" version="1.1" ...> 441 ... 442 </svg> 443 444.. _hello_svg_render: 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" 452 version="1.1" baseProfile="full" width="70px" height="40px" viewBox="0 0 700 400"> 453 <line x1="180" y1="370" x2="500" y2="50" stroke="black" stroke-width="15px"/> 454 <polygon points="585 0 525 25 585 50" transform="rotate(135 525 25)"/> 455 </svg> 456