0

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)?
1
  • 1
    "how to invoke a similar command in cmake" - You could use e.g. add_custom_command. You could look into the implementation of FindProtobuf.cmake. "how to put the generated file in a specific destination" - If the command outputs files not in the desired location, you could copy the files using add_custom_command. "what are the required dependencies before running cmake" - The requirements are the same as ones for the command line you show. Commented May 1, 2022 at 11:33

1 Answer 1

0

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 )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.