Publishing nanopb-0.2.1
[apps/agl-service-can-low-level.git] / generator / nanopb_generator.py
index 0e89874..c772c8b 100644 (file)
@@ -1,5 +1,5 @@
 '''Generate header file for nanopb from a ProtoBuf FileDescriptorSet.'''
-nanopb_version = "nanopb-0.2.1-dev"
+nanopb_version = "nanopb-0.2.1"
 
 try:
     import google.protobuf.descriptor_pb2 as descriptor
@@ -186,7 +186,7 @@ class Field:
         return cmp(self.tag, other.tag)
     
     def __str__(self):
-        if self.rules == 'OPTIONAL':
+        if self.rules == 'OPTIONAL' and self.allocation == 'STATIC':
             result = '    bool has_' + self.name + ';\n'
         elif self.rules == 'REPEATED' and self.allocation == 'STATIC':
             result = '    size_t ' + self.name + '_count;\n'
@@ -312,6 +312,11 @@ class Message:
             result += ' pb_packed'
         
         result += ' %s;' % self.name
+        
+        if self.packed:
+            result = 'PB_PACKED_STRUCT_START\n' + result
+            result += '\nPB_PACKED_STRUCT_END'
+        
         return result
     
     def types(self):
@@ -455,7 +460,11 @@ def generate_header(dependencies, headername, enums, messages, options):
     symbol = make_identifier(headername)
     yield '#ifndef _PB_%s_\n' % symbol
     yield '#define _PB_%s_\n' % symbol
-    yield options.libformat % ('pb.h')
+    try:
+        yield options.libformat % ('pb.h')
+    except TypeError:
+        # no %s specified - use whatever was passed in as options.libformat
+        yield options.libformat
     yield '\n'
     
     for dependency in dependencies:
@@ -688,7 +697,11 @@ def process(filenames, options):
         fdesc = descriptor.FileDescriptorSet.FromString(data)
         
         # Check if any separate options are specified
-        optfilename = options.options_file % os.path.splitext(filename)[0]
+        try:
+            optfilename = options.options_file % os.path.splitext(filename)[0]
+        except TypeError:
+            # No %s specified, use the filename as-is
+            optfilename = options.options_file
         
         if options.verbose:
             print 'Reading options from ' + optfilename