Skip to content

Commit

Permalink
[clangd] Optionally add reflection for clangd-index-server
Browse files Browse the repository at this point in the history
This was originally landed without the optional part and reverted later:

8080ea4

Reviewed By: kadircet

Differential Revision: https://reviews.llvm.org/D98404
  • Loading branch information
kirillbobyrev committed Mar 15, 2021
1 parent 68e4084 commit 9bcf0ef
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ option(CLANGD_MALLOC_TRIM "Call malloc_trim(3) periodically in Clangd. (only tak
llvm_canonicalize_cmake_booleans(
CLANGD_BUILD_XPC
CLANGD_ENABLE_REMOTE
ENABLE_GRPC_REFLECTION
CLANGD_MALLOC_TRIM
LLVM_ENABLE_ZLIB
)
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/Features.inc.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define CLANGD_BUILD_XPC @CLANGD_BUILD_XPC@
#define CLANGD_ENABLE_REMOTE @CLANGD_ENABLE_REMOTE@
#define ENABLE_GRPC_REFLECTION @ENABLE_GRPC_REFLECTION@
#define CLANGD_MALLOC_TRIM @CLANGD_MALLOC_TRIM@
2 changes: 2 additions & 0 deletions clang-tools-extra/clangd/index/remote/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ target_link_libraries(clangd-index-server
RemoteIndexProto
RemoteIndexServiceProto
clangdRemoteMarshalling

${REFLECTION_LIBRARY}
)
8 changes: 8 additions & 0 deletions clang-tools-extra/clangd/index/remote/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "Features.inc"
#include "Index.pb.h"
#include "Service.grpc.pb.h"
#include "index/Index.h"
Expand Down Expand Up @@ -35,6 +36,10 @@
#include <memory>
#include <thread>

#if ENABLE_GRPC_REFLECTION
#include <grpc++/ext/proto_server_reflection_plugin.h>
#endif

namespace clang {
namespace clangd {
namespace remote {
Expand Down Expand Up @@ -313,6 +318,9 @@ void runServerAndWait(clangd::SymbolIndex &Index, llvm::StringRef ServerAddress,
RemoteIndexServer Service(Index, IndexRoot);

grpc::EnableDefaultHealthCheckService(true);
#if ENABLE_GRPC_REFLECTION
grpc::reflection::InitProtoReflectionServerBuilderPlugin();
#endif
grpc::ServerBuilder Builder;
Builder.AddListeningPort(ServerAddress.str(),
grpc::InsecureServerCredentials());
Expand Down
15 changes: 15 additions & 0 deletions llvm/cmake/modules/FindGRPC.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
option(ENABLE_GRPC_REFLECTION "Link clangd-index-server to gRPC Reflection library" OFF)

# FIXME(kirillbobyrev): Check if gRPC and Protobuf headers can be included at
# configure time.
find_package(Threads REQUIRED)
Expand All @@ -22,6 +24,10 @@ if (GRPC_INSTALL_PATH)
add_library(protobuf ALIAS protobuf::libprotobuf)
set_target_properties(gRPC::grpc++ PROPERTIES IMPORTED_GLOBAL TRUE)
add_library(grpc++ ALIAS gRPC::grpc++)
if (ENABLE_GRPC_REFLECTION)
set_target_properties(gRPC::grpc++_reflection PROPERTIES IMPORTED_GLOBAL TRUE)
add_library(grpc++_reflection ALIAS gRPC::grpc++_reflection)
endif()

set(GRPC_CPP_PLUGIN $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
set(PROTOC ${Protobuf_PROTOC_EXECUTABLE})
Expand Down Expand Up @@ -71,12 +77,21 @@ else()
add_library(grpc++ UNKNOWN IMPORTED GLOBAL)
message(STATUS "Using grpc++: " ${GRPC_LIBRARY})
set_target_properties(grpc++ PROPERTIES IMPORTED_LOCATION ${GRPC_LIBRARY})
if (ENABLE_GRPC_REFLECTION)
find_library(GRPC_REFLECTION_LIBRARY grpc++_reflection $GRPC_OPTS REQUIRED)
add_library(grpc++_reflection UNKNOWN IMPORTED GLOBAL)
set_target_properties(grpc++_reflection PROPERTIES IMPORTED_LOCATION ${GRPC_REFLECTION_LIBRARY})
endif()
find_library(PROTOBUF_LIBRARY protobuf $PROTOBUF_OPTS REQUIRED)
message(STATUS "Using protobuf: " ${PROTOBUF_LIBRARY})
add_library(protobuf UNKNOWN IMPORTED GLOBAL)
set_target_properties(protobuf PROPERTIES IMPORTED_LOCATION ${PROTOBUF_LIBRARY})
endif()

if (ENABLE_GRPC_REFLECTION)
set(REFLECTION_LIBRARY grpc++_reflection)
endif()

# Proto headers are generated in ${CMAKE_CURRENT_BINARY_DIR}.
# Libraries that use these headers should adjust the include path.
# If the "GRPC" argument is given, services are also generated.
Expand Down

0 comments on commit 9bcf0ef

Please sign in to comment.