Federico Vaga | f77af63 | 2018-11-21 01:35:19 +0100 | [diff] [blame] | 1 | .. _sphinxdoc: |
| 2 | |
Mauro Carvalho Chehab | 1dc4bbf | 2016-11-17 08:32:33 -0200 | [diff] [blame] | 3 | Introduction |
| 4 | ============ |
| 5 | |
| 6 | The Linux kernel uses `Sphinx`_ to generate pretty documentation from |
| 7 | `reStructuredText`_ files under ``Documentation``. To build the documentation in |
| 8 | HTML or PDF formats, use ``make htmldocs`` or ``make pdfdocs``. The generated |
| 9 | documentation is placed in ``Documentation/output``. |
| 10 | |
| 11 | .. _Sphinx: http://www.sphinx-doc.org/ |
| 12 | .. _reStructuredText: http://docutils.sourceforge.net/rst.html |
| 13 | |
| 14 | The reStructuredText files may contain directives to include structured |
| 15 | documentation comments, or kernel-doc comments, from source files. Usually these |
| 16 | are used to describe the functions and types and design of the code. The |
| 17 | kernel-doc comments have some special structure and formatting, but beyond that |
| 18 | they are also treated as reStructuredText. |
| 19 | |
Mauro Carvalho Chehab | 1dc4bbf | 2016-11-17 08:32:33 -0200 | [diff] [blame] | 20 | Finally, there are thousands of plain text documentation files scattered around |
| 21 | ``Documentation``. Some of these will likely be converted to reStructuredText |
| 22 | over time, but the bulk of them will remain in plain text. |
| 23 | |
Mauro Carvalho Chehab | b8b07b5 | 2017-07-14 13:41:17 -0300 | [diff] [blame] | 24 | .. _sphinx_install: |
| 25 | |
| 26 | Sphinx Install |
| 27 | ============== |
| 28 | |
| 29 | The ReST markups currently used by the Documentation/ files are meant to be |
Mauro Carvalho Chehab | a700767 | 2019-05-29 20:09:32 -0300 | [diff] [blame] | 30 | built with ``Sphinx`` version 1.3 or higher. |
Mauro Carvalho Chehab | b8b07b5 | 2017-07-14 13:41:17 -0300 | [diff] [blame] | 31 | |
Federico Vaga | c1ec85f | 2018-06-06 00:48:58 +0200 | [diff] [blame] | 32 | There's a script that checks for the Sphinx requirements. Please see |
Mauro Carvalho Chehab | 868366a | 2017-07-17 18:46:42 -0300 | [diff] [blame] | 33 | :ref:`sphinx-pre-install` for further details. |
| 34 | |
Mauro Carvalho Chehab | 58ef4a4 | 2017-07-14 13:41:18 -0300 | [diff] [blame] | 35 | Most distributions are shipped with Sphinx, but its toolchain is fragile, |
| 36 | and it is not uncommon that upgrading it or some other Python packages |
| 37 | on your machine would cause the documentation build to break. |
| 38 | |
Joel Nider | 4d01460 | 2019-01-14 09:14:59 +0200 | [diff] [blame] | 39 | A way to avoid that is to use a different version than the one shipped |
| 40 | with your distributions. In order to do so, it is recommended to install |
Mauro Carvalho Chehab | 58ef4a4 | 2017-07-14 13:41:18 -0300 | [diff] [blame] | 41 | Sphinx inside a virtual environment, using ``virtualenv-3`` |
| 42 | or ``virtualenv``, depending on how your distribution packaged Python 3. |
| 43 | |
| 44 | .. note:: |
| 45 | |
| 46 | #) Sphinx versions below 1.5 don't work properly with Python's |
Joel Nider | 4d01460 | 2019-01-14 09:14:59 +0200 | [diff] [blame] | 47 | docutils version 0.13.1 or higher. So, if you're willing to use |
Mauro Carvalho Chehab | 58ef4a4 | 2017-07-14 13:41:18 -0300 | [diff] [blame] | 48 | those versions, you should run ``pip install 'docutils==0.12'``. |
| 49 | |
| 50 | #) It is recommended to use the RTD theme for html output. Depending |
| 51 | on the Sphinx version, it should be installed in separate, |
| 52 | with ``pip install sphinx_rtd_theme``. |
| 53 | |
Mauro Carvalho Chehab | 868366a | 2017-07-17 18:46:42 -0300 | [diff] [blame] | 54 | #) Some ReST pages contain math expressions. Due to the way Sphinx work, |
| 55 | those expressions are written using LaTeX notation. It needs texlive |
| 56 | installed with amdfonts and amsmath in order to evaluate them. |
| 57 | |
Mauro Carvalho Chehab | a700767 | 2019-05-29 20:09:32 -0300 | [diff] [blame] | 58 | In summary, if you want to install Sphinx version 1.7.9, you should do:: |
Mauro Carvalho Chehab | 58ef4a4 | 2017-07-14 13:41:18 -0300 | [diff] [blame] | 59 | |
Mauro Carvalho Chehab | a700767 | 2019-05-29 20:09:32 -0300 | [diff] [blame] | 60 | $ virtualenv sphinx_1.7.9 |
| 61 | $ . sphinx_1.7.9/bin/activate |
| 62 | (sphinx_1.7.9) $ pip install -r Documentation/sphinx/requirements.txt |
Mauro Carvalho Chehab | 58ef4a4 | 2017-07-14 13:41:18 -0300 | [diff] [blame] | 63 | |
Mauro Carvalho Chehab | a700767 | 2019-05-29 20:09:32 -0300 | [diff] [blame] | 64 | After running ``. sphinx_1.7.9/bin/activate``, the prompt will change, |
Mauro Carvalho Chehab | 58ef4a4 | 2017-07-14 13:41:18 -0300 | [diff] [blame] | 65 | in order to indicate that you're using the new environment. If you |
| 66 | open a new shell, you need to rerun this command to enter again at |
| 67 | the virtual environment before building the documentation. |
| 68 | |
Mauro Carvalho Chehab | d43e5ae | 2017-07-14 13:41:20 -0300 | [diff] [blame] | 69 | Image output |
| 70 | ------------ |
| 71 | |
| 72 | The kernel documentation build system contains an extension that |
| 73 | handles images on both GraphViz and SVG formats (see |
| 74 | :ref:`sphinx_kfigure`). |
| 75 | |
| 76 | For it to work, you need to install both GraphViz and ImageMagick |
| 77 | packages. If those packages are not installed, the build system will |
| 78 | still build the documentation, but won't include any images at the |
| 79 | output. |
| 80 | |
Mauro Carvalho Chehab | 6e322a1 | 2017-07-14 13:41:21 -0300 | [diff] [blame] | 81 | PDF and LaTeX builds |
| 82 | -------------------- |
| 83 | |
Joel Nider | 4d01460 | 2019-01-14 09:14:59 +0200 | [diff] [blame] | 84 | Such builds are currently supported only with Sphinx versions 1.4 and higher. |
Mauro Carvalho Chehab | 6e322a1 | 2017-07-14 13:41:21 -0300 | [diff] [blame] | 85 | |
| 86 | For PDF and LaTeX output, you'll also need ``XeLaTeX`` version 3.14159265. |
| 87 | |
| 88 | Depending on the distribution, you may also need to install a series of |
| 89 | ``texlive`` packages that provide the minimal set of functionalities |
Mauro Carvalho Chehab | 868366a | 2017-07-17 18:46:42 -0300 | [diff] [blame] | 90 | required for ``XeLaTeX`` to work. |
| 91 | |
| 92 | .. _sphinx-pre-install: |
| 93 | |
| 94 | Checking for Sphinx dependencies |
| 95 | -------------------------------- |
| 96 | |
| 97 | There's a script that automatically check for Sphinx dependencies. If it can |
| 98 | recognize your distribution, it will also give a hint about the install |
| 99 | command line options for your distro:: |
| 100 | |
| 101 | $ ./scripts/sphinx-pre-install |
| 102 | Checking if the needed tools for Fedora release 26 (Twenty Six) are available |
| 103 | Warning: better to also install "texlive-luatex85". |
| 104 | You should run: |
| 105 | |
| 106 | sudo dnf install -y texlive-luatex85 |
Mauro Carvalho Chehab | a700767 | 2019-05-29 20:09:32 -0300 | [diff] [blame] | 107 | /usr/bin/virtualenv sphinx_1.7.9 |
| 108 | . sphinx_1.7.9/bin/activate |
Mauro Carvalho Chehab | 868366a | 2017-07-17 18:46:42 -0300 | [diff] [blame] | 109 | pip install -r Documentation/sphinx/requirements.txt |
| 110 | |
| 111 | Can't build as 1 mandatory dependency is missing at ./scripts/sphinx-pre-install line 468. |
| 112 | |
| 113 | By default, it checks all the requirements for both html and PDF, including |
| 114 | the requirements for images, math expressions and LaTeX build, and assumes |
| 115 | that a virtual Python environment will be used. The ones needed for html |
| 116 | builds are assumed to be mandatory; the others to be optional. |
| 117 | |
| 118 | It supports two optional parameters: |
| 119 | |
| 120 | ``--no-pdf`` |
| 121 | Disable checks for PDF; |
| 122 | |
| 123 | ``--no-virtualenv`` |
| 124 | Use OS packaging for Sphinx instead of Python virtual environment. |
| 125 | |
Mauro Carvalho Chehab | 6e322a1 | 2017-07-14 13:41:21 -0300 | [diff] [blame] | 126 | |
Mauro Carvalho Chehab | 1dc4bbf | 2016-11-17 08:32:33 -0200 | [diff] [blame] | 127 | Sphinx Build |
| 128 | ============ |
| 129 | |
| 130 | The usual way to generate the documentation is to run ``make htmldocs`` or |
| 131 | ``make pdfdocs``. There are also other formats available, see the documentation |
| 132 | section of ``make help``. The generated documentation is placed in |
| 133 | format-specific subdirectories under ``Documentation/output``. |
| 134 | |
| 135 | To generate documentation, Sphinx (``sphinx-build``) must obviously be |
| 136 | installed. For prettier HTML output, the Read the Docs Sphinx theme |
Markus Heiser | db6ccf2 | 2017-03-06 14:09:27 +0100 | [diff] [blame] | 137 | (``sphinx_rtd_theme``) is used if available. For PDF output you'll also need |
| 138 | ``XeLaTeX`` and ``convert(1)`` from ImageMagick (https://www.imagemagick.org). |
| 139 | All of these are widely available and packaged in distributions. |
Mauro Carvalho Chehab | 1dc4bbf | 2016-11-17 08:32:33 -0200 | [diff] [blame] | 140 | |
| 141 | To pass extra options to Sphinx, you can use the ``SPHINXOPTS`` make |
| 142 | variable. For example, use ``make SPHINXOPTS=-v htmldocs`` to get more verbose |
| 143 | output. |
| 144 | |
| 145 | To remove the generated documentation, run ``make cleandocs``. |
| 146 | |
| 147 | Writing Documentation |
| 148 | ===================== |
| 149 | |
| 150 | Adding new documentation can be as simple as: |
| 151 | |
| 152 | 1. Add a new ``.rst`` file somewhere under ``Documentation``. |
| 153 | 2. Refer to it from the Sphinx main `TOC tree`_ in ``Documentation/index.rst``. |
| 154 | |
| 155 | .. _TOC tree: http://www.sphinx-doc.org/en/stable/markup/toctree.html |
| 156 | |
| 157 | This is usually good enough for simple documentation (like the one you're |
| 158 | reading right now), but for larger documents it may be advisable to create a |
| 159 | subdirectory (or use an existing one). For example, the graphics subsystem |
| 160 | documentation is under ``Documentation/gpu``, split to several ``.rst`` files, |
| 161 | and has a separate ``index.rst`` (with a ``toctree`` of its own) referenced from |
| 162 | the main index. |
| 163 | |
| 164 | See the documentation for `Sphinx`_ and `reStructuredText`_ on what you can do |
| 165 | with them. In particular, the Sphinx `reStructuredText Primer`_ is a good place |
| 166 | to get started with reStructuredText. There are also some `Sphinx specific |
| 167 | markup constructs`_. |
| 168 | |
| 169 | .. _reStructuredText Primer: http://www.sphinx-doc.org/en/stable/rest.html |
| 170 | .. _Sphinx specific markup constructs: http://www.sphinx-doc.org/en/stable/markup/index.html |
| 171 | |
| 172 | Specific guidelines for the kernel documentation |
| 173 | ------------------------------------------------ |
| 174 | |
| 175 | Here are some specific guidelines for the kernel documentation: |
| 176 | |
Daniel Vetter | c3c5360 | 2017-03-02 16:16:33 +0100 | [diff] [blame] | 177 | * Please don't go overboard with reStructuredText markup. Keep it |
| 178 | simple. For the most part the documentation should be plain text with |
| 179 | just enough consistency in formatting that it can be converted to |
| 180 | other formats. |
| 181 | |
| 182 | * Please keep the formatting changes minimal when converting existing |
| 183 | documentation to reStructuredText. |
| 184 | |
| 185 | * Also update the content, not just the formatting, when converting |
| 186 | documentation. |
Mauro Carvalho Chehab | 1dc4bbf | 2016-11-17 08:32:33 -0200 | [diff] [blame] | 187 | |
| 188 | * Please stick to this order of heading adornments: |
| 189 | |
| 190 | 1. ``=`` with overline for document title:: |
| 191 | |
| 192 | ============== |
| 193 | Document title |
| 194 | ============== |
| 195 | |
| 196 | 2. ``=`` for chapters:: |
| 197 | |
| 198 | Chapters |
| 199 | ======== |
| 200 | |
| 201 | 3. ``-`` for sections:: |
| 202 | |
| 203 | Section |
| 204 | ------- |
| 205 | |
| 206 | 4. ``~`` for subsections:: |
| 207 | |
| 208 | Subsection |
| 209 | ~~~~~~~~~~ |
| 210 | |
| 211 | Although RST doesn't mandate a specific order ("Rather than imposing a fixed |
| 212 | number and order of section title adornment styles, the order enforced will be |
| 213 | the order as encountered."), having the higher levels the same overall makes |
| 214 | it easier to follow the documents. |
| 215 | |
Daniel Vetter | c3c5360 | 2017-03-02 16:16:33 +0100 | [diff] [blame] | 216 | * For inserting fixed width text blocks (for code examples, use case |
| 217 | examples, etc.), use ``::`` for anything that doesn't really benefit |
| 218 | from syntax highlighting, especially short snippets. Use |
| 219 | ``.. code-block:: <language>`` for longer code blocks that benefit |
André Almeida | 83e8b97 | 2019-06-11 17:03:16 -0300 | [diff] [blame^] | 220 | from highlighting. For a short snippet of code embedded in the text, use \`\`. |
Daniel Vetter | c3c5360 | 2017-03-02 16:16:33 +0100 | [diff] [blame] | 221 | |
Mauro Carvalho Chehab | 1dc4bbf | 2016-11-17 08:32:33 -0200 | [diff] [blame] | 222 | |
| 223 | the C domain |
| 224 | ------------ |
| 225 | |
Mauro Carvalho Chehab | 29fd35b | 2017-07-14 13:41:19 -0300 | [diff] [blame] | 226 | The **Sphinx C Domain** (name c) is suited for documentation of C API. E.g. a |
Mauro Carvalho Chehab | 1dc4bbf | 2016-11-17 08:32:33 -0200 | [diff] [blame] | 227 | function prototype: |
| 228 | |
| 229 | .. code-block:: rst |
| 230 | |
| 231 | .. c:function:: int ioctl( int fd, int request ) |
| 232 | |
| 233 | The C domain of the kernel-doc has some additional features. E.g. you can |
| 234 | *rename* the reference name of a function with a common name like ``open`` or |
| 235 | ``ioctl``: |
| 236 | |
| 237 | .. code-block:: rst |
| 238 | |
| 239 | .. c:function:: int ioctl( int fd, int request ) |
| 240 | :name: VIDIOC_LOG_STATUS |
| 241 | |
| 242 | The func-name (e.g. ioctl) remains in the output but the ref-name changed from |
| 243 | ``ioctl`` to ``VIDIOC_LOG_STATUS``. The index entry for this function is also |
| 244 | changed to ``VIDIOC_LOG_STATUS`` and the function can now referenced by: |
| 245 | |
| 246 | .. code-block:: rst |
| 247 | |
| 248 | :c:func:`VIDIOC_LOG_STATUS` |
| 249 | |
| 250 | |
| 251 | list tables |
| 252 | ----------- |
| 253 | |
| 254 | We recommend the use of *list table* formats. The *list table* formats are |
| 255 | double-stage lists. Compared to the ASCII-art they might not be as |
| 256 | comfortable for |
| 257 | readers of the text files. Their advantage is that they are easy to |
| 258 | create or modify and that the diff of a modification is much more meaningful, |
| 259 | because it is limited to the modified content. |
| 260 | |
| 261 | The ``flat-table`` is a double-stage list similar to the ``list-table`` with |
| 262 | some additional features: |
| 263 | |
| 264 | * column-span: with the role ``cspan`` a cell can be extended through |
| 265 | additional columns |
| 266 | |
| 267 | * row-span: with the role ``rspan`` a cell can be extended through |
| 268 | additional rows |
| 269 | |
| 270 | * auto span rightmost cell of a table row over the missing cells on the right |
| 271 | side of that table-row. With Option ``:fill-cells:`` this behavior can |
| 272 | changed from *auto span* to *auto fill*, which automatically inserts (empty) |
| 273 | cells instead of spanning the last cell. |
| 274 | |
| 275 | options: |
| 276 | |
| 277 | * ``:header-rows:`` [int] count of header rows |
| 278 | * ``:stub-columns:`` [int] count of stub columns |
| 279 | * ``:widths:`` [[int] [int] ... ] widths of columns |
| 280 | * ``:fill-cells:`` instead of auto-spanning missing cells, insert missing cells |
| 281 | |
| 282 | roles: |
| 283 | |
| 284 | * ``:cspan:`` [int] additional columns (*morecols*) |
| 285 | * ``:rspan:`` [int] additional rows (*morerows*) |
| 286 | |
| 287 | The example below shows how to use this markup. The first level of the staged |
| 288 | list is the *table-row*. In the *table-row* there is only one markup allowed, |
| 289 | the list of the cells in this *table-row*. Exceptions are *comments* ( ``..`` ) |
| 290 | and *targets* (e.g. a ref to ``:ref:`last row <last row>``` / :ref:`last row |
| 291 | <last row>`). |
| 292 | |
| 293 | .. code-block:: rst |
| 294 | |
| 295 | .. flat-table:: table title |
| 296 | :widths: 2 1 1 3 |
| 297 | |
| 298 | * - head col 1 |
| 299 | - head col 2 |
| 300 | - head col 3 |
| 301 | - head col 4 |
| 302 | |
| 303 | * - column 1 |
| 304 | - field 1.1 |
| 305 | - field 1.2 with autospan |
| 306 | |
| 307 | * - column 2 |
| 308 | - field 2.1 |
| 309 | - :rspan:`1` :cspan:`1` field 2.2 - 3.3 |
| 310 | |
| 311 | * .. _`last row`: |
| 312 | |
| 313 | - column 3 |
| 314 | |
| 315 | Rendered as: |
| 316 | |
| 317 | .. flat-table:: table title |
| 318 | :widths: 2 1 1 3 |
| 319 | |
| 320 | * - head col 1 |
| 321 | - head col 2 |
| 322 | - head col 3 |
| 323 | - head col 4 |
| 324 | |
| 325 | * - column 1 |
| 326 | - field 1.1 |
| 327 | - field 1.2 with autospan |
| 328 | |
| 329 | * - column 2 |
| 330 | - field 2.1 |
| 331 | - :rspan:`1` :cspan:`1` field 2.2 - 3.3 |
| 332 | |
| 333 | * .. _`last row`: |
| 334 | |
| 335 | - column 3 |
Markus Heiser | db6ccf2 | 2017-03-06 14:09:27 +0100 | [diff] [blame] | 336 | |
Mauro Carvalho Chehab | d43e5ae | 2017-07-14 13:41:20 -0300 | [diff] [blame] | 337 | .. _sphinx_kfigure: |
Markus Heiser | db6ccf2 | 2017-03-06 14:09:27 +0100 | [diff] [blame] | 338 | |
| 339 | Figures & Images |
| 340 | ================ |
| 341 | |
| 342 | If you want to add an image, you should use the ``kernel-figure`` and |
| 343 | ``kernel-image`` directives. E.g. to insert a figure with a scalable |
| 344 | image format use SVG (:ref:`svg_image_example`):: |
| 345 | |
| 346 | .. kernel-figure:: svg_image.svg |
| 347 | :alt: simple SVG image |
| 348 | |
| 349 | SVG image example |
| 350 | |
| 351 | .. _svg_image_example: |
| 352 | |
| 353 | .. kernel-figure:: svg_image.svg |
| 354 | :alt: simple SVG image |
| 355 | |
| 356 | SVG image example |
| 357 | |
| 358 | The kernel figure (and image) directive support **DOT** formated files, see |
| 359 | |
| 360 | * DOT: http://graphviz.org/pdf/dotguide.pdf |
| 361 | * Graphviz: http://www.graphviz.org/content/dot-language |
| 362 | |
| 363 | A simple example (:ref:`hello_dot_file`):: |
| 364 | |
| 365 | .. kernel-figure:: hello.dot |
| 366 | :alt: hello world |
| 367 | |
| 368 | DOT's hello world example |
| 369 | |
| 370 | .. _hello_dot_file: |
| 371 | |
| 372 | .. kernel-figure:: hello.dot |
| 373 | :alt: hello world |
| 374 | |
| 375 | DOT's hello world example |
| 376 | |
| 377 | Embed *render* markups (or languages) like Graphviz's **DOT** is provided by the |
| 378 | ``kernel-render`` directives.:: |
| 379 | |
| 380 | .. kernel-render:: DOT |
| 381 | :alt: foobar digraph |
| 382 | :caption: Embedded **DOT** (Graphviz) code |
| 383 | |
| 384 | digraph foo { |
| 385 | "bar" -> "baz"; |
| 386 | } |
| 387 | |
| 388 | How this will be rendered depends on the installed tools. If Graphviz is |
| 389 | installed, you will see an vector image. If not the raw markup is inserted as |
| 390 | *literal-block* (:ref:`hello_dot_render`). |
| 391 | |
| 392 | .. _hello_dot_render: |
| 393 | |
| 394 | .. kernel-render:: DOT |
| 395 | :alt: foobar digraph |
| 396 | :caption: Embedded **DOT** (Graphviz) code |
| 397 | |
| 398 | digraph foo { |
| 399 | "bar" -> "baz"; |
| 400 | } |
| 401 | |
| 402 | The *render* directive has all the options known from the *figure* directive, |
| 403 | plus option ``caption``. If ``caption`` has a value, a *figure* node is |
| 404 | inserted. If not, a *image* node is inserted. A ``caption`` is also needed, if |
| 405 | you want to refer it (:ref:`hello_svg_render`). |
| 406 | |
| 407 | Embedded **SVG**:: |
| 408 | |
| 409 | .. kernel-render:: SVG |
| 410 | :caption: Embedded **SVG** markup |
| 411 | :alt: so-nw-arrow |
| 412 | |
| 413 | <?xml version="1.0" encoding="UTF-8"?> |
| 414 | <svg xmlns="http://www.w3.org/2000/svg" version="1.1" ...> |
| 415 | ... |
| 416 | </svg> |
| 417 | |
| 418 | .. _hello_svg_render: |
| 419 | |
| 420 | .. kernel-render:: SVG |
| 421 | :caption: Embedded **SVG** markup |
| 422 | :alt: so-nw-arrow |
| 423 | |
| 424 | <?xml version="1.0" encoding="UTF-8"?> |
| 425 | <svg xmlns="http://www.w3.org/2000/svg" |
| 426 | version="1.1" baseProfile="full" width="70px" height="40px" viewBox="0 0 700 400"> |
| 427 | <line x1="180" y1="370" x2="500" y2="50" stroke="black" stroke-width="15px"/> |
| 428 | <polygon points="585 0 525 25 585 50" transform="rotate(135 525 25)"/> |
| 429 | </svg> |