1

In the following project structure(from my previous question):

root/
├─ CMakeLists.txt
├─ protocol/
│  ├─ msg.proto
│  ├─ myrpc.proto
│  ├─ CMakeLists.txt
├─ app/
│  ├─ main.cpp
│  ├─ CMakeLists.txt

I could generate the protobuf files and add them as dependency into the app target. Now I am trying to generate grpc files inside the same library.

Something is not right with the grpc generation function, which is based on this answer and blog post;as the following error message is printed:

-- Configuring done
-- Generating done
-- Build files have been written to: /media/davids91/Work/rafko/src/main/cxx/build
[  1%] Running grpc protocol buffer compiler on /usr/local/bin/grpc_cpp_plugin
/usr/local/bin/grpc_cpp_plugin: File does not reside within any path specified using --proto_path (or -I).  You must specify a --proto_path which encompasses this file.  Note that the proto_path must be an exact prefix of the .proto file names -- protoc is too dumb to figure out when two paths (e.g. absolute and relative) are equivalent (it's harder than you think).
make[2]: * [protocol/CMakeFiles/protocol.dir/build.make:138: /media/usr/local/bin/grpc_cpp_plugin.grpc.pb.h] Error 1
make[1]: * [CMakeFiles/Makefile2:276: protocol/CMakeFiles/protocol.dir/all] Error 2
make: * [Makefile:103: all] Error 2

I can see that the install folder looks correct, as that is where the grpc library was installed from source.

root CMakeLists.txt:

cmake_minimum_required(VERSION 3.18.4) 
project(
  root
  VERSION 0.1
  LANGUAGES CXX
)

add_subdirectory(protocol)
add_subdirectory(app) 

protocol CMakeLists.txt:

add_library(protocol)
target_include_directories(protocol
  PUBLIC
  .
  ${CMAKE_CURRENT_BINARY_DIR}
  ${Protobuf_INCLUDE_DIRS}
)
find_package(Protobuf REQUIRED)
find_package(gRPC CONFIG REQUIRED)
target_link_libraries(protocol ${Protobuf_LIBRARIES})
get_target_property(grpc_cpp_plugin_location gRPC::grpc_cpp_plugin LOCATION)

protobuf_generate( TARGET protocol LANGUAGE CPP PROTOS msg.proto )
protobuf_generate( 
    TARGET
        protocol 
    LANGUAGE
        grpc 
    PROTOS 
        myrpc.proto 
    PLUGIN
        "protoc-gen-grpc=${grpc_cpp_plugin_location}"
)

app CMakeLists.txt:

add_library(app)
target_link_libraries(app PUBLIC protocol)
target_include_directories(app PUBLIC .)
target_sources(app
  PRIVATE
  main.cpp
)

What might be missing from this solution to generate out the grpc files based on the plugins?

Dávid Tóth
  • 2,788
  • 1
  • 21
  • 46

1 Answers1

0

After some experimenting and closely looking at the example here I figured it out!

I updated protocol CMakeLists.txt: changed

find_package(Protobuf REQUIRED)

to

find_package(Protobuf CONFIG REQUIRED)

I figure it tells CMake that the proto files are being procesed at configuration time, but any deeper explanation is welcome!

Dávid Tóth
  • 2,788
  • 1
  • 21
  • 46