| 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.5.1) |
| 21 |
|
| 22 |
if (NOT DEFINED CMAKE_CXX_STANDARD) |
| 23 |
set (CMAKE_CXX_STANDARD 14) |
| 24 |
endif() |
| 25 |
|
| 26 |
if(MSVC) |
| 27 |
add_definitions(-D_WIN32_WINNT=0x600) |
| 28 |
endif() |
| 29 |
|
| 30 |
find_package(Threads REQUIRED) |
| 31 |
|
| 32 |
if(GRPC_AS_SUBMODULE) |
| 33 |
# One way to build a projects that uses gRPC is to just include the |
| 34 |
# entire gRPC project tree via "add_subdirectory". |
| 35 |
# This approach is very simple to use, but the are some potential |
| 36 |
# disadvantages: |
| 37 |
# * it includes gRPC's CMakeLists.txt directly into your build script |
| 38 |
# without and that can make gRPC's internal setting interfere with your |
| 39 |
# own build. |
| 40 |
# * depending on what's installed on your system, the contents of submodules |
| 41 |
# in gRPC's third_party/* might need to be available (and there might be |
| 42 |
# additional prerequisites required to build them). Consider using |
| 43 |
# the gRPC_*_PROVIDER options to fine-tune the expected behavior. |
| 44 |
# |
| 45 |
# A more robust approach to add dependency on gRPC is using |
| 46 |
# cmake's ExternalProject_Add (see cmake_externalproject/CMakeLists.txt). |
| 47 |
|
| 48 |
# Include the gRPC's cmake build (normally grpc source code would live |
| 49 |
# in a git submodule called "third_party/grpc", but this example lives in |
| 50 |
# the same repository as gRPC sources, so we just look a few directories up) |
| 51 |
add_subdirectory(../../.. ${CMAKE_CURRENT_BINARY_DIR}/grpc EXCLUDE_FROM_ALL) |
| 52 |
message(STATUS "Using gRPC via add_subdirectory.") |
| 53 |
|
| 54 |
# After using add_subdirectory, we can now use the grpc targets directly from |
| 55 |
# this build. |
| 56 |
set(_PROTOBUF_LIBPROTOBUF libprotobuf) |
| 57 |
set(_REFLECTION grpc++_reflection) |
| 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() |
| 69 |
elseif(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() |
| 96 |
else() |
| 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 |
set(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() |
| 125 |
endif() |