^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) # coding=utf-8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) # Copyright © 2016 Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) # Permission is hereby granted, free of charge, to any person obtaining a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) # copy of this software and associated documentation files (the "Software"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) # to deal in the Software without restriction, including without limitation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) # the rights to use, copy, modify, merge, publish, distribute, sublicense,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) # and/or sell copies of the Software, and to permit persons to whom the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) # Software is furnished to do so, subject to the following conditions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) # The above copyright notice and this permission notice (including the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) # paragraph) shall be included in all copies or substantial portions of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) # Software.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) # IN THE SOFTWARE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) # Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) # Jani Nikula <jani.nikula@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) # Please make sure this works on both python2 and python3.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) import codecs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) import os
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) import subprocess
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) import sys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) import re
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) import glob
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) from docutils import nodes, statemachine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) from docutils.statemachine import ViewList
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) from docutils.parsers.rst import directives, Directive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) # AutodocReporter is only good up to Sphinx 1.7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) import sphinx
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) Use_SSI = sphinx.__version__[:3] >= '1.7'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if Use_SSI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) from sphinx.util.docutils import switch_source_input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) from sphinx.ext.autodoc import AutodocReporter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) import kernellog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) __version__ = '1.0'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) class KernelDocDirective(Directive):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) """Extract kernel-doc comments from the specified file"""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) required_argument = 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) optional_arguments = 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) option_spec = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) 'doc': directives.unchanged_required,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) 'export': directives.unchanged,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) 'internal': directives.unchanged,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) 'identifiers': directives.unchanged,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) 'no-identifiers': directives.unchanged,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) 'functions': directives.unchanged,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) has_content = False
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) def run(self):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) env = self.state.document.settings.env
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) cmd = [env.config.kerneldoc_bin, '-rst', '-enable-lineno']
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) # Pass the version string to kernel-doc, as it needs to use a different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) # dialect, depending what the C domain supports for each specific
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) # Sphinx versions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) cmd += ['-sphinx-version', sphinx.__version__]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) filename = env.config.kerneldoc_srctree + '/' + self.arguments[0]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) export_file_patterns = []
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) # Tell sphinx of the dependency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) env.note_dependency(os.path.abspath(filename))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) tab_width = self.options.get('tab-width', self.state.document.settings.tab_width)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) # 'function' is an alias of 'identifiers'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if 'functions' in self.options:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) self.options['identifiers'] = self.options.get('functions')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) # FIXME: make this nicer and more robust against errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if 'export' in self.options:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) cmd += ['-export']
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) export_file_patterns = str(self.options.get('export')).split()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) elif 'internal' in self.options:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) cmd += ['-internal']
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) export_file_patterns = str(self.options.get('internal')).split()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) elif 'doc' in self.options:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) cmd += ['-function', str(self.options.get('doc'))]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) elif 'identifiers' in self.options:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) identifiers = self.options.get('identifiers').split()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if identifiers:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) for i in identifiers:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) cmd += ['-function', i]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) cmd += ['-no-doc-sections']
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if 'no-identifiers' in self.options:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) no_identifiers = self.options.get('no-identifiers').split()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if no_identifiers:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) for i in no_identifiers:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) cmd += ['-nosymbol', i]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) for pattern in export_file_patterns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) for f in glob.glob(env.config.kerneldoc_srctree + '/' + pattern):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) env.note_dependency(os.path.abspath(f))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) cmd += ['-export-file', f]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) cmd += [filename]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) try:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) kernellog.verbose(env.app,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 'calling kernel-doc \'%s\'' % (" ".join(cmd)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) out, err = p.communicate()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) out, err = codecs.decode(out, 'utf-8'), codecs.decode(err, 'utf-8')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if p.returncode != 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) sys.stderr.write(err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) kernellog.warn(env.app,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 'kernel-doc \'%s\' failed with return code %d' % (" ".join(cmd), p.returncode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return [nodes.error(None, nodes.paragraph(text = "kernel-doc missing"))]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) elif env.config.kerneldoc_verbosity > 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) sys.stderr.write(err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) lines = statemachine.string2lines(out, tab_width, convert_whitespace=True)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) result = ViewList()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) lineoffset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) line_regex = re.compile("^#define LINENO ([0-9]+)$")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) for line in lines:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) match = line_regex.search(line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if match:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) # sphinx counts lines from 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) lineoffset = int(match.group(1)) - 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) # we must eat our comments since the upset the markup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) doc = env.srcdir + "/" + env.docname + ":" + str(self.lineno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) result.append(line, doc + ": " + filename, lineoffset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) lineoffset += 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) node = nodes.section()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) self.do_parse(result, node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return node.children
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) except Exception as e: # pylint: disable=W0703
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) kernellog.warn(env.app, 'kernel-doc \'%s\' processing failed with: %s' %
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) (" ".join(cmd), str(e)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return [nodes.error(None, nodes.paragraph(text = "kernel-doc missing"))]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) def do_parse(self, result, node):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if Use_SSI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) with switch_source_input(self.state, result):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) self.state.nested_parse(result, 0, node, match_titles=1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) else:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) save = self.state.memo.title_styles, self.state.memo.section_level, self.state.memo.reporter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) self.state.memo.reporter = AutodocReporter(result, self.state.memo.reporter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) self.state.memo.title_styles, self.state.memo.section_level = [], 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) try:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) self.state.nested_parse(result, 0, node, match_titles=1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) finally:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) self.state.memo.title_styles, self.state.memo.section_level, self.state.memo.reporter = save
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) def setup(app):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) app.add_config_value('kerneldoc_bin', None, 'env')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) app.add_config_value('kerneldoc_srctree', None, 'env')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) app.add_config_value('kerneldoc_verbosity', 1, 'env')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) app.add_directive('kernel-doc', KernelDocDirective)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return dict(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) version = __version__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) parallel_read_safe = True,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) parallel_write_safe = True
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) )