common.cmake 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Copyright 2018 gRPC authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # cmake build file for C++ route_guide example.
  16. # Assumes protobuf and gRPC have been installed using cmake.
  17. # See cmake_externalproject/CMakeLists.txt for all-in-one cmake build
  18. # that automatically builds all the dependencies before building route_guide.
  19. cmake_minimum_required(VERSION 3.16)
  20. set(CMAKE_CXX_STANDARD 17)
  21. set(CMAKE_CXX_STANDARD_REQUIRED True)
  22. if(MSVC)
  23. add_definitions(-D_WIN32_WINNT=0x600)
  24. endif()
  25. find_package(Threads REQUIRED)
  26. if(GRPC_AS_SUBMODULE)
  27. # One way to build a projects that uses gRPC is to just include the
  28. # entire gRPC project tree via "add_subdirectory".
  29. # This approach is very simple to use, but the are some potential
  30. # disadvantages:
  31. # * it includes gRPC's CMakeLists.txt directly into your build script
  32. # without and that can make gRPC's internal setting interfere with your
  33. # own build.
  34. # * depending on what's installed on your system, the contents of submodules
  35. # in gRPC's third_party/* might need to be available (and there might be
  36. # additional prerequisites required to build them). Consider using
  37. # the gRPC_*_PROVIDER options to fine-tune the expected behavior.
  38. #
  39. # A more robust approach to add dependency on gRPC is using
  40. # cmake's ExternalProject_Add (see cmake_externalproject/CMakeLists.txt).
  41. # Include the gRPC's cmake build (normally grpc source code would live
  42. # in a git submodule called "third_party/grpc", but this example lives in
  43. # the same repository as gRPC sources, so we just look a few directories up)
  44. add_subdirectory(../../.. ${CMAKE_CURRENT_BINARY_DIR}/grpc EXCLUDE_FROM_ALL)
  45. message(STATUS "Using gRPC via add_subdirectory.")
  46. # After using add_subdirectory, we can now use the grpc targets directly from
  47. # this build.
  48. set(_PROTOBUF_LIBPROTOBUF libprotobuf)
  49. set(_REFLECTION grpc++_reflection)
  50. set(_ORCA_SERVICE grpcpp_orca_service)
  51. if(CMAKE_CROSSCOMPILING)
  52. find_program(_PROTOBUF_PROTOC protoc)
  53. else()
  54. set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
  55. endif()
  56. set(_GRPC_GRPCPP grpc++)
  57. if(CMAKE_CROSSCOMPILING)
  58. find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
  59. else()
  60. set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
  61. endif()
  62. elseif(GRPC_FETCHCONTENT)
  63. # Another way is to use CMake's FetchContent module to clone gRPC at
  64. # configure time. This makes gRPC's source code available to your project,
  65. # similar to a git submodule.
  66. message(STATUS "Using gRPC via add_subdirectory (FetchContent).")
  67. include(FetchContent)
  68. FetchContent_Declare(
  69. grpc
  70. GIT_REPOSITORY https://github.com/grpc/grpc.git
  71. # when using gRPC, you will actually set this to an existing tag, such as
  72. # v1.25.0, v1.26.0 etc..
  73. # For the purpose of testing, we override the tag used to the commit
  74. # that's currently under test.
  75. GIT_TAG vGRPC_TAG_VERSION_OF_YOUR_CHOICE)
  76. FetchContent_MakeAvailable(grpc)
  77. # Since FetchContent uses add_subdirectory under the hood, we can use
  78. # the grpc targets directly from this build.
  79. set(_PROTOBUF_LIBPROTOBUF libprotobuf)
  80. set(_REFLECTION grpc++_reflection)
  81. set(_PROTOBUF_PROTOC $<TARGET_FILE:protoc>)
  82. set(_GRPC_GRPCPP grpc++)
  83. if(CMAKE_CROSSCOMPILING)
  84. find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
  85. else()
  86. set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
  87. endif()
  88. else()
  89. # This branch assumes that gRPC and all its dependencies are already installed
  90. # on this system, so they can be located by find_package().
  91. # Find Protobuf installation
  92. # Looks for protobuf-config.cmake file installed by Protobuf's cmake installation.
  93. option(protobuf_MODULE_COMPATIBLE TRUE)
  94. find_package(Protobuf CONFIG REQUIRED)
  95. message(STATUS "Using protobuf ${Protobuf_VERSION}")
  96. set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
  97. set(_REFLECTION gRPC::grpc++_reflection)
  98. if(CMAKE_CROSSCOMPILING)
  99. find_program(_PROTOBUF_PROTOC protoc)
  100. else()
  101. set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
  102. endif()
  103. # Find gRPC installation
  104. # Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.
  105. find_package(gRPC CONFIG REQUIRED)
  106. message(STATUS "Using gRPC ${gRPC_VERSION}")
  107. set(_GRPC_GRPCPP gRPC::grpc++)
  108. if(CMAKE_CROSSCOMPILING)
  109. find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
  110. else()
  111. set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
  112. endif()
  113. endif()