CMakeLists.txt.template 24 KB

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