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)?
jsofri
  • 227
  • 1
  • 10
  • 1
    "how to invoke a similar command in cmake" - You could use e.g. [add_custom_command](https://cmake.org/cmake/help/latest/command/add_custom_command.html). You could look into the implementation of [FindProtobuf.cmake](https://github.com/Kitware/CMake/blob/master/Modules/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. – Tsyvarev May 01 '22 at 11:33

1 Answers1

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 )
jsofri
  • 227
  • 1
  • 10