'How to run protoc for grpc python files using cmake?
I have a file my.proto and I want to create the corresponding my_pb2.py and my_pb2_grpc.py files using cmake. (Unfortunately, I cannot use other build system)
Normally, I use protoc from grpcio-tools python module to do so:
python3 -m grpc_tools.protoc -I. --grpc_python_out=. --python_out=. my.proto
cmake has some support for python protobuf, but implementing it is not clear for my use case.
My questions are:
- how to invoke a similar command in cmake
- how to put the generated file in a specific destination
- what are the required dependencies before running cmake (grpcio-tools/protobuf)?
Solution 1:[1]
Following @Tsyvarev comment and further read of cmake doc I got the following CMakeLists.txt content (sits in my.proto directory)
# files generated by protoc
set ( PROTO_PY ${CMAKE_CURRENT_LIST_DIR}/my_pb2.py ${CMAKE_CURRENT_LIST_DIR}/my_pb2_grpc.py )
# compile dpe.proto
add_custom_command (
OUTPUT ${PROTO_PY}
COMMAND python3 -m grpc_tools.protoc -I${CMAKE_CURRENT_LIST_DIR} --grpc_python_out=${CMAKE_CURRENT_LIST_DIR} --python_out=${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}/my.proto
)
# invoke custom command
add_custom_target ( proto_py ALL
DEPENDS ${PROTO_PY} )
install( PROGRAMS ${PROTO_PY} COMPONENT mycomponent DESTINATION mypath )
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jsofri |
