common.cmake 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.5.1)
  20. set (CMAKE_CXX_STANDARD 11)
  21. if(MSVC)
  22. add_definitions(-D_WIN32_WINNT=0x600)
  23. endif()
  24. find_package(Threads REQUIRED)
  25. if(GRPC_AS_SUBMODULE)
  26. # One way to build a projects that uses gRPC is to just include the
  27. # entire gRPC project tree via "add_subdirectory".
  28. # This approach is very simple to use, but the are some potential
  29. # disadvantages:
  30. # * it includes gRPC's CMakeLists.txt directly into your build script
  31. # without and that can make gRPC's internal setting interfere with your
  32. # own build.
  33. # * depending on what's installed on your system, the contents of submodules
  34. # in gRPC's third_party/* might need to be available (and there might be
  35. # additional prerequisites required to build them). Consider using
  36. # the gRPC_*_PROVIDER options to fine-tune the expected behavior.
  37. #
  38. # A more robust approach to add dependency on gRPC is using
  39. # cmake's ExternalProject_Add (see cmake_externalproject/CMakeLists.txt).
  40. # Include the gRPC's cmake build (normally grpc source code would live
  41. # in a git submodule called "third_party/grpc", but this example lives in
  42. # the same repository as gRPC sources, so we just look a few directories up)
  43. add_subdirectory(../../.. ${CMAKE_CURRENT_BINARY_DIR}/grpc EXCLUDE_FROM_ALL)
  44. message(STATUS "Using gRPC via add_subdirectory.")
  45. # After using add_subdirectory, we can now use the grpc targets directly from
  46. # this build.
  47. set(_PROTOBUF_LIBPROTOBUF libprotobuf)
  48. set(_REFLECTION grpc++_reflection)
  49. if(CMAKE_CROSSCOMPILING)
  50. find_program(_PROTOBUF_PROTOC protoc)
  51. else()
  52. set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
  53. endif()
  54. set(_GRPC_GRPCPP grpc++)
  55. if(CMAKE_CROSSCOMPILING)
  56. find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
  57. else()
  58. set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
  59. endif()
  60. elseif(GRPC_FETCHCONTENT)
  61. # Another way is to use CMake's FetchContent module to clone gRPC at
  62. # configure time. This makes gRPC's source code available to your project,
  63. # similar to a git submodule.
  64. message(STATUS "Using gRPC via add_subdirectory (FetchContent).")
  65. include(FetchContent)
  66. FetchContent_Declare(
  67. grpc
  68. GIT_REPOSITORY https://github.com/grpc/grpc.git
  69. # when using gRPC, you will actually set this to an existing tag, such as
  70. # v1.25.0, v1.26.0 etc..
  71. # For the purpose of testing, we override the tag used to the commit
  72. # that's currently under test.
  73. GIT_TAG vGRPC_TAG_VERSION_OF_YOUR_CHOICE)
  74. FetchContent_MakeAvailable(grpc)
  75. # Since FetchContent uses add_subdirectory under the hood, we can use
  76. # the grpc targets directly from this build.
  77. set(_PROTOBUF_LIBPROTOBUF libprotobuf)
  78. set(_REFLECTION grpc++_reflection)
  79. set(_PROTOBUF_PROTOC $<TARGET_FILE:protoc>)
  80. set(_GRPC_GRPCPP grpc++)
  81. if(CMAKE_CROSSCOMPILING)
  82. find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
  83. else()
  84. set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
  85. endif()
  86. else()
  87. # This branch assumes that gRPC and all its dependencies are already installed
  88. # on this system, so they can be located by find_package().
  89. # Find Protobuf installation
  90. # Looks for protobuf-config.cmake file installed by Protobuf's cmake installation.
  91. set(protobuf_MODULE_COMPATIBLE TRUE)
  92. find_package(Protobuf CONFIG REQUIRED)
  93. message(STATUS "Using protobuf ${Protobuf_VERSION}")
  94. set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
  95. set(_REFLECTION gRPC::grpc++_reflection)
  96. if(CMAKE_CROSSCOMPILING)
  97. find_program(_PROTOBUF_PROTOC protoc)
  98. else()
  99. set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
  100. endif()
  101. # Find gRPC installation
  102. # Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.
  103. find_package(gRPC CONFIG REQUIRED)
  104. message(STATUS "Using gRPC ${gRPC_VERSION}")
  105. set(_GRPC_GRPCPP gRPC::grpc++)
  106. if(CMAKE_CROSSCOMPILING)
  107. find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
  108. else()
  109. set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
  110. endif()
  111. endif()