xref: /openbmc/qemu/scripts/qapi/gen.py (revision 12893a8ea7163e871abae05b5a42cdce1ad45225)
1# -*- coding: utf-8 -*-
2#
3# QAPI code generation
4#
5# Copyright (c) 2015-2019 Red Hat Inc.
6#
7# Authors:
8#  Markus Armbruster <armbru@redhat.com>
9#  Marc-André Lureau <marcandre.lureau@redhat.com>
10#
11# This work is licensed under the terms of the GNU GPL, version 2.
12# See the COPYING file in the top-level directory.
13
14from contextlib import contextmanager
15import os
16import re
17from typing import (
18    Dict,
19    Iterator,
20    List,
21    Optional,
22    Tuple,
23)
24
25from .common import (
26    c_fname,
27    c_name,
28    gen_endif,
29    gen_if,
30    guardend,
31    guardstart,
32    mcgen,
33)
34from .schema import (
35    QAPISchemaModule,
36    QAPISchemaObjectType,
37    QAPISchemaVisitor,
38)
39from .source import QAPISourceInfo
40
41
42class QAPIGen:
43    def __init__(self, fname: Optional[str]):
44        self.fname = fname
45        self._preamble = ''
46        self._body = ''
47
48    def preamble_add(self, text: str) -> None:
49        self._preamble += text
50
51    def add(self, text: str) -> None:
52        self._body += text
53
54    def get_content(self) -> str:
55        return self._top() + self._preamble + self._body + self._bottom()
56
57    def _top(self) -> str:
58        # pylint: disable=no-self-use
59        return ''
60
61    def _bottom(self) -> str:
62        # pylint: disable=no-self-use
63        return ''
64
65    def write(self, output_dir: str) -> None:
66        # Include paths starting with ../ are used to reuse modules of the main
67        # schema in specialised schemas. Don't overwrite the files that are
68        # already generated for the main schema.
69        if self.fname.startswith('../'):
70            return
71        pathname = os.path.join(output_dir, self.fname)
72        odir = os.path.dirname(pathname)
73
74        if odir:
75            os.makedirs(odir, exist_ok=True)
76
77        # use os.open for O_CREAT to create and read a non-existant file
78        fd = os.open(pathname, os.O_RDWR | os.O_CREAT, 0o666)
79        with os.fdopen(fd, 'r+', encoding='utf-8') as fp:
80            text = self.get_content()
81            oldtext = fp.read(len(text) + 1)
82            if text != oldtext:
83                fp.seek(0)
84                fp.truncate(0)
85                fp.write(text)
86
87
88def _wrap_ifcond(ifcond: List[str], before: str, after: str) -> str:
89    if before == after:
90        return after   # suppress empty #if ... #endif
91
92    assert after.startswith(before)
93    out = before
94    added = after[len(before):]
95    if added[0] == '\n':
96        out += '\n'
97        added = added[1:]
98    out += gen_if(ifcond)
99    out += added
100    out += gen_endif(ifcond)
101    return out
102
103
104def build_params(arg_type: Optional[QAPISchemaObjectType],
105                 boxed: bool,
106                 extra: Optional[str] = None) -> str:
107    ret = ''
108    sep = ''
109    if boxed:
110        assert arg_type
111        ret += '%s arg' % arg_type.c_param_type()
112        sep = ', '
113    elif arg_type:
114        assert not arg_type.variants
115        for memb in arg_type.members:
116            ret += sep
117            sep = ', '
118            if memb.optional:
119                ret += 'bool has_%s, ' % c_name(memb.name)
120            ret += '%s %s' % (memb.type.c_param_type(),
121                              c_name(memb.name))
122    if extra:
123        ret += sep + extra
124    return ret if ret else 'void'
125
126
127class QAPIGenCCode(QAPIGen):
128    def __init__(self, fname: Optional[str]):
129        super().__init__(fname)
130        self._start_if: Optional[Tuple[List[str], str, str]] = None
131
132    def start_if(self, ifcond: List[str]) -> None:
133        assert self._start_if is None
134        self._start_if = (ifcond, self._body, self._preamble)
135
136    def end_if(self) -> None:
137        assert self._start_if is not None
138        self._body = _wrap_ifcond(self._start_if[0],
139                                  self._start_if[1], self._body)
140        self._preamble = _wrap_ifcond(self._start_if[0],
141                                      self._start_if[2], self._preamble)
142        self._start_if = None
143
144    def get_content(self) -> str:
145        assert self._start_if is None
146        return super().get_content()
147
148
149class QAPIGenC(QAPIGenCCode):
150    def __init__(self, fname: str, blurb: str, pydoc: str):
151        super().__init__(fname)
152        self._blurb = blurb
153        self._copyright = '\n * '.join(re.findall(r'^Copyright .*', pydoc,
154                                                  re.MULTILINE))
155
156    def _top(self) -> str:
157        return mcgen('''
158/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
159
160/*
161%(blurb)s
162 *
163 * %(copyright)s
164 *
165 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
166 * See the COPYING.LIB file in the top-level directory.
167 */
168
169''',
170                     blurb=self._blurb, copyright=self._copyright)
171
172    def _bottom(self) -> str:
173        return mcgen('''
174
175/* Dummy declaration to prevent empty .o file */
176char qapi_dummy_%(name)s;
177''',
178                     name=c_fname(self.fname))
179
180
181class QAPIGenH(QAPIGenC):
182    def _top(self) -> str:
183        return super()._top() + guardstart(self.fname)
184
185    def _bottom(self) -> str:
186        return guardend(self.fname)
187
188
189@contextmanager
190def ifcontext(ifcond: List[str], *args: QAPIGenCCode) -> Iterator[None]:
191    """
192    A with-statement context manager that wraps with `start_if()` / `end_if()`.
193
194    :param ifcond: A list of conditionals, passed to `start_if()`.
195    :param args: any number of `QAPIGenCCode`.
196
197    Example::
198
199        with ifcontext(ifcond, self._genh, self._genc):
200            modify self._genh and self._genc ...
201
202    Is equivalent to calling::
203
204        self._genh.start_if(ifcond)
205        self._genc.start_if(ifcond)
206        modify self._genh and self._genc ...
207        self._genh.end_if()
208        self._genc.end_if()
209    """
210    for arg in args:
211        arg.start_if(ifcond)
212    yield
213    for arg in args:
214        arg.end_if()
215
216
217class QAPISchemaMonolithicCVisitor(QAPISchemaVisitor):
218    def __init__(self,
219                 prefix: str,
220                 what: str,
221                 blurb: str,
222                 pydoc: str):
223        self._prefix = prefix
224        self._what = what
225        self._genc = QAPIGenC(self._prefix + self._what + '.c',
226                              blurb, pydoc)
227        self._genh = QAPIGenH(self._prefix + self._what + '.h',
228                              blurb, pydoc)
229
230    def write(self, output_dir: str) -> None:
231        self._genc.write(output_dir)
232        self._genh.write(output_dir)
233
234
235class QAPISchemaModularCVisitor(QAPISchemaVisitor):
236    def __init__(self,
237                 prefix: str,
238                 what: str,
239                 user_blurb: str,
240                 builtin_blurb: Optional[str],
241                 pydoc: str):
242        self._prefix = prefix
243        self._what = what
244        self._user_blurb = user_blurb
245        self._builtin_blurb = builtin_blurb
246        self._pydoc = pydoc
247        self._genc: Optional[QAPIGenC] = None
248        self._genh: Optional[QAPIGenH] = None
249        self._module: Dict[Optional[str], Tuple[QAPIGenC, QAPIGenH]] = {}
250        self._main_module: Optional[str] = None
251
252    @staticmethod
253    def _module_dirname(name: Optional[str]) -> str:
254        if QAPISchemaModule.is_user_module(name):
255            return os.path.dirname(name)
256        return ''
257
258    def _module_basename(self, what: str, name: Optional[str]) -> str:
259        ret = '' if QAPISchemaModule.is_builtin_module(name) else self._prefix
260        if QAPISchemaModule.is_user_module(name):
261            basename = os.path.basename(name)
262            ret += what
263            if name != self._main_module:
264                ret += '-' + os.path.splitext(basename)[0]
265        else:
266            name = name[2:] if name else 'builtin'
267            ret += re.sub(r'-', '-' + name + '-', what)
268        return ret
269
270    def _module_filename(self, what: str, name: Optional[str]) -> str:
271        return os.path.join(self._module_dirname(name),
272                            self._module_basename(what, name))
273
274    def _add_module(self, name: Optional[str], blurb: str) -> None:
275        basename = self._module_filename(self._what, name)
276        genc = QAPIGenC(basename + '.c', blurb, self._pydoc)
277        genh = QAPIGenH(basename + '.h', blurb, self._pydoc)
278        self._module[name] = (genc, genh)
279        self._genc, self._genh = self._module[name]
280
281    def _add_user_module(self, name: str, blurb: str) -> None:
282        assert QAPISchemaModule.is_user_module(name)
283        if self._main_module is None:
284            self._main_module = name
285        self._add_module(name, blurb)
286
287    def _add_system_module(self, name: Optional[str], blurb: str) -> None:
288        assert QAPISchemaModule.is_system_module(name)
289        self._add_module(name, blurb)
290
291    def write(self, output_dir: str, opt_builtins: bool = False) -> None:
292        for name in self._module:
293            if QAPISchemaModule.is_builtin_module(name) and not opt_builtins:
294                continue
295            (genc, genh) = self._module[name]
296            genc.write(output_dir)
297            genh.write(output_dir)
298
299    def _begin_builtin_module(self) -> None:
300        pass
301
302    def _begin_user_module(self, name: str) -> None:
303        pass
304
305    def visit_module(self, name: Optional[str]) -> None:
306        if QAPISchemaModule.is_builtin_module(name):
307            if self._builtin_blurb:
308                self._add_system_module(name, self._builtin_blurb)
309                self._begin_builtin_module()
310            else:
311                # The built-in module has not been created.  No code may
312                # be generated.
313                self._genc = None
314                self._genh = None
315        else:
316            assert QAPISchemaModule.is_user_module(name)
317            self._add_user_module(name, self._user_blurb)
318            self._begin_user_module(name)
319
320    def visit_include(self, name: str, info: QAPISourceInfo) -> None:
321        relname = os.path.relpath(self._module_filename(self._what, name),
322                                  os.path.dirname(self._genh.fname))
323        self._genh.preamble_add(mcgen('''
324#include "%(relname)s.h"
325''',
326                                      relname=relname))
327