]>
git.proxmox.com Git - mirror_qemu.git/blob - scripts/qapi/gen.py
1 # -*- coding: utf-8 -*-
5 # Copyright (c) 2015-2019 Red Hat Inc.
8 # Markus Armbruster <armbru@redhat.com>
9 # Marc-André Lureau <marcandre.lureau@redhat.com>
11 # This work is licensed under the terms of the GNU GPL, version 2.
12 # See the COPYING file in the top-level directory.
14 from contextlib
import contextmanager
37 from .source
import QAPISourceInfo
41 def __init__(self
, fname
: str):
46 def preamble_add(self
, text
: str) -> None:
47 self
._preamble
+= text
49 def add(self
, text
: str) -> None:
52 def get_content(self
) -> str:
53 return self
._top
() + self
._preamble
+ self
._body
+ self
._bottom
()
55 def _top(self
) -> str:
56 # pylint: disable=no-self-use
59 def _bottom(self
) -> str:
60 # pylint: disable=no-self-use
63 def write(self
, output_dir
: str) -> None:
64 # Include paths starting with ../ are used to reuse modules of the main
65 # schema in specialised schemas. Don't overwrite the files that are
66 # already generated for the main schema.
67 if self
.fname
.startswith('../'):
69 pathname
= os
.path
.join(output_dir
, self
.fname
)
70 odir
= os
.path
.dirname(pathname
)
73 os
.makedirs(odir
, exist_ok
=True)
75 # use os.open for O_CREAT to create and read a non-existant file
76 fd
= os
.open(pathname
, os
.O_RDWR | os
.O_CREAT
, 0o666)
77 with os
.fdopen(fd
, 'r+', encoding
='utf-8') as fp
:
78 text
= self
.get_content()
79 oldtext
= fp
.read(len(text
) + 1)
86 def _wrap_ifcond(ifcond
: QAPISchemaIfCond
, before
: str, after
: str) -> str:
88 return after
# suppress empty #if ... #endif
90 assert after
.startswith(before
)
92 added
= after
[len(before
):]
96 out
+= ifcond
.gen_if()
98 out
+= ifcond
.gen_endif()
102 def build_params(arg_type
: Optional
[QAPISchemaObjectType
],
104 extra
: Optional
[str] = None) -> str:
109 ret
+= '%s arg' % arg_type
.c_param_type()
112 assert not arg_type
.variants
113 for memb
in arg_type
.members
:
117 ret
+= 'bool has_%s, ' % c_name(memb
.name
)
118 ret
+= '%s %s' % (memb
.type.c_param_type(),
122 return ret
if ret
else 'void'
125 class QAPIGenCCode(QAPIGen
):
126 def __init__(self
, fname
: str):
127 super().__init
__(fname
)
128 self
._start
_if
: Optional
[Tuple
[QAPISchemaIfCond
, str, str]] = None
130 def start_if(self
, ifcond
: QAPISchemaIfCond
) -> None:
131 assert self
._start
_if
is None
132 self
._start
_if
= (ifcond
, self
._body
, self
._preamble
)
134 def end_if(self
) -> None:
135 assert self
._start
_if
is not None
136 self
._body
= _wrap_ifcond(self
._start
_if
[0],
137 self
._start
_if
[1], self
._body
)
138 self
._preamble
= _wrap_ifcond(self
._start
_if
[0],
139 self
._start
_if
[2], self
._preamble
)
140 self
._start
_if
= None
142 def get_content(self
) -> str:
143 assert self
._start
_if
is None
144 return super().get_content()
147 class QAPIGenC(QAPIGenCCode
):
148 def __init__(self
, fname
: str, blurb
: str, pydoc
: str):
149 super().__init
__(fname
)
151 self
._copyright
= '\n * '.join(re
.findall(r
'^Copyright .*', pydoc
,
154 def _top(self
) -> str:
156 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
163 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
164 * See the COPYING.LIB file in the top-level directory.
168 blurb
=self
._blurb
, copyright
=self
._copyright
)
170 def _bottom(self
) -> str:
173 /* Dummy declaration to prevent empty .o file */
174 char qapi_dummy_%(name)s;
176 name
=c_fname(self
.fname
))
179 class QAPIGenH(QAPIGenC
):
180 def _top(self
) -> str:
181 return super()._top
() + guardstart(self
.fname
)
183 def _bottom(self
) -> str:
184 return guardend(self
.fname
)
188 def ifcontext(ifcond
: QAPISchemaIfCond
, *args
: QAPIGenCCode
) -> Iterator
[None]:
190 A with-statement context manager that wraps with `start_if()` / `end_if()`.
192 :param ifcond: A sequence of conditionals, passed to `start_if()`.
193 :param args: any number of `QAPIGenCCode`.
197 with ifcontext(ifcond, self._genh, self._genc):
198 modify self._genh and self._genc ...
200 Is equivalent to calling::
202 self._genh.start_if(ifcond)
203 self._genc.start_if(ifcond)
204 modify self._genh and self._genc ...
215 class QAPISchemaMonolithicCVisitor(QAPISchemaVisitor
):
221 self
._prefix
= prefix
223 self
._genc
= QAPIGenC(self
._prefix
+ self
._what
+ '.c',
225 self
._genh
= QAPIGenH(self
._prefix
+ self
._what
+ '.h',
228 def write(self
, output_dir
: str) -> None:
229 self
._genc
.write(output_dir
)
230 self
._genh
.write(output_dir
)
233 class QAPISchemaModularCVisitor(QAPISchemaVisitor
):
238 builtin_blurb
: Optional
[str],
240 self
._prefix
= prefix
242 self
._user
_blurb
= user_blurb
243 self
._builtin
_blurb
= builtin_blurb
245 self
._current
_module
: Optional
[str] = None
246 self
._module
: Dict
[str, Tuple
[QAPIGenC
, QAPIGenH
]] = {}
247 self
._main
_module
: Optional
[str] = None
250 def _genc(self
) -> QAPIGenC
:
251 assert self
._current
_module
is not None
252 return self
._module
[self
._current
_module
][0]
255 def _genh(self
) -> QAPIGenH
:
256 assert self
._current
_module
is not None
257 return self
._module
[self
._current
_module
][1]
260 def _module_dirname(name
: str) -> str:
261 if QAPISchemaModule
.is_user_module(name
):
262 return os
.path
.dirname(name
)
265 def _module_basename(self
, what
: str, name
: str) -> str:
266 ret
= '' if QAPISchemaModule
.is_builtin_module(name
) else self
._prefix
267 if QAPISchemaModule
.is_user_module(name
):
268 basename
= os
.path
.basename(name
)
270 if name
!= self
._main
_module
:
271 ret
+= '-' + os
.path
.splitext(basename
)[0]
273 assert QAPISchemaModule
.is_system_module(name
)
274 ret
+= re
.sub(r
'-', '-' + name
[2:] + '-', what
)
277 def _module_filename(self
, what
: str, name
: str) -> str:
278 return os
.path
.join(self
._module
_dirname
(name
),
279 self
._module
_basename
(what
, name
))
281 def _add_module(self
, name
: str, blurb
: str) -> None:
282 if QAPISchemaModule
.is_user_module(name
):
283 if self
._main
_module
is None:
284 self
._main
_module
= name
285 basename
= self
._module
_filename
(self
._what
, name
)
286 genc
= QAPIGenC(basename
+ '.c', blurb
, self
._pydoc
)
287 genh
= QAPIGenH(basename
+ '.h', blurb
, self
._pydoc
)
288 self
._module
[name
] = (genc
, genh
)
289 self
._current
_module
= name
292 def _temp_module(self
, name
: str) -> Iterator
[None]:
293 old_module
= self
._current
_module
294 self
._current
_module
= name
296 self
._current
_module
= old_module
298 def write(self
, output_dir
: str, opt_builtins
: bool = False) -> None:
299 for name
, (genc
, genh
) in self
._module
.items():
300 if QAPISchemaModule
.is_builtin_module(name
) and not opt_builtins
:
302 genc
.write(output_dir
)
303 genh
.write(output_dir
)
305 def _begin_builtin_module(self
) -> None:
308 def _begin_user_module(self
, name
: str) -> None:
311 def visit_module(self
, name
: str) -> None:
312 if QAPISchemaModule
.is_builtin_module(name
):
313 if self
._builtin
_blurb
:
314 self
._add
_module
(name
, self
._builtin
_blurb
)
315 self
._begin
_builtin
_module
()
317 # The built-in module has not been created. No code may
319 self
._current
_module
= None
321 assert QAPISchemaModule
.is_user_module(name
)
322 self
._add
_module
(name
, self
._user
_blurb
)
323 self
._begin
_user
_module
(name
)
325 def visit_include(self
, name
: str, info
: Optional
[QAPISourceInfo
]) -> None:
326 relname
= os
.path
.relpath(self
._module
_filename
(self
._what
, name
),
327 os
.path
.dirname(self
._genh
.fname
))
328 self
._genh
.preamble_add(mcgen('''
329 #include "%(relname)s.h"