BUILD.chromium.gn.template 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. %YAML 1.2
  2. --- |
  3. <%doc>
  4. Header piece
  5. </%doc>\
  6. # GRPC Chromium GN build file
  7. # This file has been automatically generated from a template file.
  8. # Please look at the templates directory instead.
  9. # See //third_party/grpc/README.chromium for more information.
  10. declare_args() {
  11. # Compiles with ares.
  12. enable_grpc_ares = false
  13. # TODO(169395837): Somehow gRPC symbols cannot be found on Android.
  14. # Keep using static linking for now.
  15. # In windows and mac use static linking.
  16. # Use static linking on Chrome OS as a workaround for the symbol lookup
  17. # error(crbug/1241330) due to a gRPC version mismatch between what Chrome
  18. # uses and what CrOS provides.
  19. grpc_use_static_linking = is_android || is_win || is_chromeos || is_mac
  20. }
  21. if (is_android) {
  22. import("//build/config/android/config.gni")
  23. import("//build/config/android/rules.gni")
  24. }
  25. config("grpc_config") {
  26. include_dirs = [
  27. "src/include",
  28. "src",
  29. "src/src/core/ext/upb-generated",
  30. "src/src/core/ext/upbdefs-generated",
  31. "src/third_party/cares",
  32. "//third_party/cares/include",
  33. "src/third_party/upb",
  34. "//third_party/abseil-cpp",
  35. ]
  36. defines = [
  37. "GRPC_USE_PROTO_LITE",
  38. "HAVE_CONFIG_H",
  39. "PB_FIELD_16BIT",
  40. "GRPC_NO_XDS",
  41. "GRPC_NO_RLS",
  42. ]
  43. if (!is_android) {
  44. # This prevents android specific object files from getting
  45. # included in shared library built for other platforms
  46. defines += [ "GRPC_NO_BINDER", ]
  47. }
  48. cflags = [
  49. "-Wno-implicit-fallthrough",
  50. "-Wno-shadow",
  51. "-Wno-unreachable-code-return",
  52. "-Wno-unreachable-code-break",
  53. ]
  54. if (is_android) {
  55. libs = [ "log" ] # For __android_log_write
  56. }
  57. if (is_android) {
  58. include_dirs += [ "src/third_party/cares/config_android" ]
  59. } else if (is_fuchsia) {
  60. include_dirs += [ "third_party/cares/config_fuchsia" ]
  61. } else {
  62. include_dirs += [ "src/third_party/cares/config_linux" ]
  63. }
  64. if (is_fuchsia) {
  65. defines += [
  66. # Allows zircon sockets to use file descriptors with gRPC.
  67. "GPR_SUPPORT_CHANNELS_FROM_FD",
  68. ]
  69. }
  70. if (!enable_grpc_ares) {
  71. defines += [
  72. # Disable c-ares since it doesn't currently support Fuchsia
  73. "GRPC_ARES=0",
  74. ]
  75. }
  76. }
  77. config("grpc_config_private") {
  78. cflags = [
  79. "-Wno-c++98-compat-extra-semi",
  80. "-Wno-extra-semi",
  81. "-Wno-sign-compare",
  82. "-Wno-sometimes-uninitialized",
  83. "-Wno-string-concatenation",
  84. "-Wno-unreachable-code",
  85. "-Wno-unused-variable",
  86. ]
  87. }
  88. template("grpc_so") {
  89. if (grpc_use_static_linking) {
  90. source_set(target_name) {
  91. forward_variables_from(invoker, "*")
  92. }
  93. } else {
  94. shared_library(target_name) {
  95. forward_variables_from(invoker, "*")
  96. inputs = [ "./grpc_shared_lib.map" ]
  97. ldflags = [ "-Wl,--version-script=" + rebase_path("./grpc_shared_lib.map", root_build_dir) ]
  98. }
  99. }
  100. }
  101. <%doc>
  102. Python convenience functions.
  103. </%doc>
  104. <%!
  105. import os
  106. import re
  107. import glob
  108. # Sort list of sources or dependencies in a GN target.
  109. def gn_sort(l):
  110. new_l = []
  111. l = set(l)
  112. for i in l:
  113. if i.startswith(':'):
  114. new_l.append('1_{}'.format(i))
  115. elif i.startswith('//'):
  116. new_l.append('3_{}'.format(i))
  117. else:
  118. new_l.append('2_{}'.format(i))
  119. new_l.sort()
  120. return [i[2:] for i in new_l]
  121. # Find repeated basenames to avoid conflicts in GN.
  122. # Split the sources into 3 set of sources so that
  123. # sources with repeated basenames are in different sets
  124. def find_repeated(sources):
  125. # Deduplicate the sources files in the input so the same source file
  126. # don't appear in multiple sets
  127. sources = set(sources)
  128. # Convert sources to sorted list so the sources are split into 3 targets deterministically
  129. sources = sorted(sources)
  130. out_sources = []
  131. repeated1 = []
  132. repeated2 = []
  133. out_sources_basenames = set()
  134. repeated1_basenames = set()
  135. repeated2_basenames = set()
  136. for s in sources:
  137. basename = os.path.basename(s)
  138. ext = os.path.splitext(basename)
  139. if (len(ext) > 1) and (ext[1] == '.h') and not re.search('ext/upb', s):
  140. out_sources.append(s)
  141. continue
  142. if basename in out_sources_basenames:
  143. if basename in repeated1_basenames:
  144. # If there is a basename that appears more than 3 times,
  145. # we need to create more GN targets for it
  146. assert(s not in repeated2_basenames)
  147. repeated2_basenames.add(basename)
  148. repeated2.append(s)
  149. else:
  150. repeated1_basenames.add(basename)
  151. repeated1.append(s)
  152. else:
  153. out_sources_basenames.add(basename)
  154. out_sources.append(s)
  155. return (gn_sort(out_sources), gn_sort(repeated1), gn_sort(repeated2))
  156. def is_xds_source(s):
  157. file_names = [
  158. 'src/src/core/ext/filters/client_channel/resolver/google_c2p/google_c2p_resolver.cc',
  159. 'src/src/core/ext/xds/xds_server_config_fetcher.cc',
  160. 'src/src/core/lib/security/credentials/xds/xds_credentials.cc',
  161. 'src/src/core/lib/security/credentials/xds/xds_credentials.h',
  162. 'src/src/cpp/client/xds_credentials.cc',
  163. 'src/src/cpp/client/xds_credentials.cc',
  164. 'src/src/cpp/client/xds_credentials.h',
  165. 'src/src/cpp/client/xds_credentials.h',
  166. 'src/src/cpp/server/csds/csds.cc',
  167. 'src/src/cpp/server/xds_server_credentials.cc',
  168. 'src/src/cpp/server/xds_server_credentials.cc',
  169. 'src/src/cpp/server/xds_server_credentials.h',
  170. 'src/src/core/ext/filters/rbac/rbac_filter.cc',
  171. 'src/src/core/ext/filters/rbac/rbac_filter.h',
  172. 'src/src/core/ext/filters/rbac/rbac_service_config_parser.cc',
  173. 'src/src/core/ext/filters/rbac/rbac_service_config_parser.h',
  174. 'src/src/core/lib/security/authorization/grpc_authorization_engine.cc',
  175. 'src/src/core/lib/security/authorization/grpc_authorization_engine.h',
  176. 'src/src/core/lib/security/authorization/matchers.cc',
  177. 'src/src/core/lib/security/authorization/matchers.h',
  178. 'src/src/core/lib/security/authorization/rbac_policy.cc',
  179. 'src/src/core/lib/security/authorization/rbac_policy.h',
  180. ]
  181. return s in file_names
  182. # Add comments for some files.
  183. def get_commented_sources(sources):
  184. out_sources = []
  185. for s in sources:
  186. if s == 'src/src/core/lib/gpr/wrap_memcpy.cc':
  187. out_sources.append('# gRPC memcpy wrapping logic isn\'t useful here.')
  188. out_sources.append('# See https://crbug.com/661171')
  189. out_sources.append('# "{}",'.format(s))
  190. elif s == 'src/src/core/plugin_registry/grpc_plugin_registry.cc':
  191. out_sources.append('# Disabling some default plugins.')
  192. out_sources.append('# "{}",'.format(s))
  193. out_sources.append('"plugin_registry/grpc_plugin_registry.cc",')
  194. elif s == 'src/src/core/lib/matchers/matchers.cc':
  195. # matchers are disabled to reduce binary size
  196. out_sources.append('# "{}",'.format(s))
  197. elif s == 'src/src/core/lib/matchers/matchers.h':
  198. # matchers are disabled to reduce binary size
  199. out_sources.append('# "{}",'.format(s))
  200. elif is_xds_source(s):
  201. # xds is disabled to reduce binary size
  202. # We need to manually remove these sources because generated build
  203. # target info provided by upstream does not consider xds disabled
  204. out_sources.append('# "{}",'.format(s))
  205. else:
  206. out_sources.append('"{}",'.format(s))
  207. return out_sources
  208. # Get dependencies for a target.
  209. def get_deps_from_target(target_dict):
  210. deps = set()
  211. if target_dict.get("secure", False):
  212. deps.add("//third_party/boringssl")
  213. if target_dict.get("build", None) == "protoc":
  214. deps.add("//third_party/protobuf:protoc_lib")
  215. name = target_dict.get("name", None)
  216. if name in ("grpc++", "grpc++_codegen_lib"):
  217. deps.add("//third_party/protobuf:protobuf_lite")
  218. elif name in ("grpc", "grpc_unsecure"):
  219. deps.add("//third_party/zlib")
  220. add_absl = False
  221. add_boring_ssl = False
  222. for d in target_dict.get("deps", []):
  223. if d.startswith('libssl'):
  224. add_boring_ssl = True
  225. elif d.startswith('absl'):
  226. add_absl = True
  227. elif d.startswith(("//", ":")):
  228. deps.add(d)
  229. else:
  230. deps.add(":%s" % d)
  231. if add_absl:
  232. deps.add("//third_party/abseil-cpp:absl")
  233. if add_boring_ssl:
  234. deps.add("//third_party/boringssl",)
  235. return list(deps)
  236. # Get dependencies for a list of sources.
  237. def get_deps_from_sources(sources):
  238. deps = set()
  239. if needs_ares(sources):
  240. deps.add(":cares")
  241. deps.add(":address_sorting")
  242. return list(deps)
  243. def needs_ares(srcs):
  244. return any("/c_ares/" in f for f in srcs) if srcs else False
  245. def needs_address_sorting(sources):
  246. return needs_ares(sources) or any("address_sorting" in s for s in sources)
  247. def get_include_dirs(sources):
  248. dirs = []
  249. if needs_ares(sources):
  250. dirs = [":cares"]
  251. if needs_address_sorting(sources):
  252. dirs.append("src/third_party/address_sorting/include")
  253. return dirs
  254. def get_extra_stuff():
  255. extra_stuff = []
  256. extra_stuff.append('visibility = [ "./*" ]')
  257. extra_stuff.append('if (!grpc_use_static_linking) {')
  258. extra_stuff.append(' configs -= [ "//build/config/gcc:symbol_visibility_hidden" ]')
  259. extra_stuff.append(' configs += [ "//build/config/gcc:symbol_visibility_default" ]')
  260. extra_stuff.append('}')
  261. return extra_stuff
  262. def strip_sources(sources):
  263. exceptions = [
  264. "src/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.h",
  265. "src/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.cc",
  266. "src/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc",
  267. ]
  268. return [f for f in sources
  269. if "ruby_generator" not in f
  270. and not (re.match("src/src/core/ext/filters/client_channel/lb_policy/.*/.*",f)
  271. and not f in exceptions)
  272. and not re.match("src/src/core/ext/filters/client_channel/resolver/xds/.*",f)
  273. and not re.match("src/src/core/ext/xds/.*",f)
  274. ]
  275. def adjust_srcs(sources):
  276. return ["src/" + f for f in sources]
  277. def get_sources(target):
  278. sources = (([] if not hasattr(target, "public_headers") else target.public_headers or []) +
  279. (target.headers or []) +
  280. (target.src or []))
  281. return adjust_srcs(sources)
  282. def in_main_lib(lib):
  283. main_libs = ("gpr", "grpc", "grpc++")
  284. return lib.name in main_libs
  285. def wanted_lib(lib):
  286. wanted_libs = ("grpc_plugin_support", "address_sorting", "upb")
  287. return lib.build in ("all", "protoc") and lib.get("name", "") in wanted_libs
  288. def wanted_binary(tgt):
  289. wanted_binaries = ("grpc_cpp_plugin",)
  290. return tgt.build == "protoc" and tgt.get("name", "") in wanted_binaries
  291. def only_on_host_toolchain(tgt):
  292. return tgt.get("name", "") in ("grpc_plugin_support", "grpc_cpp_plugin")
  293. def find_sources(path):
  294. sources = []
  295. for (root, ds, fs) in os.walk(path):
  296. for f in fs:
  297. ext = os.path.splitext(f)
  298. if (len(ext) > 1) and (ext[1] in ('.h', '.c')):
  299. sources.append(os.path.join(root, f))
  300. return ["src/" + s for s in sources]
  301. cares_sources = glob.glob('third_party/cares/cares/include/*.h') + \
  302. glob.glob('third_party/cares/cares/src/lib/*.c')
  303. # cares is only used in chromecast. Use chromium/src/third_party/cares in chromecast
  304. cares_sources = gn_sort([s.replace('third_party/cares/cares', '//third_party/cares')
  305. for s in cares_sources])
  306. cares_sources = [s for s in cares_sources if s not in (
  307. '//third_party/cares/cares/src/lib/ahost.c',
  308. '//third_party/cares/cares/src/lib/adig.c',
  309. '//third_party/cares/cares/src/lib/acountry.c')]
  310. %>\
  311. <%doc>
  312. Body of GN file
  313. </%doc>\
  314. ${cc_main_library(libs)}
  315. % for lib in libs:
  316. % if wanted_lib(lib):
  317. % if only_on_host_toolchain(lib):
  318. # Only compile the plugin for the host architecture.
  319. if (current_toolchain == host_toolchain) {
  320. ${cc_library(lib, " ", True)}
  321. }
  322. % else:
  323. ${cc_library(lib, "", False)}
  324. % endif
  325. % endif
  326. % endfor
  327. % for tgt in targets:
  328. % if wanted_binary(tgt):
  329. % if only_on_host_toolchain(tgt):
  330. # Only compile the plugin for the host architecture.
  331. if (current_toolchain == host_toolchain) {${cc_binary(tgt, " ")}}
  332. % else:
  333. ${cc_binary(tgt, "")}
  334. % endif
  335. % endif
  336. % endfor
  337. <%doc>
  338. Template Functions
  339. </%doc>\
  340. <%def name="cc_main_library(libs)">\
  341. <%
  342. extra_configs = [':grpc_config_private']
  343. sources = []
  344. headers = []
  345. deps = []
  346. upb_sources = []
  347. for lib in libs:
  348. if lib.name == 'upb':
  349. upb_sources = get_sources(lib)
  350. for lib in libs:
  351. if in_main_lib(lib):
  352. if lib.src:
  353. sources += lib.src
  354. if lib.headers:
  355. headers += lib.headers
  356. if lib.public_headers:
  357. headers += lib.public_headers
  358. deps += get_deps_from_target(lib)
  359. headers = adjust_srcs(headers)
  360. headers = [f for f in headers if f not in upb_sources]
  361. headers = strip_sources(headers)
  362. sources = adjust_srcs(sources)
  363. sources = [f for f in sources if f not in upb_sources]
  364. sources = strip_sources(sources)
  365. (sources, repeated1, repeated2) = find_repeated(sources)
  366. deps = [d for d in deps if d not in (':gpr', ':grpc')]
  367. cc_lib_name = 'grpc++_cc'
  368. h_lib_name = 'grpc++_h'
  369. repeated_lib_name1 = 'grpc++_repeated1'
  370. repeated_lib_name2 = 'grpc++_repeated2'
  371. other_deps = deps[:]
  372. other_deps.append(":{}".format(h_lib_name))
  373. deps_so = [
  374. ':{}'.format(cc_lib_name),
  375. ':{}'.format(repeated_lib_name1),
  376. ':{}'.format(repeated_lib_name2),
  377. ]
  378. public_deps_so = [
  379. ':{}'.format(h_lib_name),
  380. ]
  381. extra_stuff = get_extra_stuff()
  382. %>\
  383. grpc_so("grpc++") {
  384. deps = [
  385. % for dep in deps_so:
  386. "${dep}",
  387. % endfor
  388. ]
  389. public_deps = [
  390. % for dep in public_deps_so:
  391. "${dep}",
  392. % endfor
  393. ]
  394. }
  395. # GN doesn't like .cc files with the same base name in the same target.
  396. # Moving them to another target.
  397. ${cc_library_internal(h_lib_name, '', headers, deps, extra_stuff, extra_configs)}
  398. ${cc_library_internal(cc_lib_name, '', sources, other_deps, extra_stuff, extra_configs)}
  399. ${cc_library_internal(repeated_lib_name1, '', repeated1, other_deps, extra_stuff, extra_configs)}
  400. ${cc_library_internal(repeated_lib_name2, '', repeated2, other_deps, extra_stuff, extra_configs)}
  401. </%def>\
  402. <%def name="cc_library(lib, indent, is_host)">\
  403. <%
  404. sources = get_sources(lib)
  405. sources = strip_sources(sources)
  406. repeated_lib_name1 = "{}_repeated1".format(lib.name)
  407. repeated_lib_name2 = "{}_repeated2".format(lib.name)
  408. (sources, repeated1, repeated2) = find_repeated(sources)
  409. extra_configs = [':grpc_config_private']
  410. extra_stuff = []
  411. target_type = 'source_set'
  412. if not is_host:
  413. extra_stuff = get_extra_stuff()
  414. deps = get_deps_from_target(lib)
  415. repeated_deps = deps[:]
  416. if repeated1:
  417. deps.append(":{}".format(repeated_lib_name1))
  418. if repeated2:
  419. deps.append(":{}".format(repeated_lib_name2))
  420. %>\
  421. ${cc_library_internal(lib.name, indent, sources, deps, extra_stuff, extra_configs)}
  422. % if repeated1:
  423. # GN doesn't like .cc files with the same base name in the same target.
  424. # Moving them to another target.
  425. ${cc_library_internal(repeated_lib_name1, indent, repeated1, repeated_deps, extra_stuff, extra_configs)}
  426. % endif
  427. % if repeated2:
  428. # There are some .cc files that are in multiple places. GN doesn't like
  429. # that. Moving them to another target.
  430. ${cc_library_internal(repeated_lib_name2, indent, repeated2, repeated_deps, extra_stuff, extra_configs)}
  431. % endif
  432. </%def>\
  433. <%def name="cc_library_internal(name, indent, sources, lib_deps, extra_stuff, extra_configs)">\
  434. <%
  435. include_dirs = get_include_dirs(sources)
  436. lib_deps += get_deps_from_sources(sources)
  437. lib_deps = gn_sort(lib_deps)
  438. sources = gn_sort(sources)
  439. sources = get_commented_sources(sources)
  440. %>\
  441. ${indent}source_set("${name}") {
  442. % if sources:
  443. ${indent} sources = [
  444. % for src in sources:
  445. ${indent} ${src}
  446. % endfor
  447. ${indent} ]
  448. % endif
  449. % if lib_deps:
  450. ${indent} deps = [
  451. % for dep in lib_deps:
  452. ${indent} "${dep}",
  453. % endfor
  454. ${indent} ]
  455. % endif
  456. ${indent} public_configs = [
  457. ${indent} ":grpc_config",
  458. ${indent} ]
  459. % if extra_configs:
  460. ${indent} configs += [
  461. % for config in extra_configs:
  462. ${indent} "${config}",
  463. % endfor
  464. ${indent} ]
  465. % endif
  466. % if include_dirs:
  467. ${indent} include_dirs = [
  468. % for d in include_dirs:
  469. ${indent} "${d}",
  470. % endfor
  471. ${indent} ]
  472. % endif
  473. % if extra_stuff:
  474. % for e in extra_stuff:
  475. ${indent} ${e}
  476. % endfor
  477. % endif
  478. ${indent}}\
  479. </%def>
  480. <%def name="cc_binary(tgt, indent)">\
  481. <%
  482. sources = ["src/"+s for s in tgt.src]
  483. sources = gn_sort(sources)
  484. deps = get_deps_from_target(tgt) + get_deps_from_sources(sources)
  485. deps = gn_sort(deps)
  486. %>
  487. ${indent}executable("${tgt.name}") {
  488. ${indent} sources = [
  489. % for src in sources:
  490. ${indent} "${src}",
  491. % endfor
  492. ${indent} ]
  493. ${indent} deps = [
  494. % for dep in deps:
  495. ${indent} "${dep}",
  496. % endfor
  497. ${indent} ]
  498. ${indent} configs += [
  499. ${indent} "//third_party/protobuf:protobuf_config",
  500. ${indent} ]
  501. ${indent} public_configs = [ ":grpc_config" ]
  502. ${indent}}
  503. </%def><%!
  504. %>\
  505. <%doc>
  506. Manual targets
  507. </%doc>\
  508. config("cares_config") {
  509. cflags = [
  510. "-Wno-macro-redefined",
  511. "-Wno-unused-variable",
  512. ]
  513. }
  514. source_set("cares") {
  515. sources = [
  516. "src/third_party/cares/ares_build.h",
  517. ]
  518. if (enable_grpc_ares) {
  519. include_dirs = [ "//third_party/cares/include" ]
  520. sources += [
  521. % for src in cares_sources:
  522. "${src}",
  523. % endfor
  524. ]
  525. }
  526. if (is_android) {
  527. sources += [ "src/third_party/cares/config_android/ares_config.h" ]
  528. configs += [ ":cares_config" ]
  529. } else if (is_fuchsia) {
  530. sources += [ "third_party/cares/config_fuchsia/ares_config.h" ]
  531. } else {
  532. sources += [ "src/third_party/cares/config_linux/ares_config.h" ]
  533. }
  534. deps = [
  535. "//third_party/boringssl",
  536. ]
  537. public_configs = [ ":grpc_config" ]
  538. }
  539. group("fuzzers") {}
  540. if (is_android) {
  541. # gRPC BinderTransport's Java API and internal code, which cannot be handled by GN's source_set
  542. # and we need manually create a android_library target for them.
  543. android_library("binder_java_helper") {
  544. sources = [
  545. "src/src/core/ext/transport/binder/java/io/grpc/binder/cpp/GrpcBinderConnection.java",
  546. "src/src/core/ext/transport/binder/java/io/grpc/binder/cpp/GrpcCppServerBuilder.java",
  547. "src/src/core/ext/transport/binder/java/io/grpc/binder/cpp/NativeConnectionHelper.java",
  548. ]
  549. }
  550. }