1 # FIXME(kirillbobyrev): Check if gRPC and Protobuf headers can be included at
3 find_package(Threads REQUIRED)
5 # This setup requires gRPC to be built from sources using CMake and installed
6 # to ${GRPC_INSTALL_PATH} via -DCMAKE_INSTALL_PREFIX=${GRPC_INSTALL_PATH}.
7 # Libraries will be linked according to gRPC build policy which generates
8 # static libraries when BUILD_SHARED_LIBS is Off and dynamic libraries when
9 # it's On (NOTE: This is a variable passed to gRPC CMake build invocation,
10 # LLVM's BUILD_SHARED_LIBS has no effect).
11 set(protobuf_MODULE_COMPATIBLE TRUE)
12 find_package(Protobuf CONFIG REQUIRED HINTS ${GRPC_INSTALL_PATH})
13 message(STATUS "Using protobuf ${protobuf_VERSION}")
14 find_package(gRPC CONFIG REQUIRED HINTS ${GRPC_INSTALL_PATH})
15 message(STATUS "Using gRPC ${gRPC_VERSION}")
17 include_directories(${Protobuf_INCLUDE_DIRS})
19 # gRPC CMake CONFIG gives the libraries slightly odd names, make them match
20 # the conventional system-installed names.
21 set_target_properties(protobuf::libprotobuf PROPERTIES IMPORTED_GLOBAL TRUE)
22 add_library(protobuf ALIAS protobuf::libprotobuf)
23 set_target_properties(gRPC::grpc++ PROPERTIES IMPORTED_GLOBAL TRUE)
24 add_library(grpc++ ALIAS gRPC::grpc++)
26 set(GRPC_CPP_PLUGIN $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
27 set(PROTOC ${Protobuf_PROTOC_EXECUTABLE})
29 # This setup requires system-installed gRPC and Protobuf.
30 # We always link dynamically in this mode. While the static libraries are
31 # usually installed, the CMake files telling us *which* static libraries to
33 if (NOT BUILD_SHARED_LIBS)
34 message(NOTICE "gRPC and Protobuf will be linked dynamically. If you want static linking, build gRPC from sources with -DBUILD_SHARED_LIBS=Off.")
36 find_program(GRPC_CPP_PLUGIN grpc_cpp_plugin)
37 find_program(PROTOC protoc)
38 if (NOT GRPC_CPP_PLUGIN OR NOT PROTOC)
39 message(FATAL_ERROR "gRPC C++ Plugin and Protoc must be on $PATH for Clangd remote index build.")
41 # On macOS the libraries are typically installed via Homebrew and are not on
44 find_program(HOMEBREW brew)
45 # If Homebrew is not found, the user might have installed libraries
46 # manually. Fall back to the system path.
48 execute_process(COMMAND ${HOMEBREW} --prefix grpc
49 OUTPUT_VARIABLE GRPC_HOMEBREW_PATH
50 RESULT_VARIABLE GRPC_HOMEBREW_RETURN_CODE
51 OUTPUT_STRIP_TRAILING_WHITESPACE)
52 execute_process(COMMAND ${HOMEBREW} --prefix protobuf
53 OUTPUT_VARIABLE PROTOBUF_HOMEBREW_PATH
54 RESULT_VARIABLE PROTOBUF_HOMEBREW_RETURN_CODE
55 OUTPUT_STRIP_TRAILING_WHITESPACE)
56 # If either library is not installed via Homebrew, fall back to the
58 if (GRPC_HOMEBREW_RETURN_CODE EQUAL "0")
59 include_directories(${GRPC_HOMEBREW_PATH}/include)
60 find_library(GRPC_LIBRARY
62 PATHS ${GRPC_HOMEBREW_PATH}/lib
65 add_library(grpc++ UNKNOWN IMPORTED GLOBAL)
66 set_target_properties(grpc++ PROPERTIES
67 IMPORTED_LOCATION ${GRPC_LIBRARY})
69 if (PROTOBUF_HOMEBREW_RETURN_CODE EQUAL "0")
70 include_directories(${PROTOBUF_HOMEBREW_PATH}/include)
71 find_library(PROTOBUF_LIBRARY
73 PATHS ${PROTOBUF_HOMEBREW_PATH}/lib
76 add_library(protobuf UNKNOWN IMPORTED GLOBAL)
77 set_target_properties(protobuf PROPERTIES
78 IMPORTED_LOCATION ${PROTOBUF_LIBRARY})
84 # Proto headers are generated in ${CMAKE_CURRENT_BINARY_DIR}.
85 # Libraries that use these headers should adjust the include path.
86 # FIXME(kirillbobyrev): Allow optional generation of gRPC code and give callers
87 # control over it via additional parameters.
88 function(generate_grpc_protos LibraryName ProtoFile)
89 get_filename_component(ProtoSourceAbsolutePath "${CMAKE_CURRENT_SOURCE_DIR}/${ProtoFile}" ABSOLUTE)
90 get_filename_component(ProtoSourcePath ${ProtoSourceAbsolutePath} PATH)
92 set(GeneratedProtoSource "${CMAKE_CURRENT_BINARY_DIR}/Index.pb.cc")
93 set(GeneratedProtoHeader "${CMAKE_CURRENT_BINARY_DIR}/Index.pb.h")
94 set(GeneratedGRPCSource "${CMAKE_CURRENT_BINARY_DIR}/Index.grpc.pb.cc")
95 set(GeneratedGRPCHeader "${CMAKE_CURRENT_BINARY_DIR}/Index.grpc.pb.h")
97 OUTPUT "${GeneratedProtoSource}" "${GeneratedProtoHeader}" "${GeneratedGRPCSource}" "${GeneratedGRPCHeader}"
99 ARGS --grpc_out="${CMAKE_CURRENT_BINARY_DIR}"
100 --cpp_out="${CMAKE_CURRENT_BINARY_DIR}"
101 --proto_path="${ProtoSourcePath}"
102 --plugin=protoc-gen-grpc="${GRPC_CPP_PLUGIN}"
103 "${ProtoSourceAbsolutePath}"
104 DEPENDS "${ProtoSourceAbsolutePath}")
106 add_clang_library(${LibraryName} ${GeneratedProtoSource} ${GeneratedGRPCSource}
107 PARTIAL_SOURCES_INTENDED
108 LINK_LIBS grpc++ protobuf)