brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.9 KiB · 2eec35b Raw
122 lines · plain
1option(ENABLE_GRPC_REFLECTION "Link to gRPC Reflection library" OFF)2 3# FIXME(kirillbobyrev): Check if gRPC and Protobuf headers can be included at4# configure time.5find_package(Threads REQUIRED)6 7# Prefer finding gPRC through CMakeConfig and a hint can be provided via8# GRPC_INSTALL_PATH. This requires gRPC to be built and installed9# to ${GRPC_INSTALL_PATH} via -DCMAKE_INSTALL_PREFIX=${GRPC_INSTALL_PATH}.10# Libraries will be linked according to gRPC build policy which generates11# static libraries when BUILD_SHARED_LIBS is Off and dynamic libraries when12# 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 can15# specify GRPC_INSTALL_PATH or CMAKE_PREFIX_PATH to locate installed package.16set(protobuf_MODULE_COMPATIBLE TRUE)17find_package(Protobuf CONFIG HINTS ${GRPC_INSTALL_PATH})18message(STATUS "Using protobuf ${Protobuf_VERSION}")19find_package(gRPC CONFIG HINTS ${GRPC_INSTALL_PATH})20message(STATUS "Using gRPC ${gRPC_VERSION}")21 22if (Protobuf_FOUND AND gRPC_FOUND)23  # gRPC CMake CONFIG gives the libraries slightly odd names, make them match24  # the conventional system-installed names.25  set_target_properties(protobuf::libprotobuf PROPERTIES IMPORTED_GLOBAL TRUE)26  add_library(protobuf ALIAS protobuf::libprotobuf)27  set_target_properties(gRPC::grpc++ PROPERTIES IMPORTED_GLOBAL TRUE)28  add_library(grpc++ ALIAS gRPC::grpc++)29  if (ENABLE_GRPC_REFLECTION)30    set_target_properties(gRPC::grpc++_reflection PROPERTIES IMPORTED_GLOBAL TRUE)31    add_library(grpc++_reflection ALIAS gRPC::grpc++_reflection)32  endif()33 34  set(GRPC_CPP_PLUGIN $<TARGET_FILE:gRPC::grpc_cpp_plugin>)35  set(PROTOC ${Protobuf_PROTOC_EXECUTABLE})36else()37  # Now fallback to system-installed gRPC and ProtoBuf.38  # We always link dynamically in this mode. While the static libraries are39  # usually installed, the CMake files telling us *which* static libraries to40  # link are not.41  # FIXME: this path should not work on newer grpc versions and should be42  # removed in favor of `find_package` implementation.43  if (NOT BUILD_SHARED_LIBS)44    message(NOTICE "gRPC and Protobuf will be linked dynamically. If you want static linking, build gRPC from sources with -DBUILD_SHARED_LIBS=Off.")45  endif()46  find_program(GRPC_CPP_PLUGIN grpc_cpp_plugin)47  find_program(PROTOC protoc)48  if (NOT GRPC_CPP_PLUGIN OR NOT PROTOC)49    message(FATAL_ERROR "gRPC C++ Plugin and Protoc must be on $PATH for gRPC-enabled build.")50  endif()51  if(NOT TARGET grpc++)52    find_library(GRPC_LIBRARY grpc++ REQUIRED)53    add_library(grpc++ UNKNOWN IMPORTED GLOBAL)54    message(STATUS "Using grpc++: " ${GRPC_LIBRARY})55    set_target_properties(grpc++ PROPERTIES IMPORTED_LOCATION ${GRPC_LIBRARY})56    if (ENABLE_GRPC_REFLECTION)57      find_library(GRPC_REFLECTION_LIBRARY grpc++_reflection REQUIRED)58      add_library(grpc++_reflection UNKNOWN IMPORTED GLOBAL)59      set_target_properties(grpc++_reflection PROPERTIES IMPORTED_LOCATION ${GRPC_REFLECTION_LIBRARY})60    endif()61    find_library(PROTOBUF_LIBRARY protobuf REQUIRED)62    message(STATUS "Using protobuf: " ${PROTOBUF_LIBRARY})63    add_library(protobuf UNKNOWN IMPORTED GLOBAL)64    set_target_properties(protobuf PROPERTIES IMPORTED_LOCATION ${PROTOBUF_LIBRARY})65  endif()66endif()67 68if (ENABLE_GRPC_REFLECTION)69  set(REFLECTION_LIBRARY grpc++_reflection)70endif()71 72# Proto headers are generated in ${CMAKE_CURRENT_BINARY_DIR}.73# Libraries that use these headers should adjust the include path.74# If the "GRPC" argument is given, services are also generated.75# The DEPENDS list should name *.proto source files that are imported.76# They may be relative to the source dir or absolute (for generated protos).77function(generate_proto_sources GeneratedSource ProtoFile)78  cmake_parse_arguments(PARSE_ARGV 2 PROTO "GRPC" "" "DEPENDS")79  get_filename_component(ProtoSourceAbsolutePath "${CMAKE_CURRENT_SOURCE_DIR}/${ProtoFile}" ABSOLUTE)80  get_filename_component(ProtoSourcePath ${ProtoSourceAbsolutePath} PATH)81  get_filename_component(Basename ${ProtoSourceAbsolutePath} NAME_WLE)82 83  set(GeneratedProtoSource "${CMAKE_CURRENT_BINARY_DIR}/${Basename}.pb.cc")84  set(GeneratedProtoHeader "${CMAKE_CURRENT_BINARY_DIR}/${Basename}.pb.h")85  set(Flags86    --cpp_out="${CMAKE_CURRENT_BINARY_DIR}"87    --proto_path="${ProtoSourcePath}")88  if (PROTO_GRPC)89    list(APPEND Flags90      --grpc_out="${CMAKE_CURRENT_BINARY_DIR}"91      --plugin=protoc-gen-grpc="${GRPC_CPP_PLUGIN}")92    list(APPEND GeneratedProtoSource "${CMAKE_CURRENT_BINARY_DIR}/${Basename}.grpc.pb.cc")93    list(APPEND GeneratedProtoHeader "${CMAKE_CURRENT_BINARY_DIR}/${Basename}.grpc.pb.h")94  endif()95  add_custom_command(96        OUTPUT ${GeneratedProtoSource} ${GeneratedProtoHeader}97        COMMAND ${PROTOC}98        ARGS ${Flags} "${ProtoSourceAbsolutePath}"99        DEPENDS "${ProtoSourceAbsolutePath}")100 101  set(${GeneratedSource} ${GeneratedProtoSource} PARENT_SCOPE)102 103  # Ensure dependency headers are generated before dependent protos are built.104  # DEPENDS arg is a list of "Foo.proto". While they're logically relative to105  # the source dir, the generated headers we need are in the binary dir.106  foreach(ImportedProto IN LISTS PROTO_DEPENDS)107    # Foo.proto -> Foo.pb.h108    STRING(REGEX REPLACE "\\.proto$" ".pb.h" ImportedHeader "${ImportedProto}")109    # Foo.pb.h -> ${CMAKE_CURRENT_BINARY_DIR}/Foo.pb.h110    get_filename_component(ImportedHeader "${ImportedHeader}"111      ABSOLUTE112      BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}")113    # Compilation of each generated source depends on ${BINARY}/Foo.pb.h.114    foreach(Generated IN LISTS GeneratedProtoSource)115      # FIXME: CMake docs suggest OBJECT_DEPENDS isn't needed, but I can't get116      #        the recommended add_dependencies() approach to work.117      set_source_files_properties("${Generated}"118        PROPERTIES OBJECT_DEPENDS "${ImportedHeader}")119    endforeach(Generated)120  endforeach(ImportedProto)121endfunction()122