1 option(ENABLE_GRPC_REFLECTION "Link to gRPC Reflection library" OFF)
3 # FIXME(kirillbobyrev): Check if gRPC and Protobuf headers can be included at
5 find_package(Threads REQUIRED)
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}")
19 include_directories(${Protobuf_INCLUDE_DIRS})
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)
32 set(GRPC_CPP_PLUGIN $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
33 set(PROTOC ${Protobuf_PROTOC_EXECUTABLE})
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
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.")
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 gRPC-enabled build.")
47 # On macOS the libraries are typically installed via Homebrew and are not on
51 set(GRPC_INCLUDE_PATHS "")
53 find_program(HOMEBREW brew)
54 # If Homebrew is not found, the user might have installed libraries
55 # manually. Fall back to the system path.
57 execute_process(COMMAND ${HOMEBREW} --prefix grpc
58 OUTPUT_VARIABLE GRPC_HOMEBREW_PATH
59 RESULT_VARIABLE GRPC_HOMEBREW_RETURN_CODE
60 OUTPUT_STRIP_TRAILING_WHITESPACE)
61 execute_process(COMMAND ${HOMEBREW} --prefix protobuf
62 OUTPUT_VARIABLE PROTOBUF_HOMEBREW_PATH
63 RESULT_VARIABLE PROTOBUF_HOMEBREW_RETURN_CODE
64 OUTPUT_STRIP_TRAILING_WHITESPACE)
65 execute_process(COMMAND ${HOMEBREW} --prefix abseil
66 OUTPUT_VARIABLE ABSL_HOMEBREW_PATH
67 RESULT_VARIABLE ABSL_HOMEBREW_RETURN_CODE
68 OUTPUT_STRIP_TRAILING_WHITESPACE)
69 # If either library is not installed via Homebrew, fall back to the
71 if (GRPC_HOMEBREW_RETURN_CODE EQUAL "0")
72 list(APPEND GRPC_INCLUDE_PATHS ${GRPC_HOMEBREW_PATH}/include)
73 list(APPEND GRPC_OPTS PATHS ${GRPC_HOMEBREW_PATH}/lib NO_DEFAULT_PATH)
75 if (PROTOBUF_HOMEBREW_RETURN_CODE EQUAL "0")
76 list(APPEND GRPC_INCLUDE_PATHS ${PROTOBUF_HOMEBREW_PATH}/include)
77 list(APPEND PROTOBUF_OPTS PATHS ${PROTOBUF_HOMEBREW_PATH}/lib NO_DEFAULT_PATH)
79 if (ABSL_HOMEBREW_RETURN_CODE EQUAL "0")
80 list(APPEND GRPC_INCLUDE_PATHS ${ABSL_HOMEBREW_PATH}/include)
85 find_library(GRPC_LIBRARY grpc++ ${GRPC_OPTS} REQUIRED)
86 add_library(grpc++ UNKNOWN IMPORTED GLOBAL)
87 message(STATUS "Using grpc++: " ${GRPC_LIBRARY})
88 set_target_properties(grpc++ PROPERTIES IMPORTED_LOCATION ${GRPC_LIBRARY})
89 target_include_directories(grpc++ INTERFACE ${GRPC_INCLUDE_PATHS})
90 if (ENABLE_GRPC_REFLECTION)
91 find_library(GRPC_REFLECTION_LIBRARY grpc++_reflection ${GRPC_OPTS} REQUIRED)
92 add_library(grpc++_reflection UNKNOWN IMPORTED GLOBAL)
93 set_target_properties(grpc++_reflection PROPERTIES IMPORTED_LOCATION ${GRPC_REFLECTION_LIBRARY})
95 find_library(PROTOBUF_LIBRARY protobuf ${PROTOBUF_OPTS} REQUIRED)
96 message(STATUS "Using protobuf: " ${PROTOBUF_LIBRARY})
97 add_library(protobuf UNKNOWN IMPORTED GLOBAL)
98 set_target_properties(protobuf PROPERTIES IMPORTED_LOCATION ${PROTOBUF_LIBRARY})
102 if (ENABLE_GRPC_REFLECTION)
103 set(REFLECTION_LIBRARY grpc++_reflection)
106 # Proto headers are generated in ${CMAKE_CURRENT_BINARY_DIR}.
107 # Libraries that use these headers should adjust the include path.
108 # If the "GRPC" argument is given, services are also generated.
109 # The DEPENDS list should name *.proto source files that are imported.
110 # They may be relative to the source dir or absolute (for generated protos).
111 function(generate_protos LibraryName ProtoFile)
112 cmake_parse_arguments(PARSE_ARGV 2 PROTO "GRPC" "" "DEPENDS")
113 get_filename_component(ProtoSourceAbsolutePath "${CMAKE_CURRENT_SOURCE_DIR}/${ProtoFile}" ABSOLUTE)
114 get_filename_component(ProtoSourcePath ${ProtoSourceAbsolutePath} PATH)
115 get_filename_component(Basename ${ProtoSourceAbsolutePath} NAME_WLE)
117 set(GeneratedProtoSource "${CMAKE_CURRENT_BINARY_DIR}/${Basename}.pb.cc")
118 set(GeneratedProtoHeader "${CMAKE_CURRENT_BINARY_DIR}/${Basename}.pb.h")
120 --cpp_out="${CMAKE_CURRENT_BINARY_DIR}"
121 --proto_path="${ProtoSourcePath}")
124 --grpc_out="${CMAKE_CURRENT_BINARY_DIR}"
125 --plugin=protoc-gen-grpc="${GRPC_CPP_PLUGIN}")
126 list(APPEND GeneratedProtoSource "${CMAKE_CURRENT_BINARY_DIR}/${Basename}.grpc.pb.cc")
127 list(APPEND GeneratedProtoHeader "${CMAKE_CURRENT_BINARY_DIR}/${Basename}.grpc.pb.h")
130 OUTPUT ${GeneratedProtoSource} ${GeneratedProtoHeader}
132 ARGS ${Flags} "${ProtoSourceAbsolutePath}"
133 DEPENDS "${ProtoSourceAbsolutePath}")
135 add_llvm_library(${LibraryName} ${GeneratedProtoSource}
136 PARTIAL_SOURCES_INTENDED
137 LINK_LIBS PUBLIC grpc++ protobuf)
139 # Ensure dependency headers are generated before dependent protos are built.
140 # DEPENDS arg is a list of "Foo.proto". While they're logically relative to
141 # the source dir, the generated headers we need are in the binary dir.
142 foreach(ImportedProto IN LISTS PROTO_DEPENDS)
143 # Foo.proto -> Foo.pb.h
144 STRING(REGEX REPLACE "\\.proto$" ".pb.h" ImportedHeader "${ImportedProto}")
145 # Foo.pb.h -> ${CMAKE_CURRENT_BINARY_DIR}/Foo.pb.h
146 get_filename_component(ImportedHeader "${ImportedHeader}"
148 BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
149 # Compilation of each generated source depends on ${BINARY}/Foo.pb.h.
150 foreach(Generated IN LISTS GeneratedProtoSource)
151 # FIXME: CMake docs suggest OBJECT_DEPENDS isn't needed, but I can't get
152 # the recommended add_dependencies() approach to work.
153 set_source_files_properties("${Generated}"
154 PROPERTIES OBJECT_DEPENDS "${ImportedHeader}")
155 endforeach(Generated)
156 endforeach(ImportedProto)