• 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++ helloworld 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 helloworld.
19
20cmake_minimum_required(VERSION 3.5.1)
21
22project(HelloWorld C CXX)
23
24if(NOT MSVC)
25  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
26else()
27  add_definitions(-D_WIN32_WINNT=0x600)
28endif()
29
30find_package(Threads REQUIRED)
31
32if(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()
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  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()
125endif()
126
127# Proto file
128get_filename_component(hw_proto "../../protos/helloworld.proto" ABSOLUTE)
129get_filename_component(hw_proto_path "${hw_proto}" PATH)
130
131# Generated sources
132set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.cc")
133set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.h")
134set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.cc")
135set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.h")
136add_custom_command(
137      OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
138      COMMAND ${_PROTOBUF_PROTOC}
139      ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
140        --cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
141        -I "${hw_proto_path}"
142        --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
143        "${hw_proto}"
144      DEPENDS "${hw_proto}")
145
146# Include generated *.pb.h files
147include_directories("${CMAKE_CURRENT_BINARY_DIR}")
148
149# Targets greeter_[async_](client|server)
150foreach(_target
151  greeter_client greeter_server
152  greeter_async_client greeter_async_client2 greeter_async_server)
153  add_executable(${_target} "${_target}.cc"
154    ${hw_proto_srcs}
155    ${hw_grpc_srcs})
156  target_link_libraries(${_target}
157    ${_REFLECTION}
158    ${_GRPC_GRPCPP}
159    ${_PROTOBUF_LIBPROTOBUF})
160endforeach()
161