7 from vapi_c_gen import CField, CEnum, CStruct, CSimpleType, CStructType,\
8 CMessage, json_to_c_header_name, CAlias
9 from vapi_json_parser import JsonParser
12 class CppField(CField):
16 class CppStruct(CStruct):
24 class CppAlias(CAlias):
28 class CppSimpleType (CSimpleType):
32 class CppStructType (CStructType, CppStruct):
36 class CppMessage (CMessage):
37 def get_swap_to_be_template_instantiation(self):
39 "template <> inline void vapi_swap_to_be<%s>(%s *msg)" %
40 (self.get_c_name(), self.get_c_name()),
42 " %s(msg);" % self.get_swap_to_be_func_name(),
46 def get_swap_to_host_template_instantiation(self):
48 "template <> inline void vapi_swap_to_host<%s>(%s *msg)" %
49 (self.get_c_name(), self.get_c_name()),
51 " %s(msg);" % self.get_swap_to_host_func_name(),
55 def get_alloc_template_instantiation(self):
57 "template <> inline %s* vapi_alloc<%s%s>"
58 "(Connection &con%s)" %
59 (self.get_c_name(), self.get_c_name(),
60 ", size_t" * len(self.get_alloc_vla_param_names()),
61 "".join([", size_t %s" % n for n in
62 self.get_alloc_vla_param_names()])
65 " %s* result = %s(con.vapi_ctx%s);" %
66 (self.get_c_name(), self.get_alloc_func_name(),
68 for n in self.get_alloc_vla_param_names()])),
69 "#if VAPI_CPP_DEBUG_LEAKS",
70 " con.on_shm_data_alloc(result);",
76 def get_cpp_name(self):
77 return "%s%s" % (self.name[0].upper(), self.name[1:])
79 def get_req_template_name(self):
80 if self.reply_is_stream:
85 return "%s<%s, %s%s>" % (
88 self.reply.get_c_name(),
89 "".join([", size_t"] * len(self.get_alloc_vla_param_names()))
92 def get_req_template_instantiation(self):
93 return "template class %s;" % self.get_req_template_name()
95 def get_type_alias(self):
96 return "using %s = %s;" % (
97 self.get_cpp_name(), self.get_req_template_name())
99 def get_reply_template_name(self):
100 return "Msg<%s>" % (self.get_c_name())
102 def get_reply_type_alias(self):
103 return "using %s = %s;" % (
104 self.get_cpp_name(), self.get_reply_template_name())
106 def get_msg_class_instantiation(self):
107 return "template class Msg<%s>;" % self.get_c_name()
109 def get_get_msg_id_t_instantiation(self):
111 ("template <> inline vapi_msg_id_t vapi_get_msg_id_t<%s>()"
112 % self.get_c_name()),
114 " return ::%s; " % self.get_msg_id_name(),
117 ("template <> inline vapi_msg_id_t "
118 "vapi_get_msg_id_t<Msg<%s>>()" % self.get_c_name()),
120 " return ::%s; " % self.get_msg_id_name(),
124 def get_cpp_constructor(self):
126 ('static void __attribute__((constructor)) '
127 '__vapi_cpp_constructor_%s()'
130 (' vapi::vapi_msg_set_msg_id<%s>(%s);' % (
131 self.get_c_name(), self.get_msg_id_name())),
136 def gen_json_header(parser, logger, j, io, gen_h_prefix, add_debug_comments):
137 logger.info("Generating header `%s'" % io.name)
138 orig_stdout = sys.stdout
140 d, f = os.path.split(j)
141 include_guard = "__included_hpp_%s" % (
142 f.replace(".", "_").replace("/", "_").replace("-", "_"))
143 print("#ifndef %s" % include_guard)
144 print("#define %s" % include_guard)
146 print("#include <vapi/vapi.hpp>")
147 print("#include <%s%s>" % (gen_h_prefix, json_to_c_header_name(f)))
149 print("namespace vapi {")
151 for m in parser.messages_by_json[j].values():
152 # utility functions need to go first, otherwise internal instantiation
153 # causes headaches ...
154 if add_debug_comments:
155 print("/* m.get_swap_to_be_template_instantiation() */")
156 print("%s" % m.get_swap_to_be_template_instantiation())
158 if add_debug_comments:
159 print("/* m.get_swap_to_host_template_instantiation() */")
160 print("%s" % m.get_swap_to_host_template_instantiation())
162 if add_debug_comments:
163 print("/* m.get_get_msg_id_t_instantiation() */")
164 print("%s" % m.get_get_msg_id_t_instantiation())
166 if add_debug_comments:
167 print("/* m.get_cpp_constructor() */")
168 print("%s" % m.get_cpp_constructor())
170 if not m.is_reply and not m.is_event:
171 if add_debug_comments:
172 print("/* m.get_alloc_template_instantiation() */")
173 print("%s" % m.get_alloc_template_instantiation())
175 if add_debug_comments:
176 print("/* m.get_msg_class_instantiation() */")
177 print("%s" % m.get_msg_class_instantiation())
179 if m.is_reply or m.is_event:
180 if add_debug_comments:
181 print("/* m.get_reply_type_alias() */")
182 print("%s" % m.get_reply_type_alias())
184 if add_debug_comments:
185 print("/* m.get_req_template_instantiation() */")
186 print("%s" % m.get_req_template_instantiation())
188 if add_debug_comments:
189 print("/* m.get_type_alias() */")
190 print("%s" % m.get_type_alias())
192 print("}") # namespace vapi
195 sys.stdout = orig_stdout
198 def json_to_cpp_header_name(json_name):
199 if json_name.endswith(".json"):
200 return "%s.vapi.hpp" % os.path.splitext(json_name)[0]
201 raise Exception("Unexpected json name `%s'!" % json_name)
204 def gen_cpp_headers(parser, logger, prefix, gen_h_prefix, remove_path,
205 add_debug_comments=False):
206 if prefix == "" or prefix is None:
209 prefix = "%s/" % prefix
210 if gen_h_prefix is None:
213 gen_h_prefix = "%s/" % gen_h_prefix
214 for j in parser.json_files:
216 d, f = os.path.split(j)
219 with open('%s%s' % (prefix, json_to_cpp_header_name(f)), "w") as io:
220 gen_json_header(parser, logger, j, io,
221 gen_h_prefix, add_debug_comments)
224 if __name__ == '__main__':
226 verbose = int(os.getenv("V", 0))
237 logging.basicConfig(stream=sys.stdout, level=log_level)
238 logger = logging.getLogger("VAPI CPP GEN")
239 logger.setLevel(log_level)
241 argparser = argparse.ArgumentParser(description="VPP C++ API generator")
242 argparser.add_argument('files', metavar='api-file', action='append',
243 type=str, help='json api file'
244 '(may be specified multiple times)')
245 argparser.add_argument('--prefix', action='store', default=None,
247 argparser.add_argument('--gen-h-prefix', action='store', default=None,
248 help='generated C header prefix')
249 argparser.add_argument('--remove-path', action='store_true',
250 help='remove path from filename')
251 args = argparser.parse_args()
253 jsonparser = JsonParser(logger, args.files,
254 simple_type_class=CppSimpleType,
255 struct_type_class=CppStructType,
256 field_class=CppField,
258 message_class=CppMessage,
259 alias_class=CppAlias)
261 gen_cpp_headers(jsonparser, logger, args.prefix, args.gen_h_prefix,
264 for e in jsonparser.exceptions: