1option(ENABLE_GRPC_REFLECTION "Link clangd-index-server to gRPC Reflection library" OFF) 2 3# FIXME(kirillbobyrev): Check if gRPC and Protobuf headers can be included at 4# configure time. 5find_package(Threads REQUIRED) 6if (GRPC_INSTALL_PATH) 7 # This setup requires gRPC to be built from sources using CMake and installed 8 # to ${GRPC_INSTALL_PATH} via -DCMAKE_INSTALL_PREFIX=${GRPC_INSTALL_PATH}. 9 # Libraries will be linked according to gRPC build policy which generates 10 # static libraries when BUILD_SHARED_LIBS is Off and dynamic libraries when 11 # it's On (NOTE: This is a variable passed to gRPC CMake build invocation, 12 # LLVM's BUILD_SHARED_LIBS has no effect). 13 set(protobuf_MODULE_COMPATIBLE TRUE) 14 find_package(Protobuf CONFIG REQUIRED HINTS ${GRPC_INSTALL_PATH}) 15 message(STATUS "Using protobuf ${protobuf_VERSION}") 16 find_package(gRPC CONFIG REQUIRED HINTS ${GRPC_INSTALL_PATH}) 17 message(STATUS "Using gRPC ${gRPC_VERSION}") 18 19 include_directories(${Protobuf_INCLUDE_DIRS}) 20 21 # gRPC CMake CONFIG gives the libraries slightly odd names, make them match 22 # the conventional system-installed names. 23 set_target_properties(protobuf::libprotobuf PROPERTIES IMPORTED_GLOBAL TRUE) 24 add_library(protobuf ALIAS protobuf::libprotobuf) 25 set_target_properties(gRPC::grpc++ PROPERTIES IMPORTED_GLOBAL TRUE) 26 add_library(grpc++ ALIAS gRPC::grpc++) 27 if (ENABLE_GRPC_REFLECTION) 28 set_target_properties(gRPC::grpc++_reflection PROPERTIES IMPORTED_GLOBAL TRUE) 29 add_library(grpc++_reflection ALIAS gRPC::grpc++_reflection) 30 endif() 31 32 set(GRPC_CPP_PLUGIN $<TARGET_FILE:gRPC::grpc_cpp_plugin>) 33 set(PROTOC ${Protobuf_PROTOC_EXECUTABLE}) 34else() 35 # This setup requires system-installed gRPC and Protobuf. 36 # We always link dynamically in this mode. While the static libraries are 37 # usually installed, the CMake files telling us *which* static libraries to 38 # link are not. 39 if (NOT BUILD_SHARED_LIBS) 40 message(NOTICE "gRPC and Protobuf will be linked dynamically. If you want static linking, build gRPC from sources with -DBUILD_SHARED_LIBS=Off.") 41 endif() 42 find_program(GRPC_CPP_PLUGIN grpc_cpp_plugin) 43 find_program(PROTOC protoc) 44 if (NOT GRPC_CPP_PLUGIN OR NOT PROTOC) 45 message(FATAL_ERROR "gRPC C++ Plugin and Protoc must be on $PATH for Clangd remote index build.") 46 endif() 47 # On macOS the libraries are typically installed via Homebrew and are not on 48 # the system path. 49 set(GRPC_OPTS "") 50 set(PROTOBUF_OPTS "") 51 if (${APPLE}) 52 find_program(HOMEBREW brew) 53 # If Homebrew is not found, the user might have installed libraries 54 # manually. Fall back to the system path. 55 if (HOMEBREW) 56 execute_process(COMMAND ${HOMEBREW} --prefix grpc 57 OUTPUT_VARIABLE GRPC_HOMEBREW_PATH 58 RESULT_VARIABLE GRPC_HOMEBREW_RETURN_CODE 59 OUTPUT_STRIP_TRAILING_WHITESPACE) 60 execute_process(COMMAND ${HOMEBREW} --prefix protobuf 61 OUTPUT_VARIABLE PROTOBUF_HOMEBREW_PATH 62 RESULT_VARIABLE PROTOBUF_HOMEBREW_RETURN_CODE 63 OUTPUT_STRIP_TRAILING_WHITESPACE) 64 # If either library is not installed via Homebrew, fall back to the 65 # system path. 66 if (GRPC_HOMEBREW_RETURN_CODE EQUAL "0") 67 include_directories(${GRPC_HOMEBREW_PATH}/include) 68 list(APPEND GRPC_OPTS PATHS ${GRPC_HOMEBREW_PATH}/lib NO_DEFAULT_PATH) 69 endif() 70 if (PROTOBUF_HOMEBREW_RETURN_CODE EQUAL "0") 71 include_directories(${PROTOBUF_HOMEBREW_PATH}/include) 72 list(APPEND PROTOBUF_OPTS PATHS ${PROTOBUF_HOMEBREW_PATH}/lib NO_DEFAULT_PATH) 73 endif() 74 endif() 75 endif() 76 find_library(GRPC_LIBRARY grpc++ $GRPC_OPTS REQUIRED) 77 add_library(grpc++ UNKNOWN IMPORTED GLOBAL) 78 message(STATUS "Using grpc++: " ${GRPC_LIBRARY}) 79 set_target_properties(grpc++ PROPERTIES IMPORTED_LOCATION ${GRPC_LIBRARY}) 80 if (ENABLE_GRPC_REFLECTION) 81 find_library(GRPC_REFLECTION_LIBRARY grpc++_reflection $GRPC_OPTS REQUIRED) 82 add_library(grpc++_reflection UNKNOWN IMPORTED GLOBAL) 83 set_target_properties(grpc++_reflection PROPERTIES IMPORTED_LOCATION ${GRPC_REFLECTION_LIBRARY}) 84 endif() 85 find_library(PROTOBUF_LIBRARY protobuf $PROTOBUF_OPTS REQUIRED) 86 message(STATUS "Using protobuf: " ${PROTOBUF_LIBRARY}) 87 add_library(protobuf UNKNOWN IMPORTED GLOBAL) 88 set_target_properties(protobuf PROPERTIES IMPORTED_LOCATION ${PROTOBUF_LIBRARY}) 89endif() 90 91if (ENABLE_GRPC_REFLECTION) 92 set(REFLECTION_LIBRARY grpc++_reflection) 93endif() 94 95# Proto headers are generated in ${CMAKE_CURRENT_BINARY_DIR}. 96# Libraries that use these headers should adjust the include path. 97# If the "GRPC" argument is given, services are also generated. 98# The DEPENDS list should name *.proto source files that are imported. 99# They may be relative to the source dir or absolute (for generated protos). 100function(generate_protos LibraryName ProtoFile) 101 cmake_parse_arguments(PARSE_ARGV 2 PROTO "GRPC" "" "DEPENDS") 102 get_filename_component(ProtoSourceAbsolutePath "${CMAKE_CURRENT_SOURCE_DIR}/${ProtoFile}" ABSOLUTE) 103 get_filename_component(ProtoSourcePath ${ProtoSourceAbsolutePath} PATH) 104 get_filename_component(Basename ${ProtoSourceAbsolutePath} NAME_WLE) 105 106 set(GeneratedProtoSource "${CMAKE_CURRENT_BINARY_DIR}/${Basename}.pb.cc") 107 set(GeneratedProtoHeader "${CMAKE_CURRENT_BINARY_DIR}/${Basename}.pb.h") 108 set(Flags 109 --cpp_out="${CMAKE_CURRENT_BINARY_DIR}" 110 --proto_path="${ProtoSourcePath}") 111 if (PROTO_GRPC) 112 list(APPEND Flags 113 --grpc_out="${CMAKE_CURRENT_BINARY_DIR}" 114 --plugin=protoc-gen-grpc="${GRPC_CPP_PLUGIN}") 115 list(APPEND GeneratedProtoSource "${CMAKE_CURRENT_BINARY_DIR}/${Basename}.grpc.pb.cc") 116 list(APPEND GeneratedProtoHeader "${CMAKE_CURRENT_BINARY_DIR}/${Basename}.grpc.pb.h") 117 endif() 118 add_custom_command( 119 OUTPUT ${GeneratedProtoSource} ${GeneratedProtoHeader} 120 COMMAND ${PROTOC} 121 ARGS ${Flags} "${ProtoSourceAbsolutePath}" 122 DEPENDS "${ProtoSourceAbsolutePath}") 123 124 add_clang_library(${LibraryName} ${GeneratedProtoSource} 125 PARTIAL_SOURCES_INTENDED 126 LINK_LIBS PUBLIC grpc++ protobuf) 127 128 # Ensure dependency headers are generated before dependent protos are built. 129 # DEPENDS arg is a list of "Foo.proto". While they're logically relative to 130 # the source dir, the generated headers we need are in the binary dir. 131 foreach(ImportedProto IN LISTS PROTO_DEPENDS) 132 # Foo.proto -> Foo.pb.h 133 STRING(REGEX REPLACE "\\.proto$" ".pb.h" ImportedHeader "${ImportedProto}") 134 # Foo.pb.h -> ${CMAKE_CURRENT_BINARY_DIR}/Foo.pb.h 135 get_filename_component(ImportedHeader "${ImportedHeader}" 136 ABSOLUTE 137 BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}") 138 # Compilation of each generated source depends on ${BINARY}/Foo.pb.h. 139 foreach(Generated IN LISTS GeneratedProtoSource) 140 # FIXME: CMake docs suggest OBJECT_DEPENDS isn't needed, but I can't get 141 # the recommended add_dependencies() approach to work. 142 set_source_files_properties("${Generated}" 143 PROPERTIES OBJECT_DEPENDS "${ImportedHeader}") 144 endforeach(Generated) 145 endforeach(ImportedProto) 146endfunction() 147