grpc_build_system.bzl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. # Copyright 2016 gRPC authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # This is for the gRPC build system. This isn't intended to be used outsite of
  16. # the BUILD file for gRPC. It contains the mapping for the template system we
  17. # use to generate other platform's build system files.
  18. #
  19. # Please consider that there should be a high bar for additions and changes to
  20. # this file.
  21. # Each rule listed must be re-written for Google's internal build system, and
  22. # each change must be ported from one to the other.
  23. #
  24. load("//bazel:cc_grpc_library.bzl", "cc_grpc_library")
  25. load("//bazel:copts.bzl", "GRPC_DEFAULT_COPTS")
  26. load("@upb//bazel:upb_proto_library.bzl", "upb_proto_library")
  27. load("@build_bazel_rules_apple//apple:ios.bzl", "ios_unit_test")
  28. # The set of pollers to test against if a test exercises polling
  29. POLLERS = ["epollex", "epoll1", "poll"]
  30. def if_not_windows(a):
  31. return select({
  32. "//:windows": [],
  33. "//:windows_msvc": [],
  34. "//conditions:default": a,
  35. })
  36. def if_mac(a):
  37. return select({
  38. "//:mac_x86_64": a,
  39. "//conditions:default": [],
  40. })
  41. def _get_external_deps(external_deps):
  42. ret = []
  43. for dep in external_deps:
  44. if dep == "address_sorting":
  45. ret += ["//third_party/address_sorting"]
  46. elif dep == "xxhash":
  47. ret += ["//third_party/xxhash"]
  48. elif dep == "cares":
  49. ret += select({
  50. "//:grpc_no_ares": [],
  51. "//conditions:default": ["//external:cares"],
  52. })
  53. elif dep == "cronet_c_for_grpc":
  54. ret += ["//third_party/objective_c/Cronet:cronet_c_for_grpc"]
  55. elif dep.startswith("absl/"):
  56. ret += ["@com_google_absl//" + dep]
  57. else:
  58. ret += ["//external:" + dep]
  59. return ret
  60. def grpc_cc_library(
  61. name,
  62. srcs = [],
  63. public_hdrs = [],
  64. hdrs = [],
  65. external_deps = [],
  66. defines = [],
  67. deps = [],
  68. select_deps = None,
  69. standalone = False,
  70. language = "C++",
  71. testonly = False,
  72. visibility = None,
  73. alwayslink = 0,
  74. data = [],
  75. use_cfstream = False,
  76. tags = [],
  77. linkstatic = False):
  78. copts = []
  79. if use_cfstream:
  80. copts = if_mac(["-DGRPC_CFSTREAM"])
  81. if language.upper() == "C":
  82. copts = copts + if_not_windows(["-std=c99"])
  83. linkopts = if_not_windows(["-pthread"])
  84. if use_cfstream:
  85. linkopts = linkopts + if_mac(["-framework CoreFoundation"])
  86. if select_deps:
  87. deps += select(select_deps)
  88. native.cc_library(
  89. name = name,
  90. srcs = srcs,
  91. defines = defines +
  92. select({
  93. "//:grpc_no_ares": ["GRPC_ARES=0"],
  94. "//conditions:default": [],
  95. }) +
  96. select({
  97. "//:remote_execution": ["GRPC_PORT_ISOLATED_RUNTIME=1"],
  98. "//conditions:default": [],
  99. }) +
  100. select({
  101. "//:grpc_allow_exceptions": ["GRPC_ALLOW_EXCEPTIONS=1"],
  102. "//:grpc_disallow_exceptions": ["GRPC_ALLOW_EXCEPTIONS=0"],
  103. "//conditions:default": [],
  104. }),
  105. hdrs = hdrs + public_hdrs,
  106. deps = deps + _get_external_deps(external_deps),
  107. copts = GRPC_DEFAULT_COPTS + copts,
  108. visibility = visibility,
  109. testonly = testonly,
  110. linkopts = linkopts,
  111. includes = [
  112. "include",
  113. "src/core/ext/upb-generated", # Once upb code-gen issue is resolved, remove this.
  114. "src/core/ext/upbdefs-generated", # Once upb code-gen issue is resolved, remove this.
  115. ],
  116. alwayslink = alwayslink,
  117. data = data,
  118. tags = tags,
  119. linkstatic = linkstatic,
  120. )
  121. # TODO(lidiz) remove this rule once we can depend on the xDS protos internally
  122. def grpc_cc_library_xds(
  123. *args,
  124. **kwargs):
  125. grpc_cc_library(*args, **kwargs)
  126. def grpc_proto_plugin(name, srcs = [], deps = []):
  127. native.cc_binary(
  128. name = name,
  129. srcs = srcs,
  130. deps = deps,
  131. )
  132. def grpc_proto_library(
  133. name,
  134. srcs = [],
  135. deps = [],
  136. well_known_protos = False,
  137. has_services = True,
  138. use_external = False,
  139. generate_mocks = False):
  140. cc_grpc_library(
  141. name = name,
  142. srcs = srcs,
  143. deps = deps,
  144. well_known_protos = well_known_protos,
  145. proto_only = not has_services,
  146. use_external = use_external,
  147. generate_mocks = generate_mocks,
  148. )
  149. def ios_cc_test(
  150. name,
  151. tags = [],
  152. **kwargs):
  153. ios_test_adapter = "//third_party/objective_c/google_toolbox_for_mac:GTM_GoogleTestRunner_GTM_USING_XCTEST"
  154. test_lib_ios = name + "_test_lib_ios"
  155. ios_tags = tags + ["manual", "ios_cc_test"]
  156. if not any([t for t in tags if t.startswith("no_test_ios")]):
  157. native.objc_library(
  158. name = test_lib_ios,
  159. srcs = kwargs.get("srcs"),
  160. deps = kwargs.get("deps"),
  161. copts = kwargs.get("copts"),
  162. tags = ios_tags,
  163. alwayslink = 1,
  164. testonly = 1,
  165. )
  166. ios_test_deps = [ios_test_adapter, ":" + test_lib_ios]
  167. ios_unit_test(
  168. name = name + "_on_ios",
  169. size = kwargs.get("size"),
  170. tags = ios_tags,
  171. minimum_os_version = "9.0",
  172. deps = ios_test_deps,
  173. )
  174. def grpc_cc_test(name, srcs = [], deps = [], external_deps = [], args = [], data = [], uses_polling = True, language = "C++", size = "medium", timeout = None, tags = [], exec_compatible_with = [], exec_properties = {}, shard_count = None, flaky = None, copts = []):
  175. copts = copts + if_mac(["-DGRPC_CFSTREAM"])
  176. if language.upper() == "C":
  177. copts = copts + if_not_windows(["-std=c99"])
  178. # NOTE: these attributes won't be used for the poller-specific versions of a test
  179. # automatically, you need to set them explicitly (if applicable)
  180. args = {
  181. "srcs": srcs,
  182. "args": args,
  183. "data": data,
  184. "deps": deps + _get_external_deps(external_deps),
  185. "copts": GRPC_DEFAULT_COPTS + copts,
  186. "linkopts": if_not_windows(["-pthread"]),
  187. "size": size,
  188. "timeout": timeout,
  189. "exec_compatible_with": exec_compatible_with,
  190. "exec_properties": exec_properties,
  191. "shard_count": shard_count,
  192. "flaky": flaky,
  193. }
  194. if uses_polling:
  195. # the vanilla version of the test should run on platforms that only
  196. # support a single poller
  197. native.cc_test(
  198. name = name,
  199. testonly = True,
  200. tags = (tags + [
  201. "no_linux", # linux supports multiple pollers
  202. ]),
  203. **args
  204. )
  205. # on linux we run the same test multiple times, once for each poller
  206. for poller in POLLERS:
  207. native.sh_test(
  208. name = name + "@poller=" + poller,
  209. data = [name] + data,
  210. srcs = [
  211. "//test/core/util:run_with_poller_sh",
  212. ],
  213. size = size,
  214. timeout = timeout,
  215. args = [
  216. poller,
  217. "$(location %s)" % name,
  218. ] + args["args"],
  219. tags = (tags + ["no_windows", "no_mac"]),
  220. exec_compatible_with = exec_compatible_with,
  221. exec_properties = exec_properties,
  222. shard_count = shard_count,
  223. flaky = flaky,
  224. )
  225. else:
  226. # the test behavior doesn't depend on polling, just generate the test
  227. native.cc_test(name = name, tags = tags + ["no_uses_polling"], **args)
  228. ios_cc_test(
  229. name = name,
  230. tags = tags,
  231. **args
  232. )
  233. # TODO(lidiz) remove this rule once we can depend on the xDS protos internally
  234. def grpc_cc_test_xds(*args, **kwargs):
  235. grpc_cc_test(*args, **kwargs)
  236. def grpc_cc_binary(name, srcs = [], deps = [], external_deps = [], args = [], data = [], language = "C++", testonly = False, linkshared = False, linkopts = [], tags = [], features = []):
  237. copts = []
  238. if language.upper() == "C":
  239. copts = ["-std=c99"]
  240. native.cc_binary(
  241. name = name,
  242. srcs = srcs,
  243. args = args,
  244. data = data,
  245. testonly = testonly,
  246. linkshared = linkshared,
  247. deps = deps + _get_external_deps(external_deps),
  248. copts = GRPC_DEFAULT_COPTS + copts,
  249. linkopts = if_not_windows(["-pthread"]) + linkopts,
  250. tags = tags,
  251. features = features,
  252. )
  253. def grpc_generate_one_off_targets():
  254. # In open-source, grpc_objc* libraries depend directly on //:grpc
  255. native.alias(
  256. name = "grpc_objc",
  257. actual = "//:grpc",
  258. )
  259. def grpc_generate_objc_one_off_targets():
  260. pass
  261. def grpc_sh_test(name, srcs, args = [], data = []):
  262. native.sh_test(
  263. name = name,
  264. srcs = srcs,
  265. args = args,
  266. data = data,
  267. )
  268. def grpc_sh_binary(name, srcs, data = []):
  269. native.sh_binary(
  270. name = name,
  271. srcs = srcs,
  272. data = data,
  273. )
  274. def grpc_py_binary(
  275. name,
  276. srcs,
  277. data = [],
  278. deps = [],
  279. external_deps = [],
  280. testonly = False,
  281. python_version = "PY2",
  282. **kwargs):
  283. native.py_binary(
  284. name = name,
  285. srcs = srcs,
  286. testonly = testonly,
  287. data = data,
  288. deps = deps + _get_external_deps(external_deps),
  289. python_version = python_version,
  290. **kwargs
  291. )
  292. def grpc_package(name, visibility = "private", features = []):
  293. if visibility == "tests":
  294. visibility = ["//test:__subpackages__"]
  295. elif visibility == "public":
  296. visibility = ["//visibility:public"]
  297. elif visibility == "private":
  298. visibility = []
  299. else:
  300. fail("Unknown visibility " + visibility)
  301. if len(visibility) != 0:
  302. native.package(
  303. default_visibility = visibility,
  304. features = features,
  305. )
  306. def grpc_objc_library(
  307. name,
  308. srcs = [],
  309. hdrs = [],
  310. textual_hdrs = [],
  311. data = [],
  312. deps = [],
  313. defines = [],
  314. includes = [],
  315. visibility = ["//visibility:public"]):
  316. """The grpc version of objc_library, only used for the Objective-C library compilation
  317. Args:
  318. name: name of target
  319. hdrs: public headers
  320. srcs: all source files (.m)
  321. textual_hdrs: private headers
  322. data: any other bundle resources
  323. defines: preprocessors
  324. includes: added to search path, always [the path to objc directory]
  325. deps: dependencies
  326. visibility: visibility, default to public
  327. """
  328. native.objc_library(
  329. name = name,
  330. hdrs = hdrs,
  331. srcs = srcs,
  332. textual_hdrs = textual_hdrs,
  333. data = data,
  334. deps = deps,
  335. defines = defines,
  336. includes = includes,
  337. visibility = visibility,
  338. )
  339. def grpc_upb_proto_library(name, deps):
  340. upb_proto_library(name = name, deps = deps)
  341. def python_config_settings():
  342. native.config_setting(
  343. name = "python3",
  344. flag_values = {"@bazel_tools//tools/python:python_version": "PY3"},
  345. )