1option(ENABLE_GRPC_REFLECTION "Link 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 gRPC-enabled 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 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() 84 if(NOT TARGET grpc++) 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}) 94 endif() 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}) 99 endif() 100endif() 101 102if (ENABLE_GRPC_REFLECTION) 103 set(REFLECTION_LIBRARY grpc++_reflection) 104endif() 105 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). 111function(generate_proto_sources GeneratedSource 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) 116 117 set(GeneratedProtoSource "${CMAKE_CURRENT_BINARY_DIR}/${Basename}.pb.cc") 118 set(GeneratedProtoHeader "${CMAKE_CURRENT_BINARY_DIR}/${Basename}.pb.h") 119 set(Flags 120 --cpp_out="${CMAKE_CURRENT_BINARY_DIR}" 121 --proto_path="${ProtoSourcePath}") 122 if (PROTO_GRPC) 123 list(APPEND Flags 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") 128 endif() 129 add_custom_command( 130 OUTPUT ${GeneratedProtoSource} ${GeneratedProtoHeader} 131 COMMAND ${PROTOC} 132 ARGS ${Flags} "${ProtoSourceAbsolutePath}" 133 DEPENDS "${ProtoSourceAbsolutePath}") 134 135 set(${GeneratedSource} ${GeneratedProtoSource} PARENT_SCOPE) 136 137 # Ensure dependency headers are generated before dependent protos are built. 138 # DEPENDS arg is a list of "Foo.proto". While they're logically relative to 139 # the source dir, the generated headers we need are in the binary dir. 140 foreach(ImportedProto IN LISTS PROTO_DEPENDS) 141 # Foo.proto -> Foo.pb.h 142 STRING(REGEX REPLACE "\\.proto$" ".pb.h" ImportedHeader "${ImportedProto}") 143 # Foo.pb.h -> ${CMAKE_CURRENT_BINARY_DIR}/Foo.pb.h 144 get_filename_component(ImportedHeader "${ImportedHeader}" 145 ABSOLUTE 146 BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}") 147 # Compilation of each generated source depends on ${BINARY}/Foo.pb.h. 148 foreach(Generated IN LISTS GeneratedProtoSource) 149 # FIXME: CMake docs suggest OBJECT_DEPENDS isn't needed, but I can't get 150 # the recommended add_dependencies() approach to work. 151 set_source_files_properties("${Generated}" 152 PROPERTIES OBJECT_DEPENDS "${ImportedHeader}") 153 endforeach(Generated) 154 endforeach(ImportedProto) 155endfunction() 156