slirp: fix build on mingw32
[qemu-kvm.git] / scripts / qapi-types.py
blob4a734f58d516dbe14cbd9cb224136e3c6f76daec
2 # QAPI types generator
4 # Copyright IBM, Corp. 2011
6 # Authors:
7 # Anthony Liguori <aliguori@us.ibm.com>
9 # This work is licensed under the terms of the GNU GPLv2.
10 # 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
19 def generate_fwd_struct(name, members):
20 return mcgen('''
21 typedef struct %(name)s %(name)s;
23 typedef struct %(name)sList
25 %(name)s *value;
26 struct %(name)sList *next;
27 } %(name)sList;
28 ''',
29 name=name)
31 def generate_struct(structname, fieldname, members):
32 ret = mcgen('''
33 struct %(name)s
35 ''',
36 name=structname)
38 for argname, argentry, optional, structured in parse_args(members):
39 if optional:
40 ret += mcgen('''
41 bool has_%(c_name)s;
42 ''',
43 c_name=c_var(argname))
44 if structured:
45 push_indent()
46 ret += generate_struct("", argname, argentry)
47 pop_indent()
48 else:
49 ret += mcgen('''
50 %(c_type)s %(c_name)s;
51 ''',
52 c_type=c_type(argentry), c_name=c_var(argname))
54 if len(fieldname):
55 fieldname = " " + fieldname
56 ret += mcgen('''
57 }%(field)s;
58 ''',
59 field=fieldname)
61 return ret
63 def generate_enum_lookup(name, values):
64 ret = mcgen('''
65 const char *%(name)s_lookup[] = {
66 ''',
67 name=name)
68 i = 0
69 for value in values:
70 ret += mcgen('''
71 "%(value)s",
72 ''',
73 value=value.lower())
75 ret += mcgen('''
76 NULL,
79 ''')
80 return ret
82 def generate_enum(name, values):
83 lookup_decl = mcgen('''
84 extern const char *%(name)s_lookup[];
85 ''',
86 name=name)
88 enum_decl = mcgen('''
89 typedef enum %(name)s
91 ''',
92 name=name)
94 # append automatically generated _MAX value
95 enum_values = values + [ 'MAX' ]
97 i = 0
98 for value in enum_values:
99 enum_decl += mcgen('''
100 %(abbrev)s_%(value)s = %(i)d,
101 ''',
102 abbrev=de_camel_case(name).upper(),
103 value=c_fun(value).upper(),
104 i=i)
105 i += 1
107 enum_decl += mcgen('''
108 } %(name)s;
109 ''',
110 name=name)
112 return lookup_decl + enum_decl
114 def generate_union(name, typeinfo):
115 ret = mcgen('''
116 struct %(name)s
118 %(name)sKind kind;
119 union {
120 void *data;
121 ''',
122 name=name)
124 for key in typeinfo:
125 ret += mcgen('''
126 %(c_type)s %(c_name)s;
127 ''',
128 c_type=c_type(typeinfo[key]),
129 c_name=c_fun(key))
131 ret += mcgen('''
134 ''')
136 return ret
138 def generate_type_cleanup_decl(name):
139 ret = mcgen('''
140 void qapi_free_%(type)s(%(c_type)s obj);
141 ''',
142 c_type=c_type(name),type=name)
143 return ret
145 def generate_type_cleanup(name):
146 ret = mcgen('''
147 void qapi_free_%(type)s(%(c_type)s obj)
149 QapiDeallocVisitor *md;
150 Visitor *v;
152 if (!obj) {
153 return;
156 md = qapi_dealloc_visitor_new();
157 v = qapi_dealloc_get_visitor(md);
158 visit_type_%(type)s(v, &obj, NULL, NULL);
159 qapi_dealloc_visitor_cleanup(md);
161 ''',
162 c_type=c_type(name),type=name)
163 return ret
166 try:
167 opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:o:",
168 ["source", "header", "prefix=", "output-dir="])
169 except getopt.GetoptError, err:
170 print str(err)
171 sys.exit(1)
173 output_dir = ""
174 prefix = ""
175 c_file = 'qapi-types.c'
176 h_file = 'qapi-types.h'
178 do_c = False
179 do_h = False
181 for o, a in opts:
182 if o in ("-p", "--prefix"):
183 prefix = a
184 elif o in ("-o", "--output-dir"):
185 output_dir = a + "/"
186 elif o in ("-c", "--source"):
187 do_c = True
188 elif o in ("-h", "--header"):
189 do_h = True
191 if not do_c and not do_h:
192 do_c = True
193 do_h = True
195 c_file = output_dir + prefix + c_file
196 h_file = output_dir + prefix + h_file
198 try:
199 os.makedirs(output_dir)
200 except os.error, e:
201 if e.errno != errno.EEXIST:
202 raise
204 def maybe_open(really, name, opt):
205 if really:
206 return open(name, opt)
207 else:
208 import StringIO
209 return StringIO.StringIO()
211 fdef = maybe_open(do_c, c_file, 'w')
212 fdecl = maybe_open(do_h, h_file, 'w')
214 fdef.write(mcgen('''
215 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
218 * deallocation functions for schema-defined QAPI types
220 * Copyright IBM, Corp. 2011
222 * Authors:
223 * Anthony Liguori <aliguori@us.ibm.com>
224 * Michael Roth <mdroth@linux.vnet.ibm.com>
226 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
227 * See the COPYING.LIB file in the top-level directory.
231 #include "qapi/qapi-dealloc-visitor.h"
232 #include "%(prefix)sqapi-types.h"
233 #include "%(prefix)sqapi-visit.h"
235 ''', prefix=prefix))
237 fdecl.write(mcgen('''
238 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
241 * schema-defined QAPI types
243 * Copyright IBM, Corp. 2011
245 * Authors:
246 * Anthony Liguori <aliguori@us.ibm.com>
248 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
249 * See the COPYING.LIB file in the top-level directory.
253 #ifndef %(guard)s
254 #define %(guard)s
256 #include "qapi/qapi-types-core.h"
257 ''',
258 guard=guardname(h_file)))
260 exprs = parse_schema(sys.stdin)
261 exprs = filter(lambda expr: not expr.has_key('gen'), exprs)
263 for expr in exprs:
264 ret = "\n"
265 if expr.has_key('type'):
266 ret += generate_fwd_struct(expr['type'], expr['data'])
267 elif expr.has_key('enum'):
268 ret += generate_enum(expr['enum'], expr['data'])
269 fdef.write(generate_enum_lookup(expr['enum'], expr['data']))
270 elif expr.has_key('union'):
271 ret += generate_fwd_struct(expr['union'], expr['data']) + "\n"
272 ret += generate_enum('%sKind' % expr['union'], expr['data'].keys())
273 fdef.write(generate_enum_lookup('%sKind' % expr['union'], expr['data'].keys()))
274 else:
275 continue
276 fdecl.write(ret)
278 for expr in exprs:
279 ret = "\n"
280 if expr.has_key('type'):
281 ret += generate_struct(expr['type'], "", expr['data']) + "\n"
282 ret += generate_type_cleanup_decl(expr['type'] + "List")
283 fdef.write(generate_type_cleanup(expr['type'] + "List") + "\n")
284 ret += generate_type_cleanup_decl(expr['type'])
285 fdef.write(generate_type_cleanup(expr['type']) + "\n")
286 elif expr.has_key('union'):
287 ret += generate_union(expr['union'], expr['data'])
288 ret += generate_type_cleanup_decl(expr['union'] + "List")
289 fdef.write(generate_type_cleanup(expr['union'] + "List") + "\n")
290 ret += generate_type_cleanup_decl(expr['union'])
291 fdef.write(generate_type_cleanup(expr['union']) + "\n")
292 else:
293 continue
294 fdecl.write(ret)
296 fdecl.write('''
297 #endif
298 ''')
300 fdecl.flush()
301 fdecl.close()
303 fdef.flush()
304 fdef.close()