nanopb_cpp_generator.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #!/usr/bin/env python
  2. # Copyright 2018 Google
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Generates and massages protocol buffer outputs.
  16. """
  17. from __future__ import print_function
  18. import sys
  19. import io
  20. import nanopb_generator as nanopb
  21. import os
  22. import os.path
  23. import shlex
  24. from google.protobuf.descriptor_pb2 import FieldDescriptorProto
  25. if sys.platform == 'win32':
  26. import msvcrt # pylint: disable=g-import-not-at-top
  27. # The plugin_pb2 package loads descriptors on import, but doesn't defend
  28. # against multiple imports. Reuse the plugin package as loaded by the
  29. # nanopb_generator.
  30. plugin_pb2 = nanopb.plugin_pb2
  31. nanopb_pb2 = nanopb.nanopb_pb2
  32. def main():
  33. # Parse request
  34. if sys.platform == 'win32':
  35. # Set stdin and stdout to binary mode
  36. msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
  37. msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
  38. data = io.open(sys.stdin.fileno(), 'rb').read()
  39. request = plugin_pb2.CodeGeneratorRequest.FromString(data)
  40. # Preprocess inputs, changing types and nanopb defaults
  41. use_anonymous_oneof(request)
  42. use_bytes_for_strings(request)
  43. use_malloc(request)
  44. # Generate code
  45. options = nanopb_parse_options(request)
  46. parsed_files = nanopb_parse_files(request, options)
  47. results = nanopb_generate(request, options, parsed_files)
  48. response = nanopb_write(results)
  49. # Write to stdout
  50. io.open(sys.stdout.fileno(), 'wb').write(response.SerializeToString())
  51. def use_malloc(request):
  52. """Mark all variable length items as requiring malloc.
  53. By default nanopb renders string, bytes, and repeated fields (dynamic fields)
  54. as having the C type pb_callback_t. Unfortunately this type is incompatible
  55. with nanopb's union support.
  56. The function performs the equivalent of adding the following annotation to
  57. each dynamic field in all the protos.
  58. string name = 1 [(nanopb).type = FT_POINTER];
  59. Args:
  60. request: A CodeGeneratorRequest from protoc. The descriptors are modified
  61. in place.
  62. """
  63. dynamic_types = [
  64. FieldDescriptorProto.TYPE_STRING,
  65. FieldDescriptorProto.TYPE_BYTES,
  66. ]
  67. for _, message_type in iterate_messages(request):
  68. for field in message_type.field:
  69. dynamic_type = field.type in dynamic_types
  70. repeated = field.label == FieldDescriptorProto.LABEL_REPEATED
  71. if dynamic_type or repeated:
  72. ext = field.options.Extensions[nanopb_pb2.nanopb]
  73. ext.type = nanopb_pb2.FT_POINTER
  74. def use_anonymous_oneof(request):
  75. """Use anonymous unions for oneofs if they're the only one in a message.
  76. Equivalent to setting this option on messages where it applies:
  77. option (nanopb).anonymous_oneof = true;
  78. Args:
  79. request: A CodeGeneratorRequest from protoc. The descriptors are modified
  80. in place.
  81. """
  82. for _, message_type in iterate_messages(request):
  83. if len(message_type.oneof_decl) == 1:
  84. ext = message_type.options.Extensions[nanopb_pb2.nanopb_msgopt]
  85. ext.anonymous_oneof = True
  86. def use_bytes_for_strings(request):
  87. """Always use the bytes type instead of string.
  88. By default, nanopb renders proto strings as having the C type char* and does
  89. not include a separate size field, getting the length of the string via
  90. strlen(). Unfortunately this prevents using strings with embedded nulls,
  91. which is something the wire format supports.
  92. Fortunately, string and bytes proto fields are identical on the wire and
  93. nanopb's bytes representation does have an explicit length, so this function
  94. changes the types of all string fields to bytes. The generated code will now
  95. contain pb_bytes_array_t.
  96. There's no nanopb or proto option to control this behavior. The equivalent
  97. would be to hand edit all the .proto files :-(.
  98. Args:
  99. request: A CodeGeneratorRequest from protoc. The descriptors are modified
  100. in place.
  101. """
  102. for names, message_type in iterate_messages(request):
  103. for field in message_type.field:
  104. if field.type == FieldDescriptorProto.TYPE_STRING:
  105. field.type = FieldDescriptorProto.TYPE_BYTES
  106. def iterate_messages(request):
  107. """Iterates over all messages in all files in the request.
  108. Args:
  109. request: A CodeGeneratorRequest passed by protoc.
  110. Yields:
  111. names: a nanopb.Names object giving a qualified name for the message
  112. message_type: a DescriptorProto for the message.
  113. """
  114. for fdesc in request.proto_file:
  115. for names, message_type in nanopb.iterate_messages(fdesc):
  116. yield names, message_type
  117. def nanopb_parse_options(request):
  118. """Parses nanopb_generator command-line options from the given request.
  119. Args:
  120. request: A CodeGeneratorRequest passed by protoc.
  121. Returns:
  122. Nanopb's options object, obtained via optparser.
  123. """
  124. # Parse options the same as nanopb_generator.main_plugin() does.
  125. args = shlex.split(request.parameter)
  126. options, _ = nanopb.optparser.parse_args(args)
  127. # Force certain options
  128. options.extension = '.nanopb'
  129. # Replicate options setup from nanopb_generator.main_plugin.
  130. nanopb.Globals.verbose_options = options.verbose
  131. # Google's protoc does not currently indicate the full path of proto files.
  132. # Instead always add the main file path to the search dirs, that works for
  133. # the common case.
  134. options.options_path.append(os.path.dirname(request.file_to_generate[0]))
  135. return options
  136. def nanopb_parse_files(request, options):
  137. """Parses the files in the given request into nanopb ProtoFile objects.
  138. Args:
  139. request: A CodeGeneratorRequest, as passed by protoc.
  140. options: The command-line options from nanopb_parse_options.
  141. Returns:
  142. A dictionary of filename to nanopb.ProtoFile objects, each one representing
  143. the parsed form of a FileDescriptor in the request.
  144. """
  145. # Process any include files first, in order to have them
  146. # available as dependencies
  147. parsed_files = {}
  148. for fdesc in request.proto_file:
  149. parsed_files[fdesc.name] = nanopb.parse_file(fdesc.name, fdesc, options)
  150. return parsed_files
  151. def nanopb_generate(request, options, parsed_files):
  152. """Generates C sources from the given parsed files.
  153. Args:
  154. request: A CodeGeneratorRequest, as passed by protoc.
  155. options: The command-line options from nanopb_parse_options.
  156. parsed_files: A dictionary of filename to nanopb.ProtoFile, as returned by
  157. nanopb_parse_files().
  158. Returns:
  159. A list of nanopb output dictionaries, each one representing the code
  160. generation result for each file to generate. The output dictionaries have
  161. the following form:
  162. {
  163. 'headername': Name of header file, ending in .h,
  164. 'headerdata': Contents of the header file,
  165. 'sourcename': Name of the source code file, ending in .c,
  166. 'sourcedata': Contents of the source code file
  167. }
  168. """
  169. output = []
  170. for filename in request.file_to_generate:
  171. for fdesc in request.proto_file:
  172. if fdesc.name == filename:
  173. results = nanopb.process_file(filename, fdesc, options, parsed_files)
  174. output.append(results)
  175. return output
  176. def nanopb_write(results):
  177. """Translates nanopb output dictionaries to a CodeGeneratorResponse.
  178. Args:
  179. results: A list of generated source dictionaries, as returned by
  180. nanopb_generate().
  181. Returns:
  182. A CodeGeneratorResponse describing the result of the code generation
  183. process to protoc.
  184. """
  185. response = plugin_pb2.CodeGeneratorResponse()
  186. for result in results:
  187. f = response.file.add()
  188. f.name = result['headername']
  189. f.content = result['headerdata']
  190. f = response.file.add()
  191. f.name = result['sourcename']
  192. f.content = result['sourcedata']
  193. return response
  194. if __name__ == '__main__':
  195. main()