You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
3.0 KiB

  1. #
  2. # Locate and configure the gRPC library
  3. #
  4. # Adds the following targets:
  5. #
  6. # gRPC::grpc - gRPC library
  7. # gRPC::grpc++ - gRPC C++ library
  8. # gRPC::grpc++_reflection - gRPC C++ reflection library
  9. # gRPC::grpc_cpp_plugin - C++ generator plugin for Protocol Buffers
  10. #
  11. # Generates C++ sources from the .proto files
  12. #
  13. # grpc_generate_cpp (<SLIB> <HDRS> <DEST> [<ARGN>...])
  14. # PLIB - Proto Static Library generated by .proto file
  15. # HDRS - variable to define with autogenerated source files and header files
  16. # DEST - directory where the source files will be created
  17. # ARGN - .proto files
  18. function(GRPC_GENERATE_CPP PLIB HDRS DEST)
  19. add_custom_command(
  20. OUTPUT ${DEST}/${HDRS}.pb.cc
  21. ${DEST}/${HDRS}.pb.h
  22. ${DEST}/${HDRS}.grpc.pb.cc
  23. ${DEST}/${HDRS}.grpc.pb.h
  24. COMMAND ${GRPC_BINARY_DIR}/protoc
  25. ARGS --grpc_out "${DEST}"
  26. --cpp_out "${DEST}"
  27. -I "${DEST}"
  28. --plugin=protoc-gen-grpc=${GRPC_BINARY_DIR}/grpc_cpp_plugin
  29. ${ARGN})
  30. add_library(${PLIB}
  31. ${DEST}/${HDRS}.pb.cc
  32. ${DEST}/${HDRS}.pb.h
  33. ${DEST}/${HDRS}.grpc.pb.cc
  34. ${DEST}/${HDRS}.grpc.pb.h)
  35. set_target_properties(${PLIB} PROPERTIES
  36. ARCHIVE_OUTPUT_DIRECTORY ${DEST})
  37. target_link_libraries(${PLIB}
  38. ${GRPC_CLIENT_LIBS})
  39. if (EXISTS ${DEST}/${PLIB})
  40. message("${BoldGreen} Added ${PLIB} files at ${DEST} successfully!${ColourReset}")
  41. else ()
  42. message(FATAL_ERROR "Add ${PLIB} code failed!")
  43. endif ()
  44. endfunction()
  45. if(NOT DEFINED GRPC_ROOT)
  46. message(FATAL_ERROR "GRPC_ROOT not set in FindgRPC.cmake")
  47. else()
  48. set(CMAKE_CXX_STANDARD 14)
  49. set(protobuf_MODULE_COMPATIBLE TRUE)
  50. list(APPEND CMAKE_PREFIX_PATH "${GRPC_ROOT}/lib/cmake/protobuf/")
  51. find_package(Protobuf CONFIG REQUIRED)
  52. message("${BoldGreen}Using protobuf ${Protobuf_VERSION}${ColourReset}")
  53. list(APPEND CMAKE_PREFIX_PATH "${GRPC_ROOT}/lib/cmake/absl/")
  54. find_package(absl CONFIG REQUIRED)
  55. message("${BoldGreen}Using absl-cpp ${absl_VERSION}${ColourReset}")
  56. list(APPEND CMAKE_PREFIX_PATH "${GRPC_ROOT}/lib/cmake/grpc/")
  57. find_package(gRPC CONFIG REQUIRED)
  58. message("${BoldGreen}Using gRPC-cpp ${gRPC_VERSION}${ColourReset}")
  59. find_path(GRPC_BINARY_DIR grpc_cpp_plugin ${GRPC_ROOT}/bin)
  60. find_path(GRPC_LIBRARY_DIR cmake/grpc/gRPCConfig.cmake ${GRPC_ROOT}/lib)
  61. find_path(GRPC_INCLUDE_DIR grpc/grpc.h PATHS ${_GRPC_INCLUDE_DIR} )
  62. mark_as_advanced(
  63. GRPC_BINARY_DIR
  64. GRPC_LIBRARY_DIR
  65. GRPC_INCLUDE_DIR
  66. )
  67. message("${BoldGreen}GRPC_INCLUDE_DIR is ${GRPC_INCLUDE_DIR}${ColourReset}")
  68. endif ()
  69. find_program(PROTOBUF_PROTOC protoc)
  70. find_program(GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
  71. set(REFLECTION gRPC::grpc++_reflection)
  72. set(GRPC_GRPCPP gRPC::grpc++)
  73. include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
  74. FIND_PACKAGE_HANDLE_STANDARD_ARGS(gRPC DEFAULT_MSG
  75. PROTOBUF_PROTOC REFLECTION GRPC_GRPCPP GRPC_CPP_PLUGIN_EXECUTABLE)