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.8) 21 22if(MSVC) 23 add_definitions(-D_WIN32_WINNT=0x600) 24endif() 25 26find_package(Threads REQUIRED) 27 28if(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 add_subdirectory(../../.. ${CMAKE_CURRENT_BINARY_DIR}/grpc EXCLUDE_FROM_ALL) 48 message(STATUS "Using gRPC via add_subdirectory.") 49 50 # After using add_subdirectory, we can now use the grpc targets directly from 51 # this build. 52 set(_PROTOBUF_LIBPROTOBUF libprotobuf) 53 set(_REFLECTION grpc++_reflection) 54 set(_ORCA_SERVICE grpcpp_orca_service) 55 if(CMAKE_CROSSCOMPILING) 56 find_program(_PROTOBUF_PROTOC protoc) 57 else() 58 set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>) 59 endif() 60 set(_GRPC_GRPCPP grpc++) 61 if(CMAKE_CROSSCOMPILING) 62 find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin) 63 else() 64 set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>) 65 endif() 66elseif(GRPC_FETCHCONTENT) 67 # Another way is to use CMake's FetchContent module to clone gRPC at 68 # configure time. This makes gRPC's source code available to your project, 69 # similar to a git submodule. 70 message(STATUS "Using gRPC via add_subdirectory (FetchContent).") 71 include(FetchContent) 72 FetchContent_Declare( 73 grpc 74 GIT_REPOSITORY https://github.com/grpc/grpc.git 75 # when using gRPC, you will actually set this to an existing tag, such as 76 # v1.25.0, v1.26.0 etc.. 77 # For the purpose of testing, we override the tag used to the commit 78 # that's currently under test. 79 GIT_TAG vGRPC_TAG_VERSION_OF_YOUR_CHOICE) 80 FetchContent_MakeAvailable(grpc) 81 82 # Since FetchContent uses add_subdirectory under the hood, we can use 83 # the grpc targets directly from this build. 84 set(_PROTOBUF_LIBPROTOBUF libprotobuf) 85 set(_REFLECTION grpc++_reflection) 86 set(_PROTOBUF_PROTOC $<TARGET_FILE:protoc>) 87 set(_GRPC_GRPCPP grpc++) 88 if(CMAKE_CROSSCOMPILING) 89 find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin) 90 else() 91 set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>) 92 endif() 93else() 94 # This branch assumes that gRPC and all its dependencies are already installed 95 # on this system, so they can be located by find_package(). 96 97 # Find Protobuf installation 98 # Looks for protobuf-config.cmake file installed by Protobuf's cmake installation. 99 option(protobuf_MODULE_COMPATIBLE TRUE) 100 find_package(Protobuf CONFIG REQUIRED) 101 message(STATUS "Using protobuf ${Protobuf_VERSION}") 102 103 set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf) 104 set(_REFLECTION gRPC::grpc++_reflection) 105 if(CMAKE_CROSSCOMPILING) 106 find_program(_PROTOBUF_PROTOC protoc) 107 else() 108 set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>) 109 endif() 110 111 # Find gRPC installation 112 # Looks for gRPCConfig.cmake file installed by gRPC's cmake installation. 113 find_package(gRPC CONFIG REQUIRED) 114 message(STATUS "Using gRPC ${gRPC_VERSION}") 115 116 set(_GRPC_GRPCPP gRPC::grpc++) 117 if(CMAKE_CROSSCOMPILING) 118 find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin) 119 else() 120 set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>) 121 endif() 122endif() 123