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.

125 lines
4.4 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. #
  12. # Generates C++ sources from the .proto files
  13. #
  14. # grpc_generate_cpp (<SRCS> <HDRS> <DEST> [<ARGN>...])
  15. #
  16. # SRCS - variable to define with autogenerated source files
  17. # HDRS - variable to define with autogenerated header files
  18. # DEST - directory where the source files will be created
  19. # ARGN - .proto files
  20. #
  21. function(GRPC_GENERATE_CPP SRCS HDRS DEST)
  22. if(NOT ARGN)
  23. message(SEND_ERROR "Error: GRPC_GENERATE_CPP() called without any proto files")
  24. return()
  25. endif()
  26. if(GRPC_GENERATE_CPP_APPEND_PATH)
  27. # Create an include path for each file specified
  28. foreach(FIL ${ARGN})
  29. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  30. get_filename_component(ABS_PATH ${ABS_FIL} PATH)
  31. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  32. if(${_contains_already} EQUAL -1)
  33. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  34. endif()
  35. endforeach()
  36. else()
  37. set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
  38. endif()
  39. if(DEFINED PROTOBUF_IMPORT_DIRS)
  40. foreach(DIR ${PROTOBUF_IMPORT_DIRS})
  41. get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
  42. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  43. if(${_contains_already} EQUAL -1)
  44. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  45. endif()
  46. endforeach()
  47. endif()
  48. set(${SRCS})
  49. set(${HDRS})
  50. foreach(FIL ${ARGN})
  51. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  52. get_filename_component(FIL_WE ${FIL} NAME_WE)
  53. list(APPEND ${SRCS} "${DEST}/${FIL_WE}.grpc.pb.cc")
  54. list(APPEND ${HDRS} "${DEST}/${FIL_WE}.grpc.pb.h")
  55. add_custom_command(
  56. OUTPUT "${DEST}/${FIL_WE}.grpc.pb.cc"
  57. "${DEST}/${FIL_WE}.grpc.pb.h"
  58. COMMAND protobuf::protoc
  59. ARGS --grpc_out ${DEST} ${_protobuf_include_path} --plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN} ${ABS_FIL}
  60. DEPENDS ${ABS_FIL} protobuf::protoc gRPC::grpc_cpp_plugin
  61. COMMENT "Running C++ gRPC compiler on ${FIL}"
  62. VERBATIM )
  63. endforeach()
  64. set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
  65. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  66. set(${HDRS} ${${HDRS}} PARENT_SCOPE)
  67. endfunction()
  68. # By default have GRPC_GENERATE_CPP macro pass -I to protoc
  69. # for each directory where a proto file is referenced.
  70. if(NOT DEFINED GRPC_GENERATE_CPP_APPEND_PATH)
  71. set(GRPC_GENERATE_CPP_APPEND_PATH TRUE)
  72. endif()
  73. # Find gRPC include directory
  74. find_path(GRPC_INCLUDE_DIR grpc/grpc.h)
  75. mark_as_advanced(GRPC_INCLUDE_DIR)
  76. # Find gRPC library
  77. find_library(GRPC_LIBRARY NAMES grpc)
  78. mark_as_advanced(GRPC_LIBRARY)
  79. add_library(gRPC::grpc UNKNOWN IMPORTED)
  80. set_target_properties(gRPC::grpc PROPERTIES
  81. INTERFACE_INCLUDE_DIRECTORIES ${GRPC_INCLUDE_DIR}
  82. INTERFACE_LINK_LIBRARIES "-lpthread;-ldl"
  83. IMPORTED_LOCATION ${GRPC_LIBRARY}
  84. )
  85. # Find gRPC C++ library
  86. find_library(GRPC_GRPC++_LIBRARY NAMES grpc++)
  87. mark_as_advanced(GRPC_GRPC++_LIBRARY)
  88. add_library(gRPC::grpc++ UNKNOWN IMPORTED)
  89. set_target_properties(gRPC::grpc++ PROPERTIES
  90. INTERFACE_INCLUDE_DIRECTORIES ${GRPC_INCLUDE_DIR}
  91. INTERFACE_LINK_LIBRARIES gRPC::grpc
  92. IMPORTED_LOCATION ${GRPC_GRPC++_LIBRARY}
  93. )
  94. # Find gRPC C++ reflection library
  95. find_library(GRPC_GRPC++_REFLECTION_LIBRARY NAMES grpc++_reflection)
  96. mark_as_advanced(GRPC_GRPC++_REFLECTION_LIBRARY)
  97. add_library(gRPC::grpc++_reflection UNKNOWN IMPORTED)
  98. set_target_properties(gRPC::grpc++_reflection PROPERTIES
  99. INTERFACE_INCLUDE_DIRECTORIES ${GRPC_INCLUDE_DIR}
  100. INTERFACE_LINK_LIBRARIES gRPC::grpc++
  101. IMPORTED_LOCATION ${GRPC_GRPC++_REFLECTION_LIBRARY}
  102. )
  103. # Find gRPC CPP generator
  104. find_program(GRPC_CPP_PLUGIN NAMES grpc_cpp_plugin)
  105. mark_as_advanced(GRPC_CPP_PLUGIN)
  106. add_executable(gRPC::grpc_cpp_plugin IMPORTED)
  107. set_target_properties(gRPC::grpc_cpp_plugin PROPERTIES
  108. IMPORTED_LOCATION ${GRPC_CPP_PLUGIN}
  109. )
  110. include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
  111. FIND_PACKAGE_HANDLE_STANDARD_ARGS(gRPC DEFAULT_MSG
  112. GRPC_LIBRARY GRPC_INCLUDE_DIR GRPC_GRPC++_REFLECTION_LIBRARY GRPC_CPP_PLUGIN)