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