18c2ecf20Sopenharmony_ci# coding=utf-8
28c2ecf20Sopenharmony_ci#
38c2ecf20Sopenharmony_ci# Copyright © 2016 Intel Corporation
48c2ecf20Sopenharmony_ci#
58c2ecf20Sopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a
68c2ecf20Sopenharmony_ci# copy of this software and associated documentation files (the "Software"),
78c2ecf20Sopenharmony_ci# to deal in the Software without restriction, including without limitation
88c2ecf20Sopenharmony_ci# the rights to use, copy, modify, merge, publish, distribute, sublicense,
98c2ecf20Sopenharmony_ci# and/or sell copies of the Software, and to permit persons to whom the
108c2ecf20Sopenharmony_ci# Software is furnished to do so, subject to the following conditions:
118c2ecf20Sopenharmony_ci#
128c2ecf20Sopenharmony_ci# The above copyright notice and this permission notice (including the next
138c2ecf20Sopenharmony_ci# paragraph) shall be included in all copies or substantial portions of the
148c2ecf20Sopenharmony_ci# Software.
158c2ecf20Sopenharmony_ci#
168c2ecf20Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
178c2ecf20Sopenharmony_ci# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
188c2ecf20Sopenharmony_ci# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
198c2ecf20Sopenharmony_ci# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
208c2ecf20Sopenharmony_ci# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
218c2ecf20Sopenharmony_ci# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
228c2ecf20Sopenharmony_ci# IN THE SOFTWARE.
238c2ecf20Sopenharmony_ci#
248c2ecf20Sopenharmony_ci# Authors:
258c2ecf20Sopenharmony_ci#    Jani Nikula <jani.nikula@intel.com>
268c2ecf20Sopenharmony_ci#
278c2ecf20Sopenharmony_ci# Please make sure this works on both python2 and python3.
288c2ecf20Sopenharmony_ci#
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ciimport codecs
318c2ecf20Sopenharmony_ciimport os
328c2ecf20Sopenharmony_ciimport subprocess
338c2ecf20Sopenharmony_ciimport sys
348c2ecf20Sopenharmony_ciimport re
358c2ecf20Sopenharmony_ciimport glob
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cifrom docutils import nodes, statemachine
388c2ecf20Sopenharmony_cifrom docutils.statemachine import ViewList
398c2ecf20Sopenharmony_cifrom docutils.parsers.rst import directives, Directive
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#
428c2ecf20Sopenharmony_ci# AutodocReporter is only good up to Sphinx 1.7
438c2ecf20Sopenharmony_ci#
448c2ecf20Sopenharmony_ciimport sphinx
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ciUse_SSI = sphinx.__version__[:3] >= '1.7'
478c2ecf20Sopenharmony_ciif Use_SSI:
488c2ecf20Sopenharmony_ci    from sphinx.util.docutils import switch_source_input
498c2ecf20Sopenharmony_cielse:
508c2ecf20Sopenharmony_ci    from sphinx.ext.autodoc import AutodocReporter
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ciimport kernellog
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci__version__  = '1.0'
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ciclass KernelDocDirective(Directive):
578c2ecf20Sopenharmony_ci    """Extract kernel-doc comments from the specified file"""
588c2ecf20Sopenharmony_ci    required_argument = 1
598c2ecf20Sopenharmony_ci    optional_arguments = 4
608c2ecf20Sopenharmony_ci    option_spec = {
618c2ecf20Sopenharmony_ci        'doc': directives.unchanged_required,
628c2ecf20Sopenharmony_ci        'export': directives.unchanged,
638c2ecf20Sopenharmony_ci        'internal': directives.unchanged,
648c2ecf20Sopenharmony_ci        'identifiers': directives.unchanged,
658c2ecf20Sopenharmony_ci        'no-identifiers': directives.unchanged,
668c2ecf20Sopenharmony_ci        'functions': directives.unchanged,
678c2ecf20Sopenharmony_ci    }
688c2ecf20Sopenharmony_ci    has_content = False
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci    def run(self):
718c2ecf20Sopenharmony_ci        env = self.state.document.settings.env
728c2ecf20Sopenharmony_ci        cmd = [env.config.kerneldoc_bin, '-rst', '-enable-lineno']
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	# Pass the version string to kernel-doc, as it needs to use a different
758c2ecf20Sopenharmony_ci	# dialect, depending what the C domain supports for each specific
768c2ecf20Sopenharmony_ci	# Sphinx versions
778c2ecf20Sopenharmony_ci        cmd += ['-sphinx-version', sphinx.__version__]
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci        filename = env.config.kerneldoc_srctree + '/' + self.arguments[0]
808c2ecf20Sopenharmony_ci        export_file_patterns = []
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci        # Tell sphinx of the dependency
838c2ecf20Sopenharmony_ci        env.note_dependency(os.path.abspath(filename))
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci        tab_width = self.options.get('tab-width', self.state.document.settings.tab_width)
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci        # 'function' is an alias of 'identifiers'
888c2ecf20Sopenharmony_ci        if 'functions' in self.options:
898c2ecf20Sopenharmony_ci            self.options['identifiers'] = self.options.get('functions')
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci        # FIXME: make this nicer and more robust against errors
928c2ecf20Sopenharmony_ci        if 'export' in self.options:
938c2ecf20Sopenharmony_ci            cmd += ['-export']
948c2ecf20Sopenharmony_ci            export_file_patterns = str(self.options.get('export')).split()
958c2ecf20Sopenharmony_ci        elif 'internal' in self.options:
968c2ecf20Sopenharmony_ci            cmd += ['-internal']
978c2ecf20Sopenharmony_ci            export_file_patterns = str(self.options.get('internal')).split()
988c2ecf20Sopenharmony_ci        elif 'doc' in self.options:
998c2ecf20Sopenharmony_ci            cmd += ['-function', str(self.options.get('doc'))]
1008c2ecf20Sopenharmony_ci        elif 'identifiers' in self.options:
1018c2ecf20Sopenharmony_ci            identifiers = self.options.get('identifiers').split()
1028c2ecf20Sopenharmony_ci            if identifiers:
1038c2ecf20Sopenharmony_ci                for i in identifiers:
1048c2ecf20Sopenharmony_ci                    cmd += ['-function', i]
1058c2ecf20Sopenharmony_ci            else:
1068c2ecf20Sopenharmony_ci                cmd += ['-no-doc-sections']
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci        if 'no-identifiers' in self.options:
1098c2ecf20Sopenharmony_ci            no_identifiers = self.options.get('no-identifiers').split()
1108c2ecf20Sopenharmony_ci            if no_identifiers:
1118c2ecf20Sopenharmony_ci                for i in no_identifiers:
1128c2ecf20Sopenharmony_ci                    cmd += ['-nosymbol', i]
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci        for pattern in export_file_patterns:
1158c2ecf20Sopenharmony_ci            for f in glob.glob(env.config.kerneldoc_srctree + '/' + pattern):
1168c2ecf20Sopenharmony_ci                env.note_dependency(os.path.abspath(f))
1178c2ecf20Sopenharmony_ci                cmd += ['-export-file', f]
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci        cmd += [filename]
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci        try:
1228c2ecf20Sopenharmony_ci            kernellog.verbose(env.app,
1238c2ecf20Sopenharmony_ci                              'calling kernel-doc \'%s\'' % (" ".join(cmd)))
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci            p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1268c2ecf20Sopenharmony_ci            out, err = p.communicate()
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci            out, err = codecs.decode(out, 'utf-8'), codecs.decode(err, 'utf-8')
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci            if p.returncode != 0:
1318c2ecf20Sopenharmony_ci                sys.stderr.write(err)
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci                kernellog.warn(env.app,
1348c2ecf20Sopenharmony_ci                               'kernel-doc \'%s\' failed with return code %d' % (" ".join(cmd), p.returncode))
1358c2ecf20Sopenharmony_ci                return [nodes.error(None, nodes.paragraph(text = "kernel-doc missing"))]
1368c2ecf20Sopenharmony_ci            elif env.config.kerneldoc_verbosity > 0:
1378c2ecf20Sopenharmony_ci                sys.stderr.write(err)
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci            lines = statemachine.string2lines(out, tab_width, convert_whitespace=True)
1408c2ecf20Sopenharmony_ci            result = ViewList()
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci            lineoffset = 0;
1438c2ecf20Sopenharmony_ci            line_regex = re.compile("^#define LINENO ([0-9]+)$")
1448c2ecf20Sopenharmony_ci            for line in lines:
1458c2ecf20Sopenharmony_ci                match = line_regex.search(line)
1468c2ecf20Sopenharmony_ci                if match:
1478c2ecf20Sopenharmony_ci                    # sphinx counts lines from 0
1488c2ecf20Sopenharmony_ci                    lineoffset = int(match.group(1)) - 1
1498c2ecf20Sopenharmony_ci                    # we must eat our comments since the upset the markup
1508c2ecf20Sopenharmony_ci                else:
1518c2ecf20Sopenharmony_ci                    doc = env.srcdir + "/" + env.docname + ":" + str(self.lineno)
1528c2ecf20Sopenharmony_ci                    result.append(line, doc + ": " + filename, lineoffset)
1538c2ecf20Sopenharmony_ci                    lineoffset += 1
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci            node = nodes.section()
1568c2ecf20Sopenharmony_ci            self.do_parse(result, node)
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci            return node.children
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci        except Exception as e:  # pylint: disable=W0703
1618c2ecf20Sopenharmony_ci            kernellog.warn(env.app, 'kernel-doc \'%s\' processing failed with: %s' %
1628c2ecf20Sopenharmony_ci                           (" ".join(cmd), str(e)))
1638c2ecf20Sopenharmony_ci            return [nodes.error(None, nodes.paragraph(text = "kernel-doc missing"))]
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci    def do_parse(self, result, node):
1668c2ecf20Sopenharmony_ci        if Use_SSI:
1678c2ecf20Sopenharmony_ci            with switch_source_input(self.state, result):
1688c2ecf20Sopenharmony_ci                self.state.nested_parse(result, 0, node, match_titles=1)
1698c2ecf20Sopenharmony_ci        else:
1708c2ecf20Sopenharmony_ci            save = self.state.memo.title_styles, self.state.memo.section_level, self.state.memo.reporter
1718c2ecf20Sopenharmony_ci            self.state.memo.reporter = AutodocReporter(result, self.state.memo.reporter)
1728c2ecf20Sopenharmony_ci            self.state.memo.title_styles, self.state.memo.section_level = [], 0
1738c2ecf20Sopenharmony_ci            try:
1748c2ecf20Sopenharmony_ci                self.state.nested_parse(result, 0, node, match_titles=1)
1758c2ecf20Sopenharmony_ci            finally:
1768c2ecf20Sopenharmony_ci                self.state.memo.title_styles, self.state.memo.section_level, self.state.memo.reporter = save
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cidef setup(app):
1808c2ecf20Sopenharmony_ci    app.add_config_value('kerneldoc_bin', None, 'env')
1818c2ecf20Sopenharmony_ci    app.add_config_value('kerneldoc_srctree', None, 'env')
1828c2ecf20Sopenharmony_ci    app.add_config_value('kerneldoc_verbosity', 1, 'env')
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci    app.add_directive('kernel-doc', KernelDocDirective)
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci    return dict(
1878c2ecf20Sopenharmony_ci        version = __version__,
1888c2ecf20Sopenharmony_ci        parallel_read_safe = True,
1898c2ecf20Sopenharmony_ci        parallel_write_safe = True
1908c2ecf20Sopenharmony_ci    )
191