Cross compiling gRPC using cmake

假如想象 提交于 2020-12-04 08:24:57

问题


I'm trying to cross-compile a gRPC using cmake.

I actually managed to do it. Unfortunately my method involves tinkering inside CMakeLists.txt.

Problem was that, when I was trying to compile gRPC it was using protobuffer he just compiled. It cannot run ARM compiled executables on x86 machine.

I managed to compile it by specifying path to protoc and grpc_cpp_plugin manually in gRPCs main CMakeLists.txt. It is dirty and since I would like to include gRPC as submodule I need clean way to do it.

Has anyone managed to cross-compile gRPC using cmake?


回答1:


This way should work:

  1. Cross-compile Protobuf for target(ARM) and install it.

    Make sure that cross-compiled Protobuf can be searched in the toolchain(ARM) you have. E.g. it is installed into system-default prefix under sysroot.

  2. On host, install Protobuf of the same version. It needn't to be searchable (that is, user-local installation is sufficient).

  3. Cross-compile gRPC with following parameters (set in CMake GUI or as -D option for cmake executable):

    • gRPC_PROTOBUF_PROVIDER: package
    • gRPC_PROTOBUF_PACKAGE_TYPE: MODULE
    • Protobuf_PROTOC_EXECUTABLE: <path to your host protoc executable>

Explanations

Setting parameter gRPC_PROTOBUF_PROVIDER to "package" tells gRPC to not build its own Protobuf, but use already installed variant. This variant is searched by find_package(Protobuf).

Setting parameter gRPC_PROTOBUF_PACKAGE_TYPE to "MODULE" tells gRPC to not use "Config" file, provided by Protobuf installation, for detect Protobuf things (libraries and executable). Config file contains hardcoded paths which cannot be adjusted outside. Instead, FindProtobuf.cmake script is used for find Protobuf.

Setting parameter Protobuf_PROTOC_EXECUTABLE tells FindProtobuf.cmake script to not search Protobuf executable, but takes it from the parameter.



来源:https://stackoverflow.com/questions/52202453/cross-compiling-grpc-using-cmake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!