diff options
author | Markus Armbruster <armbru@redhat.com> | 2018-02-11 10:36:00 +0100 |
---|---|---|
committer | Eric Blake <eblake@redhat.com> | 2018-03-02 13:44:24 -0600 |
commit | 252dc3105fc494182e236e97fe20f2d6b1d652cb (patch) | |
tree | ca648e575eaa83195f4a12cca3632a36a48478aa /scripts | |
parent | 3da7a4161a96d7539588729042e987f4c6fd97c6 (diff) |
qapi: Generate separate .h, .c for each module
Our qapi-schema.json is composed of modules connected by include
directives, but the generated code is monolithic all the same: one
qapi-types.h with all the types, one qapi-visit.h with all the
visitors, and so forth. These monolithic headers get included all
over the place. In my "build everything" tree, adding a QAPI type
recompiles about 4800 out of 5100 objects.
We wouldn't write such monolithic headers by hand. It stands to
reason that we shouldn't generate them, either.
Split up generated qapi-types.h to mirror the schema's modular
structure: one header per module. Name the main module's header
qapi-types.h, and sub-module D/B.json's header D/qapi-types-B.h.
Mirror the schema's includes in the headers, so that qapi-types.h gets
you everything exactly as before. If you need less, you can include
one or more of the sub-module headers. To be exploited shortly.
Split up qapi-types.c, qapi-visit.h, qapi-visit.c, qmp-commands.h,
qmp-commands.c, qapi-event.h, qapi-event.c the same way.
qmp-introspect.h, qmp-introspect.c and qapi.texi remain monolithic.
The split of qmp-commands.c duplicates static helper function
qmp_marshal_output_str() in qapi-commands-char.c and
qapi-commands-misc.c. This happens when commands returning the same
type occur in multiple modules. Not worth avoiding.
Since I'm going to rename qapi-event.[ch] to qapi-events.[ch], and
qmp-commands.[ch] to qapi-commands.[ch], name the shards that way
already, to reduce churn. This requires temporary hacks in
commands.py and events.py. Similarly, c_name() must temporarily
be taught to munge '/' in common.py. They'll go away with the rename.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20180211093607.27351-23-armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
[eblake: declare a dummy variable in each .c file, to shut up OSX
toolchain warnings about empty .o files, including hacking c_name()]
Signed-off-by: Eric Blake <eblake@redhat.com>
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/qapi/commands.py | 35 | ||||
-rw-r--r-- | scripts/qapi/common.py | 33 | ||||
-rw-r--r-- | scripts/qapi/events.py | 19 |
3 files changed, 66 insertions, 21 deletions
diff --git a/scripts/qapi/commands.py b/scripts/qapi/commands.py index 46757db771..a43bccb190 100644 --- a/scripts/qapi/commands.py +++ b/scripts/qapi/commands.py @@ -223,14 +223,24 @@ void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds) return ret -class QAPISchemaGenCommandVisitor(QAPISchemaMonolithicCVisitor): +class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor): def __init__(self, prefix): - QAPISchemaMonolithicCVisitor.__init__( - self, prefix, 'qmp-commands', + QAPISchemaModularCVisitor.__init__( + self, prefix, 'qapi-commands', ' * Schema-defined QAPI/QMP commands', __doc__) self._regy = '' - self._visited_ret_types = set() + self._visited_ret_types = {} + + # Temporary HACK: + def _module_basename(self, what, name): + basename = QAPISchemaModularCVisitor._module_basename(self, what, name) + if name == self._main_module: + return re.sub(r'qapi-commands', 'qmp-commands', basename) + return basename + + def _begin_module(self, name): + self._visited_ret_types[self._genc] = set() self._genc.add(mcgen(''' #include "qemu/osdep.h" #include "qemu-common.h" @@ -246,26 +256,29 @@ class QAPISchemaGenCommandVisitor(QAPISchemaMonolithicCVisitor): #include "%(prefix)sqmp-commands.h" ''', - prefix=prefix)) + prefix=self._prefix)) self._genh.add(mcgen(''' #include "%(prefix)sqapi-types.h" #include "qapi/qmp/dispatch.h" -void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds); ''', - prefix=prefix, - c_prefix=c_name(prefix, protect=False))) + prefix=self._prefix)) def visit_end(self): - self._genc.add(gen_registry(self._regy, self._prefix)) + (genc, genh) = self._module[self._main_module] + genh.add(mcgen(''' +void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds); +''', + c_prefix=c_name(self._prefix, protect=False))) + genc.add(gen_registry(self._regy, self._prefix)) def visit_command(self, name, info, arg_type, ret_type, gen, success_response, boxed): if not gen: return self._genh.add(gen_command_decl(name, arg_type, boxed, ret_type)) - if ret_type and ret_type not in self._visited_ret_types: - self._visited_ret_types.add(ret_type) + if ret_type and ret_type not in self._visited_ret_types[self._genc]: + self._visited_ret_types[self._genc].add(ret_type) self._genc.add(gen_marshal_output(ret_type)) self._genh.add(gen_marshal_decl(name)) self._genc.add(gen_marshal(name, arg_type, boxed, ret_type)) diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py index 069ec3715d..e0b88f1896 100644 --- a/scripts/qapi/common.py +++ b/scripts/qapi/common.py @@ -1775,10 +1775,11 @@ def c_enum_const(type_name, const_name, prefix=None): type_name = prefix return camel_to_upper(type_name) + '_' + c_name(const_name, False).upper() +# Temporary HACK for '/': if hasattr(str, 'maketrans'): - c_name_trans = str.maketrans('.-', '__') + c_name_trans = str.maketrans('.-/', '___') else: - c_name_trans = string.maketrans('.-', '__') + c_name_trans = string.maketrans('.-/', '___') # Map @name to a valid C identifier. @@ -2035,6 +2036,13 @@ class QAPIGenC(QAPIGen): ''', blurb=self._blurb, copyright=self._copyright) + def _bottom(self, fname): + return mcgen(''' +/* Dummy declaration to prevent empty .o file */ +char dummy_%(name)s; +''', + name=c_name(fname)) + class QAPIGenH(QAPIGenC): @@ -2073,13 +2081,20 @@ class QAPISchemaModularCVisitor(QAPISchemaVisitor): self._blurb = blurb self._pydoc = pydoc self._module = {} + self._main_module = None def _module_basename(self, what, name): if name is None: return re.sub(r'-', '-builtin-', what) - return self._prefix + what + basename = os.path.join(os.path.dirname(name), + self._prefix + what) + if name == self._main_module: + return basename + return basename + '-' + os.path.splitext(os.path.basename(name))[0] def _add_module(self, name, blurb): + if self._main_module is None and name is not None: + self._main_module = name genc = QAPIGenC(blurb, self._pydoc) genh = QAPIGenH(blurb, self._pydoc) self._module[name] = (genc, genh) @@ -2088,7 +2103,7 @@ class QAPISchemaModularCVisitor(QAPISchemaVisitor): def _set_module(self, name): self._genc, self._genh = self._module[name] - def write(self, output_dir, opt_builtins): + def write(self, output_dir, opt_builtins=False): for name in self._module: if name is None and not opt_builtins: continue @@ -2101,7 +2116,15 @@ class QAPISchemaModularCVisitor(QAPISchemaVisitor): pass def visit_module(self, name): - if len(self._module) != 1: + if name in self._module: + self._set_module(name) return self._add_module(name, self._blurb) self._begin_module(name) + + def visit_include(self, name, info): + basename = self._module_basename(self._what, name) + self._genh.preamble_add(mcgen(''' +#include "%(basename)s.h" +''', + basename=basename)) diff --git a/scripts/qapi/events.py b/scripts/qapi/events.py index 81ab3abb30..1e0b990f35 100644 --- a/scripts/qapi/events.py +++ b/scripts/qapi/events.py @@ -148,14 +148,23 @@ out: return ret -class QAPISchemaGenEventVisitor(QAPISchemaMonolithicCVisitor): +class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor): def __init__(self, prefix): - QAPISchemaMonolithicCVisitor.__init__( - self, prefix, 'qapi-event', + QAPISchemaModularCVisitor.__init__( + self, prefix, 'qapi-events', ' * Schema-defined QAPI/QMP events', __doc__) self._enum_name = c_name(prefix + 'QAPIEvent', protect=False) self._event_names = [] + + # Temporary HACK: + def _module_basename(self, what, name): + basename = QAPISchemaModularCVisitor._module_basename(self, what, name) + if name == self._main_module: + return re.sub(r'qapi-events', 'qapi-event', basename) + return basename + + def _begin_module(self, name): self._genc.add(mcgen(''' #include "qemu/osdep.h" #include "qemu-common.h" @@ -167,13 +176,13 @@ class QAPISchemaGenEventVisitor(QAPISchemaMonolithicCVisitor): #include "qapi/qmp-event.h" ''', - prefix=prefix)) + prefix=self._prefix)) self._genh.add(mcgen(''' #include "qapi/util.h" #include "%(prefix)sqapi-types.h" ''', - prefix=prefix)) + prefix=self._prefix)) def visit_end(self): self._genh.add(gen_enum(self._enum_name, self._event_names)) |