Add skip_message option to generator.
[apps/agl-service-can-low-level.git] / generator / nanopb_generator.py
1 #!/usr/bin/python
2
3 '''Generate header file for nanopb from a ProtoBuf FileDescriptorSet.'''
4 nanopb_version = "nanopb-0.2.9-dev"
5
6 import sys
7
8 try:
9     # Add some dummy imports to keep packaging tools happy.
10     import google, distutils.util # bbfreeze seems to need these
11     import pkg_resources # pyinstaller / protobuf 2.5 seem to need these
12 except:
13     # Don't care, we will error out later if it is actually important.
14     pass
15
16 try:
17     import google.protobuf.text_format as text_format
18     import google.protobuf.descriptor_pb2 as descriptor
19 except:
20     sys.stderr.write('''
21          *************************************************************
22          *** Could not import the Google protobuf Python libraries ***
23          *** Try installing package 'python-protobuf' or similar.  ***
24          *************************************************************
25     ''' + '\n')
26     raise
27
28 try:
29     import proto.nanopb_pb2 as nanopb_pb2
30     import proto.plugin_pb2 as plugin_pb2
31 except:
32     sys.stderr.write('''
33          ********************************************************************
34          *** Failed to import the protocol definitions for generator.     ***
35          *** You have to run 'make' in the nanopb/generator/proto folder. ***
36          ********************************************************************
37     ''' + '\n')
38     raise
39
40 # ---------------------------------------------------------------------------
41 #                     Generation of single fields
42 # ---------------------------------------------------------------------------
43
44 import time
45 import os.path
46
47 # Values are tuple (c type, pb type, encoded size)
48 FieldD = descriptor.FieldDescriptorProto
49 datatypes = {
50     FieldD.TYPE_BOOL:       ('bool',     'BOOL',        1),
51     FieldD.TYPE_DOUBLE:     ('double',   'DOUBLE',      8),
52     FieldD.TYPE_FIXED32:    ('uint32_t', 'FIXED32',     4),
53     FieldD.TYPE_FIXED64:    ('uint64_t', 'FIXED64',     8),
54     FieldD.TYPE_FLOAT:      ('float',    'FLOAT',       4),
55     FieldD.TYPE_INT32:      ('int32_t',  'INT32',      10),
56     FieldD.TYPE_INT64:      ('int64_t',  'INT64',      10),
57     FieldD.TYPE_SFIXED32:   ('int32_t',  'SFIXED32',    4),
58     FieldD.TYPE_SFIXED64:   ('int64_t',  'SFIXED64',    8),
59     FieldD.TYPE_SINT32:     ('int32_t',  'SINT32',      5),
60     FieldD.TYPE_SINT64:     ('int64_t',  'SINT64',     10),
61     FieldD.TYPE_UINT32:     ('uint32_t', 'UINT32',      5),
62     FieldD.TYPE_UINT64:     ('uint64_t', 'UINT64',     10)
63 }
64
65 class Names:
66     '''Keeps a set of nested names and formats them to C identifier.'''
67     def __init__(self, parts = ()):
68         if isinstance(parts, Names):
69             parts = parts.parts
70         self.parts = tuple(parts)
71     
72     def __str__(self):
73         return '_'.join(self.parts)
74
75     def __add__(self, other):
76         if isinstance(other, (str, unicode)):
77             return Names(self.parts + (other,))
78         elif isinstance(other, tuple):
79             return Names(self.parts + other)
80         else:
81             raise ValueError("Name parts should be of type str")
82     
83     def __eq__(self, other):
84         return isinstance(other, Names) and self.parts == other.parts
85     
86 def names_from_type_name(type_name):
87     '''Parse Names() from FieldDescriptorProto type_name'''
88     if type_name[0] != '.':
89         raise NotImplementedError("Lookup of non-absolute type names is not supported")
90     return Names(type_name[1:].split('.'))
91
92 def varint_max_size(max_value):
93     '''Returns the maximum number of bytes a varint can take when encoded.'''
94     for i in range(1, 11):
95         if (max_value >> (i * 7)) == 0:
96             return i
97     raise ValueError("Value too large for varint: " + str(max_value))
98
99 assert varint_max_size(0) == 1
100 assert varint_max_size(127) == 1
101 assert varint_max_size(128) == 2
102
103 class EncodedSize:
104     '''Class used to represent the encoded size of a field or a message.
105     Consists of a combination of symbolic sizes and integer sizes.'''
106     def __init__(self, value = 0, symbols = []):
107         if isinstance(value, (str, Names)):
108             symbols = [str(value)]
109             value = 0
110         self.value = value
111         self.symbols = symbols
112     
113     def __add__(self, other):
114         if isinstance(other, (int, long)):
115             return EncodedSize(self.value + other, self.symbols)
116         elif isinstance(other, (str, Names)):
117             return EncodedSize(self.value, self.symbols + [str(other)])
118         elif isinstance(other, EncodedSize):
119             return EncodedSize(self.value + other.value, self.symbols + other.symbols)
120         else:
121             raise ValueError("Cannot add size: " + repr(other))
122
123     def __mul__(self, other):
124         if isinstance(other, (int, long)):
125             return EncodedSize(self.value * other, [str(other) + '*' + s for s in self.symbols])
126         else:
127             raise ValueError("Cannot multiply size: " + repr(other))
128
129     def __str__(self):
130         if not self.symbols:
131             return str(self.value)
132         else:
133             return '(' + str(self.value) + ' + ' + ' + '.join(self.symbols) + ')'
134
135     def upperlimit(self):
136         if not self.symbols:
137             return self.value
138         else:
139             return 2**32 - 1
140
141 class Enum:
142     def __init__(self, names, desc, enum_options):
143         '''desc is EnumDescriptorProto'''
144         
145         self.options = enum_options
146         self.names = names + desc.name
147         
148         if enum_options.long_names:
149             self.values = [(self.names + x.name, x.number) for x in desc.value]            
150         else:
151             self.values = [(names + x.name, x.number) for x in desc.value] 
152         
153         self.value_longnames = [self.names + x.name for x in desc.value]
154     
155     def __str__(self):
156         result = 'typedef enum _%s {\n' % self.names
157         result += ',\n'.join(["    %s = %d" % x for x in self.values])
158         result += '\n} %s;' % self.names
159         return result
160
161 class Field:
162     def __init__(self, struct_name, desc, field_options):
163         '''desc is FieldDescriptorProto'''
164         self.tag = desc.number
165         self.struct_name = struct_name
166         self.name = desc.name
167         self.default = None
168         self.max_size = None
169         self.max_count = None
170         self.array_decl = ""
171         self.enc_size = None
172         self.ctype = None
173         
174         # Parse field options
175         if field_options.HasField("max_size"):
176             self.max_size = field_options.max_size
177         
178         if field_options.HasField("max_count"):
179             self.max_count = field_options.max_count
180         
181         if desc.HasField('default_value'):
182             self.default = desc.default_value
183            
184         # Check field rules, i.e. required/optional/repeated.
185         can_be_static = True
186         if desc.label == FieldD.LABEL_REQUIRED:
187             self.rules = 'REQUIRED'
188         elif desc.label == FieldD.LABEL_OPTIONAL:
189             self.rules = 'OPTIONAL'
190         elif desc.label == FieldD.LABEL_REPEATED:
191             self.rules = 'REPEATED'
192             if self.max_count is None:
193                 can_be_static = False
194             else:
195                 self.array_decl = '[%d]' % self.max_count
196         else:
197             raise NotImplementedError(desc.label)
198         
199         # Check if the field can be implemented with static allocation
200         # i.e. whether the data size is known.
201         if desc.type == FieldD.TYPE_STRING and self.max_size is None:
202             can_be_static = False
203         
204         if desc.type == FieldD.TYPE_BYTES and self.max_size is None:
205             can_be_static = False
206         
207         # Decide how the field data will be allocated
208         if field_options.type == nanopb_pb2.FT_DEFAULT:
209             if can_be_static:
210                 field_options.type = nanopb_pb2.FT_STATIC
211             else:
212                 field_options.type = nanopb_pb2.FT_CALLBACK
213         
214         if field_options.type == nanopb_pb2.FT_STATIC and not can_be_static:
215             raise Exception("Field %s is defined as static, but max_size or "
216                             "max_count is not given." % self.name)
217         
218         if field_options.type == nanopb_pb2.FT_STATIC:
219             self.allocation = 'STATIC'
220         elif field_options.type == nanopb_pb2.FT_POINTER:
221             self.allocation = 'POINTER'
222         elif field_options.type == nanopb_pb2.FT_CALLBACK:
223             self.allocation = 'CALLBACK'
224         else:
225             raise NotImplementedError(field_options.type)
226         
227         # Decide the C data type to use in the struct.
228         if datatypes.has_key(desc.type):
229             self.ctype, self.pbtype, self.enc_size = datatypes[desc.type]
230         elif desc.type == FieldD.TYPE_ENUM:
231             self.pbtype = 'ENUM'
232             self.ctype = names_from_type_name(desc.type_name)
233             if self.default is not None:
234                 self.default = self.ctype + self.default
235             self.enc_size = 5 # protoc rejects enum values > 32 bits
236         elif desc.type == FieldD.TYPE_STRING:
237             self.pbtype = 'STRING'
238             self.ctype = 'char'
239             if self.allocation == 'STATIC':
240                 self.ctype = 'char'
241                 self.array_decl += '[%d]' % self.max_size
242                 self.enc_size = varint_max_size(self.max_size) + self.max_size
243         elif desc.type == FieldD.TYPE_BYTES:
244             self.pbtype = 'BYTES'
245             if self.allocation == 'STATIC':
246                 self.ctype = self.struct_name + self.name + 't'
247                 self.enc_size = varint_max_size(self.max_size) + self.max_size
248             elif self.allocation == 'POINTER':
249                 self.ctype = 'pb_bytes_array_t'
250         elif desc.type == FieldD.TYPE_MESSAGE:
251             self.pbtype = 'MESSAGE'
252             self.ctype = self.submsgname = names_from_type_name(desc.type_name)
253             self.enc_size = None # Needs to be filled in after the message type is available
254         else:
255             raise NotImplementedError(desc.type)
256         
257     def __cmp__(self, other):
258         return cmp(self.tag, other.tag)
259     
260     def __str__(self):
261         result = ''
262         if self.allocation == 'POINTER':
263             if self.rules == 'REPEATED':
264                 result += '    size_t ' + self.name + '_count;\n'
265             
266             if self.pbtype == 'MESSAGE':
267                 # Use struct definition, so recursive submessages are possible
268                 result += '    struct _%s *%s;' % (self.ctype, self.name)
269             elif self.rules == 'REPEATED' and self.pbtype in ['STRING', 'BYTES']:
270                 # String/bytes arrays need to be defined as pointers to pointers
271                 result += '    %s **%s;' % (self.ctype, self.name)
272             else:
273                 result += '    %s *%s;' % (self.ctype, self.name)
274         elif self.allocation == 'CALLBACK':
275             result += '    pb_callback_t %s;' % self.name
276         else:
277             if self.rules == 'OPTIONAL' and self.allocation == 'STATIC':
278                 result += '    bool has_' + self.name + ';\n'
279             elif self.rules == 'REPEATED' and self.allocation == 'STATIC':
280                 result += '    size_t ' + self.name + '_count;\n'
281             result += '    %s %s%s;' % (self.ctype, self.name, self.array_decl)
282         return result
283     
284     def types(self):
285         '''Return definitions for any special types this field might need.'''
286         if self.pbtype == 'BYTES' and self.allocation == 'STATIC':
287             result = 'typedef struct {\n'
288             result += '    size_t size;\n'
289             result += '    uint8_t bytes[%d];\n' % self.max_size
290             result += '} %s;\n' % self.ctype
291         else:
292             result = None
293         return result
294     
295     def default_decl(self, declaration_only = False):
296         '''Return definition for this field's default value.'''
297         if self.default is None:
298             return None
299
300         ctype, default = self.ctype, self.default
301         array_decl = ''
302         
303         if self.pbtype == 'STRING':
304             if self.allocation != 'STATIC':
305                 return None # Not implemented
306         
307             array_decl = '[%d]' % self.max_size
308             default = str(self.default).encode('string_escape')
309             default = default.replace('"', '\\"')
310             default = '"' + default + '"'
311         elif self.pbtype == 'BYTES':
312             if self.allocation != 'STATIC':
313                 return None # Not implemented
314
315             data = self.default.decode('string_escape')
316             data = ['0x%02x' % ord(c) for c in data]
317             default = '{%d, {%s}}' % (len(data), ','.join(data))
318         elif self.pbtype in ['FIXED32', 'UINT32']:
319             default += 'u'
320         elif self.pbtype in ['FIXED64', 'UINT64']:
321             default += 'ull'
322         elif self.pbtype in ['SFIXED64', 'INT64']:
323             default += 'll'
324         
325         if declaration_only:
326             return 'extern const %s %s_default%s;' % (ctype, self.struct_name + self.name, array_decl)
327         else:
328             return 'const %s %s_default%s = %s;' % (ctype, self.struct_name + self.name, array_decl, default)
329     
330     def tags(self):
331         '''Return the #define for the tag number of this field.'''
332         identifier = '%s_%s_tag' % (self.struct_name, self.name)
333         return '#define %-40s %d\n' % (identifier, self.tag)
334     
335     def pb_field_t(self, prev_field_name):
336         '''Return the pb_field_t initializer to use in the constant array.
337         prev_field_name is the name of the previous field or None.
338         '''
339         result  = '    PB_FIELD2(%3d, ' % self.tag
340         result += '%-8s, ' % self.pbtype
341         result += '%s, ' % self.rules
342         result += '%-8s, ' % self.allocation
343         result += '%s, ' % ("FIRST" if not prev_field_name else "OTHER")
344         result += '%s, ' % self.struct_name
345         result += '%s, ' % self.name
346         result += '%s, ' % (prev_field_name or self.name)
347         
348         if self.pbtype == 'MESSAGE':
349             result += '&%s_fields)' % self.submsgname
350         elif self.default is None:
351             result += '0)'
352         elif self.pbtype in ['BYTES', 'STRING'] and self.allocation != 'STATIC':
353             result += '0)' # Arbitrary size default values not implemented
354         elif self.rules == 'OPTEXT':
355             result += '0)' # Default value for extensions is not implemented
356         else:
357             result += '&%s_default)' % (self.struct_name + self.name)
358         
359         return result
360     
361     def largest_field_value(self):
362         '''Determine if this field needs 16bit or 32bit pb_field_t structure to compile properly.
363         Returns numeric value or a C-expression for assert.'''
364         if self.pbtype == 'MESSAGE':
365             if self.rules == 'REPEATED' and self.allocation == 'STATIC':
366                 return 'pb_membersize(%s, %s[0])' % (self.struct_name, self.name)
367             else:
368                 return 'pb_membersize(%s, %s)' % (self.struct_name, self.name)
369
370         return max(self.tag, self.max_size, self.max_count)        
371
372     def encoded_size(self, allmsgs):
373         '''Return the maximum size that this field can take when encoded,
374         including the field tag. If the size cannot be determined, returns
375         None.'''
376         
377         if self.allocation != 'STATIC':
378             return None
379         
380         if self.pbtype == 'MESSAGE':
381             for msg in allmsgs:
382                 if msg.name == self.submsgname:
383                     encsize = msg.encoded_size(allmsgs)
384                     if encsize is None:
385                         return None # Submessage size is indeterminate
386                         
387                     # Include submessage length prefix
388                     encsize += varint_max_size(encsize.upperlimit())
389                     break
390             else:
391                 # Submessage cannot be found, this currently occurs when
392                 # the submessage type is defined in a different file.
393                 # Instead of direct numeric value, reference the size that
394                 # has been #defined in the other file.
395                 encsize = EncodedSize(self.submsgname + 'size')
396
397                 # We will have to make a conservative assumption on the length
398                 # prefix size, though.
399                 encsize += 5
400
401         elif self.enc_size is None:
402             raise RuntimeError("Could not determine encoded size for %s.%s"
403                                % (self.struct_name, self.name))
404         else:
405             encsize = EncodedSize(self.enc_size)
406         
407         encsize += varint_max_size(self.tag << 3) # Tag + wire type
408
409         if self.rules == 'REPEATED':
410             # Decoders must be always able to handle unpacked arrays.
411             # Therefore we have to reserve space for it, even though
412             # we emit packed arrays ourselves.
413             encsize *= self.max_count
414         
415         return encsize
416
417
418 class ExtensionRange(Field):
419     def __init__(self, struct_name, range_start, field_options):
420         '''Implements a special pb_extension_t* field in an extensible message
421         structure. The range_start signifies the index at which the extensions
422         start. Not necessarily all tags above this are extensions, it is merely
423         a speed optimization.
424         '''
425         self.tag = range_start
426         self.struct_name = struct_name
427         self.name = 'extensions'
428         self.pbtype = 'EXTENSION'
429         self.rules = 'OPTIONAL'
430         self.allocation = 'CALLBACK'
431         self.ctype = 'pb_extension_t'
432         self.array_decl = ''
433         self.default = None
434         self.max_size = 0
435         self.max_count = 0
436         
437     def __str__(self):
438         return '    pb_extension_t *extensions;'
439     
440     def types(self):
441         return None
442     
443     def tags(self):
444         return ''
445         
446     def encoded_size(self, allmsgs):
447         # We exclude extensions from the count, because they cannot be known
448         # until runtime. Other option would be to return None here, but this
449         # way the value remains useful if extensions are not used.
450         return EncodedSize(0)
451
452 class ExtensionField(Field):
453     def __init__(self, struct_name, desc, field_options):
454         self.fullname = struct_name + desc.name
455         self.extendee_name = names_from_type_name(desc.extendee)
456         Field.__init__(self, self.fullname + 'struct', desc, field_options)
457         
458         if self.rules != 'OPTIONAL':
459             self.skip = True
460         else:
461             self.skip = False
462             self.rules = 'OPTEXT'
463
464     def tags(self):
465         '''Return the #define for the tag number of this field.'''
466         identifier = '%s_tag' % self.fullname
467         return '#define %-40s %d\n' % (identifier, self.tag)
468
469     def extension_decl(self):
470         '''Declaration of the extension type in the .pb.h file'''
471         if self.skip:
472             msg = '/* Extension field %s was skipped because only "optional"\n' % self.fullname
473             msg +='   type of extension fields is currently supported. */\n'
474             return msg
475         
476         return 'extern const pb_extension_type_t %s;\n' % self.fullname
477
478     def extension_def(self):
479         '''Definition of the extension type in the .pb.c file'''
480
481         if self.skip:
482             return ''
483
484         result  = 'typedef struct {\n'
485         result += str(self)
486         result += '\n} %s;\n\n' % self.struct_name
487         result += ('static const pb_field_t %s_field = \n  %s;\n\n' %
488                     (self.fullname, self.pb_field_t(None)))
489         result += 'const pb_extension_type_t %s = {\n' % self.fullname
490         result += '    NULL,\n'
491         result += '    NULL,\n'
492         result += '    &%s_field\n' % self.fullname
493         result += '};\n'
494         return result
495
496
497 # ---------------------------------------------------------------------------
498 #                   Generation of messages (structures)
499 # ---------------------------------------------------------------------------
500
501
502 class Message:
503     def __init__(self, names, desc, message_options):
504         self.name = names
505         self.fields = []
506         
507         for f in desc.field:
508             field_options = get_nanopb_suboptions(f, message_options, self.name + f.name)
509             if field_options.type != nanopb_pb2.FT_IGNORE:
510                 self.fields.append(Field(self.name, f, field_options))
511         
512         if len(desc.extension_range) > 0:
513             field_options = get_nanopb_suboptions(desc, message_options, self.name + 'extensions')
514             range_start = min([r.start for r in desc.extension_range])
515             if field_options.type != nanopb_pb2.FT_IGNORE:
516                 self.fields.append(ExtensionRange(self.name, range_start, field_options))
517         
518         self.packed = message_options.packed_struct
519         self.ordered_fields = self.fields[:]
520         self.ordered_fields.sort()
521
522     def get_dependencies(self):
523         '''Get list of type names that this structure refers to.'''
524         return [str(field.ctype) for field in self.fields]
525     
526     def __str__(self):
527         result = 'typedef struct _%s {\n' % self.name
528
529         if not self.ordered_fields:
530             # Empty structs are not allowed in C standard.
531             # Therefore add a dummy field if an empty message occurs.
532             result += '    uint8_t dummy_field;'
533
534         result += '\n'.join([str(f) for f in self.ordered_fields])
535         result += '\n}'
536         
537         if self.packed:
538             result += ' pb_packed'
539         
540         result += ' %s;' % self.name
541         
542         if self.packed:
543             result = 'PB_PACKED_STRUCT_START\n' + result
544             result += '\nPB_PACKED_STRUCT_END'
545         
546         return result
547     
548     def types(self):
549         result = ""
550         for field in self.fields:
551             types = field.types()
552             if types is not None:
553                 result += types + '\n'
554         return result
555     
556     def default_decl(self, declaration_only = False):
557         result = ""
558         for field in self.fields:
559             default = field.default_decl(declaration_only)
560             if default is not None:
561                 result += default + '\n'
562         return result
563
564     def fields_declaration(self):
565         result = 'extern const pb_field_t %s_fields[%d];' % (self.name, len(self.fields) + 1)
566         return result
567
568     def fields_definition(self):
569         result = 'const pb_field_t %s_fields[%d] = {\n' % (self.name, len(self.fields) + 1)
570         
571         prev = None
572         for field in self.ordered_fields:
573             result += field.pb_field_t(prev)
574             result += ',\n'
575             prev = field.name
576         
577         result += '    PB_LAST_FIELD\n};'
578         return result
579
580     def encoded_size(self, allmsgs):
581         '''Return the maximum size that this message can take when encoded.
582         If the size cannot be determined, returns None.
583         '''
584         size = EncodedSize(0)
585         for field in self.fields:
586             fsize = field.encoded_size(allmsgs)
587             if fsize is None:
588                 return None
589             size += fsize
590         
591         return size
592
593
594 # ---------------------------------------------------------------------------
595 #                    Processing of entire .proto files
596 # ---------------------------------------------------------------------------
597
598
599 def iterate_messages(desc, names = Names()):
600     '''Recursively find all messages. For each, yield name, DescriptorProto.'''
601     if hasattr(desc, 'message_type'):
602         submsgs = desc.message_type
603     else:
604         submsgs = desc.nested_type
605     
606     for submsg in submsgs:
607         sub_names = names + submsg.name
608         yield sub_names, submsg
609         
610         for x in iterate_messages(submsg, sub_names):
611             yield x
612
613 def iterate_extensions(desc, names = Names()):
614     '''Recursively find all extensions.
615     For each, yield name, FieldDescriptorProto.
616     '''
617     for extension in desc.extension:
618         yield names, extension
619
620     for subname, subdesc in iterate_messages(desc, names):
621         for extension in subdesc.extension:
622             yield subname, extension
623
624 def parse_file(fdesc, file_options):
625     '''Takes a FileDescriptorProto and returns tuple (enums, messages, extensions).'''
626     
627     enums = []
628     messages = []
629     extensions = []
630     
631     if fdesc.package:
632         base_name = Names(fdesc.package.split('.'))
633     else:
634         base_name = Names()
635     
636     for enum in fdesc.enum_type:
637         enum_options = get_nanopb_suboptions(enum, file_options, base_name + enum.name)
638         enums.append(Enum(base_name, enum, enum_options))
639     
640     for names, message in iterate_messages(fdesc, base_name):
641         message_options = get_nanopb_suboptions(message, file_options, names)
642         
643         if message_options.skip_message:
644             continue
645         
646         messages.append(Message(names, message, message_options))
647         for enum in message.enum_type:
648             enum_options = get_nanopb_suboptions(enum, message_options, names + enum.name)
649             enums.append(Enum(names, enum, enum_options))
650     
651     for names, extension in iterate_extensions(fdesc, base_name):
652         field_options = get_nanopb_suboptions(extension, file_options, names + extension.name)
653         if field_options.type != nanopb_pb2.FT_IGNORE:
654             extensions.append(ExtensionField(names, extension, field_options))
655     
656     # Fix field default values where enum short names are used.
657     for enum in enums:
658         if not enum.options.long_names:
659             for message in messages:
660                 for field in message.fields:
661                     if field.default in enum.value_longnames:
662                         idx = enum.value_longnames.index(field.default)
663                         field.default = enum.values[idx][0]
664     
665     return enums, messages, extensions
666
667 def toposort2(data):
668     '''Topological sort.
669     From http://code.activestate.com/recipes/577413-topological-sort/
670     This function is under the MIT license.
671     '''
672     for k, v in data.items():
673         v.discard(k) # Ignore self dependencies
674     extra_items_in_deps = reduce(set.union, data.values(), set()) - set(data.keys())
675     data.update(dict([(item, set()) for item in extra_items_in_deps]))
676     while True:
677         ordered = set(item for item,dep in data.items() if not dep)
678         if not ordered:
679             break
680         for item in sorted(ordered):
681             yield item
682         data = dict([(item, (dep - ordered)) for item,dep in data.items()
683                 if item not in ordered])
684     assert not data, "A cyclic dependency exists amongst %r" % data
685
686 def sort_dependencies(messages):
687     '''Sort a list of Messages based on dependencies.'''
688     dependencies = {}
689     message_by_name = {}
690     for message in messages:
691         dependencies[str(message.name)] = set(message.get_dependencies())
692         message_by_name[str(message.name)] = message
693     
694     for msgname in toposort2(dependencies):
695         if msgname in message_by_name:
696             yield message_by_name[msgname]
697
698 def make_identifier(headername):
699     '''Make #ifndef identifier that contains uppercase A-Z and digits 0-9'''
700     result = ""
701     for c in headername.upper():
702         if c.isalnum():
703             result += c
704         else:
705             result += '_'
706     return result
707
708 def generate_header(dependencies, headername, enums, messages, extensions, options):
709     '''Generate content for a header file.
710     Generates strings, which should be concatenated and stored to file.
711     '''
712     
713     yield '/* Automatically generated nanopb header */\n'
714     if options.notimestamp:
715         yield '/* Generated by %s */\n\n' % (nanopb_version)
716     else:
717         yield '/* Generated by %s at %s. */\n\n' % (nanopb_version, time.asctime())
718     
719     symbol = make_identifier(headername)
720     yield '#ifndef _PB_%s_\n' % symbol
721     yield '#define _PB_%s_\n' % symbol
722     try:
723         yield options.libformat % ('pb.h')
724     except TypeError:
725         # no %s specified - use whatever was passed in as options.libformat
726         yield options.libformat
727     yield '\n'
728     
729     for dependency in dependencies:
730         noext = os.path.splitext(dependency)[0]
731         yield options.genformat % (noext + options.extension + '.h')
732         yield '\n'
733
734     yield '#ifdef __cplusplus\n'
735     yield 'extern "C" {\n'
736     yield '#endif\n\n'
737     
738     yield '/* Enum definitions */\n'
739     for enum in enums:
740         yield str(enum) + '\n\n'
741     
742     yield '/* Struct definitions */\n'
743     for msg in sort_dependencies(messages):
744         yield msg.types()
745         yield str(msg) + '\n\n'
746     
747     if extensions:
748         yield '/* Extensions */\n'
749         for extension in extensions:
750             yield extension.extension_decl()
751         yield '\n'
752         
753     yield '/* Default values for struct fields */\n'
754     for msg in messages:
755         yield msg.default_decl(True)
756     yield '\n'
757     
758     yield '/* Field tags (for use in manual encoding/decoding) */\n'
759     for msg in sort_dependencies(messages):
760         for field in msg.fields:
761             yield field.tags()
762     for extension in extensions:
763         yield extension.tags()
764     yield '\n'
765     
766     yield '/* Struct field encoding specification for nanopb */\n'
767     for msg in messages:
768         yield msg.fields_declaration() + '\n'
769     yield '\n'
770     
771     yield '/* Maximum encoded size of messages (where known) */\n'
772     for msg in messages:
773         msize = msg.encoded_size(messages)
774         if msize is not None:
775             identifier = '%s_size' % msg.name
776             yield '#define %-40s %s\n' % (identifier, msize)
777     yield '\n'
778     
779     yield '#ifdef __cplusplus\n'
780     yield '} /* extern "C" */\n'
781     yield '#endif\n'
782     
783     # End of header
784     yield '\n#endif\n'
785
786 def generate_source(headername, enums, messages, extensions, options):
787     '''Generate content for a source file.'''
788     
789     yield '/* Automatically generated nanopb constant definitions */\n'
790     if options.notimestamp:
791         yield '/* Generated by %s */\n\n' % (nanopb_version)
792     else:
793         yield '/* Generated by %s at %s. */\n\n' % (nanopb_version, time.asctime())
794     yield options.genformat % (headername)
795     yield '\n'
796     
797     for msg in messages:
798         yield msg.default_decl(False)
799     
800     yield '\n\n'
801     
802     for msg in messages:
803         yield msg.fields_definition() + '\n\n'
804     
805     for ext in extensions:
806         yield ext.extension_def() + '\n'
807         
808     # Add checks for numeric limits
809     if messages:
810         count_required_fields = lambda m: len([f for f in msg.fields if f.rules == 'REQUIRED'])
811         largest_msg = max(messages, key = count_required_fields)
812         largest_count = count_required_fields(largest_msg)
813         if largest_count > 64:
814             yield '\n/* Check that missing required fields will be properly detected */\n'
815             yield '#if PB_MAX_REQUIRED_FIELDS < %d\n' % largest_count
816             yield '#error Properly detecting missing required fields in %s requires \\\n' % largest_msg.name
817             yield '       setting PB_MAX_REQUIRED_FIELDS to %d or more.\n' % largest_count
818             yield '#endif\n'
819     
820     worst = 0
821     worst_field = ''
822     checks = []
823     checks_msgnames = []
824     for msg in messages:
825         checks_msgnames.append(msg.name)
826         for field in msg.fields:
827             status = field.largest_field_value()
828             if isinstance(status, (str, unicode)):
829                 checks.append(status)
830             elif status > worst:
831                 worst = status
832                 worst_field = str(field.struct_name) + '.' + str(field.name)
833
834     if worst > 255 or checks:
835         yield '\n/* Check that field information fits in pb_field_t */\n'
836         
837         if worst > 65535 or checks:
838             yield '#if !defined(PB_FIELD_32BIT)\n'
839             if worst > 65535:
840                 yield '#error Field descriptor for %s is too large. Define PB_FIELD_32BIT to fix this.\n' % worst_field
841             else:
842                 assertion = ' && '.join(str(c) + ' < 65536' for c in checks)
843                 msgs = '_'.join(str(n) for n in checks_msgnames)
844                 yield '/* If you get an error here, it means that you need to define PB_FIELD_32BIT\n'
845                 yield ' * compile-time option. You can do that in pb.h or on compiler command line.\n'
846                 yield ' * \n'
847                 yield ' * The reason you need to do this is that some of your messages contain tag\n'
848                 yield ' * numbers or field sizes that are larger than what can fit in 8 or 16 bit\n'
849                 yield ' * field descriptors.\n'
850                 yield ' */\n'
851                 yield 'STATIC_ASSERT((%s), YOU_MUST_DEFINE_PB_FIELD_32BIT_FOR_MESSAGES_%s)\n'%(assertion,msgs)
852             yield '#endif\n\n'
853         
854         if worst < 65536:
855             yield '#if !defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)\n'
856             if worst > 255:
857                 yield '#error Field descriptor for %s is too large. Define PB_FIELD_16BIT to fix this.\n' % worst_field
858             else:
859                 assertion = ' && '.join(str(c) + ' < 256' for c in checks)
860                 msgs = '_'.join(str(n) for n in checks_msgnames)
861                 yield '/* If you get an error here, it means that you need to define PB_FIELD_16BIT\n'
862                 yield ' * compile-time option. You can do that in pb.h or on compiler command line.\n'
863                 yield ' * \n'
864                 yield ' * The reason you need to do this is that some of your messages contain tag\n'
865                 yield ' * numbers or field sizes that are larger than what can fit in the default\n'
866                 yield ' * 8 bit descriptors.\n'
867                 yield ' */\n'
868                 yield 'STATIC_ASSERT((%s), YOU_MUST_DEFINE_PB_FIELD_16BIT_FOR_MESSAGES_%s)\n'%(assertion,msgs)
869             yield '#endif\n\n'
870     
871     # Add check for sizeof(double)
872     has_double = False
873     for msg in messages:
874         for field in msg.fields:
875             if field.ctype == 'double':
876                 has_double = True
877     
878     if has_double:
879         yield '\n'
880         yield '/* On some platforms (such as AVR), double is really float.\n'
881         yield ' * These are not directly supported by nanopb, but see example_avr_double.\n'
882         yield ' * To get rid of this error, remove any double fields from your .proto.\n'
883         yield ' */\n'
884         yield 'STATIC_ASSERT(sizeof(double) == 8, DOUBLE_MUST_BE_8_BYTES)\n'
885     
886     yield '\n'
887
888 # ---------------------------------------------------------------------------
889 #                    Options parsing for the .proto files
890 # ---------------------------------------------------------------------------
891
892 from fnmatch import fnmatch
893
894 def read_options_file(infile):
895     '''Parse a separate options file to list:
896         [(namemask, options), ...]
897     '''
898     results = []
899     for line in infile:
900         line = line.strip()
901         if not line or line.startswith('//') or line.startswith('#'):
902             continue
903         
904         parts = line.split(None, 1)
905         opts = nanopb_pb2.NanoPBOptions()
906         text_format.Merge(parts[1], opts)
907         results.append((parts[0], opts))
908
909     return results
910
911 class Globals:
912     '''Ugly global variables, should find a good way to pass these.'''
913     verbose_options = False
914     separate_options = []
915     matched_namemasks = set()
916
917 def get_nanopb_suboptions(subdesc, options, name):
918     '''Get copy of options, and merge information from subdesc.'''
919     new_options = nanopb_pb2.NanoPBOptions()
920     new_options.CopyFrom(options)
921     
922     # Handle options defined in a separate file
923     dotname = '.'.join(name.parts)
924     for namemask, options in Globals.separate_options:
925         if fnmatch(dotname, namemask):
926             Globals.matched_namemasks.add(namemask)
927             new_options.MergeFrom(options)
928     
929     # Handle options defined in .proto
930     if isinstance(subdesc.options, descriptor.FieldOptions):
931         ext_type = nanopb_pb2.nanopb
932     elif isinstance(subdesc.options, descriptor.FileOptions):
933         ext_type = nanopb_pb2.nanopb_fileopt
934     elif isinstance(subdesc.options, descriptor.MessageOptions):
935         ext_type = nanopb_pb2.nanopb_msgopt
936     elif isinstance(subdesc.options, descriptor.EnumOptions):
937         ext_type = nanopb_pb2.nanopb_enumopt
938     else:
939         raise Exception("Unknown options type")
940     
941     if subdesc.options.HasExtension(ext_type):
942         ext = subdesc.options.Extensions[ext_type]
943         new_options.MergeFrom(ext)
944     
945     if Globals.verbose_options:
946         sys.stderr.write("Options for " + dotname + ": ")
947         sys.stderr.write(text_format.MessageToString(new_options) + "\n")
948     
949     return new_options
950
951
952 # ---------------------------------------------------------------------------
953 #                         Command line interface
954 # ---------------------------------------------------------------------------
955
956 import sys
957 import os.path    
958 from optparse import OptionParser
959
960 optparser = OptionParser(
961     usage = "Usage: nanopb_generator.py [options] file.pb ...",
962     epilog = "Compile file.pb from file.proto by: 'protoc -ofile.pb file.proto'. " +
963              "Output will be written to file.pb.h and file.pb.c.")
964 optparser.add_option("-x", dest="exclude", metavar="FILE", action="append", default=[],
965     help="Exclude file from generated #include list.")
966 optparser.add_option("-e", "--extension", dest="extension", metavar="EXTENSION", default=".pb",
967     help="Set extension to use instead of '.pb' for generated files. [default: %default]")
968 optparser.add_option("-f", "--options-file", dest="options_file", metavar="FILE", default="%s.options",
969     help="Set name of a separate generator options file.")
970 optparser.add_option("-Q", "--generated-include-format", dest="genformat",
971     metavar="FORMAT", default='#include "%s"\n',
972     help="Set format string to use for including other .pb.h files. [default: %default]")
973 optparser.add_option("-L", "--library-include-format", dest="libformat",
974     metavar="FORMAT", default='#include <%s>\n',
975     help="Set format string to use for including the nanopb pb.h header. [default: %default]")
976 optparser.add_option("-T", "--no-timestamp", dest="notimestamp", action="store_true", default=False,
977     help="Don't add timestamp to .pb.h and .pb.c preambles")
978 optparser.add_option("-q", "--quiet", dest="quiet", action="store_true", default=False,
979     help="Don't print anything except errors.")
980 optparser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False,
981     help="Print more information.")
982 optparser.add_option("-s", dest="settings", metavar="OPTION:VALUE", action="append", default=[],
983     help="Set generator option (max_size, max_count etc.).")
984
985 def process_file(filename, fdesc, options):
986     '''Process a single file.
987     filename: The full path to the .proto or .pb source file, as string.
988     fdesc: The loaded FileDescriptorSet, or None to read from the input file.
989     options: Command line options as they come from OptionsParser.
990     
991     Returns a dict:
992         {'headername': Name of header file,
993          'headerdata': Data for the .h header file,
994          'sourcename': Name of the source code file,
995          'sourcedata': Data for the .c source code file
996         }
997     '''
998     toplevel_options = nanopb_pb2.NanoPBOptions()
999     for s in options.settings:
1000         text_format.Merge(s, toplevel_options)
1001     
1002     if not fdesc:
1003         data = open(filename, 'rb').read()
1004         fdesc = descriptor.FileDescriptorSet.FromString(data).file[0]
1005     
1006     # Check if there is a separate .options file
1007     had_abspath = False
1008     try:
1009         optfilename = options.options_file % os.path.splitext(filename)[0]
1010     except TypeError:
1011         # No %s specified, use the filename as-is
1012         optfilename = options.options_file
1013         had_abspath = True
1014
1015     if os.path.isfile(optfilename):
1016         if options.verbose:
1017             sys.stderr.write('Reading options from ' + optfilename + '\n')
1018
1019         Globals.separate_options = read_options_file(open(optfilename, "rU"))
1020     else:
1021         # If we are given a full filename and it does not exist, give an error.
1022         # However, don't give error when we automatically look for .options file
1023         # with the same name as .proto.
1024         if options.verbose or had_abspath:
1025             sys.stderr.write('Options file not found: ' + optfilename)
1026
1027         Globals.separate_options = []
1028
1029     Globals.matched_namemasks = set()
1030     
1031     # Parse the file
1032     file_options = get_nanopb_suboptions(fdesc, toplevel_options, Names([filename]))
1033     enums, messages, extensions = parse_file(fdesc, file_options)
1034
1035     # Decide the file names
1036     noext = os.path.splitext(filename)[0]
1037     headername = noext + options.extension + '.h'
1038     sourcename = noext + options.extension + '.c'
1039     headerbasename = os.path.basename(headername)
1040     
1041     # List of .proto files that should not be included in the C header file
1042     # even if they are mentioned in the source .proto.
1043     excludes = ['nanopb.proto', 'google/protobuf/descriptor.proto'] + options.exclude
1044     dependencies = [d for d in fdesc.dependency if d not in excludes]
1045     
1046     headerdata = ''.join(generate_header(dependencies, headerbasename, enums,
1047                                          messages, extensions, options))
1048
1049     sourcedata = ''.join(generate_source(headerbasename, enums,
1050                                          messages, extensions, options))
1051
1052     # Check if there were any lines in .options that did not match a member
1053     unmatched = [n for n,o in Globals.separate_options if n not in Globals.matched_namemasks]
1054     if unmatched and not options.quiet:
1055         sys.stderr.write("Following patterns in " + optfilename + " did not match any fields: "
1056                          + ', '.join(unmatched) + "\n")
1057         if not Globals.verbose_options:
1058             sys.stderr.write("Use  protoc --nanopb-out=-v:.   to see a list of the field names.\n")
1059
1060     return {'headername': headername, 'headerdata': headerdata,
1061             'sourcename': sourcename, 'sourcedata': sourcedata}
1062     
1063 def main_cli():
1064     '''Main function when invoked directly from the command line.'''
1065     
1066     options, filenames = optparser.parse_args()
1067     
1068     if not filenames:
1069         optparser.print_help()
1070         sys.exit(1)
1071     
1072     if options.quiet:
1073         options.verbose = False
1074
1075     Globals.verbose_options = options.verbose
1076     
1077     for filename in filenames:
1078         results = process_file(filename, None, options)
1079         
1080         if not options.quiet:
1081             sys.stderr.write("Writing to " + results['headername'] + " and "
1082                              + results['sourcename'] + "\n")
1083     
1084         open(results['headername'], 'w').write(results['headerdata'])
1085         open(results['sourcename'], 'w').write(results['sourcedata'])        
1086
1087 def main_plugin():
1088     '''Main function when invoked as a protoc plugin.'''
1089
1090     import sys
1091     if sys.platform == "win32":
1092         import os, msvcrt
1093         # Set stdin and stdout to binary mode
1094         msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
1095         msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
1096     
1097     data = sys.stdin.read()
1098     request = plugin_pb2.CodeGeneratorRequest.FromString(data)
1099     
1100     import shlex
1101     args = shlex.split(request.parameter)
1102     options, dummy = optparser.parse_args(args)
1103     
1104     Globals.verbose_options = options.verbose
1105     
1106     response = plugin_pb2.CodeGeneratorResponse()
1107     
1108     for filename in request.file_to_generate:
1109         for fdesc in request.proto_file:
1110             if fdesc.name == filename:
1111                 results = process_file(filename, fdesc, options)
1112                 
1113                 f = response.file.add()
1114                 f.name = results['headername']
1115                 f.content = results['headerdata']
1116
1117                 f = response.file.add()
1118                 f.name = results['sourcename']
1119                 f.content = results['sourcedata']    
1120     
1121     sys.stdout.write(response.SerializeToString())
1122
1123 if __name__ == '__main__':
1124     # Check if we are running as a plugin under protoc
1125     if 'protoc-gen-' in sys.argv[0] or '--protoc-plugin' in sys.argv:
1126         main_plugin()
1127     else:
1128         main_cli()
1129