如何使用cmake运行GRPC Python文件的原始protoc?

发布于 2025-01-25 08:17:29 字数 665 浏览 2 评论 0原文

我有一个文件my.proto,我想创建相应的my_pb2.pymy_pb2_grpc.py使用cmake。 (不幸的是,我无法使用其他构建系统)

通常,我使用grpcio-tools python模块的Protoc来做到这一点:

python3 -m grpc_tools.protoc -I. --grpc_python_out=. --python_out=. my.proto

cmake具有对Python Protobuf的一些支持,但对于我的用例,实现尚不清楚。

我的问题是:

  • 如何在CMake中调用类似命令,
  • 如何将生成的文件放在特定目的地中,
  • 在运行Cmake(grpcio-tools/protobuf)之前,所需的依赖项是什么?

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断肠人 2025-02-01 08:17:29

遵循@tsyvarev评论并进一步阅读CMAKE文档我得到以下cmakelists.txt content(位于my.proto目录中)

# 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 )

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 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文