jsofri
jsofri

Reputation: 245

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:

Upvotes: 0

Views: 829

Answers (1)

jsofri
jsofri

Reputation: 245

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 )

Upvotes: 0

Related Questions