2016-07-08 14:15:04 +02:00
|
|
|
#!/usr/bin/env python3
|
2025-06-22 11:39:53 +02:00
|
|
|
# SPDX-License-Identifier: GPL-2.0
|
2025-08-22 16:19:22 +02:00
|
|
|
# pylint: disable=R0903, R0912, R0914, R0915, C0209,W0707
|
2016-07-08 14:15:04 +02:00
|
|
|
|
2025-08-22 16:19:34 +02:00
|
|
|
|
2025-02-11 07:19:03 +01:00
|
|
|
"""
|
2025-08-22 16:19:34 +02:00
|
|
|
Implementation of the ``kernel-include`` reST-directive.
|
2016-07-08 14:15:04 +02:00
|
|
|
|
2025-08-22 16:19:34 +02:00
|
|
|
:copyright: Copyright (C) 2016 Markus Heiser
|
|
|
|
|
:license: GPL Version 2, June 1991 see linux/COPYING for details.
|
2016-07-08 14:15:04 +02:00
|
|
|
|
2025-08-22 16:19:34 +02:00
|
|
|
The ``kernel-include`` reST-directive is a replacement for the ``include``
|
|
|
|
|
directive. The ``kernel-include`` directive expand environment variables in
|
|
|
|
|
the path name and allows to include files from arbitrary locations.
|
2016-07-08 14:15:04 +02:00
|
|
|
|
2025-08-22 16:19:34 +02:00
|
|
|
.. hint::
|
2016-07-08 14:15:04 +02:00
|
|
|
|
2025-08-22 16:19:34 +02:00
|
|
|
Including files from arbitrary locations (e.g. from ``/etc``) is a
|
|
|
|
|
security risk for builders. This is why the ``include`` directive from
|
|
|
|
|
docutils *prohibit* pathnames pointing to locations *above* the filesystem
|
|
|
|
|
tree where the reST document with the include directive is placed.
|
2016-07-08 14:15:04 +02:00
|
|
|
|
2025-08-22 16:19:34 +02:00
|
|
|
Substrings of the form $name or ${name} are replaced by the value of
|
|
|
|
|
environment variable name. Malformed variable names and references to
|
|
|
|
|
non-existing variables are left unchanged.
|
2016-07-08 14:15:04 +02:00
|
|
|
|
2025-08-22 16:19:34 +02:00
|
|
|
**Supported Sphinx Include Options**:
|
docs: kernel_include.py: allow cross-reference generation
kernel_include extension was originally designed to be used by the
media comprehensive uAPI documentation, where, instead of simpler
kernel-doc markups, the uAPI documentation is enriched with a larger
text, with images, complex tables, graphs, etc.
There, we wanted to include the much simpler yet documented .h
file.
This extension is needed to include files from other parts of the
Kernel tree outside Documentation, because the original Sphinx
include tag doesn't allow going outside of the directory passed
via sphinx-build command line.
Yet, the cross-references themselves to the full documentation
were using a perl script to create cross-references against the
comprehensive documentation.
As the perl script is now converted to Phython and there is a
Python class producing an include-compatible output with cross
references, add two optional arguments to kernel_include.py:
1. :generate-cross-refs:
If present, instead of reading the file, it calls ParseDataStructs()
class, which converts C data structures into cross-references to
be linked to ReST files containing a more comprehensive documentation;
Don't use it together with :start-line: and/or :end-line:, as
filtering input file line range is currently not supported.
2. :exception-file:
Used together with :generate-cross-refs:. Points to a file containing
rules to ignore C data structs or to use a different reference name,
optionally using a different reference type.
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/efc39c8e54a2056ae2fdb94d5006fcb19e227198.1755872208.git.mchehab+huawei@kernel.org
2025-08-22 16:19:23 +02:00
|
|
|
|
2025-08-22 16:19:34 +02:00
|
|
|
:param literal:
|
|
|
|
|
If present, the included file is inserted as a literal block.
|
docs: kernel_include.py: allow cross-reference generation
kernel_include extension was originally designed to be used by the
media comprehensive uAPI documentation, where, instead of simpler
kernel-doc markups, the uAPI documentation is enriched with a larger
text, with images, complex tables, graphs, etc.
There, we wanted to include the much simpler yet documented .h
file.
This extension is needed to include files from other parts of the
Kernel tree outside Documentation, because the original Sphinx
include tag doesn't allow going outside of the directory passed
via sphinx-build command line.
Yet, the cross-references themselves to the full documentation
were using a perl script to create cross-references against the
comprehensive documentation.
As the perl script is now converted to Phython and there is a
Python class producing an include-compatible output with cross
references, add two optional arguments to kernel_include.py:
1. :generate-cross-refs:
If present, instead of reading the file, it calls ParseDataStructs()
class, which converts C data structures into cross-references to
be linked to ReST files containing a more comprehensive documentation;
Don't use it together with :start-line: and/or :end-line:, as
filtering input file line range is currently not supported.
2. :exception-file:
Used together with :generate-cross-refs:. Points to a file containing
rules to ignore C data structs or to use a different reference name,
optionally using a different reference type.
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/efc39c8e54a2056ae2fdb94d5006fcb19e227198.1755872208.git.mchehab+huawei@kernel.org
2025-08-22 16:19:23 +02:00
|
|
|
|
2025-08-22 16:19:34 +02:00
|
|
|
:param code:
|
|
|
|
|
Specify the language for syntax highlighting (e.g., 'c', 'python').
|
docs: kernel_include.py: allow cross-reference generation
kernel_include extension was originally designed to be used by the
media comprehensive uAPI documentation, where, instead of simpler
kernel-doc markups, the uAPI documentation is enriched with a larger
text, with images, complex tables, graphs, etc.
There, we wanted to include the much simpler yet documented .h
file.
This extension is needed to include files from other parts of the
Kernel tree outside Documentation, because the original Sphinx
include tag doesn't allow going outside of the directory passed
via sphinx-build command line.
Yet, the cross-references themselves to the full documentation
were using a perl script to create cross-references against the
comprehensive documentation.
As the perl script is now converted to Phython and there is a
Python class producing an include-compatible output with cross
references, add two optional arguments to kernel_include.py:
1. :generate-cross-refs:
If present, instead of reading the file, it calls ParseDataStructs()
class, which converts C data structures into cross-references to
be linked to ReST files containing a more comprehensive documentation;
Don't use it together with :start-line: and/or :end-line:, as
filtering input file line range is currently not supported.
2. :exception-file:
Used together with :generate-cross-refs:. Points to a file containing
rules to ignore C data structs or to use a different reference name,
optionally using a different reference type.
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/efc39c8e54a2056ae2fdb94d5006fcb19e227198.1755872208.git.mchehab+huawei@kernel.org
2025-08-22 16:19:23 +02:00
|
|
|
|
2025-08-22 16:19:34 +02:00
|
|
|
:param encoding:
|
|
|
|
|
Specify the encoding of the included file (default: 'utf-8').
|
docs: kernel_include.py: allow cross-reference generation
kernel_include extension was originally designed to be used by the
media comprehensive uAPI documentation, where, instead of simpler
kernel-doc markups, the uAPI documentation is enriched with a larger
text, with images, complex tables, graphs, etc.
There, we wanted to include the much simpler yet documented .h
file.
This extension is needed to include files from other parts of the
Kernel tree outside Documentation, because the original Sphinx
include tag doesn't allow going outside of the directory passed
via sphinx-build command line.
Yet, the cross-references themselves to the full documentation
were using a perl script to create cross-references against the
comprehensive documentation.
As the perl script is now converted to Phython and there is a
Python class producing an include-compatible output with cross
references, add two optional arguments to kernel_include.py:
1. :generate-cross-refs:
If present, instead of reading the file, it calls ParseDataStructs()
class, which converts C data structures into cross-references to
be linked to ReST files containing a more comprehensive documentation;
Don't use it together with :start-line: and/or :end-line:, as
filtering input file line range is currently not supported.
2. :exception-file:
Used together with :generate-cross-refs:. Points to a file containing
rules to ignore C data structs or to use a different reference name,
optionally using a different reference type.
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/efc39c8e54a2056ae2fdb94d5006fcb19e227198.1755872208.git.mchehab+huawei@kernel.org
2025-08-22 16:19:23 +02:00
|
|
|
|
2025-08-22 16:19:34 +02:00
|
|
|
:param tab-width:
|
|
|
|
|
Specify the number of spaces that a tab represents.
|
docs: kernel_include.py: allow cross-reference generation
kernel_include extension was originally designed to be used by the
media comprehensive uAPI documentation, where, instead of simpler
kernel-doc markups, the uAPI documentation is enriched with a larger
text, with images, complex tables, graphs, etc.
There, we wanted to include the much simpler yet documented .h
file.
This extension is needed to include files from other parts of the
Kernel tree outside Documentation, because the original Sphinx
include tag doesn't allow going outside of the directory passed
via sphinx-build command line.
Yet, the cross-references themselves to the full documentation
were using a perl script to create cross-references against the
comprehensive documentation.
As the perl script is now converted to Phython and there is a
Python class producing an include-compatible output with cross
references, add two optional arguments to kernel_include.py:
1. :generate-cross-refs:
If present, instead of reading the file, it calls ParseDataStructs()
class, which converts C data structures into cross-references to
be linked to ReST files containing a more comprehensive documentation;
Don't use it together with :start-line: and/or :end-line:, as
filtering input file line range is currently not supported.
2. :exception-file:
Used together with :generate-cross-refs:. Points to a file containing
rules to ignore C data structs or to use a different reference name,
optionally using a different reference type.
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/efc39c8e54a2056ae2fdb94d5006fcb19e227198.1755872208.git.mchehab+huawei@kernel.org
2025-08-22 16:19:23 +02:00
|
|
|
|
2025-08-22 16:19:34 +02:00
|
|
|
:param start-line:
|
|
|
|
|
Line number at which to start including the file (1-based).
|
2025-08-22 16:19:24 +02:00
|
|
|
|
2025-08-22 16:19:34 +02:00
|
|
|
:param end-line:
|
|
|
|
|
Line number at which to stop including the file (inclusive).
|
2025-08-22 16:19:24 +02:00
|
|
|
|
2025-08-22 16:19:34 +02:00
|
|
|
:param start-after:
|
|
|
|
|
Include lines after the first line matching this text.
|
2025-08-22 16:19:24 +02:00
|
|
|
|
2025-08-22 16:19:34 +02:00
|
|
|
:param end-before:
|
|
|
|
|
Include lines before the first line matching this text.
|
2025-08-22 16:19:24 +02:00
|
|
|
|
2025-08-22 16:19:34 +02:00
|
|
|
:param number-lines:
|
|
|
|
|
Number the included lines (integer specifies start number).
|
|
|
|
|
Only effective with 'literal' or 'code' options.
|
2025-08-22 16:19:24 +02:00
|
|
|
|
2025-08-22 16:19:34 +02:00
|
|
|
:param class:
|
|
|
|
|
Specify HTML class attribute for the included content.
|
|
|
|
|
|
|
|
|
|
**Kernel-specific Extensions**:
|
|
|
|
|
|
|
|
|
|
:param generate-cross-refs:
|
|
|
|
|
If present, instead of directly including the file, it calls
|
|
|
|
|
ParseDataStructs() to convert C data structures into cross-references
|
|
|
|
|
that link to comprehensive documentation in other ReST files.
|
|
|
|
|
|
|
|
|
|
:param exception-file:
|
|
|
|
|
(Used with generate-cross-refs)
|
|
|
|
|
|
|
|
|
|
Path to a file containing rules for handling special cases:
|
|
|
|
|
- Ignore specific C data structures
|
|
|
|
|
- Use alternative reference names
|
|
|
|
|
- Specify different reference types
|
|
|
|
|
|
|
|
|
|
:param warn-broken:
|
|
|
|
|
(Used with generate-cross-refs)
|
|
|
|
|
|
|
|
|
|
Enables warnings when auto-generated cross-references don't point to
|
|
|
|
|
existing documentation targets.
|
2016-07-08 14:15:04 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
# imports
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
|
|
|
|
|
import os.path
|
2025-08-22 16:19:24 +02:00
|
|
|
import re
|
docs: kernel_include.py: allow cross-reference generation
kernel_include extension was originally designed to be used by the
media comprehensive uAPI documentation, where, instead of simpler
kernel-doc markups, the uAPI documentation is enriched with a larger
text, with images, complex tables, graphs, etc.
There, we wanted to include the much simpler yet documented .h
file.
This extension is needed to include files from other parts of the
Kernel tree outside Documentation, because the original Sphinx
include tag doesn't allow going outside of the directory passed
via sphinx-build command line.
Yet, the cross-references themselves to the full documentation
were using a perl script to create cross-references against the
comprehensive documentation.
As the perl script is now converted to Phython and there is a
Python class producing an include-compatible output with cross
references, add two optional arguments to kernel_include.py:
1. :generate-cross-refs:
If present, instead of reading the file, it calls ParseDataStructs()
class, which converts C data structures into cross-references to
be linked to ReST files containing a more comprehensive documentation;
Don't use it together with :start-line: and/or :end-line:, as
filtering input file line range is currently not supported.
2. :exception-file:
Used together with :generate-cross-refs:. Points to a file containing
rules to ignore C data structs or to use a different reference name,
optionally using a different reference type.
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/efc39c8e54a2056ae2fdb94d5006fcb19e227198.1755872208.git.mchehab+huawei@kernel.org
2025-08-22 16:19:23 +02:00
|
|
|
import sys
|
2016-07-08 14:15:04 +02:00
|
|
|
|
|
|
|
|
from docutils import io, nodes, statemachine
|
2025-08-22 16:19:30 +02:00
|
|
|
from docutils.statemachine import ViewList
|
2025-08-22 16:19:33 +02:00
|
|
|
from docutils.parsers.rst import Directive, directives
|
2016-07-08 14:15:04 +02:00
|
|
|
from docutils.parsers.rst.directives.body import CodeBlock, NumberLines
|
|
|
|
|
|
2025-08-22 16:19:24 +02:00
|
|
|
from sphinx.util import logging
|
|
|
|
|
|
docs: kernel_include.py: allow cross-reference generation
kernel_include extension was originally designed to be used by the
media comprehensive uAPI documentation, where, instead of simpler
kernel-doc markups, the uAPI documentation is enriched with a larger
text, with images, complex tables, graphs, etc.
There, we wanted to include the much simpler yet documented .h
file.
This extension is needed to include files from other parts of the
Kernel tree outside Documentation, because the original Sphinx
include tag doesn't allow going outside of the directory passed
via sphinx-build command line.
Yet, the cross-references themselves to the full documentation
were using a perl script to create cross-references against the
comprehensive documentation.
As the perl script is now converted to Phython and there is a
Python class producing an include-compatible output with cross
references, add two optional arguments to kernel_include.py:
1. :generate-cross-refs:
If present, instead of reading the file, it calls ParseDataStructs()
class, which converts C data structures into cross-references to
be linked to ReST files containing a more comprehensive documentation;
Don't use it together with :start-line: and/or :end-line:, as
filtering input file line range is currently not supported.
2. :exception-file:
Used together with :generate-cross-refs:. Points to a file containing
rules to ignore C data structs or to use a different reference name,
optionally using a different reference type.
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/efc39c8e54a2056ae2fdb94d5006fcb19e227198.1755872208.git.mchehab+huawei@kernel.org
2025-08-22 16:19:23 +02:00
|
|
|
srctree = os.path.abspath(os.environ["srctree"])
|
|
|
|
|
sys.path.insert(0, os.path.join(srctree, "tools/docs/lib"))
|
|
|
|
|
|
|
|
|
|
from parse_data_structs import ParseDataStructs
|
|
|
|
|
|
2025-08-22 16:19:22 +02:00
|
|
|
__version__ = "1.0"
|
2025-08-22 16:19:24 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
2025-08-22 16:19:22 +02:00
|
|
|
|
2025-08-22 16:19:24 +02:00
|
|
|
RE_DOMAIN_REF = re.compile(r'\\ :(ref|c:type|c:func):`([^<`]+)(?:<([^>]+)>)?`\\')
|
|
|
|
|
RE_SIMPLE_REF = re.compile(r'`([^`]+)`')
|
2025-08-22 16:19:22 +02:00
|
|
|
|
2025-09-09 13:35:37 -06:00
|
|
|
def ErrorString(exc): # Shamelessly stolen from docutils
|
|
|
|
|
return f'{exc.__class__.__name}: {exc}'
|
|
|
|
|
|
2016-07-08 14:15:04 +02:00
|
|
|
|
|
|
|
|
# ==============================================================================
|
2025-08-22 16:19:33 +02:00
|
|
|
class KernelInclude(Directive):
|
|
|
|
|
"""
|
|
|
|
|
KernelInclude (``kernel-include``) directive
|
2016-07-08 14:15:04 +02:00
|
|
|
|
2025-08-22 16:19:33 +02:00
|
|
|
Most of the stuff here came from Include directive defined at:
|
|
|
|
|
docutils/parsers/rst/directives/misc.py
|
docs: kernel_include.py: allow cross-reference generation
kernel_include extension was originally designed to be used by the
media comprehensive uAPI documentation, where, instead of simpler
kernel-doc markups, the uAPI documentation is enriched with a larger
text, with images, complex tables, graphs, etc.
There, we wanted to include the much simpler yet documented .h
file.
This extension is needed to include files from other parts of the
Kernel tree outside Documentation, because the original Sphinx
include tag doesn't allow going outside of the directory passed
via sphinx-build command line.
Yet, the cross-references themselves to the full documentation
were using a perl script to create cross-references against the
comprehensive documentation.
As the perl script is now converted to Phython and there is a
Python class producing an include-compatible output with cross
references, add two optional arguments to kernel_include.py:
1. :generate-cross-refs:
If present, instead of reading the file, it calls ParseDataStructs()
class, which converts C data structures into cross-references to
be linked to ReST files containing a more comprehensive documentation;
Don't use it together with :start-line: and/or :end-line:, as
filtering input file line range is currently not supported.
2. :exception-file:
Used together with :generate-cross-refs:. Points to a file containing
rules to ignore C data structs or to use a different reference name,
optionally using a different reference type.
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/efc39c8e54a2056ae2fdb94d5006fcb19e227198.1755872208.git.mchehab+huawei@kernel.org
2025-08-22 16:19:23 +02:00
|
|
|
|
2025-08-22 16:19:33 +02:00
|
|
|
Yet, overriding the class don't has any benefits: the original class
|
|
|
|
|
only have run() and argument list. Not all of them are implemented,
|
|
|
|
|
when checked against latest Sphinx version, as with time more arguments
|
|
|
|
|
were added.
|
|
|
|
|
|
|
|
|
|
So, keep its own list of supported arguments
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
required_arguments = 1
|
|
|
|
|
optional_arguments = 0
|
|
|
|
|
final_argument_whitespace = True
|
|
|
|
|
option_spec = {
|
|
|
|
|
'literal': directives.flag,
|
|
|
|
|
'code': directives.unchanged,
|
|
|
|
|
'encoding': directives.encoding,
|
|
|
|
|
'tab-width': int,
|
|
|
|
|
'start-line': int,
|
|
|
|
|
'end-line': int,
|
|
|
|
|
'start-after': directives.unchanged_required,
|
|
|
|
|
'end-before': directives.unchanged_required,
|
|
|
|
|
# ignored except for 'literal' or 'code':
|
|
|
|
|
'number-lines': directives.unchanged, # integer or None
|
|
|
|
|
'class': directives.class_option,
|
|
|
|
|
|
|
|
|
|
# Arguments that aren't from Sphinx Include directive
|
docs: kernel_include.py: allow cross-reference generation
kernel_include extension was originally designed to be used by the
media comprehensive uAPI documentation, where, instead of simpler
kernel-doc markups, the uAPI documentation is enriched with a larger
text, with images, complex tables, graphs, etc.
There, we wanted to include the much simpler yet documented .h
file.
This extension is needed to include files from other parts of the
Kernel tree outside Documentation, because the original Sphinx
include tag doesn't allow going outside of the directory passed
via sphinx-build command line.
Yet, the cross-references themselves to the full documentation
were using a perl script to create cross-references against the
comprehensive documentation.
As the perl script is now converted to Phython and there is a
Python class producing an include-compatible output with cross
references, add two optional arguments to kernel_include.py:
1. :generate-cross-refs:
If present, instead of reading the file, it calls ParseDataStructs()
class, which converts C data structures into cross-references to
be linked to ReST files containing a more comprehensive documentation;
Don't use it together with :start-line: and/or :end-line:, as
filtering input file line range is currently not supported.
2. :exception-file:
Used together with :generate-cross-refs:. Points to a file containing
rules to ignore C data structs or to use a different reference name,
optionally using a different reference type.
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/efc39c8e54a2056ae2fdb94d5006fcb19e227198.1755872208.git.mchehab+huawei@kernel.org
2025-08-22 16:19:23 +02:00
|
|
|
'generate-cross-refs': directives.flag,
|
2025-08-22 16:19:24 +02:00
|
|
|
'warn-broken': directives.flag,
|
2025-08-22 16:19:29 +02:00
|
|
|
'toc': directives.flag,
|
docs: kernel_include.py: allow cross-reference generation
kernel_include extension was originally designed to be used by the
media comprehensive uAPI documentation, where, instead of simpler
kernel-doc markups, the uAPI documentation is enriched with a larger
text, with images, complex tables, graphs, etc.
There, we wanted to include the much simpler yet documented .h
file.
This extension is needed to include files from other parts of the
Kernel tree outside Documentation, because the original Sphinx
include tag doesn't allow going outside of the directory passed
via sphinx-build command line.
Yet, the cross-references themselves to the full documentation
were using a perl script to create cross-references against the
comprehensive documentation.
As the perl script is now converted to Phython and there is a
Python class producing an include-compatible output with cross
references, add two optional arguments to kernel_include.py:
1. :generate-cross-refs:
If present, instead of reading the file, it calls ParseDataStructs()
class, which converts C data structures into cross-references to
be linked to ReST files containing a more comprehensive documentation;
Don't use it together with :start-line: and/or :end-line:, as
filtering input file line range is currently not supported.
2. :exception-file:
Used together with :generate-cross-refs:. Points to a file containing
rules to ignore C data structs or to use a different reference name,
optionally using a different reference type.
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/efc39c8e54a2056ae2fdb94d5006fcb19e227198.1755872208.git.mchehab+huawei@kernel.org
2025-08-22 16:19:23 +02:00
|
|
|
'exception-file': directives.unchanged,
|
2025-08-22 16:19:33 +02:00
|
|
|
}
|
docs: kernel_include.py: allow cross-reference generation
kernel_include extension was originally designed to be used by the
media comprehensive uAPI documentation, where, instead of simpler
kernel-doc markups, the uAPI documentation is enriched with a larger
text, with images, complex tables, graphs, etc.
There, we wanted to include the much simpler yet documented .h
file.
This extension is needed to include files from other parts of the
Kernel tree outside Documentation, because the original Sphinx
include tag doesn't allow going outside of the directory passed
via sphinx-build command line.
Yet, the cross-references themselves to the full documentation
were using a perl script to create cross-references against the
comprehensive documentation.
As the perl script is now converted to Phython and there is a
Python class producing an include-compatible output with cross
references, add two optional arguments to kernel_include.py:
1. :generate-cross-refs:
If present, instead of reading the file, it calls ParseDataStructs()
class, which converts C data structures into cross-references to
be linked to ReST files containing a more comprehensive documentation;
Don't use it together with :start-line: and/or :end-line:, as
filtering input file line range is currently not supported.
2. :exception-file:
Used together with :generate-cross-refs:. Points to a file containing
rules to ignore C data structs or to use a different reference name,
optionally using a different reference type.
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/efc39c8e54a2056ae2fdb94d5006fcb19e227198.1755872208.git.mchehab+huawei@kernel.org
2025-08-22 16:19:23 +02:00
|
|
|
|
2025-08-22 16:19:25 +02:00
|
|
|
def read_rawtext(self, path, encoding):
|
|
|
|
|
"""Read and process file content with error handling"""
|
|
|
|
|
try:
|
|
|
|
|
self.state.document.settings.record_dependencies.add(path)
|
|
|
|
|
include_file = io.FileInput(source_path=path,
|
|
|
|
|
encoding=encoding,
|
|
|
|
|
error_handler=self.state.document.settings.input_encoding_error_handler)
|
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
|
raise self.severe('Problems with directive path:\n'
|
|
|
|
|
'Cannot encode input file path "%s" '
|
2025-09-09 13:35:37 -06:00
|
|
|
'(wrong locale?).' % path)
|
2025-08-22 16:19:25 +02:00
|
|
|
except IOError as error:
|
|
|
|
|
raise self.severe('Problems with directive path:\n%s.' % ErrorString(error))
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
return include_file.read()
|
|
|
|
|
except UnicodeError as error:
|
|
|
|
|
raise self.severe('Problem with directive:\n%s' % ErrorString(error))
|
|
|
|
|
|
2025-08-22 16:19:31 +02:00
|
|
|
def apply_range(self, rawtext):
|
|
|
|
|
"""
|
|
|
|
|
Handles start-line, end-line, start-after and end-before parameters
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Get to-be-included content
|
|
|
|
|
startline = self.options.get('start-line', None)
|
|
|
|
|
endline = self.options.get('end-line', None)
|
|
|
|
|
try:
|
|
|
|
|
if startline or (endline is not None):
|
|
|
|
|
lines = rawtext.splitlines()
|
|
|
|
|
rawtext = '\n'.join(lines[startline:endline])
|
|
|
|
|
except UnicodeError as error:
|
|
|
|
|
raise self.severe(f'Problem with "{self.name}" directive:\n'
|
|
|
|
|
+ io.error_string(error))
|
|
|
|
|
# start-after/end-before: no restrictions on newlines in match-text,
|
|
|
|
|
# and no restrictions on matching inside lines vs. line boundaries
|
|
|
|
|
after_text = self.options.get("start-after", None)
|
|
|
|
|
if after_text:
|
|
|
|
|
# skip content in rawtext before *and incl.* a matching text
|
|
|
|
|
after_index = rawtext.find(after_text)
|
|
|
|
|
if after_index < 0:
|
|
|
|
|
raise self.severe('Problem with "start-after" option of "%s" '
|
|
|
|
|
"directive:\nText not found." % self.name)
|
|
|
|
|
rawtext = rawtext[after_index + len(after_text) :]
|
|
|
|
|
before_text = self.options.get("end-before", None)
|
|
|
|
|
if before_text:
|
|
|
|
|
# skip content in rawtext after *and incl.* a matching text
|
|
|
|
|
before_index = rawtext.find(before_text)
|
|
|
|
|
if before_index < 0:
|
|
|
|
|
raise self.severe('Problem with "end-before" option of "%s" '
|
|
|
|
|
"directive:\nText not found." % self.name)
|
|
|
|
|
rawtext = rawtext[:before_index]
|
|
|
|
|
|
|
|
|
|
return rawtext
|
|
|
|
|
|
2025-08-22 16:19:30 +02:00
|
|
|
def xref_text(self, env, path, tab_width):
|
|
|
|
|
"""
|
|
|
|
|
Read and add contents from a C file parsed to have cross references.
|
|
|
|
|
|
|
|
|
|
There are two types of supported output here:
|
|
|
|
|
- A C source code with cross-references;
|
|
|
|
|
- a TOC table containing cross references.
|
|
|
|
|
"""
|
2025-08-22 16:19:25 +02:00
|
|
|
parser = ParseDataStructs()
|
|
|
|
|
parser.parse_file(path)
|
|
|
|
|
|
|
|
|
|
if 'exception-file' in self.options:
|
|
|
|
|
source_dir = os.path.dirname(os.path.abspath(
|
|
|
|
|
self.state_machine.input_lines.source(
|
|
|
|
|
self.lineno - self.state_machine.input_offset - 1)))
|
|
|
|
|
exceptions_file = os.path.join(source_dir, self.options['exception-file'])
|
|
|
|
|
parser.process_exceptions(exceptions_file)
|
|
|
|
|
|
|
|
|
|
# Store references on a symbol dict to be used at check time
|
|
|
|
|
if 'warn-broken' in self.options:
|
|
|
|
|
env._xref_files.add(path)
|
|
|
|
|
|
2025-08-22 16:19:32 +02:00
|
|
|
if "toc" not in self.options:
|
|
|
|
|
|
2025-08-22 16:19:30 +02:00
|
|
|
rawtext = ".. parsed-literal::\n\n" + parser.gen_output()
|
|
|
|
|
self.apply_range(rawtext)
|
2025-08-22 16:19:29 +02:00
|
|
|
|
2025-08-22 16:19:32 +02:00
|
|
|
include_lines = statemachine.string2lines(rawtext, tab_width,
|
|
|
|
|
convert_whitespace=True)
|
|
|
|
|
|
|
|
|
|
# Sphinx always blame the ".. <directive>", so placing
|
|
|
|
|
# line numbers here won't make any difference
|
|
|
|
|
|
|
|
|
|
self.state_machine.insert_input(include_lines, path)
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
# TOC output is a ReST file, not a literal. So, we can add line
|
|
|
|
|
# numbers
|
|
|
|
|
|
|
|
|
|
rawtext = parser.gen_toc()
|
2025-08-22 16:19:30 +02:00
|
|
|
|
|
|
|
|
include_lines = statemachine.string2lines(rawtext, tab_width,
|
|
|
|
|
convert_whitespace=True)
|
|
|
|
|
|
|
|
|
|
# Append line numbers data
|
|
|
|
|
|
|
|
|
|
startline = self.options.get('start-line', None)
|
|
|
|
|
|
|
|
|
|
result = ViewList()
|
|
|
|
|
if startline and startline > 0:
|
|
|
|
|
offset = startline - 1
|
|
|
|
|
else:
|
|
|
|
|
offset = 0
|
|
|
|
|
|
|
|
|
|
for ln, line in enumerate(include_lines, start=offset):
|
|
|
|
|
result.append(line, path, ln)
|
|
|
|
|
|
|
|
|
|
self.state_machine.insert_input(result, path)
|
|
|
|
|
|
|
|
|
|
return []
|
2025-08-22 16:19:25 +02:00
|
|
|
|
2025-08-22 16:19:28 +02:00
|
|
|
def literal(self, path, tab_width, rawtext):
|
|
|
|
|
"""Output a literal block"""
|
|
|
|
|
|
|
|
|
|
# Convert tabs to spaces, if `tab_width` is positive.
|
|
|
|
|
if tab_width >= 0:
|
|
|
|
|
text = rawtext.expandtabs(tab_width)
|
|
|
|
|
else:
|
|
|
|
|
text = rawtext
|
|
|
|
|
literal_block = nodes.literal_block(rawtext, source=path,
|
|
|
|
|
classes=self.options.get("class", []))
|
|
|
|
|
literal_block.line = 1
|
|
|
|
|
self.add_name(literal_block)
|
|
|
|
|
if "number-lines" in self.options:
|
|
|
|
|
try:
|
|
|
|
|
startline = int(self.options["number-lines"] or 1)
|
|
|
|
|
except ValueError:
|
|
|
|
|
raise self.error(":number-lines: with non-integer start value")
|
|
|
|
|
endline = startline + len(include_lines)
|
|
|
|
|
if text.endswith("\n"):
|
|
|
|
|
text = text[:-1]
|
|
|
|
|
tokens = NumberLines([([], text)], startline, endline)
|
|
|
|
|
for classes, value in tokens:
|
|
|
|
|
if classes:
|
|
|
|
|
literal_block += nodes.inline(value, value,
|
|
|
|
|
classes=classes)
|
|
|
|
|
else:
|
|
|
|
|
literal_block += nodes.Text(value, value)
|
|
|
|
|
else:
|
|
|
|
|
literal_block += nodes.Text(text, text)
|
|
|
|
|
return [literal_block]
|
|
|
|
|
|
2025-08-22 16:19:30 +02:00
|
|
|
def code(self, path, tab_width):
|
2025-08-22 16:19:28 +02:00
|
|
|
"""Output a code block"""
|
|
|
|
|
|
2025-08-22 16:19:30 +02:00
|
|
|
include_lines = statemachine.string2lines(rawtext, tab_width,
|
|
|
|
|
convert_whitespace=True)
|
|
|
|
|
|
2025-08-22 16:19:28 +02:00
|
|
|
self.options["source"] = path
|
|
|
|
|
codeblock = CodeBlock(self.name,
|
|
|
|
|
[self.options.pop("code")], # arguments
|
|
|
|
|
self.options,
|
|
|
|
|
include_lines,
|
|
|
|
|
self.lineno,
|
|
|
|
|
self.content_offset,
|
|
|
|
|
self.block_text,
|
|
|
|
|
self.state,
|
|
|
|
|
self.state_machine)
|
|
|
|
|
return codeblock.run()
|
|
|
|
|
|
2016-07-08 14:15:04 +02:00
|
|
|
def run(self):
|
2025-08-22 16:19:25 +02:00
|
|
|
"""Include a file as part of the content of this reST file."""
|
2022-03-26 11:27:22 +01:00
|
|
|
env = self.state.document.settings.env
|
2016-07-08 14:15:04 +02:00
|
|
|
|
2025-09-01 15:21:21 +02:00
|
|
|
#
|
2025-09-01 15:21:22 +02:00
|
|
|
# The include logic accepts only patches relative to the
|
|
|
|
|
# Kernel source tree. The logic does check it to prevent
|
|
|
|
|
# directory traverse issues.
|
2025-09-01 15:21:21 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
|
|
srctree = os.path.abspath(os.environ["srctree"])
|
|
|
|
|
|
|
|
|
|
path = os.path.expandvars(self.arguments[0])
|
|
|
|
|
src_path = os.path.join(srctree, path)
|
|
|
|
|
|
|
|
|
|
if os.path.isfile(src_path):
|
|
|
|
|
base = srctree
|
|
|
|
|
path = src_path
|
|
|
|
|
else:
|
|
|
|
|
raise self.warning(f'File "%s" doesn\'t exist', path)
|
|
|
|
|
|
|
|
|
|
abs_base = os.path.abspath(base)
|
|
|
|
|
abs_full_path = os.path.abspath(os.path.join(base, path))
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if os.path.commonpath([abs_full_path, abs_base]) != abs_base:
|
|
|
|
|
raise self.severe('Problems with "%s" directive, prohibited path: %s' %
|
|
|
|
|
(self.name, path))
|
|
|
|
|
except ValueError:
|
|
|
|
|
# Paths don't have the same drive (Windows) or other incompatibility
|
|
|
|
|
raise self.severe('Problems with "%s" directive, invalid path: %s' %
|
|
|
|
|
(self.name, path))
|
2016-07-08 14:15:04 +02:00
|
|
|
|
|
|
|
|
self.arguments[0] = path
|
|
|
|
|
|
2025-09-01 15:21:21 +02:00
|
|
|
#
|
|
|
|
|
# Add path location to Sphinx dependencies to ensure proper cache
|
|
|
|
|
# invalidation check.
|
|
|
|
|
#
|
|
|
|
|
|
2022-03-26 11:27:22 +01:00
|
|
|
env.note_dependency(os.path.abspath(path))
|
|
|
|
|
|
2016-07-08 14:15:04 +02:00
|
|
|
if not self.state.document.settings.file_insertion_enabled:
|
|
|
|
|
raise self.warning('"%s" directive disabled.' % self.name)
|
2025-08-22 16:19:22 +02:00
|
|
|
source = self.state_machine.input_lines.source(self.lineno -
|
|
|
|
|
self.state_machine.input_offset - 1)
|
2016-07-08 14:15:04 +02:00
|
|
|
source_dir = os.path.dirname(os.path.abspath(source))
|
|
|
|
|
path = directives.path(self.arguments[0])
|
2025-08-22 16:19:22 +02:00
|
|
|
if path.startswith("<") and path.endswith(">"):
|
2016-07-08 14:15:04 +02:00
|
|
|
path = os.path.join(self.standard_include_path, path[1:-1])
|
|
|
|
|
path = os.path.normpath(os.path.join(source_dir, path))
|
|
|
|
|
|
|
|
|
|
# HINT: this is the only line I had to change / commented out:
|
2025-08-22 16:19:22 +02:00
|
|
|
# path = utils.relative_path(None, path)
|
2016-07-08 14:15:04 +02:00
|
|
|
|
2025-08-22 16:19:22 +02:00
|
|
|
encoding = self.options.get("encoding",
|
|
|
|
|
self.state.document.settings.input_encoding)
|
|
|
|
|
tab_width = self.options.get("tab-width",
|
|
|
|
|
self.state.document.settings.tab_width)
|
docs: kernel_include.py: allow cross-reference generation
kernel_include extension was originally designed to be used by the
media comprehensive uAPI documentation, where, instead of simpler
kernel-doc markups, the uAPI documentation is enriched with a larger
text, with images, complex tables, graphs, etc.
There, we wanted to include the much simpler yet documented .h
file.
This extension is needed to include files from other parts of the
Kernel tree outside Documentation, because the original Sphinx
include tag doesn't allow going outside of the directory passed
via sphinx-build command line.
Yet, the cross-references themselves to the full documentation
were using a perl script to create cross-references against the
comprehensive documentation.
As the perl script is now converted to Phython and there is a
Python class producing an include-compatible output with cross
references, add two optional arguments to kernel_include.py:
1. :generate-cross-refs:
If present, instead of reading the file, it calls ParseDataStructs()
class, which converts C data structures into cross-references to
be linked to ReST files containing a more comprehensive documentation;
Don't use it together with :start-line: and/or :end-line:, as
filtering input file line range is currently not supported.
2. :exception-file:
Used together with :generate-cross-refs:. Points to a file containing
rules to ignore C data structs or to use a different reference name,
optionally using a different reference type.
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/efc39c8e54a2056ae2fdb94d5006fcb19e227198.1755872208.git.mchehab+huawei@kernel.org
2025-08-22 16:19:23 +02:00
|
|
|
|
|
|
|
|
# Get optional arguments to related to cross-references generation
|
2025-08-22 16:19:29 +02:00
|
|
|
if "generate-cross-refs" in self.options:
|
2025-08-22 16:19:30 +02:00
|
|
|
return self.xref_text(env, path, tab_width)
|
docs: kernel_include.py: allow cross-reference generation
kernel_include extension was originally designed to be used by the
media comprehensive uAPI documentation, where, instead of simpler
kernel-doc markups, the uAPI documentation is enriched with a larger
text, with images, complex tables, graphs, etc.
There, we wanted to include the much simpler yet documented .h
file.
This extension is needed to include files from other parts of the
Kernel tree outside Documentation, because the original Sphinx
include tag doesn't allow going outside of the directory passed
via sphinx-build command line.
Yet, the cross-references themselves to the full documentation
were using a perl script to create cross-references against the
comprehensive documentation.
As the perl script is now converted to Phython and there is a
Python class producing an include-compatible output with cross
references, add two optional arguments to kernel_include.py:
1. :generate-cross-refs:
If present, instead of reading the file, it calls ParseDataStructs()
class, which converts C data structures into cross-references to
be linked to ReST files containing a more comprehensive documentation;
Don't use it together with :start-line: and/or :end-line:, as
filtering input file line range is currently not supported.
2. :exception-file:
Used together with :generate-cross-refs:. Points to a file containing
rules to ignore C data structs or to use a different reference name,
optionally using a different reference type.
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/efc39c8e54a2056ae2fdb94d5006fcb19e227198.1755872208.git.mchehab+huawei@kernel.org
2025-08-22 16:19:23 +02:00
|
|
|
|
2025-08-22 16:19:30 +02:00
|
|
|
rawtext = self.read_rawtext(path, encoding)
|
2025-08-22 16:19:26 +02:00
|
|
|
rawtext = self.apply_range(rawtext)
|
2016-07-08 14:15:04 +02:00
|
|
|
|
2025-08-22 16:19:30 +02:00
|
|
|
if "code" in self.options:
|
|
|
|
|
return self.code(path, tab_width, rawtext)
|
2025-08-22 16:19:28 +02:00
|
|
|
|
2025-08-22 16:19:30 +02:00
|
|
|
return self.literal(path, tab_width, rawtext)
|
2025-08-22 16:19:24 +02:00
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
|
|
|
|
|
reported = set()
|
|
|
|
|
|
|
|
|
|
def check_missing_refs(app, env, node, contnode):
|
|
|
|
|
"""Check broken refs for the files it creates xrefs"""
|
|
|
|
|
if not node.source:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
xref_files = env._xref_files
|
|
|
|
|
except AttributeError:
|
|
|
|
|
logger.critical("FATAL: _xref_files not initialized!")
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
# Only show missing references for kernel-include reference-parsed files
|
|
|
|
|
if node.source not in xref_files:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
target = node.get('reftarget', '')
|
|
|
|
|
domain = node.get('refdomain', 'std')
|
|
|
|
|
reftype = node.get('reftype', '')
|
|
|
|
|
|
|
|
|
|
msg = f"can't link to: {domain}:{reftype}:: {target}"
|
|
|
|
|
|
|
|
|
|
# Don't duplicate warnings
|
|
|
|
|
data = (node.source, msg)
|
|
|
|
|
if data in reported:
|
|
|
|
|
return None
|
|
|
|
|
reported.add(data)
|
|
|
|
|
|
|
|
|
|
logger.warning(msg, location=node, type='ref', subtype='missing')
|
|
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def merge_xref_info(app, env, docnames, other):
|
|
|
|
|
"""
|
|
|
|
|
As each process modify env._xref_files, we need to merge them back.
|
|
|
|
|
"""
|
|
|
|
|
if not hasattr(other, "_xref_files"):
|
|
|
|
|
return
|
|
|
|
|
env._xref_files.update(getattr(other, "_xref_files", set()))
|
|
|
|
|
|
|
|
|
|
def init_xref_docs(app, env, docnames):
|
|
|
|
|
"""Initialize a list of files that we're generating cross references¨"""
|
|
|
|
|
app.env._xref_files = set()
|
|
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
|
|
|
|
|
|
def setup(app):
|
|
|
|
|
"""Setup Sphinx exension"""
|
|
|
|
|
|
|
|
|
|
app.connect("env-before-read-docs", init_xref_docs)
|
|
|
|
|
app.connect("env-merge-info", merge_xref_info)
|
|
|
|
|
app.add_directive("kernel-include", KernelInclude)
|
|
|
|
|
app.connect("missing-reference", check_missing_refs)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"version": __version__,
|
|
|
|
|
"parallel_read_safe": True,
|
|
|
|
|
"parallel_write_safe": True,
|
|
|
|
|
}
|