blob: 87a1332fe934f01f75542cdc255a10cb513e9cb7 [file] [log] [blame]
Jani Nikulac56de1d2016-05-18 23:30:30 +03001# 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
27import os
28import subprocess
29import sys
30
31from docutils import nodes, statemachine
32from docutils.parsers.rst import directives
33from sphinx.util.compat import Directive
34
35class KernelDocDirective(Directive):
36 """Extract kernel-doc comments from the specified file"""
37 required_argument = 1
38 optional_arguments = 4
39 option_spec = {
40 'doc': directives.unchanged_required,
41 'functions': directives.unchanged_required,
42 'export': directives.flag,
43 'internal': directives.flag,
44 }
45 has_content = False
46
47 def run(self):
48 env = self.state.document.settings.env
49 cmd = [env.config.kerneldoc_bin, '-rst']
50
51 filename = env.config.kerneldoc_srctree + '/' + self.arguments[0]
52
53 # Tell sphinx of the dependency
54 env.note_dependency(os.path.abspath(filename))
55
56 tab_width = self.options.get('tab-width', self.state.document.settings.tab_width)
57 source = self.state_machine.input_lines.source(self.lineno - self.state_machine.input_offset - 1)
58
59 # FIXME: make this nicer and more robust against errors
60 if 'export' in self.options:
61 cmd += ['-export']
62 elif 'internal' in self.options:
63 cmd += ['-internal']
64 elif 'doc' in self.options:
65 cmd += ['-function', str(self.options.get('doc'))]
66 elif 'functions' in self.options:
67 for f in str(self.options.get('functions')).split(' '):
68 cmd += ['-function', f]
69
70 cmd += [filename]
71
72 try:
73 env.app.verbose('calling kernel-doc \'%s\'' % (" ".join(cmd)))
74
75 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
76 out, err = p.communicate()
77
78 # assume the kernel sources are utf-8
79 out, err = unicode(out, 'utf-8'), unicode(err, 'utf-8')
80
81 if p.returncode != 0:
82 sys.stderr.write(err)
83
84 env.app.warn('kernel-doc \'%s\' failed with return code %d' % (" ".join(cmd), p.returncode))
85 return [nodes.error(None, nodes.paragraph(text = "kernel-doc missing"))]
86 elif env.config.kerneldoc_verbosity > 0:
87 sys.stderr.write(err)
88
89 lines = statemachine.string2lines(out, tab_width, convert_whitespace=True)
90 self.state_machine.insert_input(lines, source)
91 return []
92 except Exception as e:
93 env.app.warn('kernel-doc \'%s\' processing failed with: %s' %
94 (" ".join(cmd), str(e)))
95 return [nodes.error(None, nodes.paragraph(text = "kernel-doc missing"))]
96
97def setup(app):
98 app.add_config_value('kerneldoc_bin', None, 'env')
99 app.add_config_value('kerneldoc_srctree', None, 'env')
100 app.add_config_value('kerneldoc_verbosity', 1, 'env')
101
102 app.add_directive('kernel-doc', KernelDocDirective)