CMakeLists.txt.template 28 KB

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