1 # Copyright 2020 The Pigweed Authors
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 # use this file except in compliance with the License. You may obtain a copy of
7 # https://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations under
14 """pw_rpc protoc plugin entrypoint to generate code for RPC services."""
19 import google.protobuf.compiler.plugin_pb2 as plugin_pb2
21 import pw_rpc.codegen_nanopb as codegen_nanopb
22 import pw_rpc.codegen_raw as codegen_raw
25 class Codegen(enum.Enum):
30 def process_proto_request(codegen: Codegen,
31 req: plugin_pb2.CodeGeneratorRequest,
32 res: plugin_pb2.CodeGeneratorResponse) -> None:
33 """Handles a protoc CodeGeneratorRequest message.
35 Generates code for the files in the request and writes the output to the
36 specified CodeGeneratorResponse message.
39 req: A CodeGeneratorRequest for a proto compilation.
40 res: A CodeGeneratorResponse to populate with the plugin's output.
42 for proto_file in req.proto_file:
43 if codegen is Codegen.RAW:
44 output_files = codegen_raw.process_proto_file(proto_file)
45 elif codegen is Codegen.NANOPB:
46 output_files = codegen_nanopb.process_proto_file(proto_file)
48 raise NotImplementedError(f'Unknown codegen type {codegen}')
50 for output_file in output_files:
52 fd.name = output_file.name()
53 fd.content = output_file.content()
56 def main(codegen: Codegen) -> int:
57 """Protobuf compiler plugin entrypoint.
59 Reads a CodeGeneratorRequest proto from stdin and writes a
60 CodeGeneratorResponse to stdout.
62 data = sys.stdin.buffer.read()
63 request = plugin_pb2.CodeGeneratorRequest.FromString(data)
64 response = plugin_pb2.CodeGeneratorResponse()
65 process_proto_request(codegen, request, response)
67 # Declare that this plugin supports optional fields in proto3. No proto
68 # message code is generated, so optional in proto3 is supported trivially.
69 response.supported_features |= ( # type: ignore[attr-defined]
70 response.FEATURE_PROTO3_OPTIONAL) # type: ignore[attr-defined]
72 sys.stdout.buffer.write(response.SerializeToString())