qapi-commands.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #
  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. #
  10. # This work is licensed under the terms of the GNU GPLv2.
  11. # See the COPYING.LIB file in the top-level directory.
  12. from ordereddict import OrderedDict
  13. from qapi import *
  14. import sys
  15. import os
  16. import getopt
  17. import errno
  18. def generate_decl_enum(name, members, genlist=True):
  19. return mcgen('''
  20. void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp);
  21. ''',
  22. name=name)
  23. def generate_command_decl(name, args, ret_type):
  24. arglist=""
  25. for argname, argtype, optional, structured in parse_args(args):
  26. argtype = c_type(argtype)
  27. if argtype == "char *":
  28. argtype = "const char *"
  29. if optional:
  30. arglist += "bool has_%s, " % c_var(argname)
  31. arglist += "%s %s, " % (argtype, c_var(argname))
  32. return mcgen('''
  33. %(ret_type)s qmp_%(name)s(%(args)sError **errp);
  34. ''',
  35. ret_type=c_type(ret_type), name=c_var(name), args=arglist).strip()
  36. def gen_sync_call(name, args, ret_type, indent=0):
  37. ret = ""
  38. arglist=""
  39. retval=""
  40. if ret_type:
  41. retval = "retval = "
  42. for argname, argtype, optional, structured in parse_args(args):
  43. if optional:
  44. arglist += "has_%s, " % c_var(argname)
  45. arglist += "%s, " % (c_var(argname))
  46. push_indent(indent)
  47. ret = mcgen('''
  48. %(retval)sqmp_%(name)s(%(args)serrp);
  49. ''',
  50. name=c_var(name), args=arglist, retval=retval).rstrip()
  51. if ret_type:
  52. ret += "\n" + mcgen(''''
  53. %(marshal_output_call)s
  54. ''',
  55. marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
  56. pop_indent(indent)
  57. return ret.rstrip()
  58. def gen_marshal_output_call(name, ret_type):
  59. if not ret_type:
  60. return ""
  61. return "qmp_marshal_output_%s(retval, ret, errp);" % c_var(name)
  62. def gen_visitor_output_containers_decl(ret_type):
  63. ret = ""
  64. push_indent()
  65. if ret_type:
  66. ret += mcgen('''
  67. QmpOutputVisitor *mo;
  68. QapiDeallocVisitor *md;
  69. Visitor *v;
  70. ''')
  71. pop_indent()
  72. return ret
  73. def gen_visitor_input_containers_decl(args):
  74. ret = ""
  75. push_indent()
  76. if len(args) > 0:
  77. ret += mcgen('''
  78. QmpInputVisitor *mi;
  79. QapiDeallocVisitor *md;
  80. Visitor *v;
  81. ''')
  82. pop_indent()
  83. return ret.rstrip()
  84. def gen_visitor_input_vars_decl(args):
  85. ret = ""
  86. push_indent()
  87. for argname, argtype, optional, structured in parse_args(args):
  88. if optional:
  89. ret += mcgen('''
  90. bool has_%(argname)s = false;
  91. ''',
  92. argname=c_var(argname))
  93. if c_type(argtype).endswith("*"):
  94. ret += mcgen('''
  95. %(argtype)s %(argname)s = NULL;
  96. ''',
  97. argname=c_var(argname), argtype=c_type(argtype))
  98. else:
  99. ret += mcgen('''
  100. %(argtype)s %(argname)s;
  101. ''',
  102. argname=c_var(argname), argtype=c_type(argtype))
  103. pop_indent()
  104. return ret.rstrip()
  105. def gen_visitor_input_block(args, obj, dealloc=False):
  106. ret = ""
  107. if len(args) == 0:
  108. return ret
  109. push_indent()
  110. if dealloc:
  111. ret += mcgen('''
  112. md = qapi_dealloc_visitor_new();
  113. v = qapi_dealloc_get_visitor(md);
  114. ''')
  115. else:
  116. ret += mcgen('''
  117. mi = qmp_input_visitor_new(%(obj)s);
  118. v = qmp_input_get_visitor(mi);
  119. ''',
  120. obj=obj)
  121. for argname, argtype, optional, structured in parse_args(args):
  122. if optional:
  123. ret += mcgen('''
  124. visit_start_optional(v, &has_%(c_name)s, "%(name)s", errp);
  125. if (has_%(c_name)s) {
  126. ''',
  127. c_name=c_var(argname), name=argname)
  128. push_indent()
  129. ret += mcgen('''
  130. visit_type_%(argtype)s(v, &%(c_name)s, "%(name)s", errp);
  131. ''',
  132. c_name=c_var(argname), name=argname, argtype=argtype)
  133. if optional:
  134. pop_indent()
  135. ret += mcgen('''
  136. }
  137. visit_end_optional(v, errp);
  138. ''')
  139. if dealloc:
  140. ret += mcgen('''
  141. qapi_dealloc_visitor_cleanup(md);
  142. ''')
  143. else:
  144. ret += mcgen('''
  145. qmp_input_visitor_cleanup(mi);
  146. ''')
  147. pop_indent()
  148. return ret.rstrip()
  149. def gen_marshal_output(name, args, ret_type):
  150. if not ret_type:
  151. return ""
  152. ret = mcgen('''
  153. static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
  154. {
  155. QapiDeallocVisitor *md = qapi_dealloc_visitor_new();
  156. QmpOutputVisitor *mo = qmp_output_visitor_new();
  157. Visitor *v;
  158. v = qmp_output_get_visitor(mo);
  159. visit_type_%(ret_type)s(v, &ret_in, "unused", errp);
  160. if (!error_is_set(errp)) {
  161. *ret_out = qmp_output_get_qobject(mo);
  162. }
  163. qmp_output_visitor_cleanup(mo);
  164. v = qapi_dealloc_get_visitor(md);
  165. visit_type_%(ret_type)s(v, &ret_in, "unused", errp);
  166. qapi_dealloc_visitor_cleanup(md);
  167. }
  168. ''',
  169. c_ret_type=c_type(ret_type), c_name=c_var(name), ret_type=ret_type)
  170. return ret
  171. def gen_marshal_input(name, args, ret_type):
  172. ret = mcgen('''
  173. static void qmp_marshal_input_%(c_name)s(QDict *args, QObject **ret, Error **errp)
  174. {
  175. ''',
  176. c_name=c_var(name))
  177. if ret_type:
  178. if c_type(ret_type).endswith("*"):
  179. retval = " %s retval = NULL;" % c_type(ret_type)
  180. else:
  181. retval = " %s retval;" % c_type(ret_type)
  182. ret += mcgen('''
  183. %(retval)s
  184. ''',
  185. retval=retval)
  186. if len(args) > 0:
  187. ret += mcgen('''
  188. %(visitor_input_containers_decl)s
  189. %(visitor_input_vars_decl)s
  190. %(visitor_input_block)s
  191. ''',
  192. visitor_input_containers_decl=gen_visitor_input_containers_decl(args),
  193. visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
  194. visitor_input_block=gen_visitor_input_block(args, "QOBJECT(args)"))
  195. ret += mcgen('''
  196. if (error_is_set(errp)) {
  197. goto out;
  198. }
  199. %(sync_call)s
  200. ''',
  201. sync_call=gen_sync_call(name, args, ret_type, indent=4))
  202. ret += mcgen('''
  203. out:
  204. ''')
  205. ret += mcgen('''
  206. %(visitor_input_block_cleanup)s
  207. return;
  208. }
  209. ''',
  210. visitor_input_block_cleanup=gen_visitor_input_block(args, None, dealloc=True))
  211. return ret
  212. def gen_registry(commands):
  213. registry=""
  214. push_indent()
  215. for cmd in commands:
  216. registry += mcgen('''
  217. qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s);
  218. ''',
  219. name=cmd['command'], c_name=c_var(cmd['command']))
  220. pop_indent()
  221. ret = mcgen('''
  222. static void qmp_init_marshal(void)
  223. {
  224. %(registry)s
  225. }
  226. qapi_init(qmp_init_marshal);
  227. ''',
  228. registry=registry.rstrip())
  229. return ret
  230. def gen_command_decl_prologue(header, guard, prefix=""):
  231. ret = mcgen('''
  232. /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
  233. /*
  234. * schema-defined QAPI function prototypes
  235. *
  236. * Copyright IBM, Corp. 2011
  237. *
  238. * Authors:
  239. * Anthony Liguori <aliguori@us.ibm.com>
  240. *
  241. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  242. * See the COPYING.LIB file in the top-level directory.
  243. *
  244. */
  245. #ifndef %(guard)s
  246. #define %(guard)s
  247. #include "%(prefix)sqapi-types.h"
  248. #include "error.h"
  249. ''',
  250. header=basename(h_file), guard=guardname(h_file), prefix=prefix)
  251. return ret
  252. def gen_command_def_prologue(prefix="", proxy=False):
  253. ret = mcgen('''
  254. /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
  255. /*
  256. * schema-defined QMP->QAPI command dispatch
  257. *
  258. * Copyright IBM, Corp. 2011
  259. *
  260. * Authors:
  261. * Anthony Liguori <aliguori@us.ibm.com>
  262. *
  263. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  264. * See the COPYING.LIB file in the top-level directory.
  265. *
  266. */
  267. #include "qemu-objects.h"
  268. #include "qapi/qmp-core.h"
  269. #include "qapi/qapi-visit-core.h"
  270. #include "qapi/qmp-output-visitor.h"
  271. #include "qapi/qmp-input-visitor.h"
  272. #include "qapi/qapi-dealloc-visitor.h"
  273. #include "%(prefix)sqapi-types.h"
  274. #include "%(prefix)sqapi-visit.h"
  275. ''',
  276. prefix=prefix)
  277. if not proxy:
  278. ret += '#include "%sqmp-commands.h"' % prefix
  279. return ret + "\n"
  280. try:
  281. opts, args = getopt.gnu_getopt(sys.argv[1:], "p:o:", ["prefix=", "output-dir=", "type="])
  282. except getopt.GetoptError, err:
  283. print str(err)
  284. sys.exit(1)
  285. output_dir = ""
  286. prefix = ""
  287. dispatch_type = "sync"
  288. c_file = 'qmp-marshal.c'
  289. h_file = 'qmp-commands.h'
  290. for o, a in opts:
  291. if o in ("-p", "--prefix"):
  292. prefix = a
  293. elif o in ("-o", "--output-dir"):
  294. output_dir = a + "/"
  295. elif o in ("-t", "--type"):
  296. dispatch_type = a
  297. c_file = output_dir + prefix + c_file
  298. h_file = output_dir + prefix + h_file
  299. try:
  300. os.makedirs(output_dir)
  301. except os.error, e:
  302. if e.errno != errno.EEXIST:
  303. raise
  304. exprs = parse_schema(sys.stdin)
  305. commands = filter(lambda expr: expr.has_key('command'), exprs)
  306. if dispatch_type == "sync":
  307. fdecl = open(h_file, 'w')
  308. fdef = open(c_file, 'w')
  309. ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
  310. fdecl.write(ret)
  311. ret = gen_command_def_prologue(prefix=prefix)
  312. fdef.write(ret)
  313. for cmd in commands:
  314. arglist = []
  315. ret_type = None
  316. if cmd.has_key('data'):
  317. arglist = cmd['data']
  318. if cmd.has_key('returns'):
  319. ret_type = cmd['returns']
  320. ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
  321. fdecl.write(ret)
  322. if ret_type:
  323. ret = gen_marshal_output(cmd['command'], arglist, ret_type) + "\n"
  324. fdef.write(ret)
  325. ret = gen_marshal_input(cmd['command'], arglist, ret_type) + "\n"
  326. fdef.write(ret)
  327. fdecl.write("\n#endif\n");
  328. ret = gen_registry(commands)
  329. fdef.write(ret)
  330. fdef.flush()
  331. fdef.close()
  332. fdecl.flush()
  333. fdecl.close()