Imported Upstream version 1.27.0
[platform/upstream/grpc.git] / examples / cpp / helloworld / CMakeLists.txt
1 # Copyright 2018 gRPC authors.\r
2 #\r
3 # Licensed under the Apache License, Version 2.0 (the "License");\r
4 # you may not use this file except in compliance with the License.\r
5 # You may obtain a copy of the License at\r
6 #\r
7 #     http://www.apache.org/licenses/LICENSE-2.0\r
8 #\r
9 # Unless required by applicable law or agreed to in writing, software\r
10 # distributed under the License is distributed on an "AS IS" BASIS,\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 # See the License for the specific language governing permissions and\r
13 # limitations under the License.\r
14 #\r
15 # cmake build file for C++ helloworld example.\r
16 # Assumes protobuf and gRPC have been installed using cmake.\r
17 # See cmake_externalproject/CMakeLists.txt for all-in-one cmake build\r
18 # that automatically builds all the dependencies before building helloworld.\r
19 \r
20 cmake_minimum_required(VERSION 3.5.1)\r
21 \r
22 project(HelloWorld C CXX)\r
23 \r
24 if(NOT MSVC)\r
25   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")\r
26 else()\r
27   add_definitions(-D_WIN32_WINNT=0x600)\r
28 endif()\r
29 \r
30 find_package(Threads REQUIRED)\r
31 \r
32 if(GRPC_AS_SUBMODULE)\r
33   # One way to build a projects that uses gRPC is to just include the\r
34   # entire gRPC project tree via "add_subdirectory".\r
35   # This approach is very simple to use, but the are some potential\r
36   # disadvantages:\r
37   # * it includes gRPC's CMakeLists.txt directly into your build script\r
38   #   without and that can make gRPC's internal setting interfere with your\r
39   #   own build.\r
40   # * depending on what's installed on your system, the contents of submodules\r
41   #   in gRPC's third_party/* might need to be available (and there might be\r
42   #   additional prerequisites required to build them). Consider using\r
43   #   the gRPC_*_PROVIDER options to fine-tune the expected behavior.\r
44   #\r
45   # A more robust approach to add dependency on gRPC is using\r
46   # cmake's ExternalProject_Add (see cmake_externalproject/CMakeLists.txt).\r
47   \r
48   # Include the gRPC's cmake build (normally grpc source code would live\r
49   # in a git submodule called "third_party/grpc", but this example lives in\r
50   # the same repository as gRPC sources, so we just look a few directories up)\r
51   add_subdirectory(../../.. ${CMAKE_CURRENT_BINARY_DIR}/grpc EXCLUDE_FROM_ALL)\r
52   message(STATUS "Using gRPC via add_subdirectory.")\r
53 \r
54   # After using add_subdirectory, we can now use the grpc targets directly from\r
55   # this build.\r
56   set(_PROTOBUF_LIBPROTOBUF libprotobuf)\r
57   if(CMAKE_CROSSCOMPILING)\r
58     find_program(_PROTOBUF_PROTOC protoc)\r
59   else()\r
60     set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)\r
61   endif()\r
62   set(_GRPC_GRPCPP_UNSECURE grpc++_unsecure)\r
63   if(CMAKE_CROSSCOMPILING)\r
64     find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)\r
65   else()\r
66     set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)\r
67   endif()\r
68 elseif(GRPC_FETCHCONTENT)\r
69   # Another way is to use CMake's FetchContent module to clone gRPC at\r
70   # configure time. This makes gRPC's source code available to your project,\r
71   # similar to a git submodule.\r
72   message(STATUS "Using gRPC via add_subdirectory (FetchContent).")\r
73   include(FetchContent)\r
74   FetchContent_Declare(\r
75     grpc\r
76     GIT_REPOSITORY https://github.com/grpc/grpc.git\r
77     # when using gRPC, you will actually set this to an existing tag, such as\r
78     # v1.25.0, v1.26.0 etc..\r
79     # For the purpose of testing, we override the tag used to the commit\r
80     # that's currently under test.\r
81     GIT_TAG        vGRPC_TAG_VERSION_OF_YOUR_CHOICE)\r
82   FetchContent_MakeAvailable(grpc)\r
83 \r
84   # Since FetchContent uses add_subdirectory under the hood, we can use\r
85   # the grpc targets directly from this build.\r
86   set(_PROTOBUF_LIBPROTOBUF libprotobuf)\r
87   set(_PROTOBUF_PROTOC $<TARGET_FILE:protoc>)\r
88   set(_GRPC_GRPCPP_UNSECURE grpc++_unsecure)\r
89   if(CMAKE_CROSSCOMPILING)\r
90     find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)\r
91   else()\r
92     set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)\r
93   endif()\r
94 else()\r
95   # This branch assumes that gRPC and all its dependencies are already installed\r
96   # on this system, so they can be located by find_package().\r
97 \r
98   # Find Protobuf installation\r
99   # Looks for protobuf-config.cmake file installed by Protobuf's cmake installation.\r
100   set(protobuf_MODULE_COMPATIBLE TRUE)\r
101   find_package(Protobuf CONFIG REQUIRED)\r
102   message(STATUS "Using protobuf ${protobuf_VERSION}")\r
103 \r
104   set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)\r
105   if(CMAKE_CROSSCOMPILING)\r
106     find_program(_PROTOBUF_PROTOC protoc)\r
107   else()\r
108     set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)\r
109   endif()\r
110 \r
111   # Find gRPC installation\r
112   # Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.\r
113   find_package(gRPC CONFIG REQUIRED)\r
114   message(STATUS "Using gRPC ${gRPC_VERSION}")\r
115 \r
116   set(_GRPC_GRPCPP_UNSECURE gRPC::grpc++_unsecure)\r
117   if(CMAKE_CROSSCOMPILING)\r
118     find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)\r
119   else()\r
120     set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)\r
121   endif()\r
122 endif()\r
123 \r
124 # Proto file\r
125 get_filename_component(hw_proto "../../protos/helloworld.proto" ABSOLUTE)\r
126 get_filename_component(hw_proto_path "${hw_proto}" PATH)\r
127 \r
128 # Generated sources\r
129 set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.cc")\r
130 set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.h")\r
131 set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.cc")\r
132 set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.h")\r
133 add_custom_command(\r
134       OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"\r
135       COMMAND ${_PROTOBUF_PROTOC}\r
136       ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"\r
137         --cpp_out "${CMAKE_CURRENT_BINARY_DIR}"\r
138         -I "${hw_proto_path}"\r
139         --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"\r
140         "${hw_proto}"\r
141       DEPENDS "${hw_proto}")\r
142 \r
143 # Include generated *.pb.h files\r
144 include_directories("${CMAKE_CURRENT_BINARY_DIR}")\r
145 \r
146 # Targets greeter_[async_](client|server)\r
147 foreach(_target\r
148   greeter_client greeter_server\r
149   greeter_async_client greeter_async_server)\r
150   add_executable(${_target} "${_target}.cc"\r
151     ${hw_proto_srcs}\r
152     ${hw_grpc_srcs})\r
153   target_link_libraries(${_target}\r
154     ${_GRPC_GRPCPP_UNSECURE}\r
155     ${_PROTOBUF_LIBPROTOBUF})\r
156 endforeach()\r