0

I have a working example of building a grpc library in C++ using CMake. However I need to hardcode the absolute path to the flag --plugin=protoc-gen-grpc="/home/myusername/dev/lab/MiniGL/vcpkg/installed/x64-linux/tools/grpc/grpc_cpp_plugin"

What I really would like to do is to find something like the environment variable ${PROTOBUF_PROTOC_EXECUTABLE} below for the grpc_cpp_plugin that is required but I just can't find it. i.e. ${GPRC_CPP_PLUGIN_EXECUTABLE}

Does anyone know if I have missed something simple here?

This is my entire listing of the CMakeLists.txt

project(hello_contract_library)

set(CMAKE_VERBOSE_MAKEFILE ON)

find_package(protobuf CONFIG REQUIRED)
#target_link_libraries(main PRIVATE protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite)

set(GENERATED_DIR "${PROJECT_SOURCE_DIR}/generated")
file(MAKE_DIRECTORY "${GENERATED_DIR}")

set(hello_src "${GENERATED_DIR}/hello.pb.cc")
set(hello_hdr "${GENERATED_DIR}/hello.pb.h")
set(hello_grpc_src  "${GENERATED_DIR}/hello.grpc.pb.cc")
set(hello_grpc_hdr  "${GENERATED_DIR}/hello.grpc.pb.h")

# Added to include generated header files
include_directories("${GENERATED_DIR}")

add_custom_command(
      OUTPUT "${hello_src}" "${hello_hdr}" "${hello_grpc_src}" "${hello_grpc_hdr}"
      COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
      ARGS --grpc_out "${PROJECT_SOURCE_DIR}/generated"
        --cpp_out "${PROJECT_SOURCE_DIR}/generated"
        -I "/home/myusername/dev/lab/MiniGL/src/backend/public/contracts"
        --plugin=protoc-gen-grpc="/home/myusername/dev/lab/MiniGL/vcpkg/installed/x64-linux/tools/grpc/grpc_cpp_plugin"
        "hello.proto"
      DEPENDS "hello.proto")

add_library(${PROJECT_NAME}
  ${hello_src}
  ${hello_hdr}
  ${hello_grpc_src}
  ${hello_grpc_hdr})
Decaf Sux
  • 305
  • 1
  • 11

1 Answers1

1

The only way to get this for now is by creating your own .cmake file which looks for these executables and define this variables. Fortunately Google has an example here. You can copy the common.cmake to a cmake directory that you will include to the project.

This will give you ${_GRPC_CPP_PLUGIN_EXECUTABLE} for your --plugin and ${_GRPC_GRPCPP} to put in your target_link_libraries.

Example

In root directory

# common project setup

include(cmake/common.cmake) # comes from Google examples
add_subdirectory(proto)

In proto directory

# Proto files
set(my_protos
    test.proto
)

# Generated sources
set(my_protos_srcs
    ${CMAKE_BINARY_DIR}/proto/test.pb.cc
)
set(my_protos_hdrs
    ${CMAKE_BINARY_DIR}/proto/test.pb.h
)

add_custom_command(
    OUTPUT ${my_protos_srcs} ${my_protos_hdrs}
    COMMAND ${_PROTOBUF_PROTOC}
    ARGS --cpp_out ${CMAKE_BINARY_DIR}/proto
        --grpc_out ${CMAKE_BINARY_DIR}/proto
        -I ${CMAKE_SOURCE_DIR}/proto
        --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
        ${my_protos} 
    DEPENDS ${my_protos}
)

add_library(protos
    ${my_protos_srcs}
    ${my_protos_hdrs}
)
target_link_libraries(protos
    ${_PROTOBUF_LIBPROTOBUF}
    ${_GRPC_GRPCPP}
)
Clément Jean
  • 1,735
  • 1
  • 14
  • 32