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