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

CMakeLists.txt 2.4 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. cmake_minimum_required(VERSION 3.5.1)
  2. project(HelloWorld C CXX)
  3. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  4. find_package(Threads REQUIRED)
  5. # This branch assumes that gRPC and all its dependencies are already installed
  6. # on this system, so they can be located by find_package().
  7. # Find Protobuf installation
  8. # Looks for protobuf-config.cmake file installed by Protobuf's cmake installation.
  9. set(protobuf_MODULE_COMPATIBLE TRUE)
  10. find_package(Protobuf CONFIG REQUIRED)
  11. message(STATUS "Using protobuf ${protobuf_VERSION}")
  12. set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
  13. set(_REFLECTION gRPC::grpc++_reflection)
  14. if(CMAKE_CROSSCOMPILING)
  15. find_program(_PROTOBUF_PROTOC protoc)
  16. else()
  17. set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
  18. endif()
  19. # Find gRPC installation
  20. # Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.
  21. find_package(gRPC CONFIG REQUIRED)
  22. message(STATUS "Using gRPC ${gRPC_VERSION}")
  23. set(_GRPC_GRPCPP gRPC::grpc++)
  24. if(CMAKE_CROSSCOMPILING)
  25. find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
  26. else()
  27. set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
  28. endif()
  29. # Proto file
  30. get_filename_component(hw_proto "../ms_service.proto" ABSOLUTE)
  31. get_filename_component(hw_proto_path "${hw_proto}" PATH)
  32. # Generated sources
  33. set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/ms_service.pb.cc")
  34. set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/ms_service.pb.h")
  35. set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/ms_service.grpc.pb.cc")
  36. set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/ms_service.grpc.pb.h")
  37. add_custom_command(
  38. OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
  39. COMMAND ${_PROTOBUF_PROTOC}
  40. ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
  41. --cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
  42. -I "${hw_proto_path}"
  43. --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
  44. "${hw_proto}"
  45. DEPENDS "${hw_proto}")
  46. # Include generated *.pb.h files
  47. include_directories("${CMAKE_CURRENT_BINARY_DIR}")
  48. # Targets greeter_[async_](client|server)
  49. foreach(_target
  50. ms_client ms_server)
  51. add_executable(${_target} "${_target}.cc"
  52. ${hw_proto_srcs}
  53. ${hw_grpc_srcs})
  54. target_link_libraries(${_target}
  55. ${_REFLECTION}
  56. ${_GRPC_GRPCPP}
  57. ${_PROTOBUF_LIBPROTOBUF})
  58. endforeach()