Debian Codegraph @

templates/CMakeLists.txt.template

raw
1 %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
9 #
10 # Copyright 2015 gRPC authors.
11 #
12 # Licensed under the Apache License, Version 2.0 (the "License");
13 # you may not use this file except in compliance with the License.
14 # You may obtain a copy of the License at
15 #
16 # http://www.apache.org/licenses/LICENSE-2.0
17 #
18 # Unless required by applicable law or agreed to in writing, software
19 # distributed under the License is distributed on an "AS IS" BASIS,
20 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 # See the License for the specific language governing permissions and
22 # limitations under the License.
23
24 <%
25 import re
26
27 proto_re = re.compile('(.*)\\.proto')
28 lib_map = {lib.name: lib for lib in libs}
29
30 third_party_proto_prefixes = {lib.proto_prefix for lib in external_proto_libraries}
31
32 def third_party_proto_import_path(path):
33 """Removes third_party prefix to match ProtoBuf's relative import path."""
34 for prefix in third_party_proto_prefixes:
35 if path.startswith(prefix):
36 return path[len(prefix):]
37 return path
38
39 def proto_replace_ext(filename, ext):
40 m = proto_re.match(filename)
41 if not m:
42 return filename
43 return '${_gRPC_PROTO_GENS_DIR}/' + third_party_proto_import_path(m.group(1)) + ext
44
45 def is_absl_lib(lib_name):
46 return lib_name.startswith("absl/")
47
48 def get_absl_dep(lib_name):
49 return lib_map[lib_name].cmake_target
50
51 def lib_and_transitive_deps(lib):
52 return list(sorted(set({lib} | set(lib_map[lib].transitive_deps))))
53
54 def list_abseil_pkg_targets(lib):
55 # This returns a list of abseil pkg targets which the given lib and
56 # its non-abseil transitive dependencies depend on.
57 # As a result, internal abseil libraries are excluded from the result.
58 absl_specs = set()
59 for lib_name in lib_and_transitive_deps(lib):
60 if is_absl_lib(lib_name): continue
61 for dep in lib_map[lib_name].deps:
62 if is_absl_lib(dep):
63 absl_specs.add(get_absl_dep(dep).replace("::", "_"))
64 return list(sorted(absl_specs))
65
66 def is_shared_only_lib(lib_name):
67 """Returns True if only shared library should be generated."""
68 # grpc_csharp_ext is loaded by C# runtime and it
69 # only makes sense as a shared lib.
70 return lib_name in ['grpc_csharp_ext']
71
72 def get_deps(target_dict):
73 deps = []
74 if target_dict.get('baselib', False) or target_dict['name'] == 'address_sorting':
75 deps.append("${_gRPC_BASELIB_LIBRARIES}")
76 if target_dict.get('build', None) in ['protoc']:
77 deps.append("${_gRPC_PROTOBUF_PROTOC_LIBRARIES}")
78 if target_dict.language == 'c++':
79 deps.append("${_gRPC_PROTOBUF_LIBRARIES}")
80 if target_dict['name'] in ['grpc', 'grpc_cronet', 'grpc_unsecure']:
81 deps.append("${_gRPC_ZLIB_LIBRARIES}")
82 deps.append("${_gRPC_CARES_LIBRARIES}")
83 deps.append("${_gRPC_ADDRESS_SORTING_LIBRARIES}")
84 deps.append("${_gRPC_RE2_LIBRARIES}")
85 deps.append("${_gRPC_UPB_LIBRARIES}")
86 deps.append("${_gRPC_ALLTARGETS_LIBRARIES}")
87 for d in target_dict.get('deps', []):
88 if d == 'benchmark':
89 deps.append("${_gRPC_BENCHMARK_LIBRARIES}")
90 elif d == 'libssl':
91 deps.append("${_gRPC_SSL_LIBRARIES}")
92 elif is_absl_lib(d):
93 deps.append(get_absl_dep(d))
94 else:
95 deps.append(d)
96 return deps
97
98 def get_platforms_condition_begin(platforms):
99 if all(platform in platforms for platform in ['linux', 'mac', 'posix', 'windows']):
100 return ''
101 cond = ' OR '.join(['_gRPC_PLATFORM_%s' % platform.upper() for platform in platforms])
102 return 'if(%s)' % cond
103
104 def get_platforms_condition_end(platforms):
105 if not get_platforms_condition_begin(platforms):
106 return ''
107 return 'endif()'
108
109 def platforms_condition_block(platforms):
110 def _func(text):
111 lines = text.split('\n')
112 cond = get_platforms_condition_begin(platforms)
113 if cond:
114 # Remove empty line following <%block>
115 del lines[0]
116
117 # Indent each line after
118 for i, line in enumerate(lines[:-1]):
119 if line:
120 lines[i] = ' ' + line
121
122 # Add the condition block
123 lines.insert(0, cond)
124
125 # Add endif() to the last line
126 lines[-1] += 'endif()'
127 else:
128 # Remove empty line following <%block>
129 del lines[0]
130
131 # Strip leading whitespace from first line
132 lines[0] = lines[0].lstrip(' ')
133
134 # Remove empty line before </%block>
135 del lines[-1]
136
137 return '\n'.join(lines)
138 return _func
139 %>
140 <%
141 # These files are added to a set so that they are not duplicated if multiple
142 # targets use them. Generating the same file multiple times with
143 # add_custom_command() is not allowed in CMake.
144
145 protobuf_gen_files = set()
146 for tgt in targets:
147 for src in tgt.src:
148 if proto_re.match(src):
149 protobuf_gen_files.add(src)
150 for lib in libs:
151 for src in lib.src:
152 if proto_re.match(src):
153 protobuf_gen_files.add(src)
154 %>
155
156 cmake_minimum_required(VERSION 3.5.1)
157
158 set(PACKAGE_NAME "grpc")
159 set(PACKAGE_VERSION "${settings.cpp_version}")
160 set(gRPC_CORE_VERSION "${settings.core_version}")
161 set(gRPC_CORE_SOVERSION "${settings.core_version.major}")
162 set(gRPC_CPP_VERSION "${settings.cpp_version}")
163 set(gRPC_CPP_SOVERSION "${settings.cpp_version.major}.${settings.cpp_version.minor}")
164 set(gRPC_CSHARP_VERSION "${settings.csharp_version}")
165 set(gRPC_CSHARP_SOVERSION "${settings.csharp_version.major}.${settings.csharp_version.minor}")
166 set(PACKAGE_STRING "<%text>${PACKAGE_NAME} ${PACKAGE_VERSION}</%text>")
167 set(PACKAGE_TARNAME "<%text>${PACKAGE_NAME}-${PACKAGE_VERSION}</%text>")
168 set(PACKAGE_BUGREPORT "https://github.com/grpc/grpc/issues/")
169 project(<%text>${PACKAGE_NAME}</%text> LANGUAGES C CXX)
170
171 set(gRPC_INSTALL_BINDIR "bin" CACHE STRING "Installation directory for executables")
172 set(gRPC_INSTALL_LIBDIR "lib" CACHE STRING "Installation directory for libraries")
173 set(gRPC_INSTALL_INCLUDEDIR "include" CACHE STRING "Installation directory for headers")
174 set(gRPC_INSTALL_CMAKEDIR "lib/cmake/<%text>${PACKAGE_NAME}</%text>" CACHE STRING "Installation directory for cmake config files")
175 set(gRPC_INSTALL_SHAREDIR "share/grpc" CACHE STRING "Installation directory for root certificates")
176 set(gRPC_BUILD_MSVC_MP_COUNT 0 CACHE STRING "The maximum number of processes for MSVC /MP option")
177
178 # Options
179 option(gRPC_BUILD_TESTS "Build tests" OFF)
180 option(gRPC_BUILD_CODEGEN "Build codegen" ON)
181 option(gRPC_BUILD_CSHARP_EXT "Build C# extensions" ON)
182 option(gRPC_BACKWARDS_COMPATIBILITY_MODE "Build libraries that are binary compatible across a larger number of OS and libc versions" OFF)
183
184 set(gRPC_INSTALL_default ON)
185 if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
186 # Disable gRPC_INSTALL by default if building as a submodule
187 set(gRPC_INSTALL_default OFF)
188 endif()
189 set(gRPC_INSTALL <%text>${gRPC_INSTALL_default}</%text> CACHE BOOL
190 "Generate installation target")
191
192 # We can install dependencies from submodules if we're running
193 # CMake v3.13 or newer.
194 if(CMAKE_VERSION VERSION_LESS 3.13)
195 set(_gRPC_INSTALL_SUPPORTED_FROM_MODULE OFF)
196 else()
197 set(_gRPC_INSTALL_SUPPORTED_FROM_MODULE ON)
198 endif()
199
200 # Providers for third-party dependencies (gRPC_*_PROVIDER properties):
201 # "module": build the dependency using sources from git submodule (under third_party)
202 # "package": use cmake's find_package functionality to locate a pre-installed dependency
203
204 set(gRPC_ZLIB_PROVIDER "module" CACHE STRING "Provider of zlib library")
205 set_property(CACHE gRPC_ZLIB_PROVIDER PROPERTY STRINGS "module" "package")
206
207 set(gRPC_CARES_PROVIDER "module" CACHE STRING "Provider of c-ares library")
208 set_property(CACHE gRPC_CARES_PROVIDER PROPERTY STRINGS "module" "package")
209
210 set(gRPC_RE2_PROVIDER "module" CACHE STRING "Provider of re2 library")
211 set_property(CACHE gRPC_RE2_PROVIDER PROPERTY STRINGS "module" "package")
212
213 set(gRPC_SSL_PROVIDER "module" CACHE STRING "Provider of ssl library")
214 set_property(CACHE gRPC_SSL_PROVIDER PROPERTY STRINGS "module" "package")
215
216 set(gRPC_PROTOBUF_PROVIDER "module" CACHE STRING "Provider of protobuf library")
217 set_property(CACHE gRPC_PROTOBUF_PROVIDER PROPERTY STRINGS "module" "package")
218
219 set(gRPC_PROTOBUF_PACKAGE_TYPE "" CACHE STRING "Algorithm for searching protobuf package")
220 set_property(CACHE gRPC_PROTOBUF_PACKAGE_TYPE PROPERTY STRINGS "CONFIG" "MODULE")
221
222 if(gRPC_BUILD_TESTS)
223 set(gRPC_BENCHMARK_PROVIDER "module" CACHE STRING "Provider of benchmark library")
224 set_property(CACHE gRPC_BENCHMARK_PROVIDER PROPERTY STRINGS "module" "package")
225 else()
226 set(gRPC_BENCHMARK_PROVIDER "none")
227 endif()
228
229 set(gRPC_ABSL_PROVIDER "module" CACHE STRING "Provider of absl library")
230 set_property(CACHE gRPC_ABSL_PROVIDER PROPERTY STRINGS "module" "package")
231 <%
232 # Collect all abseil rules used by gpr, grpc, so on.
233 used_abseil_rules = set()
234 for lib in libs:
235 if lib.get("baselib"):
236 for dep in lib.transitive_deps:
237 if is_absl_lib(dep):
238 used_abseil_rules.add(lib_map[dep].cmake_target.replace("::", "_"))
239 %>
240 set(gRPC_ABSL_USED_TARGETS
241 % for rule in sorted(used_abseil_rules):
242 ${rule}
243 % endfor
244 absl_meta
245 )
246
247 set(gRPC_USE_PROTO_LITE OFF CACHE BOOL "Use the protobuf-lite library")
248
249 if(UNIX)
250 if(<%text>${CMAKE_SYSTEM_NAME}</%text> MATCHES "Linux")
251 set(_gRPC_PLATFORM_LINUX ON)
252 elseif(<%text>${CMAKE_SYSTEM_NAME}</%text> MATCHES "Darwin")
253 set(_gRPC_PLATFORM_MAC ON)
254 elseif(<%text>${CMAKE_SYSTEM_NAME}</%text> MATCHES "iOS")
255 set(_gRPC_PLATFORM_IOS ON)
256 elseif(<%text>${CMAKE_SYSTEM_NAME}</%text> MATCHES "Android")
257 set(_gRPC_PLATFORM_ANDROID ON)
258 else()
259 set(_gRPC_PLATFORM_POSIX ON)
260 endif()
261 endif()
262 if(WIN32)
263 set(_gRPC_PLATFORM_WINDOWS ON)
264 endif()
265
266 # Use C11 standard
267 if (NOT DEFINED CMAKE_C_STANDARD)
268 set(CMAKE_C_STANDARD 11)
269 endif()
270
271 # Add c++14 flags
272 if (NOT DEFINED CMAKE_CXX_STANDARD)
273 set(CMAKE_CXX_STANDARD 14)
274 else()
275 if (CMAKE_CXX_STANDARD LESS 14)
276 message(FATAL_ERROR "CMAKE_CXX_STANDARD is less than 14, please specify at least SET(CMAKE_CXX_STANDARD 14)")
277 endif()
278 endif()
279 if (NOT DEFINED CMAKE_CXX_STANDARD_REQUIRED)
280 set(CMAKE_CXX_STANDARD_REQUIRED ON)
281 endif()
282 if (NOT DEFINED CMAKE_CXX_EXTENSIONS)
283 set(CMAKE_CXX_EXTENSIONS OFF)
284 endif()
285
286 ## Some libraries are shared even with BUILD_SHARED_LIBRARIES=OFF
287 if (NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE)
288 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
289 endif()
290 list(APPEND CMAKE_MODULE_PATH "<%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/cmake/modules")
291
292 if(MSVC)
293 include(cmake/msvc_static_runtime.cmake)
294 add_definitions(-D_WIN32_WINNT=0x600 -D_SCL_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_WARNINGS -D_WINSOCK_DEPRECATED_NO_WARNINGS)
295 # Set /MP option
296 if (gRPC_BUILD_MSVC_MP_COUNT GREATER 0)
297 set(_gRPC_C_CXX_FLAGS "<%text>${_gRPC_C_CXX_FLAGS} /MP${gRPC_BUILD_MSVC_MP_COUNT}</%text>")
298 elseif (gRPC_BUILD_MSVC_MP_COUNT LESS 0)
299 set(_gRPC_C_CXX_FLAGS "<%text>${_gRPC_C_CXX_FLAGS}</%text> /MP")
300 endif()
301 # needed to compile protobuf
302 set(_gRPC_C_CXX_FLAGS "<%text>${_gRPC_C_CXX_FLAGS}</%text> /wd4065 /wd4506")
303 # TODO(jtattermusch): revisit warnings that were silenced as part of upgrade to protobuf3.6.0
304 set(_gRPC_C_CXX_FLAGS "<%text>${_gRPC_C_CXX_FLAGS}</%text> /wd4200 /wd4291 /wd4244")
305 # TODO(jtattermusch): revisit C4267 occurrences throughout the code
306 set(_gRPC_C_CXX_FLAGS "<%text>${_gRPC_C_CXX_FLAGS}</%text> /wd4267")
307 # TODO(jtattermusch): needed to build boringssl with VS2017, revisit later
308 set(_gRPC_C_CXX_FLAGS "<%text>${_gRPC_C_CXX_FLAGS}</%text> /wd4987 /wd4774 /wd4819 /wd4996 /wd4619")
309 # Silences thousands of trucation warnings
310 set(_gRPC_C_CXX_FLAGS "<%text>${_gRPC_C_CXX_FLAGS}</%text> /wd4503")
311 # Tell MSVC to build grpc using utf-8
312 set(_gRPC_C_CXX_FLAGS "<%text>${_gRPC_C_CXX_FLAGS}</%text> /utf-8")
313 endif()
314 if (MINGW)
315 add_definitions(-D_WIN32_WINNT=0x600)
316 endif()
317 set(CMAKE_C_FLAGS "<%text>${CMAKE_C_FLAGS} ${_gRPC_C_CXX_FLAGS}</%text>")
318 set(CMAKE_CXX_FLAGS "<%text>${CMAKE_CXX_FLAGS} ${_gRPC_C_CXX_FLAGS}</%text>")
319
320 if(gRPC_USE_PROTO_LITE)
321 set(_gRPC_PROTOBUF_LIBRARY_NAME "libprotobuf-lite")
322 add_definitions("-DGRPC_USE_PROTO_LITE")
323 else()
324 set(_gRPC_PROTOBUF_LIBRARY_NAME "libprotobuf")
325 endif()
326
327 if(gRPC_BACKWARDS_COMPATIBILITY_MODE)
328 add_definitions(-DGPR_BACKWARDS_COMPATIBILITY_MODE)
329 if(_gRPC_PLATFORM_MAC)
330 # some C++11 constructs not supported before OS X 10.10
331 set(CMAKE_OSX_DEPLOYMENT_TARGET 10.10)
332 endif()
333 endif()
334
335 if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_IOS)
336 set(_gRPC_CORE_NOSTDCXX_FLAGS -fno-exceptions -fno-rtti)
337 else()
338 set(_gRPC_CORE_NOSTDCXX_FLAGS "")
339 endif()
340
341 if (gRPC_XDS_USER_AGENT_IS_CSHARP)
342 # The value of the defines needs to contain quotes.
343 # See https://github.com/grpc/grpc/blob/fbf32836a418cc84f58786700273b65cb9174e1d/src/core/ext/xds/xds_api.cc#L854
344 add_definitions("-DGRPC_XDS_USER_AGENT_NAME_SUFFIX=\"csharp\"" "-DGRPC_XDS_USER_AGENT_VERSION_SUFFIX=\"${settings.csharp_version}\"")
345 endif()
346
347 if(UNIX AND NOT HAIKU)
348 # -pthread does more than -lpthread
349 set(THREADS_PREFER_PTHREAD_FLAG ON)
350 find_package(Threads)
351 set(_gRPC_ALLTARGETS_LIBRARIES <%text>${CMAKE_DL_LIBS}</%text> m Threads::Threads)
352 if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_POSIX)
353 set(_gRPC_ALLTARGETS_LIBRARIES <%text>${_gRPC_ALLTARGETS_LIBRARIES}</%text> rt)
354 endif()
355 endif()
356
357 # configure ccache if requested
358 include(cmake/ccache.cmake)
359
360 include(cmake/abseil-cpp.cmake)
361 include(cmake/address_sorting.cmake)
362 include(cmake/benchmark.cmake)
363 include(cmake/cares.cmake)
364 include(cmake/protobuf.cmake)
365 include(cmake/re2.cmake)
366 include(cmake/ssl.cmake)
367 include(cmake/upb.cmake)
368 include(cmake/xxhash.cmake)
369 include(cmake/zlib.cmake)
370 include(cmake/download_archive.cmake)
371
372 % for external_proto_library in external_proto_libraries:
373 % if len(external_proto_library.urls) > 1:
374 # Setup external proto library at ${external_proto_library.destination} with ${len(external_proto_library.urls)} download URLs
375 % else:
376 # Setup external proto library at ${external_proto_library.destination} if it doesn't exist
377 % endif
378 % for download_url in external_proto_library.urls:
379 if (NOT EXISTS <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/${external_proto_library.destination})
380 # Download the archive via HTTP, validate the checksum, and extract to ${external_proto_library.destination}.
381 download_archive(
382 <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/${external_proto_library.destination}
383 ${download_url}
384 ${external_proto_library.hash}
385 ${external_proto_library.strip_prefix}
386 )
387 endif()
388 % endfor
389 % endfor
390
391 if(WIN32)
392 set(_gRPC_BASELIB_LIBRARIES ws2_32 crypt32)
393 endif()
394
395 # Create directory for proto source files
396 set(_gRPC_PROTO_SRCS_DIR <%text>${CMAKE_BINARY_DIR}/protos</%text>)
397 file(MAKE_DIRECTORY <%text>${_gRPC_PROTO_SRCS_DIR}</%text>)
398 # Create directory for generated .proto files
399 set(_gRPC_PROTO_GENS_DIR <%text>${CMAKE_BINARY_DIR}/gens</%text>)
400 file(MAKE_DIRECTORY <%text>${_gRPC_PROTO_GENS_DIR}</%text>)
401
402 # protobuf_generate_grpc_cpp
403 # --------------------------
404 #
405 # This method is no longer used by gRPC's CMake build process. However, it
406 # is used by many open source dependencies, that we might want to keep
407 # backward compatibility here.
408 #
409 # Add custom commands to process ``.proto`` files to C++ using protoc and
410 # GRPC plugin::
411 #
412 # protobuf_generate_grpc_cpp [<ARGN>...]
413 #
414 # ``ARGN``
415 # ``.proto`` files
416 #
417 function(protobuf_generate_grpc_cpp)
418 if(NOT ARGN)
419 message(SEND_ERROR "Error: PROTOBUF_GENERATE_GRPC_CPP() called without any proto files")
420 return()
421 endif()
422
423 foreach(FIL <%text>${ARGN}</%text>)
424 protobuf_generate_grpc_cpp_with_import_path_correction(<%text>${FIL}</%text> <%text>${FIL}</%text>)
425 endforeach()
426 endfunction()
427
428 # protobuf_generate_grpc_cpp_with_import_path_correction
429 # --------------------------
430 #
431 # Add custom commands to process ``.proto`` files to C++ using protoc and
432 # GRPC plugin::
433 #
434 # protobuf_generate_grpc_cpp_with_import_path_correction <FILE_LOCATION> <IMPORT_PATH>
435 #
436 # ``FILE_LOCATION``
437 # The relative path of the ``.proto`` file to the project root
438 # ``IMPORT_PATH``
439 # The proto import path that itself expected to be placed in. For
440 # example, a "bar.proto" file wants to be imported as
441 # `import "foo/bar.proto"`. Then we should place it under
442 # "<ProtoBuf_Include_Path>/foo/bar.proto" instead of
443 # "<ProtoBuf_Include_Path>/third_party/foo/bar.proto". This ensures
444 # correct symbol being generated and C++ include path being correct.
445 # More info can be found at https://github.com/grpc/grpc/pull/25272.
446 #
447 function(protobuf_generate_grpc_cpp_with_import_path_correction FILE_LOCATION IMPORT_PATH)
448 if(NOT FILE_LOCATION)
449 message(SEND_ERROR "Error: PROTOBUF_GENERATE_GRPC_CPP() called without any proto files")
450 return()
451 endif()
452
453 # Sets the include path for ProtoBuf files
454 set(_protobuf_include_path -I . -I <%text>${_gRPC_PROTOBUF_WELLKNOWN_INCLUDE_DIR}</%text>)
455 # The absolute path of the expected place for the input proto file
456 # For example, health proto has package name grpc.health.v1, it's expected to be:
457 # <%text>${_gRPC_PROTO_SRCS_DIR}/grpc/health/v1/health.proto</%text>
458 get_filename_component(ABS_FIL <%text>${_gRPC_PROTO_SRCS_DIR}/${IMPORT_PATH}</%text> ABSOLUTE)
459 # Get the name of the file, which used to generate output file names for
460 # this command.
461 # Example: "health" for "health.proto"
462 get_filename_component(FIL_WE <%text>${_gRPC_PROTO_SRCS_DIR}/${IMPORT_PATH}</%text> NAME_WE)
463 # Get the relative path between the expected place for the proto and the
464 # working directory. In normal cases, it would be equal IMPORT_PATH, but
465 # it's better to be agnostic to all the global folder locations (like the
466 # centralized location <%text>${_gRPC_PROTO_SRCS_DIR})</%text>.
467 # Example: grpc/health/v1/health.proto
468 file(RELATIVE_PATH REL_FIL <%text>${_gRPC_PROTO_SRCS_DIR}</%text> <%text>${ABS_FIL}</%text>)
469 # Get the directory of the relative path.
470 # Example: grpc/health/v1
471 get_filename_component(REL_DIR <%text>${REL_FIL}</%text> DIRECTORY)
472 # Get the directory and name for output filenames generation.
473 # Example: "grpc/health/v1/health", the file name extension is omitted.
474 set(RELFIL_WE "<%text>${REL_DIR}/${FIL_WE}</%text>")
475 # Copy the proto file to a centralized location, with the correct import
476 # path. For example, health proto has package name grpc.health.v1, the bash
477 # equivalent would be:
478 # cp src/proto/grpc/health/v1/health.proto <%text>${_gRPC_PROTO_SRCS_DIR}/grpc/health/v1</%text>
479 file(MAKE_DIRECTORY <%text>${_gRPC_PROTO_SRCS_DIR}/${REL_DIR}</%text>)
480 file(COPY <%text>${CMAKE_CURRENT_SOURCE_DIR}/${FILE_LOCATION}</%text> DESTINATION <%text>${_gRPC_PROTO_SRCS_DIR}/${REL_DIR}</%text>)
481
482 #if cross-compiling, find host plugin
483 if(CMAKE_CROSSCOMPILING)
484 find_program(_gRPC_CPP_PLUGIN grpc_cpp_plugin)
485 else()
486 set(_gRPC_CPP_PLUGIN $<TARGET_FILE:grpc_cpp_plugin>)
487 endif()
488
489 add_custom_command(
490 OUTPUT <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.cc"</%text>
491 <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.h"</%text>
492 <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}_mock.grpc.pb.h"</%text>
493 <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.cc"</%text>
494 <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.h"</%text>
495 COMMAND <%text>${_gRPC_PROTOBUF_PROTOC_EXECUTABLE}</%text>
496 ARGS --grpc_out=<%text>generate_mock_code=true:${_gRPC_PROTO_GENS_DIR}</%text>
497 --cpp_out=<%text>${_gRPC_PROTO_GENS_DIR}</%text>
498 --plugin=protoc-gen-grpc=<%text>${_gRPC_CPP_PLUGIN}</%text>
499 <%text>${_protobuf_include_path}</%text>
500 <%text>${REL_FIL}</%text>
501 DEPENDS <%text>${CMAKE_CURRENT_SOURCE_DIR}/${FILE_LOCATION}</%text> <%text>${ABS_FIL}</%text> <%text>${_gRPC_PROTOBUF_PROTOC}</%text> <%text>${_gRPC_CPP_PLUGIN}</%text>
502 WORKING_DIRECTORY <%text>${_gRPC_PROTO_SRCS_DIR}</%text>
503 COMMENT "Running gRPC C++ protocol buffer compiler for <%text>${IMPORT_PATH}</%text>"
504 VERBATIM)
505 endfunction()
506
507 # These options allow users to enable or disable the building of the various
508 # protoc plugins. For example, running CMake with
509 # -DgRPC_BUILD_GRPC_CSHARP_PLUGIN=OFF will disable building the C# plugin.
510 set(_gRPC_PLUGIN_LIST)
511 % for tgt in targets:
512 % if tgt.build == 'protoc':
513 option(gRPC_BUILD_${tgt.name.upper()} "Build ${tgt.name}" ON)
514 if (gRPC_BUILD_${tgt.name.upper()})
515 list(APPEND _gRPC_PLUGIN_LIST ${tgt.name})
516 endif ()
517 % endif
518 % endfor
519
520 add_custom_target(plugins
521 DEPENDS <%text>${_gRPC_PLUGIN_LIST}</%text>
522 )
523
524 add_custom_target(tools_c
525 DEPENDS
526 % for tgt in targets:
527 % if tgt.build == 'tool' and not tgt.language == 'c++':
528 ${tgt.name}
529 % endif
530 % endfor
531 )
532
533 add_custom_target(tools_cxx
534 DEPENDS
535 % for tgt in targets:
536 % if tgt.build == 'tool' and tgt.language == 'c++':
537 ${tgt.name}
538 % endif
539 % endfor
540 )
541
542 add_custom_target(tools
543 DEPENDS tools_c tools_cxx)
544
545 % for src in sorted(protobuf_gen_files):
546 protobuf_generate_grpc_cpp_with_import_path_correction(
547 ${src} ${third_party_proto_import_path(src)}
548 )
549 % endfor
550
551 if(gRPC_BUILD_TESTS)
552 add_custom_target(buildtests_c)
553 % for tgt in targets:
554 % if tgt.build == 'test' and not tgt.language == 'c++' and not tgt.get('external_deps', None) and not tgt.boringssl:
555 <%block filter='platforms_condition_block(tgt.platforms)'>
556 add_dependencies(buildtests_c ${tgt.name})
557 </%block>
558 % endif
559 % endfor
560
561 add_custom_target(buildtests_cxx)
562 % for tgt in targets:
563 % if tgt.build == 'test' and tgt.language == 'c++' and not tgt.get('external_deps', None) and not tgt.boringssl:
564 <%block filter='platforms_condition_block(tgt.platforms)'>
565 add_dependencies(buildtests_cxx ${tgt.name})
566 </%block>
567 % endif
568 % endfor
569
570 add_custom_target(buildtests
571 DEPENDS buildtests_c buildtests_cxx)
572 endif()
573 <%
574 cmake_libs = []
575 for lib in libs:
576 if lib.build not in ["all", "protoc", "tool", "test", "private"] or lib.boringssl: continue
577 if lib.get('build_system', []) and 'cmake' not in lib.get('build_system', []): continue
578 if lib.name in ['ares', 'benchmark', 're2', 'xxhash', 'z']: continue # we build these using CMake instead
579 if is_absl_lib(lib.name): continue # we build these using CMake instead
580 cmake_libs.append(lib)
581 %>
582 % for lib in cmake_libs:
583 % if lib.build in ["test", "private"]:
584 if(gRPC_BUILD_TESTS)
585 ${cc_library(lib)}
586 endif()
587 % elif lib.name in ['grpc_csharp_ext']:
588 if(gRPC_BUILD_CSHARP_EXT)
589 ${cc_library(lib)}
590 endif()
591 % else:
592 ${cc_library(lib)}
593 % if not lib.build in ["tool"]:
594 % if any(proto_re.match(src) for src in lib.src):
595 if(gRPC_BUILD_CODEGEN)
596 % endif
597 ${cc_install(lib)}
598 % if any(proto_re.match(src) for src in lib.src):
599 endif()
600 % endif
601 % endif
602 % endif
603 % endfor
604
605 % for tgt in targets:
606 % if tgt.build in ["all", "protoc", "tool", "test", "private"] and not tgt.boringssl:
607 % if tgt.build in ["test", "private"]:
608 if(gRPC_BUILD_TESTS)
609 <%block filter='platforms_condition_block(tgt.platforms)'>
610 ${cc_binary(tgt)}
611 </%block>
612 endif()
613 % elif tgt.build in ["protoc"]:
614 if(gRPC_BUILD_CODEGEN AND gRPC_BUILD_${tgt.name.upper()})
615 <%block filter='platforms_condition_block(tgt.platforms)'>
616 ${cc_binary(tgt)}
617 ${cc_install(tgt)}
618 </%block>
619 endif()
620 % else:
621 <%block filter='platforms_condition_block(tgt.platforms)'>
622 ${cc_binary(tgt)}
623 % if not tgt.build in ["tool"]:
624 ${cc_install(tgt)}
625 % endif
626 </%block>
627 % endif
628 % endif
629 % endfor
630
631 <%def name="cc_library(lib)">
632 % if any(proto_re.match(src) for src in lib.src):
633 % if lib.name == 'grpcpp_channelz':
634 # grpcpp_channelz doesn't build with protobuf-lite
635 # See https://github.com/grpc/grpc/issues/19473
636 if(gRPC_BUILD_CODEGEN AND NOT gRPC_USE_PROTO_LITE)
637 % else:
638 if(gRPC_BUILD_CODEGEN)
639 % endif
640 % endif
641 add_library(${lib.name}${' SHARED' if is_shared_only_lib(lib.name) else ''}
642 % for src in lib.src:
643 % if not proto_re.match(src):
644 ${src}
645 % else:
646 ${proto_replace_ext(src, '.pb.cc')}
647 ${proto_replace_ext(src, '.grpc.pb.cc')}
648 ${proto_replace_ext(src, '.pb.h')}
649 ${proto_replace_ext(src, '.grpc.pb.h')}
650 % if src in ["src/proto/grpc/testing/compiler_test.proto", "src/proto/grpc/testing/echo.proto"]:
651 ${proto_replace_ext(src, '_mock.grpc.pb.h')}
652 % endif
653 % endif
654 % endfor
655 )
656
657 set_target_properties(${lib.name} PROPERTIES
658 % if lib.language == 'c++':
659 VERSION <%text>${gRPC_CPP_VERSION}</%text>
660 SOVERSION <%text>${gRPC_CPP_SOVERSION}</%text>
661 % elif lib.language == 'csharp':
662 VERSION <%text>${gRPC_CSHARP_VERSION}</%text>
663 SOVERSION <%text>${gRPC_CSHARP_SOVERSION}</%text>
664 % else:
665 VERSION <%text>${gRPC_CORE_VERSION}</%text>
666 SOVERSION <%text>${gRPC_CORE_SOVERSION}</%text>
667 % endif
668 )
669
670 if(WIN32 AND MSVC)
671 set_target_properties(${lib.name} PROPERTIES COMPILE_PDB_NAME "${lib.name}"
672 COMPILE_PDB_OUTPUT_DIRECTORY <%text>"${CMAKE_BINARY_DIR}</%text>"
673 )
674 if(gRPC_INSTALL)
675 install(FILES <%text>${CMAKE_CURRENT_BINARY_DIR}/</%text>${lib.name}.pdb
676 DESTINATION <%text>${gRPC_INSTALL_LIBDIR}</%text> OPTIONAL
677 )
678 endif()
679 endif()
680
681 target_include_directories(${lib.name}
682 PUBLIC <%text>$<INSTALL_INTERFACE:${gRPC_INSTALL_INCLUDEDIR}> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include></%text>
683 PRIVATE
684 <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>
685 <%text>${_gRPC_ADDRESS_SORTING_INCLUDE_DIR}</%text>
686 <%text>${_gRPC_RE2_INCLUDE_DIR}</%text>
687 <%text>${_gRPC_SSL_INCLUDE_DIR}</%text>
688 <%text>${_gRPC_UPB_GENERATED_DIR}</%text>
689 <%text>${_gRPC_UPB_GRPC_GENERATED_DIR}</%text>
690 <%text>${_gRPC_UPB_INCLUDE_DIR}</%text>
691 <%text>${_gRPC_XXHASH_INCLUDE_DIR}</%text>
692 <%text>${_gRPC_ZLIB_INCLUDE_DIR}</%text>
693 % if (lib.build in ['test', 'private'] and lib.language == 'c++') or lib.name == 'end2end_tests':
694 third_party/googletest/googletest/include
695 third_party/googletest/googletest
696 third_party/googletest/googlemock/include
697 third_party/googletest/googlemock
698 % endif
699 % if lib.language == 'c++':
700 <%text>${_gRPC_PROTO_GENS_DIR}</%text>
701 % endif
702 )
703 % if len(get_deps(lib)) > 0:
704 target_link_libraries(${lib.name}
705 % for dep in get_deps(lib):
706 ${dep}
707 % endfor
708 )
709 % endif
710 % if lib.name in ["gpr"]:
711 if(_gRPC_PLATFORM_ANDROID)
712 target_link_libraries(gpr
713 android
714 log
715 )
716 endif()
717 % endif
718 % if lib.name in ["grpc", "grpc_cronet", "grpc_test_util", \
719 "grpc_test_util_unsecure", "grpc_unsecure", \
720 "grpc++_cronet"]:
721 if(_gRPC_PLATFORM_IOS OR _gRPC_PLATFORM_MAC)
722 target_link_libraries(${lib.name} "-framework CoreFoundation")
723 endif()
724 %endif
725
726 % if len(lib.get('public_headers', [])) > 0:
727 foreach(_hdr
728 % for hdr in lib.get('public_headers', []):
729 ${hdr}
730 % endfor
731 )
732 string(REPLACE "include/" "" _path <%text>${_hdr}</%text>)
733 get_filename_component(_path <%text>${_path}</%text> PATH)
734 install(FILES <%text>${_hdr}</%text>
735 DESTINATION "<%text>${gRPC_INSTALL_INCLUDEDIR}/${_path}</%text>"
736 )
737 endforeach()
738 % endif
739 % if any(proto_re.match(src) for src in lib.src):
740 endif()
741 % endif
742 </%def>
743
744 <%def name="cc_binary(tgt)">
745 add_executable(${tgt.name}
746 % for src in tgt.src:
747 % if not proto_re.match(src):
748 ${src}
749 % else:
750 ${proto_replace_ext(src, '.pb.cc')}
751 ${proto_replace_ext(src, '.grpc.pb.cc')}
752 ${proto_replace_ext(src, '.pb.h')}
753 ${proto_replace_ext(src, '.grpc.pb.h')}
754 % endif
755 % endfor
756 % if tgt.build == 'test' and tgt.language == 'c++':
757 third_party/googletest/googletest/src/gtest-all.cc
758 third_party/googletest/googlemock/src/gmock-all.cc
759 % endif
760 )
761
762 target_include_directories(${tgt.name}
763 PRIVATE
764 <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>
765 <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/include
766 <%text>${_gRPC_ADDRESS_SORTING_INCLUDE_DIR}</%text>
767 <%text>${_gRPC_RE2_INCLUDE_DIR}</%text>
768 <%text>${_gRPC_SSL_INCLUDE_DIR}</%text>
769 <%text>${_gRPC_UPB_GENERATED_DIR}</%text>
770 <%text>${_gRPC_UPB_GRPC_GENERATED_DIR}</%text>
771 <%text>${_gRPC_UPB_INCLUDE_DIR}</%text>
772 <%text>${_gRPC_XXHASH_INCLUDE_DIR}</%text>
773 <%text>${_gRPC_ZLIB_INCLUDE_DIR}</%text>
774 % if tgt.build in ['test', 'private'] and tgt.language == 'c++':
775 third_party/googletest/googletest/include
776 third_party/googletest/googletest
777 third_party/googletest/googlemock/include
778 third_party/googletest/googlemock
779 % endif
780 % if tgt.language == 'c++':
781 <%text>${_gRPC_PROTO_GENS_DIR}</%text>
782 % endif
783 )
784
785 % if len(get_deps(tgt)) > 0:
786 target_link_libraries(${tgt.name}
787 % for dep in get_deps(tgt):
788 ${dep}
789 % endfor
790 )
791
792 % endif
793 </%def>
794
795 <%def name="cc_install(tgt)">
796 % if tgt.name == 'grpcpp_channelz':
797 # grpcpp_channelz doesn't build with protobuf-lite, so no install required
798 # See https://github.com/grpc/grpc/issues/22826
799 if(gRPC_INSTALL AND NOT gRPC_USE_PROTO_LITE)
800 % else:
801 if(gRPC_INSTALL)
802 % endif
803 install(TARGETS ${tgt.name} EXPORT gRPCTargets
804 RUNTIME DESTINATION <%text>${gRPC_INSTALL_BINDIR}</%text>
805 BUNDLE DESTINATION <%text>${gRPC_INSTALL_BINDIR}</%text>
806 LIBRARY DESTINATION <%text>${gRPC_INSTALL_LIBDIR}</%text>
807 ARCHIVE DESTINATION <%text>${gRPC_INSTALL_LIBDIR}</%text>
808 )
809 endif()
810 </%def>
811
812 if(gRPC_INSTALL)
813 install(EXPORT gRPCTargets
814 DESTINATION <%text>${gRPC_INSTALL_CMAKEDIR}</%text>
815 NAMESPACE gRPC::
816 )
817 endif()
818
819 include(CMakePackageConfigHelpers)
820
821 configure_file(cmake/gRPCConfig.cmake.in
822 gRPCConfig.cmake @ONLY)
823 write_basic_package_version_file(<%text>${CMAKE_CURRENT_BINARY_DIR}/</%text>gRPCConfigVersion.cmake
824 VERSION <%text>${gRPC_CPP_VERSION}</%text>
825 COMPATIBILITY AnyNewerVersion)
826 install(FILES
827 <%text>${CMAKE_CURRENT_BINARY_DIR}/</%text>gRPCConfig.cmake
828 <%text>${CMAKE_CURRENT_BINARY_DIR}/</%text>gRPCConfigVersion.cmake
829 DESTINATION <%text>${gRPC_INSTALL_CMAKEDIR}</%text>
830 )
831 install(FILES
832 <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/cmake/modules/Findc-ares.cmake
833 <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/cmake/modules/Findre2.cmake
834 DESTINATION <%text>${gRPC_INSTALL_CMAKEDIR}</%text>/modules
835 )
836
837 install(FILES <%text>${CMAKE_CURRENT_SOURCE_DIR}/etc/roots.pem</%text>
838 DESTINATION <%text>${gRPC_INSTALL_SHAREDIR}</%text>)
839
840 # Function to generate pkg-config files.
841 function(generate_pkgconfig name description version requires
842 libs libs_private output_filename)
843 set(PC_NAME "<%text>${name}</%text>")
844 set(PC_DESCRIPTION "<%text>${description}</%text>")
845 set(PC_VERSION "<%text>${version}</%text>")
846 set(PC_REQUIRES "<%text>${requires}</%text>")
847 set(PC_LIB "<%text>${libs}</%text>")
848 set(PC_LIBS_PRIVATE "<%text>${libs_private}</%text>")
849 set(output_filepath "<%text>${grpc_BINARY_DIR}/libs/opt/pkgconfig/${output_filename}</%text>")
850 configure_file(
851 "<%text>${grpc_SOURCE_DIR}/cmake/pkg-config-template.pc.in</%text>"
852 "<%text>${output_filepath}</%text>"
853 @ONLY)
854 install(FILES "<%text>${output_filepath}</%text>"
855 DESTINATION "<%text>${gRPC_INSTALL_LIBDIR}/pkgconfig</%text>")
856 endfunction()
857
858 # gpr .pc file
859 generate_pkgconfig(
860 "gpr"
861 "gRPC platform support library"
862 "<%text>${gRPC_CORE_VERSION}</%text>"
863 "${" ".join(list_abseil_pkg_targets("gpr"))}"
864 "${" ".join(("-l" + l) for l in ["gpr"])}"
865 ""
866 "gpr.pc")
867
868 # grpc .pc file
869 generate_pkgconfig(
870 "gRPC"
871 "high performance general RPC framework"
872 "<%text>${gRPC_CORE_VERSION}</%text>"
873 "${" ".join(["gpr", "openssl"] + list_abseil_pkg_targets("grpc"))}"
874 "${" ".join(("-l" + l) for l in ["grpc", "address_sorting", "re2", "upb", "cares", "z"])}"
875 ""
876 "grpc.pc")
877
878 # grpc_unsecure .pc file
879 generate_pkgconfig(
880 "gRPC unsecure"
881 "high performance general RPC framework without SSL"
882 "<%text>${gRPC_CORE_VERSION}</%text>"
883 "${" ".join(["gpr"] + list_abseil_pkg_targets("grpc_unsecure"))}"
884 "${" ".join(("-l" + l) for l in ["grpc_unsecure"])}"
885 ""
886 "grpc_unsecure.pc")
887
888 # grpc++ .pc file
889 generate_pkgconfig(
890 "gRPC++"
891 "C++ wrapper for gRPC"
892 "<%text>${gRPC_CPP_VERSION}</%text>"
893 "${" ".join(["grpc"] + list_abseil_pkg_targets("grpc++"))}"
894 "${" ".join(("-l" + l) for l in ["grpc++"])}"
895 ""
896 "grpc++.pc")
897
898 # grpc++_unsecure .pc file
899 generate_pkgconfig(
900 "gRPC++ unsecure"
901 "C++ wrapper for gRPC without SSL"
902 "<%text>${gRPC_CPP_VERSION}</%text>"
903 "${" ".join(["grpc_unsecure"] + list_abseil_pkg_targets("grpc++_unsecure"))}"
904 "${" ".join(("-l" + l) for l in ["grpc++_unsecure"])}"
905 ""
906 "grpc++_unsecure.pc")