This project provides a Python script to automatically generate a C++ gRPC project skeleton from protobuf files. It uses the Jinja2 templating engine, making it easy to customize the output code.
- Template-driven: Uses Jinja2 templates to generate code, allowing for easy customization of the output.
- Generates a Complete Project: Creates a full project structure with:
- A generic gRPC server wrapper.
- A main server implementation (
server.cpp). - A client implementation for each defined service.
xmake.luafor easy building and dependency management.
- Automatic Proto File Handling: Copies the specified
.protofiles into the generated project. - Code Formatting: Automatically formats the generated C++ code using
clang-formatif available.
- Python 3.x
- The Python packages listed in
requirements.txt. - A C++ compiler and
xmakeinstalled to build the generated project.
- Clone this repository.
- Install the required Python packages:
python3 -m venv env source env/bin/activate pip install -r requirements.txt
This example shows how to generate a complete gRPC project from the example.proto file.
-
Generate the project:
This script performs two main steps:
a.
proto2yaml.py: Parses the.protofile to create an intermediateproto.yamlfile that describes the gRPC services and messages.python3 proto2yaml.py example/example.proto \ --namespace peak \ --server_class_name GrpcServer \ --client_class_name GrpcClient \ --include_grpc_files example.grpc.pb.h example.pb.h \ --out ./proto.yamlb.
cpp-grpc-auto-gen.py: Uses theproto.yamlfile and Jinja2 templates to generate the complete C++ project source code, including a server, client, andxmake.luabuild file.python3 cpp-grpc-auto-gen.py \ --proto proto.yaml \ --template ./template \ --out_server_file example/include/grpc_server.hpp \ --out_client_file example/include/grpc_client.hpp \ --format=clang-format -
Build and run the generated project:
cd example_project xmake build xmake install -o . ./bin/server
You can run the client in another terminal:
./bin/example_client
The generated project in the example_project/ directory will have the following structure:
example_project/
├── include/
│ ├── grpc_client.hpp
│ └── grpc_server.hpp
├── proto/
│ ├── example.proto
│ └── health.proto
├── src/
│ ├── example_client.cpp
│ └── server.cpp
└── xmake.lua
include/: Contains the generated gRPC server and client header files.proto/: Contains the.protofiles used to generate the code.src/: Contains the main server and client implementation files.xmake.lua: The build file for the project.
This project is licensed under the MIT License - see the LICENSE file for details.