blob: 2252a7ea80a18f12d1607e412f2b635cc82de581 [file] [log] [blame]
Tamas Berghammerd798a7d2016-06-21 13:58:18 +01001%YAML 1.2
2--- |
3 # GRPC global cmake file
4 # This currently builds C and C++ code.
5 # This file has been automatically generated from a template file.
6 # Please look at the templates directory instead.
7 # This file can be regenerated from the template by running
8 # tools/buildgen/generate_projects.sh
Nicolas "Pixel" Nobleb360c8a2016-07-02 00:54:28 +02009 #
Tamas Berghammerd798a7d2016-06-21 13:58:18 +010010 # Copyright 2015, Google Inc.
11 # All rights reserved.
12 #
13 # Redistribution and use in source and binary forms, with or without
14 # modification, are permitted provided that the following conditions are
15 # met:
16 #
17 # * Redistributions of source code must retain the above copyright
18 # notice, this list of conditions and the following disclaimer.
19 # * Redistributions in binary form must reproduce the above
20 # copyright notice, this list of conditions and the following disclaimer
21 # in the documentation and/or other materials provided with the
22 # distribution.
23 # * Neither the name of Google Inc. nor the names of its
24 # contributors may be used to endorse or promote products derived from
25 # this software without specific prior written permission.
26 #
27 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39 <%!
Yuchen Zenge02cbeb2017-01-19 16:33:30 -080040
Jan Tattermuschcc641682017-01-17 11:59:31 +010041 import re
Yuchen Zenge02cbeb2017-01-19 16:33:30 -080042
Jan Tattermuschcc641682017-01-17 11:59:31 +010043 proto_re = re.compile('(.*)\\.proto')
Yuchen Zenge02cbeb2017-01-19 16:33:30 -080044
Jan Tattermuschcc641682017-01-17 11:59:31 +010045 def proto_replace_ext(filename, ext):
46 m = proto_re.match(filename)
47 if not m:
48 return filename
49 return '${_gRPC_PROTO_GENS_DIR}/' + m.group(1) + ext
Yuchen Zenge02cbeb2017-01-19 16:33:30 -080050
Tamas Berghammerd798a7d2016-06-21 13:58:18 +010051 def get_deps(target_dict):
52 deps = []
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +030053 if target_dict.get('baselib', False):
54 deps.append("${_gRPC_BASELIB_LIBRARIES}")
Tamas Berghammerd798a7d2016-06-21 13:58:18 +010055 if target_dict.get('build', None) in ['protoc']:
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +030056 deps.append("${_gRPC_PROTOBUF_PROTOC_LIBRARIES}")
Tamas Berghammerd798a7d2016-06-21 13:58:18 +010057 if target_dict.get('secure', False):
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +030058 deps.append("${_gRPC_SSL_LIBRARIES}")
Jan Tattermusch438c0b52017-01-23 11:24:53 +010059 if target_dict.language == 'c++':
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +030060 deps.append("${_gRPC_PROTOBUF_LIBRARIES}")
Jan Tattermusch438c0b52017-01-23 11:24:53 +010061 if target_dict['name'] in ['grpc']:
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +030062 deps.append("${_gRPC_ZLIB_LIBRARIES}")
Yuchen Zeng8076d2b2016-08-25 13:23:13 -070063 deps.append("${_gRPC_CARES_LIBRARIES}")
Jan Tattermusch438c0b52017-01-23 11:24:53 +010064 deps.append("${_gRPC_ALLTARGETS_LIBRARIES}")
Tamas Berghammerd798a7d2016-06-21 13:58:18 +010065 for d in target_dict.get('deps', []):
66 deps.append(d)
Jan Tattermusch0a1701f2017-01-17 19:44:08 +010067 if target_dict.build == 'test' and target_dict.language == 'c++':
68 deps.append("${_gRPC_GFLAGS_LIBRARIES}")
Tamas Berghammerd798a7d2016-06-21 13:58:18 +010069 return deps
Yuchen Zeng1492f6c2017-02-02 12:42:54 -080070
Jan Tattermusch438c0b52017-01-23 11:24:53 +010071 def get_platforms_condition_begin(platforms):
72 if all(platform in platforms for platform in ['linux', 'mac', 'posix', 'windows']):
73 return ''
74 cond = ' OR '.join(['_gRPC_PLATFORM_%s' % platform.upper() for platform in platforms])
75 return 'if(%s)\n' % cond
Yuchen Zeng1492f6c2017-02-02 12:42:54 -080076
Jan Tattermusch438c0b52017-01-23 11:24:53 +010077 def get_platforms_condition_end(platforms):
78 if not get_platforms_condition_begin(platforms):
79 return ''
80 return 'endif()\n'
Tamas Berghammerd798a7d2016-06-21 13:58:18 +010081 %>
82
83 cmake_minimum_required(VERSION 2.8)
84
Tamas Berghammerdf6a44c2016-06-22 13:38:55 +010085 set(PACKAGE_NAME "grpc")
Craig Tiller83c47892016-09-13 11:14:43 -070086 set(PACKAGE_VERSION "${settings.cpp_version}")
Tamas Berghammerdf6a44c2016-06-22 13:38:55 +010087 set(PACKAGE_STRING "<%text>${PACKAGE_NAME} ${PACKAGE_VERSION}</%text>")
88 set(PACKAGE_TARNAME "<%text>${PACKAGE_NAME}-${PACKAGE_VERSION}</%text>")
89 set(PACKAGE_BUGREPORT "https://github.com/grpc/grpc/issues/")
90 project(<%text>${PACKAGE_NAME}</%text> C CXX)
91
Jan Tattermusch0a1701f2017-01-17 19:44:08 +010092 # Options
93 option(gRPC_BUILD_TESTS "Build tests" OFF)
Tamas Berghammerdf6a44c2016-06-22 13:38:55 +010094
Arkadiy Shapkin40beb372016-08-17 14:42:36 +030095 if (NOT MSVC)
96 set(gRPC_INSTALL ON CACHE BOOL "Generate installation target")
97 else()
98 set(gRPC_INSTALL OFF CACHE BOOL "Generate installation target")
99 endif()
100
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300101 set(gRPC_ZLIB_PROVIDER "module" CACHE STRING "Provider of zlib library")
102 set_property(CACHE gRPC_ZLIB_PROVIDER PROPERTY STRINGS "module" "package")
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100103
Yuchen Zeng8076d2b2016-08-25 13:23:13 -0700104 set(gRPC_CARES_PROVIDER "module" CACHE STRING "Provider of c-ares library")
105 set_property(CACHE gRPC_CARES_PROVIDER PROPERTY STRINGS "module" "package")
106
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300107 set(gRPC_SSL_PROVIDER "module" CACHE STRING "Provider of ssl library")
108 set_property(CACHE gRPC_SSL_PROVIDER PROPERTY STRINGS "module" "package")
Tamas Berghammera72712e2016-07-06 10:30:25 +0100109
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300110 set(gRPC_PROTOBUF_PROVIDER "module" CACHE STRING "Provider of protobuf library")
111 set_property(CACHE gRPC_PROTOBUF_PROVIDER PROPERTY STRINGS "module" "package")
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100112
Jan Tattermusch0a1701f2017-01-17 19:44:08 +0100113 set(gRPC_GFLAGS_PROVIDER "module" CACHE STRING "Provider of gflags library")
114 set_property(CACHE gRPC_GFLAGS_PROVIDER PROPERTY STRINGS "module" "package")
Yuchen Zenge02cbeb2017-01-19 16:33:30 -0800115
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100116 set(gRPC_BENCHMARK_PROVIDER "module" CACHE STRING "Provider of benchmark library")
117 set_property(CACHE gRPC_BENCHMARK_PROVIDER PROPERTY STRINGS "module" "package")
Yuchen Zeng1492f6c2017-02-02 12:42:54 -0800118
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300119 set(gRPC_USE_PROTO_LITE OFF CACHE BOOL "Use the protobuf-lite library")
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100120
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100121 if(UNIX)
122 if(<%text>${CMAKE_SYSTEM_NAME}</%text> MATCHES "Linux")
123 set(_gRPC_PLATFORM_LINUX ON)
124 elseif(<%text>${CMAKE_SYSTEM_NAME}</%text> MATCHES "Darwin")
125 set(_gRPC_PLATFORM_MAC ON)
126 else()
127 set(_gRPC_PLATFORM_POSIX ON)
128 endif()
129 endif()
130 if(WIN32)
131 set(_gRPC_PLATFORM_WINDOWS ON)
132 endif()
Yuchen Zengf39cf462017-02-10 13:01:25 -0800133
Jan Tattermuschc09656c2017-01-27 13:36:24 +0100134 ## Some libraries are shared even with BUILD_SHARED_LIBRARIES=OFF
135 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100136
Arkadiy Shapkin40beb372016-08-17 14:42:36 +0300137 if (MSVC)
Jan Tattermusch51407912017-02-02 20:44:22 +0100138 include(cmake/msvc_static_runtime.cmake)
Jan Tattermusch3aab96f2017-01-11 16:12:46 +0100139 add_definitions(-D_WIN32_WINNT=0x600 -D_SCL_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_WARNINGS -D_WINSOCK_DEPRECATED_NO_WARNINGS)
Jan Tattermusch3aab96f2017-01-11 16:12:46 +0100140 # needed to compile protobuf
141 add_definitions(/wd4065 /wd4506)
142 # TODO(jtattermusch): revisit C4267 occurrences throughout the code
143 add_definitions(/wd4267)
Arkadiy Shapkin40beb372016-08-17 14:42:36 +0300144 endif()
145
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300146 if (gRPC_USE_PROTO_LITE)
147 set(_gRPC_PROTOBUF_LIBRARY_NAME "libprotobuf-lite")
Tamas Berghammer23911562016-07-27 15:24:05 +0100148 add_definitions("-DGRPC_USE_PROTO_LITE")
149 else()
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300150 set(_gRPC_PROTOBUF_LIBRARY_NAME "libprotobuf")
151 endif()
152
153 if("<%text>${gRPC_ZLIB_PROVIDER}</%text>" STREQUAL "module")
154 if(NOT ZLIB_ROOT_DIR)
155 set(ZLIB_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/third_party/zlib)
156 endif()
157 set(ZLIB_INCLUDE_DIR "<%text>${ZLIB_ROOT_DIR}</%text>")
158 if(EXISTS "<%text>${ZLIB_ROOT_DIR}</%text>/CMakeLists.txt")
159 add_subdirectory(<%text>${ZLIB_ROOT_DIR}</%text> third_party/zlib)
160 if(TARGET zlibstatic)
161 set(_gRPC_ZLIB_LIBRARIES zlibstatic)
162 endif()
163 else()
164 message(WARNING "gRPC_ZLIB_PROVIDER is \"module\" but ZLIB_ROOT_DIR is wrong")
165 endif()
166 elseif("<%text>${gRPC_ZLIB_PROVIDER}</%text>" STREQUAL "package")
167 find_package(ZLIB)
168 if(TARGET ZLIB::ZLIB)
169 set(_gRPC_ZLIB_LIBRARIES ZLIB::ZLIB)
170 endif()
Konstantin Podsvirovdd331482016-07-28 00:00:25 +0300171 set(_gRPC_FIND_ZLIB "if(NOT ZLIB_FOUND)\n find_package(ZLIB)\nendif()")
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300172 endif()
173
Yuchen Zeng8076d2b2016-08-25 13:23:13 -0700174 if("<%text>${gRPC_CARES_PROVIDER}</%text>" STREQUAL "module")
175 if(NOT CARES_ROOT_DIR)
Yuchen Zeng74962182017-03-20 15:39:47 -0700176 set(CARES_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/src/c-ares)
Yuchen Zeng8076d2b2016-08-25 13:23:13 -0700177 endif()
Yuchen Zengc75bbf22017-03-20 12:35:20 -0700178 string(TOLOWER <%text>${CMAKE_SYSTEM_NAME}</%text> CARES_SYSTEM_NAME)
Yuchen Zeng74962182017-03-20 15:39:47 -0700179 set(CARES_INCLUDE_DIR "<%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/third_party/cares/cares")
180 set(CARES_BUILD_INCLUDE_DIR "<%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/third_party/cares")
Yuchen Zengc75bbf22017-03-20 12:35:20 -0700181 set(CARES_PLATFORM_INCLUDE_DIR "<%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/third_party/cares/config_<%text>${CARES_SYSTEM_NAME}</%text>")
Yuchen Zeng74962182017-03-20 15:39:47 -0700182 if(EXISTS "<%text>${CARES_ROOT_DIR}</%text>/CMakeLists.txt")
183 if("<%text>${CARES_SYSTEM_NAME}</%text>" MATCHES "windows")
184 add_definitions(-DCARES_STATICLIB=1)
185 add_definitions(-DWIN32_LEAN_AND_MEAN=1)
186 else()
187 add_definitions(-DHAVE_CONFIG_H=1)
188 add_definitions(-D_GNU_SOURCE=1)
189 endif()
190 add_subdirectory(src/c-ares third_party/cares)
191 if(TARGET cares)
192 set(_gRPC_CARES_LIBRARIES cares)
193 endif()
Yuchen Zeng8076d2b2016-08-25 13:23:13 -0700194 else()
Yuchen Zeng74962182017-03-20 15:39:47 -0700195 message(WARNING "gRPC_CARES_PROVIDER is \"module\" but CARES_ROOT_DIR is wrong")
Yuchen Zeng8076d2b2016-08-25 13:23:13 -0700196 endif()
197 elseif("<%text>${gRPC_CARES_PROVIDER}</%text>" STREQUAL "package")
198 find_package(CARES)
199 if(TARGET CARES::CARES)
200 set(_gRPC_CARES_LIBRARIES CARES::CARES)
201 endif()
202 set(_gRPC_FIND_CARES "if(NOT CARES_FOUND)\n find_package(CARES)\nendif()")
203 endif()
204
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300205 if("<%text>${gRPC_PROTOBUF_PROVIDER}</%text>" STREQUAL "module")
206 # Building the protobuf tests require gmock what is not part of a standard protobuf checkout.
207 # Disable them unless they are explicitly requested from the cmake command line (when we assume
208 # gmock is downloaded to the right location inside protobuf).
209 if(NOT protobuf_BUILD_TESTS)
210 set(protobuf_BUILD_TESTS OFF CACHE BOOL "Build protobuf tests")
211 endif()
212 if(NOT PROTOBUF_ROOT_DIR)
213 set(PROTOBUF_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/third_party/protobuf)
214 endif()
yang-g66d40c62017-04-12 10:18:10 -0700215 set(PROTOBUF_WELLKNOWN_IMPORT_DIR <%text>${PROTOBUF_ROOT_DIR}</%text>/src)
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300216 if(EXISTS "<%text>${PROTOBUF_ROOT_DIR}</%text>/cmake/CMakeLists.txt")
Arkadiy Shapkin40beb372016-08-17 14:42:36 +0300217 set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "Link static runtime libraries")
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300218 add_subdirectory(<%text>${PROTOBUF_ROOT_DIR}</%text>/cmake third_party/protobuf)
219 if(TARGET <%text>${_gRPC_PROTOBUF_LIBRARY_NAME}</%text>)
220 set(_gRPC_PROTOBUF_LIBRARIES <%text>${_gRPC_PROTOBUF_LIBRARY_NAME}</%text>)
221 endif()
222 if(TARGET libprotoc)
223 set(_gRPC_PROTOBUF_PROTOC_LIBRARIES libprotoc)
224 endif()
Jan Tattermuschcc641682017-01-17 11:59:31 +0100225 if(TARGET protoc)
226 set(_gRPC_PROTOBUF_PROTOC protoc)
227 endif()
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300228 else()
229 message(WARNING "gRPC_PROTOBUF_PROVIDER is \"module\" but PROTOBUF_ROOT_DIR is wrong")
230 endif()
231 elseif("<%text>${gRPC_PROTOBUF_PROVIDER}</%text>" STREQUAL "package")
232 find_package(protobuf CONFIG)
233 if(protobuf_FOUND)
234 if(TARGET protobuf::<%text>${_gRPC_PROTOBUF_LIBRARY_NAME}</%text>)
235 set(_gRPC_PROTOBUF_LIBRARIES protobuf::<%text>${_gRPC_PROTOBUF_LIBRARY_NAME}</%text>)
236 endif()
237 if(TARGET protobuf::libprotoc)
238 set(_gRPC_PROTOBUF_PROTOC_LIBRARIES protobuf::libprotoc)
239 endif()
Jan Tattermuschcc641682017-01-17 11:59:31 +0100240 if(TARGET protobuf::protoc)
241 set(_gRPC_PROTOBUF_PROTOC protobuf::protoc)
242 endif()
Konstantin Podsvirovdd331482016-07-28 00:00:25 +0300243 set(_gRPC_FIND_PROTOBUF "if(NOT protobuf_FOUND)\n find_package(protobuf CONFIG)\nendif()")
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300244 else()
245 find_package(Protobuf MODULE)
Konstantin Podsvirovdd331482016-07-28 00:00:25 +0300246 set(_gRPC_FIND_PROTOBUF "if(NOT Protobuf_FOUND)\n find_package(Protobuf)\nendif()")
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300247 endif()
yang-g66d40c62017-04-12 10:18:10 -0700248 set(PROTOBUF_WELLKNOWN_IMPORT_DIR /usr/local/include)
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300249 endif()
250
251 if("<%text>${gRPC_SSL_PROVIDER}</%text>" STREQUAL "module")
252 if(NOT BORINGSSL_ROOT_DIR)
253 set(BORINGSSL_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/third_party/boringssl)
254 endif()
255 if(EXISTS "<%text>${BORINGSSL_ROOT_DIR}</%text>/CMakeLists.txt")
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100256 set(OPENSSL_NO_ASM ON) # make boringssl buildable with Visual Studio
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300257 add_subdirectory(<%text>${BORINGSSL_ROOT_DIR}</%text> third_party/boringssl)
258 if(TARGET ssl)
259 set(_gRPC_SSL_LIBRARIES ssl)
260 endif()
261 else()
262 message(WARNING "gRPC_SSL_PROVIDER is \"module\" but BORINGSSL_ROOT_DIR is wrong")
263 endif()
264 elseif("<%text>${gRPC_SSL_PROVIDER}</%text>" STREQUAL "package")
265 find_package(OpenSSL)
266 if(TARGET OpenSSL::SSL)
267 set(_gRPC_SSL_LIBRARIES OpenSSL::SSL)
268 endif()
Konstantin Podsvirovdd331482016-07-28 00:00:25 +0300269 set(_gRPC_FIND_SSL "if(NOT OpenSSL_FOUND)\n find_package(OpenSSL)\nendif()")
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300270 endif()
271
Jan Tattermusch0a1701f2017-01-17 19:44:08 +0100272 if("<%text>${gRPC_GFLAGS_PROVIDER}</%text>" STREQUAL "module")
273 if(NOT GFLAGS_ROOT_DIR)
274 set(GFLAGS_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/third_party/gflags)
275 endif()
276 if(EXISTS "<%text>${GFLAGS_ROOT_DIR}</%text>/CMakeLists.txt")
277 add_subdirectory(<%text>${GFLAGS_ROOT_DIR}</%text> third_party/gflags)
Jan Tattermusch87a5d652017-01-18 16:22:40 +0100278 if(TARGET gflags_static)
279 set(_gRPC_GFLAGS_LIBRARIES gflags_static)
Jan Tattermusch0a1701f2017-01-17 19:44:08 +0100280 endif()
281 else()
282 message(WARNING "gRPC_GFLAGS_PROVIDER is \"module\" but GFLAGS_ROOT_DIR is wrong")
283 endif()
284 elseif("<%text>${gRPC_GFLAGS_PROVIDER}</%text>" STREQUAL "package")
285 find_package(gflags)
286 if(TARGET gflags::gflags)
287 set(_gRPC_GFLAGS_LIBRARIES gflags::gflags)
288 endif()
289 set(_gRPC_FIND_GFLAGS "if(NOT gflags_FOUND)\n find_package(gflags)\nendif()")
290 endif()
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300291
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100292 if("<%text>${gRPC_BENCHMARK_PROVIDER}</%text>" STREQUAL "module")
293 if(NOT BENCHMARK_ROOT_DIR)
294 set(BENCHMARK_ROOT_DIR <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/third_party/benchmark)
295 endif()
296 if(EXISTS "<%text>${BENCHMARK_ROOT_DIR}</%text>/CMakeLists.txt")
297 add_subdirectory(<%text>${BENCHMARK_ROOT_DIR}</%text> third_party/benchmark)
298 if(TARGET benchmark)
299 set(_gRPC_BENCHMARK_LIBRARIES benchmark)
300 endif()
301 else()
302 message(WARNING "gRPC_BENCHMARK_PROVIDER is \"module\" but BENCHMARK_ROOT_DIR is wrong")
303 endif()
304 elseif("<%text>${gRPC_BENCHMARK_PROVIDER}</%text>" STREQUAL "package")
305 find_package(benchmark)
306 if(TARGET benchmark::benchmark)
307 set(_gRPC_BENCHMARK_LIBRARIES benchmark::benchmark)
308 endif()
309 set(_gRPC_FIND_BENCHMARK "if(NOT benchmark_FOUND)\n find_package(benchmark)\nendif()")
310 endif()
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300311
312 if(NOT MSVC)
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100313 set(CMAKE_C_FLAGS "<%text>${CMAKE_C_FLAGS}</%text> -std=c99")
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300314 set(CMAKE_CXX_FLAGS "<%text>${CMAKE_CXX_FLAGS}</%text> -std=c++11")
315 endif()
316
Mehrdad Afshari79ec0ff2017-04-19 03:14:26 -0700317 if(_gRPC_PLATFORM_MAC)
318 set(_gRPC_ALLTARGETS_LIBRARIES <%text>${CMAKE_DL_LIBS}</%text> m pthread)
319 elseif(UNIX)
320 set(_gRPC_ALLTARGETS_LIBRARIES <%text>${CMAKE_DL_LIBS}</%text> rt m pthread)
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100321 endif()
322
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300323 if(WIN32 AND MSVC)
324 set(_gRPC_BASELIB_LIBRARIES wsock32 ws2_32)
Tamas Berghammer23911562016-07-27 15:24:05 +0100325 endif()
326
Konstantin Podsvirovd2b88302016-07-27 23:43:47 +0300327 include(GNUInstallDirs)
328 if(NOT DEFINED CMAKE_INSTALL_CMAKEDIR)
329 set(CMAKE_INSTALL_CMAKEDIR "<%text>${CMAKE_INSTALL_LIBDIR}</%text>/cmake/gRPC")
330 endif()
331
Jan Tattermuschcc641682017-01-17 11:59:31 +0100332 # Create directory for generated .proto files
333 set(_gRPC_PROTO_GENS_DIR <%text>${CMAKE_BINARY_DIR}/gens</%text>)
334 file(MAKE_DIRECTORY <%text>${_gRPC_PROTO_GENS_DIR}</%text>)
Yuchen Zenge02cbeb2017-01-19 16:33:30 -0800335
Jan Tattermuschcc641682017-01-17 11:59:31 +0100336 # protobuf_generate_grpc_cpp
337 # --------------------------
338 #
339 # Add custom commands to process ``.proto`` files to C++ using protoc and
340 # GRPC plugin::
341 #
342 # protobuf_generate_grpc_cpp [<ARGN>...]
343 #
344 # ``ARGN``
345 # ``.proto`` files
346 #
347 function(protobuf_generate_grpc_cpp)
348 if(NOT ARGN)
349 message(SEND_ERROR "Error: PROTOBUF_GENERATE_GRPC_CPP() called without any proto files")
350 return()
351 endif()
Yuchen Zenge02cbeb2017-01-19 16:33:30 -0800352
yang-g66d40c62017-04-12 10:18:10 -0700353 set(_protobuf_include_path -I . -I <%text>${PROTOBUF_WELLKNOWN_IMPORT_DIR}</%text>)
Jan Tattermuschcc641682017-01-17 11:59:31 +0100354 foreach(FIL <%text>${ARGN}</%text>)
355 get_filename_component(ABS_FIL <%text>${FIL}</%text> ABSOLUTE)
356 get_filename_component(FIL_WE <%text>${FIL}</%text> NAME_WE)
Jan Tattermuschecc457b2017-04-07 14:06:16 +0200357 file(RELATIVE_PATH REL_FIL <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text> <%text>${ABS_FIL}</%text>)
Jan Tattermuschcc641682017-01-17 11:59:31 +0100358 get_filename_component(REL_DIR <%text>${REL_FIL}</%text> DIRECTORY)
359 set(RELFIL_WE "<%text>${REL_DIR}/${FIL_WE}</%text>")
Yuchen Zenge02cbeb2017-01-19 16:33:30 -0800360
Jan Tattermuschcc641682017-01-17 11:59:31 +0100361 add_custom_command(
362 OUTPUT <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.cc"</%text>
363 <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.h"</%text>
Mahak Mukhi78ca0d12017-04-26 13:39:46 -0700364 <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}_mock.grpc.pb.h"</%text>
Jan Tattermuschcc641682017-01-17 11:59:31 +0100365 <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.cc"</%text>
366 <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.h"</%text>
367 COMMAND <%text>${_gRPC_PROTOBUF_PROTOC}</%text>
Mahak Mukhi78ca0d12017-04-26 13:39:46 -0700368 ARGS --grpc_out=<%text>generate_mock_code=true:${_gRPC_PROTO_GENS_DIR}</%text>
Jan Tattermuschcc641682017-01-17 11:59:31 +0100369 --cpp_out=<%text>${_gRPC_PROTO_GENS_DIR}</%text>
370 --plugin=protoc-gen-grpc=$<TARGET_FILE:grpc_cpp_plugin>
371 <%text>${_protobuf_include_path}</%text>
372 <%text>${REL_FIL}</%text>
373 DEPENDS <%text>${ABS_FIL}</%text> <%text>${_gRPC_PROTOBUF_PROTOC}</%text> grpc_cpp_plugin
Jan Tattermuschecc457b2017-04-07 14:06:16 +0200374 WORKING_DIRECTORY <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>
Jan Tattermuschcc641682017-01-17 11:59:31 +0100375 COMMENT "Running gRPC C++ protocol buffer compiler on <%text>${FIL}</%text>"
376 VERBATIM)
Yuchen Zenge02cbeb2017-01-19 16:33:30 -0800377
Mahak Mukhi78ca0d12017-04-26 13:39:46 -0700378 <%text>set_source_files_properties("${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.cc" "${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.h" "${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}_mock.grpc.pb.h" "${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.cc" "${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.h" PROPERTIES GENERATED TRUE)</%text>
Jan Tattermuschcc641682017-01-17 11:59:31 +0100379 endforeach()
380 endfunction()
Yuchen Zenge02cbeb2017-01-19 16:33:30 -0800381
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100382 add_custom_target(plugins
383 DEPENDS
384 % for tgt in targets:
385 % if tgt.build == 'protoc':
386 ${tgt.name}
387 % endif
388 % endfor
389 )
Yuchen Zeng1492f6c2017-02-02 12:42:54 -0800390
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100391 add_custom_target(tools_c
392 DEPENDS
393 % for tgt in targets:
394 % if tgt.build == 'tool' and not tgt.language == 'c++':
395 ${tgt.name}
396 % endif
397 % endfor
398 )
Yuchen Zeng1492f6c2017-02-02 12:42:54 -0800399
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100400 add_custom_target(tools_cxx
401 DEPENDS
402 % for tgt in targets:
403 % if tgt.build == 'tool' and tgt.language == 'c++':
404 ${tgt.name}
405 % endif
406 % endfor
407 )
Yuchen Zeng1492f6c2017-02-02 12:42:54 -0800408
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100409 add_custom_target(tools
410 DEPENDS tools_c tools_cxx)
Yuchen Zeng1492f6c2017-02-02 12:42:54 -0800411
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100412 if (gRPC_BUILD_TESTS)
413 add_custom_target(buildtests_c)
414 % for tgt in targets:
415 % if tgt.build == 'test' and not tgt.language == 'c++' and not tgt.get('external_deps', None) and not tgt.boringssl:
416 ${get_platforms_condition_begin(tgt.platforms)}\
417 add_dependencies(buildtests_c ${tgt.name})
418 ${get_platforms_condition_end(tgt.platforms)}\
419 % endif
420 % endfor
Yuchen Zeng1492f6c2017-02-02 12:42:54 -0800421
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100422 add_custom_target(buildtests_cxx)
423 % for tgt in targets:
424 % if tgt.build == 'test' and tgt.language == 'c++' and not tgt.get('external_deps', None) and not tgt.boringssl:
425 ${get_platforms_condition_begin(tgt.platforms)}\
426 add_dependencies(buildtests_cxx ${tgt.name})
427 ${get_platforms_condition_end(tgt.platforms)}\
428 % endif
429 % endfor
Yuchen Zeng1492f6c2017-02-02 12:42:54 -0800430
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100431 add_custom_target(buildtests
432 DEPENDS buildtests_c buildtests_cxx)
433 endif (gRPC_BUILD_TESTS)
Yuchen Zeng1492f6c2017-02-02 12:42:54 -0800434
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100435 % for lib in libs:
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100436 % if lib.build in ["all", "protoc", "tool", "test", "private"] and not lib.boringssl:
Jan Tattermusch0a1701f2017-01-17 19:44:08 +0100437 % if not lib.get('build_system', []) or 'cmake' in lib.get('build_system', []):
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100438 % if not lib.name in ['benchmark', 'z']: # we build these using CMake instead
Jan Tattermusch0a1701f2017-01-17 19:44:08 +0100439 % if lib.build in ["test", "private"]:
440 if (gRPC_BUILD_TESTS)
441 ${cc_library(lib)}
442 endif (gRPC_BUILD_TESTS)
443 % else:
Jan Tattermuschcc641682017-01-17 11:59:31 +0100444 ${cc_library(lib)}
445 ${cc_install(lib)}
Jan Tattermuschaf7b87f2017-01-12 09:41:03 +0100446 % endif
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100447 % endif
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100448 % endif
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100449 % endif
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100450 % endfor
451
452 % for tgt in targets:
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100453 % if tgt.build in ["all", "protoc", "tool", "test", "private"] and not tgt.boringssl:
Jan Tattermusch0a1701f2017-01-17 19:44:08 +0100454 % if tgt.build in ["test", "private"]:
455 if (gRPC_BUILD_TESTS)
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100456 ${get_platforms_condition_begin(tgt.platforms)}\
Jan Tattermusch0a1701f2017-01-17 19:44:08 +0100457 ${cc_binary(tgt)}
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100458 ${get_platforms_condition_end(tgt.platforms)}\
Jan Tattermusch0a1701f2017-01-17 19:44:08 +0100459 endif (gRPC_BUILD_TESTS)
460 % else:
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100461 ${get_platforms_condition_begin(tgt.platforms)}\
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100462 ${cc_binary(tgt)}
Konstantin Podsvirovd2b88302016-07-27 23:43:47 +0300463 ${cc_install(tgt)}
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100464 ${get_platforms_condition_end(tgt.platforms)}\
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100465 % endif
Jan Tattermusch0a1701f2017-01-17 19:44:08 +0100466 % endif
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100467 % endfor
468
469 <%def name="cc_library(lib)">
Jan Tattermuschc09656c2017-01-27 13:36:24 +0100470 add_library(${lib.name}${' SHARED' if lib.get('dll', None) == 'only' else ''}
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100471 % for src in lib.src:
Jan Tattermuschcc641682017-01-17 11:59:31 +0100472 % if not proto_re.match(src):
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100473 ${src}
Jan Tattermuschcc641682017-01-17 11:59:31 +0100474 % else:
475 ${proto_replace_ext(src, '.pb.cc')}
476 ${proto_replace_ext(src, '.grpc.pb.cc')}
477 ${proto_replace_ext(src, '.pb.h')}
478 ${proto_replace_ext(src, '.grpc.pb.h')}
Mahak Mukhi78ca0d12017-04-26 13:39:46 -0700479 % if src in ["src/proto/grpc/testing/compiler_test.proto", "src/proto/grpc/testing/echo.proto"]:
480 ${proto_replace_ext(src, '_mock.grpc.pb.h')}
481 % endif
Jan Tattermuschcc641682017-01-17 11:59:31 +0100482 % endif
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100483 % endfor
484 )
485
Piotr Pietraszkiewicz9a618102017-02-06 19:30:09 +0100486 if(WIN32 AND MSVC)
487 set_target_properties(${lib.name} PROPERTIES COMPILE_PDB_NAME "${lib.name}"
488 COMPILE_PDB_OUTPUT_DIRECTORY <%text>"${CMAKE_BINARY_DIR}</%text>"
489 )
490 if (gRPC_INSTALL)
491 install(FILES <%text>${CMAKE_CURRENT_BINARY_DIR}/</%text>${lib.name}.pdb
492 DESTINATION <%text>${CMAKE_INSTALL_LIBDIR}</%text> OPTIONAL
493 )
494 endif()
495 endif()
496
Jan Tattermuschcc641682017-01-17 11:59:31 +0100497 % for src in lib.src:
498 % if proto_re.match(src):
499 protobuf_generate_grpc_cpp(
500 ${src}
501 )
502 % endif
503 % endfor
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100504
505 target_include_directories(${lib.name}
506 PRIVATE <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>
507 PRIVATE <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/include
508 PRIVATE <%text>${BORINGSSL_ROOT_DIR}</%text>/include
509 PRIVATE <%text>${PROTOBUF_ROOT_DIR}</%text>/src
Konstantin Podsvirov5baf7252016-07-27 16:25:37 +0300510 PRIVATE <%text>${ZLIB_INCLUDE_DIR}</%text>
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100511 PRIVATE <%text>${BENCHMARK}</%text>/include
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100512 PRIVATE <%text>${CMAKE_CURRENT_BINARY_DIR}</%text>/third_party/zlib
Yuchen Zeng74962182017-03-20 15:39:47 -0700513 PRIVATE <%text>${CARES_BUILD_INCLUDE_DIR}</%text>
Yuchen Zeng8076d2b2016-08-25 13:23:13 -0700514 PRIVATE <%text>${CARES_INCLUDE_DIR}</%text>
Yuchen Zengc75bbf22017-03-20 12:35:20 -0700515 PRIVATE <%text>${CARES_PLATFORM_INCLUDE_DIR}</%text>
Yuchen Zengd6b49852017-03-20 01:44:27 -0700516 PRIVATE <%text>${CMAKE_CURRENT_BINARY_DIR}</%text>/third_party/cares/cares
Jan Tattermusch0a1701f2017-01-17 19:44:08 +0100517 PRIVATE <%text>${CMAKE_CURRENT_BINARY_DIR}</%text>/third_party/gflags/include
518 % if lib.build in ['test', 'private'] and lib.language == 'c++':
Ken Payson975b5102017-03-30 17:38:40 -0700519 PRIVATE third_party/googletest/googletest/include
520 PRIVATE third_party/googletest/googletest
Mahak Mukhi78ca0d12017-04-26 13:39:46 -0700521 PRIVATE third_party/googletest/googlemock/include
522 PRIVATE third_party/googletest/googlemock
Jan Tattermusch0a1701f2017-01-17 19:44:08 +0100523 % endif
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100524 % if lib.language == 'c++':
Jan Tattermuschcc641682017-01-17 11:59:31 +0100525 PRIVATE <%text>${_gRPC_PROTO_GENS_DIR}</%text>
526 % endif
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100527 )
528
529 % if len(get_deps(lib)) > 0:
530 target_link_libraries(${lib.name}
531 % for dep in get_deps(lib):
532 ${dep}
533 % endfor
534 )
535 % endif
Konstantin Podsvirovd2b88302016-07-27 23:43:47 +0300536
537 % if len(lib.get('public_headers', [])) > 0:
538 foreach(_hdr
539 % for hdr in lib.get('public_headers', []):
540 ${hdr}
541 % endfor
542 )
543 string(REPLACE "include/" "" _path <%text>${_hdr}</%text>)
544 get_filename_component(_path <%text>${_path}</%text> PATH)
545 install(FILES <%text>${_hdr}</%text>
546 DESTINATION "<%text>${CMAKE_INSTALL_INCLUDEDIR}/${_path}</%text>"
547 )
548 endforeach()
549 % endif
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100550 </%def>
551
552 <%def name="cc_binary(tgt)">
553 add_executable(${tgt.name}
554 % for src in tgt.src:
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100555 % if not proto_re.match(src):
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100556 ${src}
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100557 % else:
558 ${proto_replace_ext(src, '.pb.cc')}
559 ${proto_replace_ext(src, '.grpc.pb.cc')}
560 ${proto_replace_ext(src, '.pb.h')}
561 ${proto_replace_ext(src, '.grpc.pb.h')}
562 % endif
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100563 % endfor
Jan Tattermusch0a1701f2017-01-17 19:44:08 +0100564 % if tgt.build == 'test' and tgt.language == 'c++':
Ken Payson975b5102017-03-30 17:38:40 -0700565 third_party/googletest/googletest/src/gtest-all.cc
Mahak Mukhi78ca0d12017-04-26 13:39:46 -0700566 third_party/googletest/googlemock/src/gmock-all.cc
Jan Tattermusch0a1701f2017-01-17 19:44:08 +0100567 % endif
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100568 )
569
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100570 % for src in tgt.src:
571 % if proto_re.match(src):
572 protobuf_generate_grpc_cpp(
573 ${src}
574 )
575 % endif
576 % endfor
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100577
578 target_include_directories(${tgt.name}
579 PRIVATE <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>
580 PRIVATE <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/include
581 PRIVATE <%text>${BORINGSSL_ROOT_DIR}</%text>/include
582 PRIVATE <%text>${PROTOBUF_ROOT_DIR}</%text>/src
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100583 PRIVATE <%text>${BENCHMARK_ROOT_DIR}</%text>/include
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100584 PRIVATE <%text>${ZLIB_ROOT_DIR}</%text>
585 PRIVATE <%text>${CMAKE_CURRENT_BINARY_DIR}</%text>/third_party/zlib
Yuchen Zeng74962182017-03-20 15:39:47 -0700586 PRIVATE <%text>${CARES_BUILD_INCLUDE_DIR}</%text>
Yuchen Zengc75bbf22017-03-20 12:35:20 -0700587 PRIVATE <%text>${CARES_INCLUDE_DIR}</%text>
588 PRIVATE <%text>${CARES_PLATFORM_INCLUDE_DIR}</%text>
589 PRIVATE <%text>${CMAKE_CURRENT_BINARY_DIR}</%text>/third_party/cares/cares
Jan Tattermusch0a1701f2017-01-17 19:44:08 +0100590 PRIVATE <%text>${CMAKE_CURRENT_BINARY_DIR}</%text>/third_party/gflags/include
591 % if tgt.build in ['test', 'private'] and tgt.language == 'c++':
Ken Payson975b5102017-03-30 17:38:40 -0700592 PRIVATE third_party/googletest/googletest/include
593 PRIVATE third_party/googletest/googletest
Mahak Mukhi78ca0d12017-04-26 13:39:46 -0700594 PRIVATE third_party/googletest/googlemock/include
595 PRIVATE third_party/googletest/googlemock
Jan Tattermusch0a1701f2017-01-17 19:44:08 +0100596 % endif
Jan Tattermusch438c0b52017-01-23 11:24:53 +0100597 % if tgt.language == 'c++':
598 PRIVATE <%text>${_gRPC_PROTO_GENS_DIR}</%text>
599 % endif
Tamas Berghammerd798a7d2016-06-21 13:58:18 +0100600 )
601
602 % if len(get_deps(tgt)) > 0:
603 target_link_libraries(${tgt.name}
604 % for dep in get_deps(tgt):
605 ${dep}
606 % endfor
607 )
608 % endif
609 </%def>
610
Konstantin Podsvirovd2b88302016-07-27 23:43:47 +0300611 <%def name="cc_install(tgt)">
Arkadiy Shapkin40beb372016-08-17 14:42:36 +0300612 if (gRPC_INSTALL)
613 install(TARGETS ${tgt.name} EXPORT gRPCTargets
614 RUNTIME DESTINATION <%text>${CMAKE_INSTALL_BINDIR}</%text>
615 LIBRARY DESTINATION <%text>${CMAKE_INSTALL_LIBDIR}</%text>
616 ARCHIVE DESTINATION <%text>${CMAKE_INSTALL_LIBDIR}</%text>
617 )
618 endif()
Konstantin Podsvirovd2b88302016-07-27 23:43:47 +0300619 </%def>
620
Konstantin Podsvirovd2b88302016-07-27 23:43:47 +0300621 foreach(_config gRPCConfig gRPCConfigVersion)
622 configure_file(tools/cmake/<%text>${_config}</%text>.cmake.in
623 <%text>${_config}</%text>.cmake @ONLY)
624 install(FILES <%text>${CMAKE_CURRENT_BINARY_DIR}/${_config}</%text>.cmake
625 DESTINATION <%text>${CMAKE_INSTALL_CMAKEDIR}</%text>
626 )
627 endforeach()