Skip to content

Commit 2b135b9

Browse files
[CMake][gRPC] Update FindGRPC.cmake to support newer gRPC versions (#162935)
Update the logic to find gRPC to always favor CMake `find_package` implementation including for builds on macOS that uses homebrew, where gRPCConfig.cmake is also installed to provide an accurate target dependencies to link against. This fixes the problem that newer gRPC version has broken up the libraries into smaller pieces and the hard coded list of libraries in the implementation can no longer work. Fixes: #59844
1 parent 1c2f01e commit 2b135b9

File tree

1 file changed

+21
-55
lines changed

1 file changed

+21
-55
lines changed

cmake/Modules/FindGRPC.cmake

Lines changed: 21 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@ option(ENABLE_GRPC_REFLECTION "Link to gRPC Reflection library" OFF)
33
# FIXME(kirillbobyrev): Check if gRPC and Protobuf headers can be included at
44
# configure time.
55
find_package(Threads REQUIRED)
6-
if (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}")
186

19-
include_directories(${Protobuf_INCLUDE_DIRS})
7+
# Prefer finding gPRC through CMakeConfig and a hint can be provided via
8+
# GRPC_INSTALL_PATH. This requires gRPC to be built and installed
9+
# to ${GRPC_INSTALL_PATH} via -DCMAKE_INSTALL_PREFIX=${GRPC_INSTALL_PATH}.
10+
# Libraries will be linked according to gRPC build policy which generates
11+
# static libraries when BUILD_SHARED_LIBS is Off and dynamic libraries when
12+
# it's On (NOTE: This is a variable passed to gRPC CMake build invocation,
13+
# LLVM's BUILD_SHARED_LIBS has no effect).
14+
# Package managers like Homebrew will also install Config.cmake and user can
15+
# specify GRPC_INSTALL_PATH or CMAKE_PREFIX_PATH to locate installed package.
16+
set(protobuf_MODULE_COMPATIBLE TRUE)
17+
find_package(Protobuf CONFIG HINTS ${GRPC_INSTALL_PATH})
18+
message(STATUS "Using protobuf ${Protobuf_VERSION}")
19+
find_package(gRPC CONFIG HINTS ${GRPC_INSTALL_PATH})
20+
message(STATUS "Using gRPC ${gRPC_VERSION}")
2021

22+
if (Protobuf_FOUND AND gRPC_FOUND)
2123
# gRPC CMake CONFIG gives the libraries slightly odd names, make them match
2224
# the conventional system-installed names.
2325
set_target_properties(protobuf::libprotobuf PROPERTIES IMPORTED_GLOBAL TRUE)
@@ -32,10 +34,12 @@ if (GRPC_INSTALL_PATH)
3234
set(GRPC_CPP_PLUGIN $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
3335
set(PROTOC ${Protobuf_PROTOC_EXECUTABLE})
3436
else()
35-
# This setup requires system-installed gRPC and Protobuf.
37+
# Now fallback to system-installed gRPC and ProtoBuf.
3638
# We always link dynamically in this mode. While the static libraries are
3739
# usually installed, the CMake files telling us *which* static libraries to
3840
# link are not.
41+
# FIXME: this path should not work on newer grpc versions and should be
42+
# removed in favor of `find_package` implementation.
3943
if (NOT BUILD_SHARED_LIBS)
4044
message(NOTICE "gRPC and Protobuf will be linked dynamically. If you want static linking, build gRPC from sources with -DBUILD_SHARED_LIBS=Off.")
4145
endif()
@@ -44,55 +48,17 @@ else()
4448
if (NOT GRPC_CPP_PLUGIN OR NOT PROTOC)
4549
message(FATAL_ERROR "gRPC C++ Plugin and Protoc must be on $PATH for gRPC-enabled build.")
4650
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-
set(GRPC_INCLUDE_PATHS "")
52-
if (${APPLE})
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.
56-
if (HOMEBREW)
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
70-
# system path.
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)
74-
endif()
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)
78-
endif()
79-
if (ABSL_HOMEBREW_RETURN_CODE EQUAL "0")
80-
list(APPEND GRPC_INCLUDE_PATHS ${ABSL_HOMEBREW_PATH}/include)
81-
endif()
82-
endif()
83-
endif()
8451
if(NOT TARGET grpc++)
85-
find_library(GRPC_LIBRARY grpc++ ${GRPC_OPTS} REQUIRED)
52+
find_library(GRPC_LIBRARY grpc++ REQUIRED)
8653
add_library(grpc++ UNKNOWN IMPORTED GLOBAL)
8754
message(STATUS "Using grpc++: " ${GRPC_LIBRARY})
8855
set_target_properties(grpc++ PROPERTIES IMPORTED_LOCATION ${GRPC_LIBRARY})
89-
target_include_directories(grpc++ INTERFACE ${GRPC_INCLUDE_PATHS})
9056
if (ENABLE_GRPC_REFLECTION)
91-
find_library(GRPC_REFLECTION_LIBRARY grpc++_reflection ${GRPC_OPTS} REQUIRED)
57+
find_library(GRPC_REFLECTION_LIBRARY grpc++_reflection REQUIRED)
9258
add_library(grpc++_reflection UNKNOWN IMPORTED GLOBAL)
9359
set_target_properties(grpc++_reflection PROPERTIES IMPORTED_LOCATION ${GRPC_REFLECTION_LIBRARY})
9460
endif()
95-
find_library(PROTOBUF_LIBRARY protobuf ${PROTOBUF_OPTS} REQUIRED)
61+
find_library(PROTOBUF_LIBRARY protobuf REQUIRED)
9662
message(STATUS "Using protobuf: " ${PROTOBUF_LIBRARY})
9763
add_library(protobuf UNKNOWN IMPORTED GLOBAL)
9864
set_target_properties(protobuf PROPERTIES IMPORTED_LOCATION ${PROTOBUF_LIBRARY})

0 commit comments

Comments
 (0)