kerneldoc.py 7.0 KB

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