CMakeLists.txt.template 27 KB

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