blob: 341dba27a8e0e090f81f11c3831b9a13a39f50dd [file] [log] [blame]
Michael Rothc17d9902011-07-19 14:50:42 -05001#
2# QAPI command marshaller generator
3#
4# Copyright IBM, Corp. 2011
5#
6# Authors:
7# Anthony Liguori <aliguori@us.ibm.com>
8# Michael Roth <mdroth@linux.vnet.ibm.com>
9#
Markus Armbruster678e48a2014-03-01 08:40:34 +010010# This work is licensed under the terms of the GNU GPL, version 2.
11# See the COPYING file in the top-level directory.
Michael Rothc17d9902011-07-19 14:50:42 -050012
13from ordereddict import OrderedDict
14from qapi import *
15import sys
16import os
17import getopt
18import errno
19
Anthony Liguori15e43e62011-09-14 14:30:00 -050020def type_visitor(name):
21 if type(name) == list:
22 return 'visit_type_%sList' % name[0]
23 else:
24 return 'visit_type_%s' % name
25
Michael Rothc17d9902011-07-19 14:50:42 -050026def generate_command_decl(name, args, ret_type):
27 arglist=""
28 for argname, argtype, optional, structured in parse_args(args):
29 argtype = c_type(argtype)
30 if argtype == "char *":
31 argtype = "const char *"
32 if optional:
33 arglist += "bool has_%s, " % c_var(argname)
34 arglist += "%s %s, " % (argtype, c_var(argname))
35 return mcgen('''
36%(ret_type)s qmp_%(name)s(%(args)sError **errp);
37''',
Federico Simoncellic9da2282012-03-20 13:54:35 +000038 ret_type=c_type(ret_type), name=c_fun(name), args=arglist).strip()
Michael Rothc17d9902011-07-19 14:50:42 -050039
40def gen_sync_call(name, args, ret_type, indent=0):
41 ret = ""
42 arglist=""
43 retval=""
44 if ret_type:
45 retval = "retval = "
46 for argname, argtype, optional, structured in parse_args(args):
47 if optional:
48 arglist += "has_%s, " % c_var(argname)
49 arglist += "%s, " % (c_var(argname))
50 push_indent(indent)
51 ret = mcgen('''
52%(retval)sqmp_%(name)s(%(args)serrp);
53
54''',
Federico Simoncellic9da2282012-03-20 13:54:35 +000055 name=c_fun(name), args=arglist, retval=retval).rstrip()
Michael Rothc17d9902011-07-19 14:50:42 -050056 if ret_type:
57 ret += "\n" + mcgen(''''
Luiz Capitulino694a0992011-10-19 14:51:14 -020058if (!error_is_set(errp)) {
59 %(marshal_output_call)s
60}
Michael Rothc17d9902011-07-19 14:50:42 -050061''',
62 marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
63 pop_indent(indent)
64 return ret.rstrip()
65
66
67def gen_marshal_output_call(name, ret_type):
68 if not ret_type:
69 return ""
Federico Simoncellic9da2282012-03-20 13:54:35 +000070 return "qmp_marshal_output_%s(retval, ret, errp);" % c_fun(name)
Michael Rothc17d9902011-07-19 14:50:42 -050071
Markus Armbrusterf9bee752014-05-07 09:53:44 +020072def gen_visitor_input_containers_decl(args, obj):
Michael Rothc17d9902011-07-19 14:50:42 -050073 ret = ""
74
75 push_indent()
76 if len(args) > 0:
77 ret += mcgen('''
Markus Armbrusterf9bee752014-05-07 09:53:44 +020078QmpInputVisitor *mi = qmp_input_visitor_new_strict(%(obj)s);
Michael Rothc17d9902011-07-19 14:50:42 -050079QapiDeallocVisitor *md;
80Visitor *v;
Markus Armbrusterf9bee752014-05-07 09:53:44 +020081''',
82 obj=obj)
Michael Rothc17d9902011-07-19 14:50:42 -050083 pop_indent()
84
85 return ret.rstrip()
86
87def gen_visitor_input_vars_decl(args):
88 ret = ""
89 push_indent()
90 for argname, argtype, optional, structured in parse_args(args):
91 if optional:
92 ret += mcgen('''
93bool has_%(argname)s = false;
94''',
95 argname=c_var(argname))
96 if c_type(argtype).endswith("*"):
97 ret += mcgen('''
98%(argtype)s %(argname)s = NULL;
99''',
100 argname=c_var(argname), argtype=c_type(argtype))
101 else:
102 ret += mcgen('''
103%(argtype)s %(argname)s;
104''',
105 argname=c_var(argname), argtype=c_type(argtype))
106
107 pop_indent()
108 return ret.rstrip()
109
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200110def gen_visitor_input_block(args, dealloc=False):
Michael Rothc17d9902011-07-19 14:50:42 -0500111 ret = ""
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400112 errparg = 'errp'
113
Michael Rothc17d9902011-07-19 14:50:42 -0500114 if len(args) == 0:
115 return ret
116
117 push_indent()
118
119 if dealloc:
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400120 errparg = 'NULL'
Michael Rothc17d9902011-07-19 14:50:42 -0500121 ret += mcgen('''
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200122qmp_input_visitor_cleanup(mi);
Michael Rothc17d9902011-07-19 14:50:42 -0500123md = qapi_dealloc_visitor_new();
124v = qapi_dealloc_get_visitor(md);
125''')
126 else:
127 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500128v = qmp_input_get_visitor(mi);
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200129''')
Michael Rothc17d9902011-07-19 14:50:42 -0500130
131 for argname, argtype, optional, structured in parse_args(args):
132 if optional:
133 ret += mcgen('''
Markus Armbrustere2cd0f42014-05-07 09:53:46 +0200134visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500135if (has_%(c_name)s) {
136''',
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400137 c_name=c_var(argname), name=argname, errp=errparg)
Michael Rothc17d9902011-07-19 14:50:42 -0500138 push_indent()
139 ret += mcgen('''
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400140%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500141''',
Anthony Liguori15e43e62011-09-14 14:30:00 -0500142 c_name=c_var(argname), name=argname, argtype=argtype,
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400143 visitor=type_visitor(argtype), errp=errparg)
Michael Rothc17d9902011-07-19 14:50:42 -0500144 if optional:
145 pop_indent()
146 ret += mcgen('''
147}
Markus Armbrustere2cd0f42014-05-07 09:53:46 +0200148''')
Michael Rothc17d9902011-07-19 14:50:42 -0500149
150 if dealloc:
151 ret += mcgen('''
152qapi_dealloc_visitor_cleanup(md);
153''')
Michael Rothc17d9902011-07-19 14:50:42 -0500154 pop_indent()
155 return ret.rstrip()
156
Anthony Liguori776574d2011-09-02 12:34:46 -0500157def gen_marshal_output(name, args, ret_type, middle_mode):
Michael Rothc17d9902011-07-19 14:50:42 -0500158 if not ret_type:
159 return ""
Anthony Liguori776574d2011-09-02 12:34:46 -0500160
Michael Rothc17d9902011-07-19 14:50:42 -0500161 ret = mcgen('''
162static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
163{
Michael Rothc17d9902011-07-19 14:50:42 -0500164 QmpOutputVisitor *mo = qmp_output_visitor_new();
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200165 QapiDeallocVisitor *md;
Michael Rothc17d9902011-07-19 14:50:42 -0500166 Visitor *v;
167
168 v = qmp_output_get_visitor(mo);
Anthony Liguori15e43e62011-09-14 14:30:00 -0500169 %(visitor)s(v, &ret_in, "unused", errp);
Michael Rothc17d9902011-07-19 14:50:42 -0500170 if (!error_is_set(errp)) {
171 *ret_out = qmp_output_get_qobject(mo);
172 }
173 qmp_output_visitor_cleanup(mo);
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200174 md = qapi_dealloc_visitor_new();
Michael Rothc17d9902011-07-19 14:50:42 -0500175 v = qapi_dealloc_get_visitor(md);
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400176 %(visitor)s(v, &ret_in, "unused", NULL);
Michael Rothc17d9902011-07-19 14:50:42 -0500177 qapi_dealloc_visitor_cleanup(md);
178}
179''',
Federico Simoncellic9da2282012-03-20 13:54:35 +0000180 c_ret_type=c_type(ret_type), c_name=c_fun(name),
Anthony Liguori15e43e62011-09-14 14:30:00 -0500181 visitor=type_visitor(ret_type))
Michael Rothc17d9902011-07-19 14:50:42 -0500182
183 return ret
184
Anthony Liguori776574d2011-09-02 12:34:46 -0500185def gen_marshal_input_decl(name, args, ret_type, middle_mode):
186 if middle_mode:
Federico Simoncellic9da2282012-03-20 13:54:35 +0000187 return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_fun(name)
Anthony Liguori776574d2011-09-02 12:34:46 -0500188 else:
Federico Simoncellic9da2282012-03-20 13:54:35 +0000189 return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_fun(name)
Anthony Liguori776574d2011-09-02 12:34:46 -0500190
191
192
193def gen_marshal_input(name, args, ret_type, middle_mode):
194 hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
195
Michael Rothc17d9902011-07-19 14:50:42 -0500196 ret = mcgen('''
Anthony Liguori776574d2011-09-02 12:34:46 -0500197%(header)s
Michael Rothc17d9902011-07-19 14:50:42 -0500198{
199''',
Anthony Liguori776574d2011-09-02 12:34:46 -0500200 header=hdr)
201
202 if middle_mode:
203 ret += mcgen('''
204 Error *local_err = NULL;
205 Error **errp = &local_err;
206 QDict *args = (QDict *)qdict;
207''')
Michael Rothc17d9902011-07-19 14:50:42 -0500208
209 if ret_type:
210 if c_type(ret_type).endswith("*"):
211 retval = " %s retval = NULL;" % c_type(ret_type)
212 else:
213 retval = " %s retval;" % c_type(ret_type)
214 ret += mcgen('''
215%(retval)s
216''',
217 retval=retval)
218
219 if len(args) > 0:
220 ret += mcgen('''
221%(visitor_input_containers_decl)s
222%(visitor_input_vars_decl)s
223
224%(visitor_input_block)s
225
226''',
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200227 visitor_input_containers_decl=gen_visitor_input_containers_decl(args, "QOBJECT(args)"),
Michael Rothc17d9902011-07-19 14:50:42 -0500228 visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200229 visitor_input_block=gen_visitor_input_block(args))
Anthony Liguori776574d2011-09-02 12:34:46 -0500230 else:
231 ret += mcgen('''
232 (void)args;
233''')
Michael Rothc17d9902011-07-19 14:50:42 -0500234
235 ret += mcgen('''
236 if (error_is_set(errp)) {
237 goto out;
238 }
239%(sync_call)s
240''',
241 sync_call=gen_sync_call(name, args, ret_type, indent=4))
242 ret += mcgen('''
243
244out:
245''')
246 ret += mcgen('''
247%(visitor_input_block_cleanup)s
Michael Rothc17d9902011-07-19 14:50:42 -0500248''',
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200249 visitor_input_block_cleanup=gen_visitor_input_block(args,
Anthony Liguori776574d2011-09-02 12:34:46 -0500250 dealloc=True))
251
252 if middle_mode:
253 ret += mcgen('''
254
255 if (local_err) {
256 qerror_report_err(local_err);
257 error_free(local_err);
258 return -1;
259 }
260 return 0;
261''')
262 else:
263 ret += mcgen('''
264 return;
265''')
266
267 ret += mcgen('''
268}
269''')
270
Michael Rothc17d9902011-07-19 14:50:42 -0500271 return ret
272
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300273def option_value_matches(opt, val, cmd):
274 if opt in cmd and cmd[opt] == val:
275 return True
276 return False
277
Michael Rothc17d9902011-07-19 14:50:42 -0500278def gen_registry(commands):
279 registry=""
280 push_indent()
281 for cmd in commands:
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300282 options = 'QCO_NO_OPTIONS'
283 if option_value_matches('success-response', 'no', cmd):
284 options = 'QCO_NO_SUCCESS_RESP'
285
Michael Rothc17d9902011-07-19 14:50:42 -0500286 registry += mcgen('''
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300287qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500288''',
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300289 name=cmd['command'], c_name=c_fun(cmd['command']),
290 opts=options)
Michael Rothc17d9902011-07-19 14:50:42 -0500291 pop_indent()
292 ret = mcgen('''
293static void qmp_init_marshal(void)
294{
295%(registry)s
296}
297
298qapi_init(qmp_init_marshal);
299''',
300 registry=registry.rstrip())
301 return ret
302
303def gen_command_decl_prologue(header, guard, prefix=""):
304 ret = mcgen('''
305/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
306
307/*
308 * schema-defined QAPI function prototypes
309 *
310 * Copyright IBM, Corp. 2011
311 *
312 * Authors:
313 * Anthony Liguori <aliguori@us.ibm.com>
314 *
315 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
316 * See the COPYING.LIB file in the top-level directory.
317 *
318 */
319
320#ifndef %(guard)s
321#define %(guard)s
322
323#include "%(prefix)sqapi-types.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +0100324#include "qapi/qmp/qdict.h"
325#include "qapi/error.h"
Michael Rothc17d9902011-07-19 14:50:42 -0500326
327''',
Anthony Liguori776574d2011-09-02 12:34:46 -0500328 header=basename(header), guard=guardname(header), prefix=prefix)
Michael Rothc17d9902011-07-19 14:50:42 -0500329 return ret
330
331def gen_command_def_prologue(prefix="", proxy=False):
332 ret = mcgen('''
333/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
334
335/*
336 * schema-defined QMP->QAPI command dispatch
337 *
338 * Copyright IBM, Corp. 2011
339 *
340 * Authors:
341 * Anthony Liguori <aliguori@us.ibm.com>
342 *
343 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
344 * See the COPYING.LIB file in the top-level directory.
345 *
346 */
347
Paolo Bonzini79ee7df2012-12-06 11:22:34 +0100348#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100349#include "qemu/module.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +0100350#include "qapi/qmp/qerror.h"
351#include "qapi/qmp/types.h"
352#include "qapi/qmp/dispatch.h"
353#include "qapi/visitor.h"
Michael Rothc17d9902011-07-19 14:50:42 -0500354#include "qapi/qmp-output-visitor.h"
355#include "qapi/qmp-input-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +0100356#include "qapi/dealloc-visitor.h"
Michael Rothc17d9902011-07-19 14:50:42 -0500357#include "%(prefix)sqapi-types.h"
358#include "%(prefix)sqapi-visit.h"
359
360''',
361 prefix=prefix)
362 if not proxy:
363 ret += '#include "%sqmp-commands.h"' % prefix
Anthony Liguori776574d2011-09-02 12:34:46 -0500364 return ret + "\n\n"
Michael Rothc17d9902011-07-19 14:50:42 -0500365
366
367try:
Lluís Vilanova33aaad52014-05-02 15:52:35 +0200368 opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m",
Avi Kivity8d3bc512011-12-27 16:02:16 +0200369 ["source", "header", "prefix=",
Lluís Vilanova33aaad52014-05-02 15:52:35 +0200370 "input-file=", "output-dir=",
371 "type=", "middle"])
Michael Rothc17d9902011-07-19 14:50:42 -0500372except getopt.GetoptError, err:
373 print str(err)
374 sys.exit(1)
375
376output_dir = ""
377prefix = ""
378dispatch_type = "sync"
379c_file = 'qmp-marshal.c'
380h_file = 'qmp-commands.h'
Anthony Liguori776574d2011-09-02 12:34:46 -0500381middle_mode = False
Michael Rothc17d9902011-07-19 14:50:42 -0500382
Avi Kivity8d3bc512011-12-27 16:02:16 +0200383do_c = False
384do_h = False
385
Michael Rothc17d9902011-07-19 14:50:42 -0500386for o, a in opts:
387 if o in ("-p", "--prefix"):
388 prefix = a
Lluís Vilanova33aaad52014-05-02 15:52:35 +0200389 elif o in ("-i", "--input-file"):
390 input_file = a
Michael Rothc17d9902011-07-19 14:50:42 -0500391 elif o in ("-o", "--output-dir"):
392 output_dir = a + "/"
393 elif o in ("-t", "--type"):
394 dispatch_type = a
Anthony Liguori776574d2011-09-02 12:34:46 -0500395 elif o in ("-m", "--middle"):
396 middle_mode = True
Avi Kivity8d3bc512011-12-27 16:02:16 +0200397 elif o in ("-c", "--source"):
Avi Kivity8d3bc512011-12-27 16:02:16 +0200398 do_c = True
Avi Kivity19bf7c82011-12-28 12:26:58 +0200399 elif o in ("-h", "--header"):
400 do_h = True
Avi Kivity8d3bc512011-12-27 16:02:16 +0200401
402if not do_c and not do_h:
403 do_c = True
404 do_h = True
Michael Rothc17d9902011-07-19 14:50:42 -0500405
406c_file = output_dir + prefix + c_file
407h_file = output_dir + prefix + h_file
408
Avi Kivity8d3bc512011-12-27 16:02:16 +0200409def maybe_open(really, name, opt):
Avi Kivity8d3bc512011-12-27 16:02:16 +0200410 if really:
411 return open(name, opt)
412 else:
Avi Kivity19bf7c82011-12-28 12:26:58 +0200413 import StringIO
414 return StringIO.StringIO()
Avi Kivity8d3bc512011-12-27 16:02:16 +0200415
Michael Rothc17d9902011-07-19 14:50:42 -0500416try:
417 os.makedirs(output_dir)
418except os.error, e:
419 if e.errno != errno.EEXIST:
420 raise
421
Lluís Vilanova33aaad52014-05-02 15:52:35 +0200422exprs = parse_schema(input_file)
Michael Rothc17d9902011-07-19 14:50:42 -0500423commands = filter(lambda expr: expr.has_key('command'), exprs)
Anthony Liguori5dbee472011-12-12 14:29:33 -0600424commands = filter(lambda expr: not expr.has_key('gen'), commands)
Michael Rothc17d9902011-07-19 14:50:42 -0500425
426if dispatch_type == "sync":
Avi Kivity8d3bc512011-12-27 16:02:16 +0200427 fdecl = maybe_open(do_h, h_file, 'w')
428 fdef = maybe_open(do_c, c_file, 'w')
Michael Rothc17d9902011-07-19 14:50:42 -0500429 ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
430 fdecl.write(ret)
431 ret = gen_command_def_prologue(prefix=prefix)
432 fdef.write(ret)
433
434 for cmd in commands:
435 arglist = []
436 ret_type = None
437 if cmd.has_key('data'):
438 arglist = cmd['data']
439 if cmd.has_key('returns'):
440 ret_type = cmd['returns']
441 ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
442 fdecl.write(ret)
443 if ret_type:
Anthony Liguori776574d2011-09-02 12:34:46 -0500444 ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
Michael Rothc17d9902011-07-19 14:50:42 -0500445 fdef.write(ret)
Anthony Liguori776574d2011-09-02 12:34:46 -0500446
447 if middle_mode:
448 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
449
450 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
Michael Rothc17d9902011-07-19 14:50:42 -0500451 fdef.write(ret)
452
Michael Roth7534ba02011-08-10 13:10:51 -0500453 fdecl.write("\n#endif\n");
Anthony Liguori776574d2011-09-02 12:34:46 -0500454
455 if not middle_mode:
456 ret = gen_registry(commands)
457 fdef.write(ret)
Michael Rothc17d9902011-07-19 14:50:42 -0500458
459 fdef.flush()
460 fdef.close()
461 fdecl.flush()
462 fdecl.close()