]> git.proxmox.com Git - mirror_qemu.git/blob - scripts/qapi-event.py
build-sys: split util-obj- on multi-lines
[mirror_qemu.git] / scripts / qapi-event.py
1 #
2 # QAPI event generator
3 #
4 # Copyright (c) 2014 Wenchao Xia
5 # Copyright (c) 2015 Red Hat Inc.
6 #
7 # Authors:
8 # Wenchao Xia <wenchaoqemu@gmail.com>
9 # Markus Armbruster <armbru@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
14 from qapi import *
15
16
17 def gen_event_send_proto(name, arg_type):
18 return 'void qapi_event_send_%(c_name)s(%(param)s)' % {
19 'c_name': c_name(name.lower()),
20 'param': gen_params(arg_type, 'Error **errp')}
21
22
23 def gen_event_send_decl(name, arg_type):
24 return mcgen('''
25
26 %(proto)s;
27 ''',
28 proto=gen_event_send_proto(name, arg_type))
29
30
31 def gen_event_send(name, arg_type):
32 ret = mcgen('''
33
34 %(proto)s
35 {
36 QDict *qmp;
37 Error *err = NULL;
38 QMPEventFuncEmit emit;
39 ''',
40 proto=gen_event_send_proto(name, arg_type))
41
42 if arg_type and arg_type.members:
43 ret += mcgen('''
44 QmpOutputVisitor *qov;
45 Visitor *v;
46 QObject *obj;
47
48 ''')
49
50 ret += mcgen('''
51 emit = qmp_event_get_func_emit();
52 if (!emit) {
53 return;
54 }
55
56 qmp = qmp_event_build_dict("%(name)s");
57
58 ''',
59 name=name)
60
61 if arg_type and arg_type.members:
62 ret += mcgen('''
63 qov = qmp_output_visitor_new();
64 g_assert(qov);
65
66 v = qmp_output_get_visitor(qov);
67 g_assert(v);
68
69 /* Fake visit, as if all members are under a structure */
70 visit_start_struct(v, NULL, "", "%(name)s", 0, &err);
71 ''',
72 name=name)
73 ret += gen_err_check()
74 ret += gen_visit_fields(arg_type.members, need_cast=True)
75 ret += mcgen('''
76 visit_end_struct(v, &err);
77 if (err) {
78 goto out;
79 }
80
81 obj = qmp_output_get_qobject(qov);
82 g_assert(obj != NULL);
83
84 qdict_put_obj(qmp, "data", obj);
85 ''')
86
87 ret += mcgen('''
88 emit(%(c_enum)s, qmp, &err);
89
90 ''',
91 c_enum=c_enum_const(event_enum_name, name))
92
93 if arg_type and arg_type.members:
94 ret += mcgen('''
95 out:
96 qmp_output_visitor_cleanup(qov);
97 ''')
98 ret += mcgen('''
99 error_propagate(errp, err);
100 QDECREF(qmp);
101 }
102 ''')
103 return ret
104
105
106 class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
107 def __init__(self):
108 self.decl = None
109 self.defn = None
110 self._event_names = None
111
112 def visit_begin(self, schema):
113 self.decl = ''
114 self.defn = ''
115 self._event_names = []
116
117 def visit_end(self):
118 self.decl += gen_enum(event_enum_name, self._event_names)
119 self.defn += gen_enum_lookup(event_enum_name, self._event_names)
120 self._event_names = None
121
122 def visit_event(self, name, info, arg_type):
123 self.decl += gen_event_send_decl(name, arg_type)
124 self.defn += gen_event_send(name, arg_type)
125 self._event_names.append(name)
126
127
128 (input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line()
129
130 c_comment = '''
131 /*
132 * schema-defined QAPI event functions
133 *
134 * Copyright (c) 2014 Wenchao Xia
135 *
136 * Authors:
137 * Wenchao Xia <wenchaoqemu@gmail.com>
138 *
139 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
140 * See the COPYING.LIB file in the top-level directory.
141 *
142 */
143 '''
144 h_comment = '''
145 /*
146 * schema-defined QAPI event functions
147 *
148 * Copyright (c) 2014 Wenchao Xia
149 *
150 * Authors:
151 * Wenchao Xia <wenchaoqemu@gmail.com>
152 *
153 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
154 * See the COPYING.LIB file in the top-level directory.
155 *
156 */
157 '''
158
159 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
160 'qapi-event.c', 'qapi-event.h',
161 c_comment, h_comment)
162
163 fdef.write(mcgen('''
164 #include "qemu-common.h"
165 #include "%(prefix)sqapi-event.h"
166 #include "%(prefix)sqapi-visit.h"
167 #include "qapi/qmp-output-visitor.h"
168 #include "qapi/qmp-event.h"
169
170 ''',
171 prefix=prefix))
172
173 fdecl.write(mcgen('''
174 #include "qapi/error.h"
175 #include "qapi/qmp/qdict.h"
176 #include "%(prefix)sqapi-types.h"
177
178 ''',
179 prefix=prefix))
180
181 event_enum_name = c_name(prefix + "QAPIEvent", protect=False)
182
183 schema = QAPISchema(input_file)
184 gen = QAPISchemaGenEventVisitor()
185 schema.visit(gen)
186 fdef.write(gen.defn)
187 fdecl.write(gen.decl)
188
189 close_output(fdef, fdecl)