@@ -3,21 +3,24 @@ 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.
55find_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+ message (STATUS "GRPC_INSTALL_PATH: " ${GRPC_INSTALL_PATH} )
17+ set (protobuf_MODULE_COMPATIBLE TRUE )
18+ find_package (Protobuf CONFIG HINTS ${GRPC_INSTALL_PATH} )
19+ message (STATUS "Using protobuf ${Protobuf_VERSION} " )
20+ find_package (gRPC CONFIG HINTS ${GRPC_INSTALL_PATH} )
21+ message (STATUS "Using gRPC ${gRPC_VERSION} " )
2022
23+ if (Protobuf_FOUND AND gRPC_FOUND)
2124 # gRPC CMake CONFIG gives the libraries slightly odd names, make them match
2225 # the conventional system-installed names.
2326 set_target_properties (protobuf::libprotobuf PROPERTIES IMPORTED_GLOBAL TRUE )
@@ -32,10 +35,12 @@ if (GRPC_INSTALL_PATH)
3235 set (GRPC_CPP_PLUGIN $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
3336 set (PROTOC ${Protobuf_PROTOC_EXECUTABLE} )
3437else ()
35- # This setup requires system-installed gRPC and Protobuf .
38+ # Now fallback to system-installed gRPC and ProtoBuf .
3639 # We always link dynamically in this mode. While the static libraries are
3740 # usually installed, the CMake files telling us *which* static libraries to
3841 # link are not.
42+ # FIXME: this path should not work on newer grpc versions and should be
43+ # removed in favor of `find_package` implementation.
3944 if (NOT BUILD_SHARED_LIBS )
4045 message (NOTICE "gRPC and Protobuf will be linked dynamically. If you want static linking, build gRPC from sources with -DBUILD_SHARED_LIBS=Off." )
4146 endif ()
@@ -44,55 +49,17 @@ else()
4449 if (NOT GRPC_CPP_PLUGIN OR NOT PROTOC)
4550 message (FATAL_ERROR "gRPC C++ Plugin and Protoc must be on $PATH for gRPC-enabled build." )
4651 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 ()
8452 if (NOT TARGET grpc++)
85- find_library (GRPC_LIBRARY grpc++ ${GRPC_OPTS} REQUIRED)
53+ find_library (GRPC_LIBRARY grpc++ REQUIRED)
8654 add_library (grpc++ UNKNOWN IMPORTED GLOBAL )
8755 message (STATUS "Using grpc++: " ${GRPC_LIBRARY} )
8856 set_target_properties (grpc++ PROPERTIES IMPORTED_LOCATION ${GRPC_LIBRARY} )
89- target_include_directories (grpc++ INTERFACE ${GRPC_INCLUDE_PATHS} )
9057 if (ENABLE_GRPC_REFLECTION)
91- find_library (GRPC_REFLECTION_LIBRARY grpc++_reflection ${GRPC_OPTS} REQUIRED)
58+ find_library (GRPC_REFLECTION_LIBRARY grpc++_reflection REQUIRED)
9259 add_library (grpc++_reflection UNKNOWN IMPORTED GLOBAL )
9360 set_target_properties (grpc++_reflection PROPERTIES IMPORTED_LOCATION ${GRPC_REFLECTION_LIBRARY} )
9461 endif ()
95- find_library (PROTOBUF_LIBRARY protobuf ${PROTOBUF_OPTS} REQUIRED)
62+ find_library (PROTOBUF_LIBRARY protobuf REQUIRED)
9663 message (STATUS "Using protobuf: " ${PROTOBUF_LIBRARY} )
9764 add_library (protobuf UNKNOWN IMPORTED GLOBAL )
9865 set_target_properties (protobuf PROPERTIES IMPORTED_LOCATION ${PROTOBUF_LIBRARY} )
0 commit comments