I'm trying to generate grpc cpp files but the generator plugin fails when I call it in cmake
find_package(protoc CONFIG REQUIRED)
find_package(gRPC CONFIG REQUIRED)
get_target_property(grpc_cpp_plugin_location gRPC::grpc_cpp_plugin LOCATION) #get plugin location
get_target_property(protoc_location protobuf::protoc LOCATION) #for sanity check
message("protoc is at ${protoc_location}") #for sanity check
message("grpc plugin is at ${grpc_cpp_plugin_location}") #for sanity check
add_library(${PRO_NAME} ${PRO_SRCS} test.proto)
protobuf_generate(TARGET ${PRO_NAME} LANGUAGE cpp)
protobuf_generate(TARGET ${PRO_NAME} LANGUAGE grpc GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc PLUGIN "protoc-gen-grpc=${grpc_cpp_plugin_location}")
Produces an error like
protoc is at /home/user/.conan/data/protoc_installer/...
grpc plugin is at /home/user/.conan/data/grpc/...
-- Configuring done
-- Generating done
-- Build files have been written to:
[ 16%] Running grpc protocol buffer compiler on test.proto
protoc-gen-grpc: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--grpc_out: protoc-gen-grpc: Plugin failed with status code 1
but if I use it manually in a shell script it works even though i copy the file path from the cmake output
PROTOC=${PATH FROM CMAKE}
GRPCC=${PATH FROM CMAKE}
$PROTOC -I src/ --grpc_out=. --plugin=protoc-gen-grpc=$GRPCC test.proto
basing method off of https://www.falkoaxmann.de/dev/2020/11/08/grpc-plugin-cmake-support.html
I initially thought my issue was a library path error, I'm using a remote build server with conan import management, but I verified that the plugins rpath includes the relative locations of the libraries it needs.
The issue is probably that cmake uses an outdated protobuf config, or rather the FindProtobuf.cmake
that comes with cmake rather than ProtobufConfig.cmake
that comes with the protobuf package.
To fix this, compile gRPC yourself and use the -DgRPC_PROTOBUF_PACKAGE_TYPE=CONFIG
flag when you run cmake for gRPC.
The problem comes from gRPC having its own find_package(Protobuf)
call in its gRPCConfig.cmake which, depending on the gRPC_PROTOBUF_PACKAGE_TYPE
, might run in MODULE mode. (and in module mode, it will find the legacy FindProtobuf.cmake)
Have a look at the recently added github workflow for an example on how to build dependencies and the example: https://github.com/faaxm/exmpl-cmake-grpc/blob/master/.github/workflows/build.yml