162306a36Sopenharmony_ci# coding=utf-8 262306a36Sopenharmony_ci# 362306a36Sopenharmony_ci# Copyright © 2016 Intel Corporation 462306a36Sopenharmony_ci# 562306a36Sopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a 662306a36Sopenharmony_ci# copy of this software and associated documentation files (the "Software"), 762306a36Sopenharmony_ci# to deal in the Software without restriction, including without limitation 862306a36Sopenharmony_ci# the rights to use, copy, modify, merge, publish, distribute, sublicense, 962306a36Sopenharmony_ci# and/or sell copies of the Software, and to permit persons to whom the 1062306a36Sopenharmony_ci# Software is furnished to do so, subject to the following conditions: 1162306a36Sopenharmony_ci# 1262306a36Sopenharmony_ci# The above copyright notice and this permission notice (including the next 1362306a36Sopenharmony_ci# paragraph) shall be included in all copies or substantial portions of the 1462306a36Sopenharmony_ci# Software. 1562306a36Sopenharmony_ci# 1662306a36Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1762306a36Sopenharmony_ci# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1862306a36Sopenharmony_ci# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1962306a36Sopenharmony_ci# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2062306a36Sopenharmony_ci# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 2162306a36Sopenharmony_ci# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 2262306a36Sopenharmony_ci# IN THE SOFTWARE. 2362306a36Sopenharmony_ci# 2462306a36Sopenharmony_ci# Authors: 2562306a36Sopenharmony_ci# Jani Nikula <jani.nikula@intel.com> 2662306a36Sopenharmony_ci# 2762306a36Sopenharmony_ci# Please make sure this works on both python2 and python3. 2862306a36Sopenharmony_ci# 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ciimport codecs 3162306a36Sopenharmony_ciimport os 3262306a36Sopenharmony_ciimport subprocess 3362306a36Sopenharmony_ciimport sys 3462306a36Sopenharmony_ciimport re 3562306a36Sopenharmony_ciimport glob 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_cifrom docutils import nodes, statemachine 3862306a36Sopenharmony_cifrom docutils.statemachine import ViewList 3962306a36Sopenharmony_cifrom docutils.parsers.rst import directives, Directive 4062306a36Sopenharmony_ciimport sphinx 4162306a36Sopenharmony_cifrom sphinx.util.docutils import switch_source_input 4262306a36Sopenharmony_ciimport kernellog 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci__version__ = '1.0' 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ciclass KernelDocDirective(Directive): 4762306a36Sopenharmony_ci """Extract kernel-doc comments from the specified file""" 4862306a36Sopenharmony_ci required_argument = 1 4962306a36Sopenharmony_ci optional_arguments = 4 5062306a36Sopenharmony_ci option_spec = { 5162306a36Sopenharmony_ci 'doc': directives.unchanged_required, 5262306a36Sopenharmony_ci 'export': directives.unchanged, 5362306a36Sopenharmony_ci 'internal': directives.unchanged, 5462306a36Sopenharmony_ci 'identifiers': directives.unchanged, 5562306a36Sopenharmony_ci 'no-identifiers': directives.unchanged, 5662306a36Sopenharmony_ci 'functions': directives.unchanged, 5762306a36Sopenharmony_ci } 5862306a36Sopenharmony_ci has_content = False 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci def run(self): 6162306a36Sopenharmony_ci env = self.state.document.settings.env 6262306a36Sopenharmony_ci cmd = [env.config.kerneldoc_bin, '-rst', '-enable-lineno'] 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci # Pass the version string to kernel-doc, as it needs to use a different 6562306a36Sopenharmony_ci # dialect, depending what the C domain supports for each specific 6662306a36Sopenharmony_ci # Sphinx versions 6762306a36Sopenharmony_ci cmd += ['-sphinx-version', sphinx.__version__] 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci filename = env.config.kerneldoc_srctree + '/' + self.arguments[0] 7062306a36Sopenharmony_ci export_file_patterns = [] 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci # Tell sphinx of the dependency 7362306a36Sopenharmony_ci env.note_dependency(os.path.abspath(filename)) 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci tab_width = self.options.get('tab-width', self.state.document.settings.tab_width) 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci # 'function' is an alias of 'identifiers' 7862306a36Sopenharmony_ci if 'functions' in self.options: 7962306a36Sopenharmony_ci self.options['identifiers'] = self.options.get('functions') 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci # FIXME: make this nicer and more robust against errors 8262306a36Sopenharmony_ci if 'export' in self.options: 8362306a36Sopenharmony_ci cmd += ['-export'] 8462306a36Sopenharmony_ci export_file_patterns = str(self.options.get('export')).split() 8562306a36Sopenharmony_ci elif 'internal' in self.options: 8662306a36Sopenharmony_ci cmd += ['-internal'] 8762306a36Sopenharmony_ci export_file_patterns = str(self.options.get('internal')).split() 8862306a36Sopenharmony_ci elif 'doc' in self.options: 8962306a36Sopenharmony_ci cmd += ['-function', str(self.options.get('doc'))] 9062306a36Sopenharmony_ci elif 'identifiers' in self.options: 9162306a36Sopenharmony_ci identifiers = self.options.get('identifiers').split() 9262306a36Sopenharmony_ci if identifiers: 9362306a36Sopenharmony_ci for i in identifiers: 9462306a36Sopenharmony_ci cmd += ['-function', i] 9562306a36Sopenharmony_ci else: 9662306a36Sopenharmony_ci cmd += ['-no-doc-sections'] 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci if 'no-identifiers' in self.options: 9962306a36Sopenharmony_ci no_identifiers = self.options.get('no-identifiers').split() 10062306a36Sopenharmony_ci if no_identifiers: 10162306a36Sopenharmony_ci for i in no_identifiers: 10262306a36Sopenharmony_ci cmd += ['-nosymbol', i] 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci for pattern in export_file_patterns: 10562306a36Sopenharmony_ci for f in glob.glob(env.config.kerneldoc_srctree + '/' + pattern): 10662306a36Sopenharmony_ci env.note_dependency(os.path.abspath(f)) 10762306a36Sopenharmony_ci cmd += ['-export-file', f] 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci cmd += [filename] 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci try: 11262306a36Sopenharmony_ci kernellog.verbose(env.app, 11362306a36Sopenharmony_ci 'calling kernel-doc \'%s\'' % (" ".join(cmd))) 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 11662306a36Sopenharmony_ci out, err = p.communicate() 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci out, err = codecs.decode(out, 'utf-8'), codecs.decode(err, 'utf-8') 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ci if p.returncode != 0: 12162306a36Sopenharmony_ci sys.stderr.write(err) 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci kernellog.warn(env.app, 12462306a36Sopenharmony_ci 'kernel-doc \'%s\' failed with return code %d' % (" ".join(cmd), p.returncode)) 12562306a36Sopenharmony_ci return [nodes.error(None, nodes.paragraph(text = "kernel-doc missing"))] 12662306a36Sopenharmony_ci elif env.config.kerneldoc_verbosity > 0: 12762306a36Sopenharmony_ci sys.stderr.write(err) 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci lines = statemachine.string2lines(out, tab_width, convert_whitespace=True) 13062306a36Sopenharmony_ci result = ViewList() 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci lineoffset = 0; 13362306a36Sopenharmony_ci line_regex = re.compile(r"^\.\. LINENO ([0-9]+)$") 13462306a36Sopenharmony_ci for line in lines: 13562306a36Sopenharmony_ci match = line_regex.search(line) 13662306a36Sopenharmony_ci if match: 13762306a36Sopenharmony_ci # sphinx counts lines from 0 13862306a36Sopenharmony_ci lineoffset = int(match.group(1)) - 1 13962306a36Sopenharmony_ci # we must eat our comments since the upset the markup 14062306a36Sopenharmony_ci else: 14162306a36Sopenharmony_ci doc = env.srcdir + "/" + env.docname + ":" + str(self.lineno) 14262306a36Sopenharmony_ci result.append(line, doc + ": " + filename, lineoffset) 14362306a36Sopenharmony_ci lineoffset += 1 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ci node = nodes.section() 14662306a36Sopenharmony_ci self.do_parse(result, node) 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci return node.children 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci except Exception as e: # pylint: disable=W0703 15162306a36Sopenharmony_ci kernellog.warn(env.app, 'kernel-doc \'%s\' processing failed with: %s' % 15262306a36Sopenharmony_ci (" ".join(cmd), str(e))) 15362306a36Sopenharmony_ci return [nodes.error(None, nodes.paragraph(text = "kernel-doc missing"))] 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci def do_parse(self, result, node): 15662306a36Sopenharmony_ci with switch_source_input(self.state, result): 15762306a36Sopenharmony_ci self.state.nested_parse(result, 0, node, match_titles=1) 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_cidef setup(app): 16062306a36Sopenharmony_ci app.add_config_value('kerneldoc_bin', None, 'env') 16162306a36Sopenharmony_ci app.add_config_value('kerneldoc_srctree', None, 'env') 16262306a36Sopenharmony_ci app.add_config_value('kerneldoc_verbosity', 1, 'env') 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_ci app.add_directive('kernel-doc', KernelDocDirective) 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ci return dict( 16762306a36Sopenharmony_ci version = __version__, 16862306a36Sopenharmony_ci parallel_read_safe = True, 16962306a36Sopenharmony_ci parallel_write_safe = True 17062306a36Sopenharmony_ci ) 171