blob: 6b62bc4da223d0ad8c3bbb2f031cdfa4ed24e9c2 [file] [log] [blame]
Klement Sekera958b7502017-09-28 06:31:53 +02001#!/usr/bin/env python2
Klement Sekeradc15be22017-06-12 06:49:33 +02002
3import argparse
4import os
5import sys
6import logging
Klement Sekera2108c0c2018-08-24 11:43:20 +02007from vapi_c_gen import CField, CEnum, CStruct, CSimpleType, CStructType,\
8 CMessage, json_to_c_header_name
Klement Sekeradc15be22017-06-12 06:49:33 +02009from vapi_json_parser import JsonParser
10
11
12class CppField(CField):
Klement Sekera8b6b5ab2018-05-03 14:27:42 +020013 pass
Klement Sekeradc15be22017-06-12 06:49:33 +020014
15
16class CppStruct(CStruct):
Klement Sekera8b6b5ab2018-05-03 14:27:42 +020017 pass
Klement Sekeradc15be22017-06-12 06:49:33 +020018
19
Klement Sekera2108c0c2018-08-24 11:43:20 +020020class CppEnum(CEnum):
21 pass
22
23
Klement Sekeradc15be22017-06-12 06:49:33 +020024class CppSimpleType (CSimpleType):
Klement Sekera8b6b5ab2018-05-03 14:27:42 +020025 pass
Klement Sekeradc15be22017-06-12 06:49:33 +020026
27
28class CppStructType (CStructType, CppStruct):
Klement Sekera8b6b5ab2018-05-03 14:27:42 +020029 pass
Klement Sekeradc15be22017-06-12 06:49:33 +020030
31
32class CppMessage (CMessage):
Klement Sekeradc15be22017-06-12 06:49:33 +020033 def get_swap_to_be_template_instantiation(self):
34 return "\n".join([
35 "template <> inline void vapi_swap_to_be<%s>(%s *msg)" %
36 (self.get_c_name(), self.get_c_name()),
37 "{",
38 " %s(msg);" % self.get_swap_to_be_func_name(),
39 "}",
40 ])
41
42 def get_swap_to_host_template_instantiation(self):
43 return "\n".join([
44 "template <> inline void vapi_swap_to_host<%s>(%s *msg)" %
45 (self.get_c_name(), self.get_c_name()),
46 "{",
47 " %s(msg);" % self.get_swap_to_host_func_name(),
48 "}",
49 ])
50
51 def get_alloc_template_instantiation(self):
52 return "\n".join([
53 "template <> inline %s* vapi_alloc<%s%s>"
54 "(Connection &con%s)" %
55 (self.get_c_name(), self.get_c_name(),
56 ", size_t" * len(self.get_alloc_vla_param_names()),
57 "".join([", size_t %s" % n for n in
58 self.get_alloc_vla_param_names()])
59 ),
60 "{",
61 " %s* result = %s(con.vapi_ctx%s);" %
62 (self.get_c_name(), self.get_alloc_func_name(),
63 "".join([", %s" % n
64 for n in self.get_alloc_vla_param_names()])),
65 "#if VAPI_CPP_DEBUG_LEAKS",
66 " con.on_shm_data_alloc(result);",
67 "#endif",
68 " return result;",
69 "}",
70 ])
71
72 def get_cpp_name(self):
73 return "%s%s" % (self.name[0].upper(), self.name[1:])
74
75 def get_req_template_name(self):
Klement Sekera2108c0c2018-08-24 11:43:20 +020076 if self.reply_is_stream:
Klement Sekeradc15be22017-06-12 06:49:33 +020077 template = "Dump"
78 else:
79 template = "Request"
80
81 return "%s<%s, %s%s>" % (
82 template,
83 self.get_c_name(),
84 self.reply.get_c_name(),
85 "".join([", size_t"] * len(self.get_alloc_vla_param_names()))
86 )
87
88 def get_req_template_instantiation(self):
89 return "template class %s;" % self.get_req_template_name()
90
91 def get_type_alias(self):
92 return "using %s = %s;" % (
93 self.get_cpp_name(), self.get_req_template_name())
94
95 def get_reply_template_name(self):
96 return "Msg<%s>" % (self.get_c_name())
97
98 def get_reply_type_alias(self):
99 return "using %s = %s;" % (
100 self.get_cpp_name(), self.get_reply_template_name())
101
102 def get_msg_class_instantiation(self):
103 return "template class Msg<%s>;" % self.get_c_name()
104
105 def get_get_msg_id_t_instantiation(self):
106 return "\n".join([
107 ("template <> inline vapi_msg_id_t vapi_get_msg_id_t<%s>()"
108 % self.get_c_name()),
109 "{",
110 " return ::%s; " % self.get_msg_id_name(),
111 "}",
112 "",
113 ("template <> inline vapi_msg_id_t "
114 "vapi_get_msg_id_t<Msg<%s>>()" % self.get_c_name()),
115 "{",
116 " return ::%s; " % self.get_msg_id_name(),
117 "}",
118 ])
119
120 def get_cpp_constructor(self):
121 return '\n'.join([
122 ('static void __attribute__((constructor)) '
123 '__vapi_cpp_constructor_%s()'
124 % self.name),
125 '{',
126 (' vapi::vapi_msg_set_msg_id<%s>(%s);' % (
127 self.get_c_name(), self.get_msg_id_name())),
128 '}',
129 ])
130
131
132def gen_json_header(parser, logger, j, io, gen_h_prefix, add_debug_comments):
133 logger.info("Generating header `%s'" % io.name)
134 orig_stdout = sys.stdout
135 sys.stdout = io
Damjan Marion4c64b6e2018-08-26 18:14:46 +0200136 d, f = os.path.split(j)
Klement Sekeradc15be22017-06-12 06:49:33 +0200137 include_guard = "__included_hpp_%s" % (
Damjan Marion4c64b6e2018-08-26 18:14:46 +0200138 f.replace(".", "_").replace("/", "_").replace("-", "_"))
Klement Sekeradc15be22017-06-12 06:49:33 +0200139 print("#ifndef %s" % include_guard)
140 print("#define %s" % include_guard)
141 print("")
142 print("#include <vapi/vapi.hpp>")
Mohsin Kazmib3abec72018-08-27 13:17:09 +0200143 print("#include <%s%s>" % (gen_h_prefix, json_to_c_header_name(f)))
Klement Sekeradc15be22017-06-12 06:49:33 +0200144 print("")
145 print("namespace vapi {")
146 print("")
147 for m in parser.messages_by_json[j].values():
148 # utility functions need to go first, otherwise internal instantiation
149 # causes headaches ...
150 if add_debug_comments:
151 print("/* m.get_swap_to_be_template_instantiation() */")
152 print("%s" % m.get_swap_to_be_template_instantiation())
153 print("")
154 if add_debug_comments:
155 print("/* m.get_swap_to_host_template_instantiation() */")
156 print("%s" % m.get_swap_to_host_template_instantiation())
157 print("")
158 if add_debug_comments:
159 print("/* m.get_get_msg_id_t_instantiation() */")
160 print("%s" % m.get_get_msg_id_t_instantiation())
161 print("")
162 if add_debug_comments:
163 print("/* m.get_cpp_constructor() */")
164 print("%s" % m.get_cpp_constructor())
165 print("")
Klement Sekera2108c0c2018-08-24 11:43:20 +0200166 if not m.is_reply and not m.is_event:
Klement Sekeradc15be22017-06-12 06:49:33 +0200167 if add_debug_comments:
168 print("/* m.get_alloc_template_instantiation() */")
169 print("%s" % m.get_alloc_template_instantiation())
170 print("")
171 if add_debug_comments:
172 print("/* m.get_msg_class_instantiation() */")
173 print("%s" % m.get_msg_class_instantiation())
174 print("")
Klement Sekera2108c0c2018-08-24 11:43:20 +0200175 if m.is_reply or m.is_event:
Klement Sekeradc15be22017-06-12 06:49:33 +0200176 if add_debug_comments:
177 print("/* m.get_reply_type_alias() */")
178 print("%s" % m.get_reply_type_alias())
179 continue
180 if add_debug_comments:
181 print("/* m.get_req_template_instantiation() */")
182 print("%s" % m.get_req_template_instantiation())
183 print("")
184 if add_debug_comments:
185 print("/* m.get_type_alias() */")
186 print("%s" % m.get_type_alias())
187 print("")
188 print("}") # namespace vapi
189
190 print("#endif")
191 sys.stdout = orig_stdout
192
193
194def json_to_cpp_header_name(json_name):
195 if json_name.endswith(".json"):
196 return "%s.vapi.hpp" % os.path.splitext(json_name)[0]
197 raise Exception("Unexpected json name `%s'!" % json_name)
198
199
Damjan Marion4c64b6e2018-08-26 18:14:46 +0200200def gen_cpp_headers(parser, logger, prefix, gen_h_prefix, remove_path,
Klement Sekeradc15be22017-06-12 06:49:33 +0200201 add_debug_comments=False):
202 if prefix == "" or prefix is None:
203 prefix = ""
204 else:
205 prefix = "%s/" % prefix
206 if gen_h_prefix is None:
207 gen_h_prefix = ""
208 else:
209 gen_h_prefix = "%s/" % gen_h_prefix
210 for j in parser.json_files:
Damjan Marion4c64b6e2018-08-26 18:14:46 +0200211 if remove_path:
212 d, f = os.path.split(j)
213 else:
214 f = j
215 with open('%s%s' % (prefix, json_to_cpp_header_name(f)), "w") as io:
Klement Sekeradc15be22017-06-12 06:49:33 +0200216 gen_json_header(parser, logger, j, io,
217 gen_h_prefix, add_debug_comments)
218
219
220if __name__ == '__main__':
221 try:
222 verbose = int(os.getenv("V", 0))
223 except:
224 verbose = 0
225
226 if verbose >= 2:
227 log_level = 10
228 elif verbose == 1:
229 log_level = 20
230 else:
231 log_level = 40
232
233 logging.basicConfig(stream=sys.stdout, level=log_level)
234 logger = logging.getLogger("VAPI CPP GEN")
235 logger.setLevel(log_level)
236
237 argparser = argparse.ArgumentParser(description="VPP C++ API generator")
238 argparser.add_argument('files', metavar='api-file', action='append',
239 type=str, help='json api file'
240 '(may be specified multiple times)')
241 argparser.add_argument('--prefix', action='store', default=None,
242 help='path prefix')
243 argparser.add_argument('--gen-h-prefix', action='store', default=None,
244 help='generated C header prefix')
Damjan Marion4c64b6e2018-08-26 18:14:46 +0200245 argparser.add_argument('--remove-path', action='store_true',
246 help='remove path from filename')
Klement Sekeradc15be22017-06-12 06:49:33 +0200247 args = argparser.parse_args()
248
249 jsonparser = JsonParser(logger, args.files,
250 simple_type_class=CppSimpleType,
251 struct_type_class=CppStructType,
252 field_class=CppField,
Klement Sekera2108c0c2018-08-24 11:43:20 +0200253 enum_class=CppEnum,
Klement Sekeradc15be22017-06-12 06:49:33 +0200254 message_class=CppMessage)
255
Damjan Marion4c64b6e2018-08-26 18:14:46 +0200256 gen_cpp_headers(jsonparser, logger, args.prefix, args.gen_h_prefix,
257 args.remove_path)
Klement Sekeradc15be22017-06-12 06:49:33 +0200258
259 for e in jsonparser.exceptions:
Damjan Marion4c64b6e2018-08-26 18:14:46 +0200260 logger.warning(e)