Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1 | # C generation |
| 2 | import datetime |
| 3 | import os |
Nirmoy Das | c9f4022 | 2018-06-28 10:18:43 +0200 | [diff] [blame] | 4 | import time |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 5 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 6 | datestring = datetime.datetime.utcfromtimestamp( |
| 7 | int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 8 | input_filename = 'inputfil' |
| 9 | top_boilerplate = '''\ |
| 10 | /* |
| 11 | * VLIB API definitions {datestring} |
| 12 | * Input file: {input_filename} |
| 13 | * Automatically generated: please edit the input file NOT this file! |
| 14 | */ |
| 15 | |
| 16 | #if defined(vl_msg_id)||defined(vl_union_id) \\ |
| 17 | || defined(vl_printfun) ||defined(vl_endianfun) \\ |
| 18 | || defined(vl_api_version)||defined(vl_typedefs) \\ |
| 19 | || defined(vl_msg_name)||defined(vl_msg_name_crc_list) \\ |
| 20 | || defined(vl_api_version_tuple) |
| 21 | /* ok, something was selected */ |
| 22 | #else |
| 23 | #warning no content included from {input_filename} |
| 24 | #endif |
| 25 | |
| 26 | #define VL_API_PACKED(x) x __attribute__ ((packed)) |
| 27 | ''' |
| 28 | |
| 29 | bottom_boilerplate = '''\ |
| 30 | /****** API CRC (whole file) *****/ |
| 31 | |
| 32 | #ifdef vl_api_version |
| 33 | vl_api_version({input_filename}, {file_crc:#08x}) |
| 34 | |
| 35 | #endif |
| 36 | ''' |
| 37 | |
| 38 | |
| 39 | def msg_ids(s): |
| 40 | output = '''\ |
| 41 | |
| 42 | /****** Message ID / handler enum ******/ |
| 43 | |
| 44 | #ifdef vl_msg_id |
| 45 | ''' |
| 46 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 47 | for t in s['Define']: |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 48 | output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \ |
| 49 | (t.name.upper(), t.name) |
| 50 | output += "#endif" |
| 51 | |
| 52 | return output |
| 53 | |
| 54 | |
| 55 | def msg_names(s): |
| 56 | output = '''\ |
| 57 | |
| 58 | /****** Message names ******/ |
| 59 | |
| 60 | #ifdef vl_msg_name |
| 61 | ''' |
| 62 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 63 | for t in s['Define']: |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 64 | dont_trace = 0 if t.dont_trace else 1 |
| 65 | output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace) |
| 66 | output += "#endif" |
| 67 | |
| 68 | return output |
| 69 | |
| 70 | |
| 71 | def msg_name_crc_list(s, suffix): |
| 72 | output = '''\ |
| 73 | |
| 74 | /****** Message name, crc list ******/ |
| 75 | |
| 76 | #ifdef vl_msg_name_crc_list |
| 77 | ''' |
| 78 | output += "#define foreach_vl_msg_name_crc_%s " % suffix |
| 79 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 80 | for t in s['Define']: |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 81 | output += "\\\n_(VL_API_%s, %s, %08x) " % \ |
| 82 | (t.name.upper(), t.name, t.crc) |
| 83 | output += "\n#endif" |
| 84 | |
| 85 | return output |
| 86 | |
| 87 | |
| 88 | def duplicate_wrapper_head(name): |
Ole Troan | 02143df | 2018-11-30 08:34:11 +0100 | [diff] [blame] | 89 | s = "#ifndef _vl_api_defined_%s\n" % name |
| 90 | s += "#define _vl_api_defined_%s\n" % name |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 91 | return s |
| 92 | |
| 93 | |
| 94 | def duplicate_wrapper_tail(): |
| 95 | return '#endif\n\n' |
| 96 | |
| 97 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 98 | def api2c(fieldtype): |
| 99 | mappingtable = {'string': 'vl_api_string_t', } |
| 100 | if fieldtype in mappingtable: |
| 101 | return mappingtable[fieldtype] |
| 102 | return fieldtype |
| 103 | |
| 104 | |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 105 | def typedefs(objs, aliases, filename): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 106 | name = filename.replace('.', '_') |
| 107 | output = '''\ |
| 108 | |
| 109 | |
| 110 | /****** Typedefs ******/ |
| 111 | |
| 112 | #ifdef vl_typedefs |
| 113 | #ifndef included_{module} |
| 114 | #define included_{module} |
| 115 | ''' |
| 116 | output = output.format(module=name) |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 117 | |
| 118 | for k, v in aliases.items(): |
| 119 | output += duplicate_wrapper_head(k) |
| 120 | if 'length' in v: |
| 121 | output += 'typedef %s vl_api_%s_t[%s];\n' % (v['type'], k, v['length']) |
| 122 | else: |
| 123 | output += 'typedef %s vl_api_%s_t;\n' % (v['type'], k) |
| 124 | output += duplicate_wrapper_tail() |
| 125 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 126 | for o in objs: |
| 127 | tname = o.__class__.__name__ |
| 128 | output += duplicate_wrapper_head(o.name) |
| 129 | if tname == 'Enum': |
| 130 | output += "typedef enum {\n" |
| 131 | for b in o.block: |
| 132 | output += " %s = %s,\n" % (b[0], b[1]) |
| 133 | output += '} vl_api_%s_t;\n' % o.name |
| 134 | else: |
| 135 | if tname == 'Union': |
| 136 | output += "typedef VL_API_PACKED(union _vl_api_%s {\n" % o.name |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 137 | else: |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 138 | output += "typedef VL_API_PACKED(struct _vl_api_%s {\n" % o.name |
| 139 | for b in o.block: |
| 140 | if b.type == 'Field': |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 141 | output += " %s %s;\n" % (api2c(b.fieldtype), b.fieldname) |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 142 | elif b.type == 'Array': |
| 143 | if b.lengthfield: |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 144 | output += " %s %s[0];\n" % (api2c(b.fieldtype), b.fieldname) |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 145 | else: |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 146 | output += " %s %s[%s];\n" % (api2c(b.fieldtype), b.fieldname, |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 147 | b.length) |
| 148 | else: |
| 149 | raise ValueError("Error in processing array type %s" % b) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 150 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 151 | output += '}) vl_api_%s_t;\n' % o.name |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 152 | output += duplicate_wrapper_tail() |
| 153 | |
| 154 | output += "\n#endif" |
| 155 | output += "\n#endif\n\n" |
| 156 | |
| 157 | return output |
| 158 | |
| 159 | |
| 160 | format_strings = {'u8': '%u', |
| 161 | 'i8': '%d', |
| 162 | 'u16': '%u', |
| 163 | 'i16': '%d', |
| 164 | 'u32': '%u', |
| 165 | 'i32': '%ld', |
| 166 | 'u64': '%llu', |
| 167 | 'i64': '%llu', |
| 168 | 'f64': '%.2f', } |
| 169 | |
| 170 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 171 | def printfun(objs): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 172 | output = '''\ |
| 173 | /****** Print functions *****/ |
| 174 | #ifdef vl_printfun |
| 175 | |
| 176 | #ifdef LP64 |
| 177 | #define _uword_fmt \"%lld\" |
| 178 | #define _uword_cast (long long) |
| 179 | #else |
| 180 | #define _uword_fmt \"%ld\" |
| 181 | #define _uword_cast long |
| 182 | #endif |
| 183 | |
| 184 | ''' |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 185 | for t in objs: |
| 186 | if t.__class__.__name__ == 'Enum': |
| 187 | continue |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 188 | if t.manual_print: |
| 189 | output += "/***** manual: vl_api_%s_t_print *****/\n\n" % t.name |
| 190 | continue |
| 191 | output += duplicate_wrapper_head(t.name + '_t_print') |
| 192 | output += "static inline void *vl_api_%s_t_print (vl_api_%s_t *a," % \ |
| 193 | (t.name, t.name) |
| 194 | output += "void *handle)\n{\n" |
| 195 | output += " vl_print(handle, \"vl_api_%s_t:\\n\");\n" % t.name |
| 196 | |
| 197 | for o in t.block: |
| 198 | if o.type != 'Field': |
| 199 | continue |
| 200 | if o.fieldtype in format_strings: |
| 201 | output += " vl_print(handle, \"%s: %s\\n\", a->%s);\n" % \ |
| 202 | (o.fieldname, format_strings[o.fieldtype], |
| 203 | o.fieldname) |
| 204 | |
| 205 | output += ' return handle;\n' |
| 206 | output += '}\n\n' |
| 207 | output += duplicate_wrapper_tail() |
| 208 | |
| 209 | output += "\n#endif /* vl_printfun */\n" |
| 210 | |
| 211 | return output |
| 212 | |
| 213 | |
| 214 | endian_strings = { |
| 215 | 'u16': 'clib_net_to_host_u16', |
| 216 | 'u32': 'clib_net_to_host_u32', |
| 217 | 'u64': 'clib_net_to_host_u64', |
| 218 | 'i16': 'clib_net_to_host_u16', |
| 219 | 'i32': 'clib_net_to_host_u32', |
| 220 | 'i64': 'clib_net_to_host_u64', |
| 221 | } |
| 222 | |
| 223 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 224 | def endianfun(objs): |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 225 | output = '''\ |
| 226 | |
| 227 | /****** Endian swap functions *****/\n\ |
| 228 | #ifdef vl_endianfun |
| 229 | |
| 230 | #undef clib_net_to_host_uword |
| 231 | #ifdef LP64 |
| 232 | #define clib_net_to_host_uword clib_net_to_host_u64 |
| 233 | #else |
| 234 | #define clib_net_to_host_uword clib_net_to_host_u32 |
| 235 | #endif |
| 236 | |
| 237 | ''' |
| 238 | |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 239 | for t in objs: |
| 240 | if t.__class__.__name__ == 'Enum': |
| 241 | continue |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 242 | if t.manual_endian: |
| 243 | output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name |
| 244 | continue |
| 245 | output += duplicate_wrapper_head(t.name + '_t_endian') |
| 246 | output += "static inline void vl_api_%s_t_endian (vl_api_%s_t *a)" % \ |
| 247 | (t.name, t.name) |
| 248 | output += "\n{\n" |
| 249 | |
| 250 | for o in t.block: |
| 251 | if o.type != 'Field': |
| 252 | continue |
| 253 | if o.fieldtype in endian_strings: |
| 254 | output += " a->%s = %s(a->%s);\n" % \ |
| 255 | (o.fieldname, endian_strings[o.fieldtype], o.fieldname) |
| 256 | else: |
| 257 | output += " /* a->%s = a->%s (no-op) */\n" % \ |
| 258 | (o.fieldname, o.fieldname) |
| 259 | |
| 260 | output += '}\n\n' |
| 261 | output += duplicate_wrapper_tail() |
| 262 | output += "\n#endif /* vl_endianfun */\n\n" |
| 263 | |
| 264 | return output |
| 265 | |
| 266 | |
| 267 | def version_tuple(s, module): |
| 268 | output = '''\ |
| 269 | /****** Version tuple *****/ |
| 270 | |
| 271 | #ifdef vl_api_version_tuple |
| 272 | |
| 273 | ''' |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 274 | if 'version' in s['Option']: |
| 275 | v = s['Option']['version'] |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 276 | (major, minor, patch) = v.split('.') |
| 277 | output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \ |
| 278 | (module, major, minor, patch) |
| 279 | |
| 280 | output += "\n#endif /* vl_api_version_tuple */\n\n" |
| 281 | |
| 282 | return output |
| 283 | |
| 284 | |
| 285 | # |
| 286 | # Plugin entry point |
| 287 | # |
| 288 | def run(input_filename, s, file_crc): |
| 289 | basename = os.path.basename(input_filename) |
| 290 | filename, file_extension = os.path.splitext(basename) |
| 291 | output = top_boilerplate.format(datestring=datestring, |
| 292 | input_filename=basename) |
| 293 | output += msg_ids(s) |
| 294 | output += msg_names(s) |
| 295 | output += msg_name_crc_list(s, filename) |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 296 | output += typedefs(s['types'] + s['Define'], s['Alias'], filename + file_extension) |
Ole Troan | 2c2feab | 2018-04-24 00:02:37 -0400 | [diff] [blame] | 297 | output += printfun(s['types'] + s['Define']) |
| 298 | output += endianfun(s['types'] + s['Define']) |
Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 299 | output += version_tuple(s, basename) |
| 300 | output += bottom_boilerplate.format(input_filename=basename, |
| 301 | file_crc=file_crc) |
| 302 | |
| 303 | return output |