#!/usr/bin/python
'''Generate header file for nanopb from a ProtoBuf FileDescriptorSet.'''
-nanopb_version = "nanopb-0.3.3-dev"
+nanopb_version = "nanopb-0.3.4-dev"
import sys
+import re
try:
# Add some dummy imports to keep packaging tools happy.
self.values = [(names + x.name, x.number) for x in desc.value]
self.value_longnames = [self.names + x.name for x in desc.value]
+ self.packed = enum_options.packed_enum
def __str__(self):
result = 'typedef enum _%s {\n' % self.names
result += ',\n'.join([" %s = %d" % x for x in self.values])
- result += '\n} %s;' % self.names
+ result += '\n}'
+
+ if self.packed:
+ result += ' pb_packed'
+
+ result += ' %s;' % self.names
+
+ if not self.options.long_names:
+ # Define the long names always so that enum value references
+ # from other files work properly.
+ for i, x in enumerate(self.values):
+ result += '\n#define %s %s' % (self.value_longnames[i], x[0])
+
return result
class Field:
result += '_'
return result
-def generate_header(noext, dependencies, headername, enums, messages, extensions, options):
+def generate_header(dependencies, headername, enums, messages, extensions, options):
'''Generate content for a header file.
Generates strings, which should be concatenated and stored to file.
'''
yield '#define %-40s %s\n' % (identifier, msize)
yield '\n'
- yield '/* helper macros for message type ids if set with */\n'
- yield '/* option (nanopb_msgopt).msgid = <id>; */\n\n'
-
+ yield '/* Message IDs (where set with "msgid" option) */\n'
+
yield '#ifdef PB_MSGID\n'
for msg in messages:
if hasattr(msg,'msgid'):
yield '#define PB_MSG_%d %s\n' % (msg.msgid, msg.name)
yield '\n'
- yield '#define %s_MESSAGES \\\n' % (noext.upper())
+ symbol = make_identifier(headername.split('.')[0])
+ yield '#define %s_MESSAGES \\\n' % symbol
for msg in messages:
m = "-1"
yield '\tPB_MSG(%d,%s,%s) \\\n' % (msg.msgid, m, msg.name)
yield '\n'
+ for msg in messages:
+ if hasattr(msg,'msgid'):
+ yield '#define %s_msgid %d\n' % (msg.name, msg.msgid)
+ yield '\n'
+
yield '#endif\n\n'
[(namemask, options), ...]
'''
results = []
- for i, line in enumerate(infile):
+ data = infile.read()
+ data = re.sub('/\*.*?\*/', '', data, flags = re.MULTILINE)
+ data = re.sub('//.*?$', '', data, flags = re.MULTILINE)
+ data = re.sub('#.*?$', '', data, flags = re.MULTILINE)
+ for i, line in enumerate(data.split('\n')):
line = line.strip()
- if not line or line.startswith('//') or line.startswith('#'):
+ if not line:
continue
parts = line.split(None, 1)
excludes = ['nanopb.proto', 'google/protobuf/descriptor.proto'] + options.exclude
dependencies = [d for d in fdesc.dependency if d not in excludes]
- headerdata = ''.join(generate_header(noext,dependencies, headerbasename, enums,
+ headerdata = ''.join(generate_header(dependencies, headerbasename, enums,
messages, extensions, options))
sourcedata = ''.join(generate_source(headerbasename, enums,
data = sys.stdin.read()
request = plugin_pb2.CodeGeneratorRequest.FromString(data)
+ try:
+ # Versions of Python prior to 2.7.3 do not support unicode
+ # input to shlex.split(). Try to convert to str if possible.
+ params = str(request.parameter)
+ except UnicodeEncodeError:
+ params = request.parameter
+
import shlex
- args = shlex.split(request.parameter)
+ args = shlex.split(params)
options, dummy = optparser.parse_args(args)
Globals.verbose_options = options.verbose