Imported Upstream version 1.33.1
[platform/upstream/grpc.git] / templates / CMakeLists.txt.template
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   def proto_replace_ext(filename, ext):
31       m = proto_re.match(filename)
32       if not m:
33         return filename
34       return '${_gRPC_PROTO_GENS_DIR}/' + m.group(1) + ext
35
36   def is_absl_lib(lib_name):
37       return lib_name.startswith("absl/");
38
39   def get_absl_dep(lib_name):
40       return lib_map[lib_name].cmake_target
41
42   def list_absl_lib_files_for(lib_name):
43     ret = []
44     lib = lib_map[lib_name]
45     for dep in lib.transitive_deps:
46       if is_absl_lib(dep) and len(lib_map[dep].src) > 0:
47         ret.append(get_absl_dep(dep).replace("::", "_"))
48     return ret
49
50   def get_deps(target_dict):
51     deps = []
52     if target_dict.get('baselib', False) or target_dict['name'] == 'address_sorting':
53       deps.append("${_gRPC_BASELIB_LIBRARIES}")
54     if target_dict.get('build', None) in ['protoc']:
55       deps.append("${_gRPC_PROTOBUF_PROTOC_LIBRARIES}")
56     if target_dict.get('secure', False):
57       deps.append("${_gRPC_SSL_LIBRARIES}")
58     if target_dict.language == 'c++':
59       deps.append("${_gRPC_PROTOBUF_LIBRARIES}")
60     if target_dict['name'] in ['grpc', 'grpc_cronet', 'grpc_unsecure']:
61       deps.append("${_gRPC_ZLIB_LIBRARIES}")
62       deps.append("${_gRPC_CARES_LIBRARIES}")
63       deps.append("${_gRPC_ADDRESS_SORTING_LIBRARIES}")
64       deps.append("${_gRPC_RE2_LIBRARIES}")
65       deps.append("${_gRPC_UPB_LIBRARIES}")
66     deps.append("${_gRPC_ALLTARGETS_LIBRARIES}")
67     for d in target_dict.get('deps', []):
68       if d == 'benchmark':
69         deps.append("${_gRPC_BENCHMARK_LIBRARIES}")
70       elif is_absl_lib(d):
71         deps.append(get_absl_dep(d))
72       else:
73         deps.append(d)
74     if (target_dict.build == 'test' or target_dict.build == 'private') and target_dict.language == 'c++':
75       deps.append("${_gRPC_GFLAGS_LIBRARIES}")
76     return deps
77
78   def get_platforms_condition_begin(platforms):
79     if all(platform in platforms for platform in ['linux', 'mac', 'posix', 'windows']):
80       return ''
81     cond = ' OR '.join(['_gRPC_PLATFORM_%s' % platform.upper() for platform in platforms])
82     return 'if(%s)' % cond
83
84   def get_platforms_condition_end(platforms):
85     if not get_platforms_condition_begin(platforms):
86       return ''
87     return 'endif()'
88
89   def platforms_condition_block(platforms):
90     def _func(text):
91       lines = text.split('\n')
92       cond = get_platforms_condition_begin(platforms)
93       if cond:
94         # Remove empty line following <%block>
95         del lines[0]
96
97         # Indent each line after
98         for i, line in enumerate(lines[:-1]):
99           if line:
100             lines[i] = '  ' + line
101
102         # Add the condition block
103         lines.insert(0, cond)
104
105         # Add endif() to the last line
106         lines[-1] += 'endif()'
107       else:
108         # Remove empty line following <%block>
109         del lines[0]
110
111         # Strip leading whitespace from first line
112         lines[0] = lines[0].lstrip(' ')
113
114         # Remove empty line before </%block>
115         del lines[-1]
116
117       return '\n'.join(lines)
118     return _func
119   %>
120   <%
121   # These files are added to a set so that they are not duplicated if multiple
122   # targets use them. Generating the same file multiple times with
123   # add_custom_command() is not allowed in CMake.
124
125   protobuf_gen_files = set()
126   for tgt in targets:
127     for src in tgt.src:
128       if proto_re.match(src):
129         protobuf_gen_files.add(src)
130   for lib in libs:
131     for src in lib.src:
132       if proto_re.match(src):
133         protobuf_gen_files.add(src)
134   %>
135
136   cmake_minimum_required(VERSION 3.5.1)
137
138   set(PACKAGE_NAME          "grpc")
139   set(PACKAGE_VERSION       "${settings.cpp_version}")
140   set(gRPC_CORE_VERSION     "${settings.core_version}")
141   set(gRPC_CORE_SOVERSION   "${settings.core_version.major}")
142   set(gRPC_CPP_VERSION      "${settings.cpp_version}")
143   set(gRPC_CPP_SOVERSION    "${settings.cpp_version.major}")
144   set(gRPC_CSHARP_VERSION   "${settings.csharp_version}")
145   set(gRPC_CSHARP_SOVERSION "${settings.csharp_version.major}")
146   set(PACKAGE_STRING        "<%text>${PACKAGE_NAME} ${PACKAGE_VERSION}</%text>")
147   set(PACKAGE_TARNAME       "<%text>${PACKAGE_NAME}-${PACKAGE_VERSION}</%text>")
148   set(PACKAGE_BUGREPORT     "https://github.com/grpc/grpc/issues/")
149   project(<%text>${PACKAGE_NAME}</%text> LANGUAGES C CXX)
150
151   set(gRPC_INSTALL_BINDIR "bin" CACHE STRING "Installation directory for executables")
152   set(gRPC_INSTALL_LIBDIR "lib" CACHE STRING "Installation directory for libraries")
153   set(gRPC_INSTALL_INCLUDEDIR "include" CACHE STRING "Installation directory for headers")
154   set(gRPC_INSTALL_CMAKEDIR "lib/cmake/<%text>${PACKAGE_NAME}</%text>" CACHE STRING "Installation directory for cmake config files")
155   set(gRPC_INSTALL_SHAREDIR "share/grpc" CACHE STRING "Installation directory for root certificates")
156
157   # Options
158   option(gRPC_BUILD_TESTS "Build tests" OFF)
159   option(gRPC_BUILD_CODEGEN "Build codegen" ON)
160   option(gRPC_BUILD_CSHARP_EXT "Build C# extensions" ON)
161   option(gRPC_BACKWARDS_COMPATIBILITY_MODE "Build libraries that are binary compatible across a larger number of OS and libc versions" OFF)
162
163   set(gRPC_INSTALL_default ON)
164   if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
165     # Disable gRPC_INSTALL by default if building as a submodule
166     set(gRPC_INSTALL_default OFF)
167   endif()
168   set(gRPC_INSTALL <%text>${gRPC_INSTALL_default}</%text> CACHE BOOL
169       "Generate installation target")
170
171   # We can install dependencies from submodules if we're running
172   # CMake v3.13 or newer.
173   if(CMAKE_VERSION VERSION_LESS 3.13)
174     set(_gRPC_INSTALL_SUPPORTED_FROM_MODULE OFF)
175   else()
176     set(_gRPC_INSTALL_SUPPORTED_FROM_MODULE ON)
177   endif()
178
179   # Providers for third-party dependencies (gRPC_*_PROVIDER properties):
180   # "module": build the dependency using sources from git submodule (under third_party)
181   # "package": use cmake's find_package functionality to locate a pre-installed dependency
182
183   set(gRPC_ZLIB_PROVIDER "module" CACHE STRING "Provider of zlib library")
184   set_property(CACHE gRPC_ZLIB_PROVIDER PROPERTY STRINGS "module" "package")
185
186   set(gRPC_CARES_PROVIDER "module" CACHE STRING "Provider of c-ares library")
187   set_property(CACHE gRPC_CARES_PROVIDER PROPERTY STRINGS "module" "package")
188
189   set(gRPC_RE2_PROVIDER "module" CACHE STRING "Provider of re2 library")
190   set_property(CACHE gRPC_RE2_PROVIDER PROPERTY STRINGS "module" "package")
191
192   set(gRPC_SSL_PROVIDER "module" CACHE STRING "Provider of ssl library")
193   set_property(CACHE gRPC_SSL_PROVIDER PROPERTY STRINGS "module" "package")
194
195   set(gRPC_PROTOBUF_PROVIDER "module" CACHE STRING "Provider of protobuf library")
196   set_property(CACHE gRPC_PROTOBUF_PROVIDER PROPERTY STRINGS "module" "package")
197
198   set(gRPC_PROTOBUF_PACKAGE_TYPE "" CACHE STRING "Algorithm for searching protobuf package")
199   set_property(CACHE gRPC_PROTOBUF_PACKAGE_TYPE PROPERTY STRINGS "CONFIG" "MODULE")
200
201   if(gRPC_BUILD_TESTS)
202     set(gRPC_GFLAGS_PROVIDER "module" CACHE STRING "Provider of gflags library")
203     set_property(CACHE gRPC_GFLAGS_PROVIDER PROPERTY STRINGS "module" "package")
204
205     set(gRPC_BENCHMARK_PROVIDER "module" CACHE STRING "Provider of benchmark library")
206     set_property(CACHE gRPC_BENCHMARK_PROVIDER PROPERTY STRINGS "module" "package")
207   else()
208     set(gRPC_GFLAGS_PROVIDER "none")
209     set(gRPC_BENCHMARK_PROVIDER "none")
210   endif()
211
212   set(gRPC_ABSL_PROVIDER "module" CACHE STRING "Provider of absl library")
213   set_property(CACHE gRPC_ABSL_PROVIDER PROPERTY STRINGS "module" "package")
214   <%
215     # Collect all abseil rules used by gpr, grpc, so on.
216     used_abseil_rules = set()
217     for lib in libs:
218       if lib.get("baselib"):
219         for dep in lib.transitive_deps:
220           if is_absl_lib(dep):
221             used_abseil_rules.add(lib_map[dep].cmake_target.replace("::", "_"))
222   %>
223   set(gRPC_ABSL_USED_TARGETS
224   % for rule in sorted(used_abseil_rules):
225     ${rule}
226   % endfor
227     absl_meta
228   )
229
230   set(gRPC_USE_PROTO_LITE OFF CACHE BOOL "Use the protobuf-lite library")
231
232   if(UNIX)
233     if(<%text>${CMAKE_SYSTEM_NAME}</%text> MATCHES "Linux")
234       set(_gRPC_PLATFORM_LINUX ON)
235     elseif(<%text>${CMAKE_SYSTEM_NAME}</%text> MATCHES "Darwin")
236       set(_gRPC_PLATFORM_MAC ON)
237     elseif(<%text>${CMAKE_SYSTEM_NAME}</%text> MATCHES "iOS")
238       set(_gRPC_PLATFORM_IOS ON)
239     elseif(<%text>${CMAKE_SYSTEM_NAME}</%text> MATCHES "Android")
240       set(_gRPC_PLATFORM_ANDROID ON)
241     else()
242       set(_gRPC_PLATFORM_POSIX ON)
243     endif()
244   endif()
245   if(WIN32)
246     set(_gRPC_PLATFORM_WINDOWS ON)
247   endif()
248
249    # Use C99 standard
250   if (NOT DEFINED CMAKE_C_STANDARD)
251     set(CMAKE_C_STANDARD 99)
252   endif()
253
254   # Add c++11 flags
255   if (NOT DEFINED CMAKE_CXX_STANDARD)
256     set(CMAKE_CXX_STANDARD 11)
257   else()
258     if (CMAKE_CXX_STANDARD LESS 11)
259       message(FATAL_ERROR "CMAKE_CXX_STANDARD is less than 11, please specify at least SET(CMAKE_CXX_STANDARD 11)")
260     endif()
261   endif()
262   if (NOT DEFINED CMAKE_CXX_STANDARD_REQUIRED)
263     set(CMAKE_CXX_STANDARD_REQUIRED ON)
264   endif()
265   if (NOT DEFINED CMAKE_CXX_EXTENSIONS)
266     set(CMAKE_CXX_EXTENSIONS OFF)
267   endif()
268
269   ## Some libraries are shared even with BUILD_SHARED_LIBRARIES=OFF
270   if (NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE)
271     set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
272   endif()
273   list(APPEND CMAKE_MODULE_PATH "<%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/cmake/modules")
274
275   if(MSVC)
276     include(cmake/msvc_static_runtime.cmake)
277     add_definitions(-D_WIN32_WINNT=0x600 -D_SCL_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_WARNINGS -D_WINSOCK_DEPRECATED_NO_WARNINGS)
278     # needed to compile protobuf
279     set(_gRPC_C_CXX_FLAGS "<%text>${_gRPC_C_CXX_FLAGS}</%text> /wd4065 /wd4506")
280     # TODO(jtattermusch): revisit warnings that were silenced as part of upgrade to protobuf3.6.0
281     set(_gRPC_C_CXX_FLAGS "<%text>${_gRPC_C_CXX_FLAGS}</%text> /wd4200 /wd4291 /wd4244")
282     # TODO(jtattermusch): revisit C4267 occurrences throughout the code
283     set(_gRPC_C_CXX_FLAGS "<%text>${_gRPC_C_CXX_FLAGS}</%text> /wd4267")
284     # TODO(jtattermusch): needed to build boringssl with VS2017, revisit later
285     set(_gRPC_C_CXX_FLAGS "<%text>${_gRPC_C_CXX_FLAGS}</%text> /wd4987 /wd4774 /wd4819 /wd4996 /wd4619")
286   endif()
287   if (MINGW)
288     add_definitions(-D_WIN32_WINNT=0x600)
289   endif()
290   set(CMAKE_C_FLAGS "<%text>${CMAKE_C_FLAGS} ${_gRPC_C_CXX_FLAGS}</%text>")
291   set(CMAKE_CXX_FLAGS "<%text>${CMAKE_CXX_FLAGS} ${_gRPC_C_CXX_FLAGS}</%text>")
292
293   if(gRPC_USE_PROTO_LITE)
294     set(_gRPC_PROTOBUF_LIBRARY_NAME "libprotobuf-lite")
295     add_definitions("-DGRPC_USE_PROTO_LITE")
296   else()
297     set(_gRPC_PROTOBUF_LIBRARY_NAME "libprotobuf")
298   endif()
299
300   if(gRPC_BACKWARDS_COMPATIBILITY_MODE)
301     add_definitions(-DGPR_BACKWARDS_COMPATIBILITY_MODE)
302     if(_gRPC_PLATFORM_MAC)
303       # some C++11 constructs not supported before OS X 10.9
304       set(CMAKE_OSX_DEPLOYMENT_TARGET 10.9)
305     endif()
306   endif()
307
308   if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_IOS)
309     set(_gRPC_CORE_NOSTDCXX_FLAGS -fno-exceptions -fno-rtti)
310   else()
311     set(_gRPC_CORE_NOSTDCXX_FLAGS "")
312   endif()
313
314   include(cmake/abseil-cpp.cmake)
315   include(cmake/address_sorting.cmake)
316   include(cmake/benchmark.cmake)
317   include(cmake/cares.cmake)
318   include(cmake/gflags.cmake)
319   include(cmake/protobuf.cmake)
320   include(cmake/re2.cmake)
321   include(cmake/ssl.cmake)
322   include(cmake/upb.cmake)
323   include(cmake/zlib.cmake)
324
325   if(_gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_IOS)
326     set(_gRPC_ALLTARGETS_LIBRARIES <%text>${CMAKE_DL_LIBS}</%text> m pthread)
327   elseif(_gRPC_PLATFORM_ANDROID)
328     set(_gRPC_ALLTARGETS_LIBRARIES <%text>${CMAKE_DL_LIBS}</%text> m)
329   elseif(UNIX)
330     set(_gRPC_ALLTARGETS_LIBRARIES <%text>${CMAKE_DL_LIBS}</%text> rt m pthread)
331   endif()
332
333   if(WIN32)
334     set(_gRPC_BASELIB_LIBRARIES wsock32 ws2_32 crypt32)
335   endif()
336
337   # Create directory for generated .proto files
338   set(_gRPC_PROTO_GENS_DIR <%text>${CMAKE_BINARY_DIR}/gens</%text>)
339   file(MAKE_DIRECTORY <%text>${_gRPC_PROTO_GENS_DIR}</%text>)
340
341   #  protobuf_generate_grpc_cpp
342   #  --------------------------
343   #
344   #   Add custom commands to process ``.proto`` files to C++ using protoc and
345   #   GRPC plugin::
346   #
347   #     protobuf_generate_grpc_cpp [<ARGN>...]
348   #
349   #   ``ARGN``
350   #     ``.proto`` files
351   #
352   function(protobuf_generate_grpc_cpp)
353     if(NOT ARGN)
354       message(SEND_ERROR "Error: PROTOBUF_GENERATE_GRPC_CPP() called without any proto files")
355       return()
356     endif()
357
358     set(_protobuf_include_path -I . -I <%text>${_gRPC_PROTOBUF_WELLKNOWN_INCLUDE_DIR}</%text>)
359     foreach(FIL <%text>${ARGN}</%text>)
360       get_filename_component(ABS_FIL <%text>${FIL}</%text> ABSOLUTE)
361       get_filename_component(FIL_WE <%text>${FIL}</%text> NAME_WE)
362       file(RELATIVE_PATH REL_FIL <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text> <%text>${ABS_FIL}</%text>)
363       get_filename_component(REL_DIR <%text>${REL_FIL}</%text> DIRECTORY)
364       set(RELFIL_WE "<%text>${REL_DIR}/${FIL_WE}</%text>")
365
366       #if cross-compiling, find host plugin
367       if(CMAKE_CROSSCOMPILING)
368         find_program(_gRPC_CPP_PLUGIN grpc_cpp_plugin)
369       else()
370         set(_gRPC_CPP_PLUGIN $<TARGET_FILE:grpc_cpp_plugin>)
371       endif()
372
373       add_custom_command(
374         OUTPUT <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.cc"</%text>
375                <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.h"</%text>
376                <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}_mock.grpc.pb.h"</%text>
377                <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.cc"</%text>
378                <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.h"</%text>
379         COMMAND <%text>${_gRPC_PROTOBUF_PROTOC_EXECUTABLE}</%text>
380         ARGS --grpc_out=<%text>generate_mock_code=true:${_gRPC_PROTO_GENS_DIR}</%text>
381              --cpp_out=<%text>${_gRPC_PROTO_GENS_DIR}</%text>
382              --plugin=protoc-gen-grpc=<%text>${_gRPC_CPP_PLUGIN}</%text>
383              <%text>${_protobuf_include_path}</%text>
384              <%text>${REL_FIL}</%text>
385         DEPENDS <%text>${ABS_FIL}</%text> <%text>${_gRPC_PROTOBUF_PROTOC}</%text> grpc_cpp_plugin
386         WORKING_DIRECTORY <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>
387         COMMENT "Running gRPC C++ protocol buffer compiler on <%text>${FIL}</%text>"
388         VERBATIM)
389     endforeach()
390   endfunction()
391
392   # These options allow users to enable or disable the building of the various
393   # protoc plugins. For example, running CMake with
394   # -DgRPC_BUILD_GRPC_CSHARP_PLUGIN=OFF will disable building the C# plugin.
395   set(_gRPC_PLUGIN_LIST)
396   % for tgt in targets:
397   % if tgt.build == 'protoc':
398   option(gRPC_BUILD_${tgt.name.upper()} "Build ${tgt.name}" ON)
399   if (gRPC_BUILD_${tgt.name.upper()})
400     list(APPEND _gRPC_PLUGIN_LIST ${tgt.name})
401   endif ()
402   % endif
403   % endfor
404
405   add_custom_target(plugins
406     DEPENDS <%text>${_gRPC_PLUGIN_LIST}</%text>
407   )
408
409   add_custom_target(tools_c
410     DEPENDS
411   % for tgt in targets:
412   % if tgt.build == 'tool' and not tgt.language == 'c++':
413     ${tgt.name}
414   % endif
415   % endfor
416   )
417
418   add_custom_target(tools_cxx
419     DEPENDS
420   % for tgt in targets:
421   % if tgt.build == 'tool' and tgt.language == 'c++':
422     ${tgt.name}
423   % endif
424   % endfor
425   )
426
427   add_custom_target(tools
428     DEPENDS tools_c tools_cxx)
429
430   % for src in sorted(protobuf_gen_files):
431   protobuf_generate_grpc_cpp(
432     ${src}
433   )
434   % endfor
435
436   if(gRPC_BUILD_TESTS)
437     add_custom_target(buildtests_c)
438     % for tgt in targets:
439     % if tgt.build == 'test' and not tgt.language == 'c++' and not tgt.get('external_deps', None) and not tgt.boringssl:
440     <%block filter='platforms_condition_block(tgt.platforms)'>
441     add_dependencies(buildtests_c ${tgt.name})
442     </%block>
443     % endif
444     % endfor
445
446     add_custom_target(buildtests_cxx)
447     % for tgt in targets:
448     % if tgt.build == 'test' and tgt.language == 'c++' and not tgt.get('external_deps', None) and not tgt.boringssl:
449     <%block filter='platforms_condition_block(tgt.platforms)'>
450     add_dependencies(buildtests_cxx ${tgt.name})
451     </%block>
452     % endif
453     % endfor
454
455     add_custom_target(buildtests
456       DEPENDS buildtests_c buildtests_cxx)
457   endif()
458   <%
459     cmake_libs = []
460     for lib in libs:
461       if lib.build not in ["all", "protoc", "tool", "test", "private"] or lib.boringssl: continue
462       if lib.get('build_system', []) and 'cmake' not in lib.get('build_system', []): continue
463       if lib.name in ['ares', 'benchmark', 're2', 'z']: continue  # we build these using CMake instead
464       if is_absl_lib(lib.name): continue  # we build these using CMake instead
465       cmake_libs.append(lib)
466   %>
467   % for lib in cmake_libs:
468   %   if lib.build in ["test", "private"]:
469   if(gRPC_BUILD_TESTS)
470   ${cc_library(lib)}
471   endif()
472   %   elif lib.name in ['grpc_csharp_ext']:
473   if(gRPC_BUILD_CSHARP_EXT)
474   ${cc_library(lib)}
475   endif()
476   %   else:
477   ${cc_library(lib)}
478   %     if not lib.build in ["tool"]:
479   %       if any(proto_re.match(src) for src in lib.src):
480   if(gRPC_BUILD_CODEGEN)
481   %       endif
482   ${cc_install(lib)}
483   %       if any(proto_re.match(src) for src in lib.src):
484   endif()
485   %       endif
486   %     endif
487   %   endif
488   % endfor
489
490   % for tgt in targets:
491   % if tgt.build in ["all", "protoc", "tool", "test", "private"] and not tgt.boringssl:
492   % if tgt.build in ["test", "private"]:
493   if(gRPC_BUILD_TESTS)
494   <%block filter='platforms_condition_block(tgt.platforms)'>
495   ${cc_binary(tgt)}
496   </%block>
497   endif()
498   % elif tgt.build in ["protoc"]:
499   if(gRPC_BUILD_CODEGEN AND gRPC_BUILD_${tgt.name.upper()})
500   <%block filter='platforms_condition_block(tgt.platforms)'>
501   ${cc_binary(tgt)}
502   ${cc_install(tgt)}
503   </%block>
504   endif()
505   % else:
506   <%block filter='platforms_condition_block(tgt.platforms)'>
507   ${cc_binary(tgt)}
508   % if not tgt.build in ["tool"]:
509   ${cc_install(tgt)}
510   % endif
511   </%block>
512   % endif
513   % endif
514   % endfor
515
516   <%def name="cc_library(lib)">
517   % if any(proto_re.match(src) for src in lib.src):
518   % if lib.name == 'grpcpp_channelz':
519   # grpcpp_channelz doesn't build with protobuf-lite
520   # See https://github.com/grpc/grpc/issues/19473
521   if(gRPC_BUILD_CODEGEN AND NOT gRPC_USE_PROTO_LITE)
522   % else:
523   if(gRPC_BUILD_CODEGEN)
524   % endif
525   % endif
526   add_library(${lib.name}${' SHARED' if lib.get('dll', None) == 'only' else ''}
527   % for src in lib.src:
528   % if not proto_re.match(src):
529     ${src}
530   % else:
531     ${proto_replace_ext(src, '.pb.cc')}
532     ${proto_replace_ext(src, '.grpc.pb.cc')}
533     ${proto_replace_ext(src, '.pb.h')}
534     ${proto_replace_ext(src, '.grpc.pb.h')}
535     % if src in ["src/proto/grpc/testing/compiler_test.proto", "src/proto/grpc/testing/echo.proto"]:
536     ${proto_replace_ext(src, '_mock.grpc.pb.h')}
537     % endif
538   % endif
539   % endfor
540   )
541
542   set_target_properties(${lib.name} PROPERTIES
543   % if lib.language == 'c++':
544     VERSION <%text>${gRPC_CPP_VERSION}</%text>
545     SOVERSION <%text>${gRPC_CPP_SOVERSION}</%text>
546   % elif lib.language == 'csharp':
547     VERSION <%text>${gRPC_CSHARP_VERSION}</%text>
548     SOVERSION <%text>${gRPC_CSHARP_SOVERSION}</%text>
549   % else:
550     VERSION <%text>${gRPC_CORE_VERSION}</%text>
551     SOVERSION <%text>${gRPC_CORE_SOVERSION}</%text>
552   % endif
553   )
554
555   if(WIN32 AND MSVC)
556     set_target_properties(${lib.name} PROPERTIES COMPILE_PDB_NAME "${lib.name}"
557       COMPILE_PDB_OUTPUT_DIRECTORY <%text>"${CMAKE_BINARY_DIR}</%text>"
558     )
559     if(gRPC_INSTALL)
560       install(FILES <%text>${CMAKE_CURRENT_BINARY_DIR}/</%text>${lib.name}.pdb
561         DESTINATION <%text>${gRPC_INSTALL_LIBDIR}</%text> OPTIONAL
562       )
563     endif()
564   endif()
565
566   target_include_directories(${lib.name}
567     PUBLIC <%text>$<INSTALL_INTERFACE:${gRPC_INSTALL_INCLUDEDIR}> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include></%text>
568     PRIVATE
569       <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>
570       <%text>${_gRPC_ADDRESS_SORTING_INCLUDE_DIR}</%text>
571       <%text>${_gRPC_RE2_INCLUDE_DIR}</%text>
572       <%text>${_gRPC_SSL_INCLUDE_DIR}</%text>
573       <%text>${_gRPC_UPB_GENERATED_DIR}</%text>
574       <%text>${_gRPC_UPB_GRPC_GENERATED_DIR}</%text>
575       <%text>${_gRPC_UPB_INCLUDE_DIR}</%text>
576       <%text>${_gRPC_ZLIB_INCLUDE_DIR}</%text>
577   % if lib.build in ['test', 'private'] and lib.language == 'c++':
578       third_party/googletest/googletest/include
579       third_party/googletest/googletest
580       third_party/googletest/googlemock/include
581       third_party/googletest/googlemock
582   % endif
583   % if lib.language == 'c++':
584       <%text>${_gRPC_PROTO_GENS_DIR}</%text>
585   % endif
586   )
587   % if len(get_deps(lib)) > 0:
588   target_link_libraries(${lib.name}
589   % for dep in get_deps(lib):
590     ${dep}
591   % endfor
592   )
593   % endif
594   % if lib.name in ["gpr"]:
595   if(_gRPC_PLATFORM_ANDROID)
596     target_link_libraries(gpr
597       android
598       log
599     )
600   endif()
601   % endif
602   % if lib.name in ["grpc", "grpc_cronet", "grpc_test_util", \
603                     "grpc_test_util_unsecure", "grpc_unsecure", \
604                     "grpc++_cronet"]:
605   if(_gRPC_PLATFORM_IOS OR _gRPC_PLATFORM_MAC)
606     target_link_libraries(${lib.name} "-framework CoreFoundation")
607   endif()
608   %endif
609
610   % if len(lib.get('public_headers', [])) > 0:
611   foreach(_hdr
612   % for hdr in lib.get('public_headers', []):
613     ${hdr}
614   % endfor
615   )
616     string(REPLACE "include/" "" _path <%text>${_hdr}</%text>)
617     get_filename_component(_path <%text>${_path}</%text> PATH)
618     install(FILES <%text>${_hdr}</%text>
619       DESTINATION "<%text>${gRPC_INSTALL_INCLUDEDIR}/${_path}</%text>"
620     )
621   endforeach()
622   % endif
623   % if any(proto_re.match(src) for src in lib.src):
624   endif()
625   % endif
626   </%def>
627
628   <%def name="cc_binary(tgt)">
629   add_executable(${tgt.name}
630   % for src in tgt.src:
631   % if not proto_re.match(src):
632     ${src}
633   % else:
634     ${proto_replace_ext(src, '.pb.cc')}
635     ${proto_replace_ext(src, '.grpc.pb.cc')}
636     ${proto_replace_ext(src, '.pb.h')}
637     ${proto_replace_ext(src, '.grpc.pb.h')}
638   % endif
639   % endfor
640   % if tgt.build == 'test' and tgt.language == 'c++':
641     third_party/googletest/googletest/src/gtest-all.cc
642     third_party/googletest/googlemock/src/gmock-all.cc
643   % endif
644   )
645
646   target_include_directories(${tgt.name}
647     PRIVATE
648       <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>
649       <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/include
650       <%text>${_gRPC_ADDRESS_SORTING_INCLUDE_DIR}</%text>
651       <%text>${_gRPC_RE2_INCLUDE_DIR}</%text>
652       <%text>${_gRPC_SSL_INCLUDE_DIR}</%text>
653       <%text>${_gRPC_UPB_GENERATED_DIR}</%text>
654       <%text>${_gRPC_UPB_GRPC_GENERATED_DIR}</%text>
655       <%text>${_gRPC_UPB_INCLUDE_DIR}</%text>
656       <%text>${_gRPC_ZLIB_INCLUDE_DIR}</%text>
657   % if tgt.build in ['test', 'private'] and tgt.language == 'c++':
658       third_party/googletest/googletest/include
659       third_party/googletest/googletest
660       third_party/googletest/googlemock/include
661       third_party/googletest/googlemock
662   % endif
663   % if tgt.language == 'c++':
664       <%text>${_gRPC_PROTO_GENS_DIR}</%text>
665   % endif
666   )
667
668   % if len(get_deps(tgt)) > 0:
669   target_link_libraries(${tgt.name}
670   % for dep in get_deps(tgt):
671     ${dep}
672   % endfor
673   )
674
675   % endif
676   </%def>
677
678   <%def name="cc_install(tgt)">
679   if(gRPC_INSTALL)
680     install(TARGETS ${tgt.name} EXPORT gRPCTargets
681       RUNTIME DESTINATION <%text>${gRPC_INSTALL_BINDIR}</%text>
682       LIBRARY DESTINATION <%text>${gRPC_INSTALL_LIBDIR}</%text>
683       ARCHIVE DESTINATION <%text>${gRPC_INSTALL_LIBDIR}</%text>
684     )
685   endif()
686   </%def>
687
688   if(gRPC_INSTALL)
689     install(EXPORT gRPCTargets
690       DESTINATION <%text>${gRPC_INSTALL_CMAKEDIR}</%text>
691       NAMESPACE gRPC::
692     )
693   endif()
694
695   include(CMakePackageConfigHelpers)
696
697   configure_file(cmake/gRPCConfig.cmake.in
698     gRPCConfig.cmake @ONLY)
699   write_basic_package_version_file(<%text>${CMAKE_CURRENT_BINARY_DIR}/</%text>gRPCConfigVersion.cmake
700     VERSION <%text>${gRPC_CPP_VERSION}</%text>
701     COMPATIBILITY AnyNewerVersion)
702   install(FILES
703       <%text>${CMAKE_CURRENT_BINARY_DIR}/</%text>gRPCConfig.cmake
704       <%text>${CMAKE_CURRENT_BINARY_DIR}/</%text>gRPCConfigVersion.cmake
705     DESTINATION <%text>${gRPC_INSTALL_CMAKEDIR}</%text>
706   )
707   install(FILES
708       <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/cmake/modules/Findc-ares.cmake
709       <%text>${CMAKE_CURRENT_SOURCE_DIR}</%text>/cmake/modules/Findre2.cmake
710     DESTINATION <%text>${gRPC_INSTALL_CMAKEDIR}</%text>/modules
711   )
712
713   install(FILES <%text>${CMAKE_CURRENT_SOURCE_DIR}/etc/roots.pem</%text>
714     DESTINATION <%text>${gRPC_INSTALL_SHAREDIR}</%text>)
715
716   # Function to generate pkg-config files.
717   function(generate_pkgconfig name description version requires
718                               libs libs_private output_filename)
719     set(PC_NAME "<%text>${name}</%text>")
720     set(PC_DESCRIPTION "<%text>${description}</%text>")
721     set(PC_VERSION "<%text>${version}</%text>")
722     set(PC_REQUIRES "<%text>${requires}</%text>")
723     set(PC_LIB "<%text>${libs}</%text>")
724     set(PC_LIBS_PRIVATE "<%text>${libs_private}</%text>")
725     set(output_filepath "<%text>${grpc_BINARY_DIR}/libs/opt/pkgconfig/${output_filename}</%text>")
726     configure_file(
727       "<%text>${grpc_SOURCE_DIR}/cmake/pkg-config-template.pc.in</%text>"
728       "<%text>${output_filepath}</%text>"
729       @ONLY)
730     install(FILES "<%text>${output_filepath}</%text>"
731       DESTINATION "lib/pkgconfig/")
732   endfunction()
733
734   # gpr .pc file
735   generate_pkgconfig(
736     "gpr"
737     "gRPC platform support library"
738     "<%text>${gRPC_CORE_VERSION}</%text>"
739     ""
740     "${" ".join(("-l" + l) for l in ["gpr",] + list_absl_lib_files_for("gpr"))}"
741     ""
742     "gpr.pc")
743
744   # grpc .pc file
745   generate_pkgconfig(
746     "gRPC"
747     "high performance general RPC framework"
748     "<%text>${gRPC_CORE_VERSION}</%text>"
749     "gpr openssl"
750     "${" ".join(("-l" + l) for l in ["grpc", "address_sorting", "re2", "upb", "cares", "z"] + list_absl_lib_files_for("grpc"))}"
751     ""
752     "grpc.pc")
753
754   # grpc_unsecure .pc file
755   generate_pkgconfig(
756     "gRPC unsecure"
757     "high performance general RPC framework without SSL"
758     "<%text>${gRPC_CORE_VERSION}</%text>"
759     "gpr"
760     "${" ".join(("-l" + l) for l in ["grpc_unsecure",] + list_absl_lib_files_for("grpc_unsecure"))}"
761     ""
762     "grpc_unsecure.pc")
763
764   # grpc++ .pc file
765   generate_pkgconfig(
766     "gRPC++"
767     "C++ wrapper for gRPC"
768     "<%text>${gRPC_CPP_VERSION}</%text>"
769     "grpc"
770     "${" ".join(("-l" + l) for l in ["grpc++",] + list_absl_lib_files_for("grpc++"))}"
771     ""
772     "grpc++.pc")
773
774   # grpc++_unsecure .pc file
775   generate_pkgconfig(
776     "gRPC++ unsecure"
777     "C++ wrapper for gRPC without SSL"
778     "<%text>${gRPC_CPP_VERSION}</%text>"
779     "grpc_unsecure"
780     "${" ".join(("-l" + l) for l in ["grpc++_unsecure",] + list_absl_lib_files_for("grpc++_unsecure"))}"
781     ""
782     "grpc++_unsecure.pc")