diff options
Diffstat (limited to 'generator/nanopb_generator.py')
-rwxr-xr-x | generator/nanopb_generator.py | 87 |
1 files changed, 70 insertions, 17 deletions
diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py index 906a1ac..b4f1d83 100755 --- a/generator/nanopb_generator.py +++ b/generator/nanopb_generator.py @@ -3,17 +3,20 @@ from __future__ import unicode_literals '''Generate header file for nanopb from a ProtoBuf FileDescriptorSet.''' -nanopb_version = "nanopb-0.3.9.2" +nanopb_version = "nanopb-0.3.9.8" import sys import re import codecs +import copy from functools import reduce try: # Add some dummy imports to keep packaging tools happy. import google, distutils.util # bbfreeze seems to need these import pkg_resources # pyinstaller / protobuf 2.5 seem to need these + import proto.nanopb_pb2 as nanopb_pb2 # pyinstaller seems to need this + import proto.plugin_pb2 as plugin_pb2 except: # Don't care, we will error out later if it is actually important. pass @@ -31,8 +34,7 @@ except: raise try: - import proto.nanopb_pb2 as nanopb_pb2 - import proto.plugin_pb2 as plugin_pb2 + from .proto import nanopb_pb2, plugin_pb2 except TypeError: sys.stderr.write(''' **************************************************************************** @@ -50,6 +52,10 @@ except TypeError: **************************************************************************** ''' + '\n') raise +except (ValueError, SystemError, ImportError): + # Probably invoked directly instead of via installed scripts. + import proto.nanopb_pb2 as nanopb_pb2 + import proto.plugin_pb2 as plugin_pb2 except: sys.stderr.write(''' ******************************************************************** @@ -223,9 +229,12 @@ class Enum: result += ' %s;' % self.names - result += '\n#define _%s_MIN %s' % (self.names, self.values[0][0]) - result += '\n#define _%s_MAX %s' % (self.names, self.values[-1][0]) - result += '\n#define _%s_ARRAYSIZE ((%s)(%s+1))' % (self.names, self.names, self.values[-1][0]) + # sort the enum by value + sorted_values = sorted(self.values, key = lambda x: (x[1], x[0])) + + result += '\n#define _%s_MIN %s' % (self.names, sorted_values[0][0]) + result += '\n#define _%s_MAX %s' % (self.names, sorted_values[-1][0]) + result += '\n#define _%s_ARRAYSIZE ((%s)(%s+1))' % (self.names, self.names, sorted_values[-1][0]) if not self.options.long_names: # Define the long names always so that enum value references @@ -347,7 +356,7 @@ class Field: if field_options.type == nanopb_pb2.FT_STATIC and not can_be_static: raise Exception("Field '%s' is defined as static, but max_size or " "max_count is not given." % self.name) - + if field_options.fixed_count and self.max_count is None: raise Exception("Field '%s' is defined as fixed count, " "but max_count is not given." % self.name) @@ -617,7 +626,15 @@ class Field: '''Determine if this field needs 16bit or 32bit pb_field_t structure to compile properly. Returns numeric value or a C-expression for assert.''' check = [] - if self.pbtype == 'MESSAGE' and self.allocation == 'STATIC': + + need_check = False + + if self.pbtype == 'BYTES' and self.allocation == 'STATIC' and self.max_size > 251: + need_check = True + elif self.pbtype == 'MESSAGE' and self.allocation == 'STATIC': + need_check = True + + if need_check: if self.rules == 'REPEATED': check.append('pb_membersize(%s, %s[0])' % (self.struct_name, self.name)) elif self.rules == 'ONEOF': @@ -627,9 +644,6 @@ class Field: check.append('pb_membersize(%s, %s.%s)' % (self.struct_name, self.union_name, self.name)) else: check.append('pb_membersize(%s, %s)' % (self.struct_name, self.name)) - elif self.pbtype == 'BYTES' and self.allocation == 'STATIC': - if self.max_size > 251: - check.append('pb_membersize(%s, %s)' % (self.struct_name, self.name)) return FieldMaxSize([self.tag, self.max_size, self.max_count], check, @@ -996,6 +1010,15 @@ class Message: result += default + '\n' return result + def all_fields(self): + '''Iterate over all fields in this message, including nested OneOfs.''' + for f in self.fields: + if isinstance(f, OneOf): + for f2 in f.fields: + yield f2 + else: + yield f + def count_required_fields(self): '''Returns number of required fields inside this message''' count = 0 @@ -1176,11 +1199,11 @@ class ProtoFile: if message_options.skip_message: continue + message = copy.deepcopy(message) for field in message.field: if field.type in (FieldD.TYPE_MESSAGE, FieldD.TYPE_ENUM): field.type_name = mangle_field_typename(field.type_name) - self.messages.append(Message(name, message, message_options)) for enum in message.enum_type: name = create_name(names + enum.name) @@ -1206,7 +1229,7 @@ class ProtoFile: for enum in other.enums: if not enum.options.long_names: for message in self.messages: - for field in message.fields: + for field in message.all_fields(): if field.default in enum.value_longnames: idx = enum.value_longnames.index(field.default) field.default = enum.values[idx][0] @@ -1215,7 +1238,7 @@ class ProtoFile: for enum in other.enums: if not enum.has_negative(): for message in self.messages: - for field in message.fields: + for field in message.all_fields(): if field.pbtype == 'ENUM' and field.ctype == enum.names: field.pbtype = 'UENUM' @@ -1416,7 +1439,7 @@ class ProtoFile: msgs = '_'.join(str(n) for n in checks_msgnames) yield '/* If you get an error here, it means that you need to define PB_FIELD_32BIT\n' yield ' * compile-time option. You can do that in pb.h or on compiler command line.\n' - yield ' * \n' + yield ' *\n' yield ' * The reason you need to do this is that some of your messages contain tag\n' yield ' * numbers or field sizes that are larger than what can fit in 8 or 16 bit\n' yield ' * field descriptors.\n' @@ -1433,7 +1456,7 @@ class ProtoFile: msgs = '_'.join(str(n) for n in checks_msgnames) yield '/* If you get an error here, it means that you need to define PB_FIELD_16BIT\n' yield ' * compile-time option. You can do that in pb.h or on compiler command line.\n' - yield ' * \n' + yield ' *\n' yield ' * The reason you need to do this is that some of your messages contain tag\n' yield ' * numbers or field sizes that are larger than what can fit in the default\n' yield ' * 8 bit descriptors.\n' @@ -1444,7 +1467,7 @@ class ProtoFile: # Add check for sizeof(double) has_double = False for msg in self.messages: - for field in msg.fields: + for field in msg.all_fields(): if field.ctype == 'double': has_double = True @@ -1556,6 +1579,8 @@ optparser = OptionParser( usage = "Usage: nanopb_generator.py [options] file.pb ...", epilog = "Compile file.pb from file.proto by: 'protoc -ofile.pb file.proto'. " + "Output will be written to file.pb.h and file.pb.c.") +optparser.add_option("--version", dest="version", action="store_true", + help="Show version info and exit") optparser.add_option("-x", dest="exclude", metavar="FILE", action="append", default=[], help="Exclude file from generated #include list.") optparser.add_option("-e", "--extension", dest="extension", metavar="EXTENSION", default=".pb", @@ -1689,6 +1714,10 @@ def main_cli(): options, filenames = optparser.parse_args() + if options.version: + print(nanopb_version) + sys.exit(0) + if not filenames: optparser.print_help() sys.exit(1) @@ -1702,6 +1731,7 @@ def main_cli(): sys.exit(1) if options.verbose: + sys.stderr.write("Nanopb version %s\n" % nanopb_version) sys.stderr.write('Google Python protobuf library imported from %s, version %s\n' % (google.protobuf.__file__, google.protobuf.__version__)) @@ -1746,11 +1776,34 @@ def main_plugin(): import shlex args = shlex.split(params) + + if len(args) == 1 and ',' in args[0]: + # For compatibility with other protoc plugins, support options + # separated by comma. + lex = shlex.shlex(params) + lex.whitespace_split = True + lex.whitespace = ',' + args = list(lex) + + optparser.usage = "Usage: protoc --nanopb_out=[options][,more_options]:outdir file.proto" + optparser.epilog = "Output will be written to file.pb.h and file.pb.c." + + if '-h' in args or '--help' in args: + # By default optparser prints help to stdout, which doesn't work for + # protoc plugins. + optparser.print_help(sys.stderr) + sys.exit(1) + options, dummy = optparser.parse_args(args) + if options.version: + sys.stderr.write('%s\n' % (nanopb_version)) + sys.exit(0) + Globals.verbose_options = options.verbose if options.verbose: + sys.stderr.write("Nanopb version %s\n" % nanopb_version) sys.stderr.write('Google Python protobuf library imported from %s, version %s\n' % (google.protobuf.__file__, google.protobuf.__version__)) |