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