Protocol Buffer Code Generation Skill
Generate C++ and Python code from Protocol Buffer (.proto) service and message definitions.
Prerequisites
- •
ORDER_PARSER_PROCESSOR_ROOTenvironment variable must be set - •
protoccompiler must be installed - •For C++:
grpc_cpp_pluginmust be available - •For Python:
grpc_tools.protocmust be installed
Instructions
- •Verify
ORDER_PARSER_PROCESSOR_ROOTis set - •Determine which language to generate (C++, Python, or both)
- •Generate gRPC service files first, then message files
- •Report success or any errors encountered
C++ Code Generation
Generate gRPC Services (C++)
bash
protoc -I$ORDER_PARSER_PROCESSOR_ROOT/proto \ --cpp_out=$ORDER_PARSER_PROCESSOR_ROOT/generated/cpp/messages/ \ --grpc_out=$ORDER_PARSER_PROCESSOR_ROOT/generated/cpp/ \ --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` \ $(find $ORDER_PARSER_PROCESSOR_ROOT/proto/services/ -iname "*.proto")
Note: The --cpp_out is in messages/ directory while --grpc_out is in root cpp/ - this is intentional and works correctly.
Generate Protobuf Messages (C++)
bash
protoc -I$ORDER_PARSER_PROCESSOR_ROOT/proto/messages \ --cpp_out=$ORDER_PARSER_PROCESSOR_ROOT/generated/cpp/messages/ \ $(find $ORDER_PARSER_PROCESSOR_ROOT/proto/messages/ -iname "*.proto")
Python Code Generation
Generate gRPC Services (Python)
bash
python3 -m grpc_tools.protoc \ --proto_path=$ORDER_PARSER_PROCESSOR_ROOT/proto \ --python_out=$ORDER_PARSER_PROCESSOR_ROOT/generated/python/ \ --pyi_out=$ORDER_PARSER_PROCESSOR_ROOT/generated/python/ \ --grpc_python_out=$ORDER_PARSER_PROCESSOR_ROOT/generated/python/ \ $(find $ORDER_PARSER_PROCESSOR_ROOT/proto/services/ -iname "*.proto")
Generate Protobuf Messages (Python)
bash
python3 -m grpc_tools.protoc \ --proto_path=$ORDER_PARSER_PROCESSOR_ROOT/proto \ --python_out=$ORDER_PARSER_PROCESSOR_ROOT/generated/python/ \ $(find $ORDER_PARSER_PROCESSOR_ROOT/proto/messages/ -iname "*.proto")
Generated File Locations
C++ Generated Files
- •Services:
generated/cpp/services/*.pb.ccand*.pb.h - •Messages:
generated/cpp/messages/*.pb.ccand*.pb.h - •gRPC stubs:
generated/cpp/services/*.grpc.pb.ccand*.grpc.pb.h
Python Generated Files
- •Services:
generated/python/*_pb2.pyand*_pb2_grpc.py - •Messages:
generated/python/*_pb2.py - •Type stubs:
generated/python/*_pb2.pyi
Common Commands
Generate All (C++ and Python)
Run all four commands in sequence:
- •C++ gRPC services
- •C++ protobuf messages
- •Python gRPC services
- •Python protobuf messages
Generate Only C++
Run only the two C++ commands
Generate Only Python
Run only the two Python commands
Proto File Organization
- •Services:
proto/services/*.proto- gRPC service definitions - •Messages:
proto/messages/*.proto- Message structure definitions
Common Issues
- •ORDER_PARSER_PROCESSOR_ROOT not set: Export it in your shell profile
- •protoc not found: Install Protocol Buffers compiler
- •grpc_cpp_plugin not found: Install gRPC C++ plugin
- •grpc_tools not found: Install Python package:
pip install grpcio-tools - •Import errors in proto files: Check that proto paths are correct and imports use relative paths
Important Notes
- •Order matters: Generate services before messages (services may depend on messages)
- •C++ quirk: The
--cpp_outfor services goes tomessages/but--grpc_outgoes to rootcpp/- this is correct - •Regenerate after proto changes: Always regenerate when modifying
.protofiles - •CMake integration:
- •CMake does NOT handle gRPC proto file generation automatically
- •CMake does handle ANTLR
.g4file generation automatically - •For proto files, use this skill to manually generate code before building
Workflow
- •Modify
.protofiles inproto/services/orproto/messages/ - •Run this proto-gen skill to generate C++/Python code
- •Run the build skill to compile the project with updated generated code
Success Criteria
- •All proto files compile without errors
- •Generated files appear in
generated/cpp/and/orgenerated/python/ - •No import errors or missing dependencies
- •Generated code compiles successfully with the project