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.5 kB

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