CMakeLists.txt.template 26 KB

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