Kirill Bobyrev | cee80c0 | 2020-04-16 11:10:03 +0200 | [diff] [blame] | 1 | # This setup requires gRPC to be built from sources using CMake and installed to |
| 2 | # ${GRPC_INSTALL_PATH} via -DCMAKE_INSTALL_PREFIX=${GRPC_INSTALL_PATH}. |
| 3 | if (GRPC_INSTALL_PATH) |
| 4 | set(protobuf_MODULE_COMPATIBLE TRUE) |
| 5 | find_package(Protobuf CONFIG REQUIRED HINTS ${GRPC_INSTALL_PATH}) |
| 6 | message(STATUS "Using protobuf ${protobuf_VERSION}") |
| 7 | find_package(gRPC CONFIG REQUIRED HINTS ${GRPC_INSTALL_PATH}) |
| 8 | message(STATUS "Using gRPC ${gRPC_VERSION}") |
| 9 | |
| 10 | include_directories(${Protobuf_INCLUDE_DIRS}) |
| 11 | |
| 12 | # gRPC CMake CONFIG gives the libraries slightly odd names, make them match |
| 13 | # the conventional system-installed names. |
| 14 | set_target_properties(protobuf::libprotobuf PROPERTIES IMPORTED_GLOBAL TRUE) |
| 15 | add_library(protobuf ALIAS protobuf::libprotobuf) |
| 16 | set_target_properties(gRPC::grpc++ PROPERTIES IMPORTED_GLOBAL TRUE) |
| 17 | add_library(grpc++ ALIAS gRPC::grpc++) |
| 18 | |
| 19 | set(GRPC_CPP_PLUGIN $<TARGET_FILE:gRPC::grpc_cpp_plugin>) |
| 20 | set(PROTOC ${Protobuf_PROTOC_EXECUTABLE}) |
| 21 | else() |
| 22 | find_program(GRPC_CPP_PLUGIN grpc_cpp_plugin) |
| 23 | find_program(PROTOC protoc) |
| 24 | endif() |
| 25 | |
| 26 | # Proto headers are generated in ${CMAKE_CURRENT_BINARY_DIR}. |
| 27 | # Libraries that use these headers should adjust the include path. |
| 28 | # FIXME(kirillbobyrev): Allow optional generation of gRPC code and give callers |
| 29 | # control over it via additional parameters. |
| 30 | function(generate_grpc_protos LibraryName ProtoFile) |
| 31 | get_filename_component(ProtoSourceAbsolutePath "${CMAKE_CURRENT_SOURCE_DIR}/${ProtoFile}" ABSOLUTE) |
| 32 | get_filename_component(ProtoSourcePath ${ProtoSourceAbsolutePath} PATH) |
| 33 | |
| 34 | set(GeneratedProtoSource "${CMAKE_CURRENT_BINARY_DIR}/Index.pb.cc") |
| 35 | set(GeneratedProtoHeader "${CMAKE_CURRENT_BINARY_DIR}/Index.pb.h") |
| 36 | set(GeneratedGRPCSource "${CMAKE_CURRENT_BINARY_DIR}/Index.grpc.pb.cc") |
| 37 | set(GeneratedGRPCHeader "${CMAKE_CURRENT_BINARY_DIR}/Index.grpc.pb.h") |
| 38 | add_custom_command( |
| 39 | OUTPUT "${GeneratedProtoSource}" "${GeneratedProtoHeader}" "${GeneratedGRPCSource}" "${GeneratedGRPCHeader}" |
| 40 | COMMAND ${PROTOC} |
| 41 | ARGS --grpc_out="${CMAKE_CURRENT_BINARY_DIR}" |
| 42 | --cpp_out="${CMAKE_CURRENT_BINARY_DIR}" |
| 43 | --proto_path="${ProtoSourcePath}" |
| 44 | --plugin=protoc-gen-grpc="${GRPC_CPP_PLUGIN}" |
| 45 | "${ProtoSourceAbsolutePath}" |
| 46 | DEPENDS "${ProtoSourceAbsolutePath}") |
| 47 | |
Kirill Bobyrev | 9ff3f33 | 2020-04-28 19:15:49 +0200 | [diff] [blame] | 48 | add_clang_library(${LibraryName} ${GeneratedProtoSource} ${GeneratedGRPCSource} |
| 49 | PARTIAL_SOURCES_INTENDED |
| 50 | LINK_LIBS grpc++ protobuf) |
Kirill Bobyrev | cee80c0 | 2020-04-16 11:10:03 +0200 | [diff] [blame] | 51 | endfunction() |