grpc_build_system.bzl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 == "cares":
  47. ret += select({
  48. "//:grpc_no_ares": [],
  49. "//conditions:default": ["//external:cares"],
  50. })
  51. elif dep == "cronet_c_for_grpc":
  52. ret += ["//third_party/objective_c/Cronet:cronet_c_for_grpc"]
  53. elif dep.startswith("absl/"):
  54. ret += ["@com_google_absl//" + dep]
  55. else:
  56. ret += ["//external:" + dep]
  57. return ret
  58. def grpc_cc_library(
  59. name,
  60. srcs = [],
  61. public_hdrs = [],
  62. hdrs = [],
  63. external_deps = [],
  64. defines = [],
  65. deps = [],
  66. select_deps = None,
  67. standalone = False,
  68. language = "C++",
  69. testonly = False,
  70. visibility = None,
  71. alwayslink = 0,
  72. data = [],
  73. use_cfstream = False,
  74. tags = [],
  75. linkstatic = False):
  76. copts = []
  77. if use_cfstream:
  78. copts = if_mac(["-DGRPC_CFSTREAM"])
  79. if language.upper() == "C":
  80. copts = copts + if_not_windows(["-std=c99"])
  81. linkopts = if_not_windows(["-pthread"])
  82. if use_cfstream:
  83. linkopts = linkopts + if_mac(["-framework CoreFoundation"])
  84. if select_deps:
  85. deps += select(select_deps)
  86. native.cc_library(
  87. name = name,
  88. srcs = srcs,
  89. defines = defines +
  90. select({
  91. "//:grpc_no_ares": ["GRPC_ARES=0"],
  92. "//conditions:default": [],
  93. }) +
  94. select({
  95. "//:remote_execution": ["GRPC_PORT_ISOLATED_RUNTIME=1"],
  96. "//conditions:default": [],
  97. }) +
  98. select({
  99. "//:grpc_allow_exceptions": ["GRPC_ALLOW_EXCEPTIONS=1"],
  100. "//:grpc_disallow_exceptions": ["GRPC_ALLOW_EXCEPTIONS=0"],
  101. "//conditions:default": [],
  102. }),
  103. hdrs = hdrs + public_hdrs,
  104. deps = deps + _get_external_deps(external_deps),
  105. copts = GRPC_DEFAULT_COPTS + copts,
  106. visibility = visibility,
  107. testonly = testonly,
  108. linkopts = linkopts,
  109. includes = [
  110. "include",
  111. "src/core/ext/upb-generated", # Once upb code-gen issue is resolved, remove this.
  112. "src/core/ext/upbdefs-generated", # Once upb code-gen issue is resolved, remove this.
  113. ],
  114. alwayslink = alwayslink,
  115. data = data,
  116. tags = tags,
  117. linkstatic = linkstatic,
  118. )
  119. def grpc_proto_plugin(name, srcs = [], deps = []):
  120. native.cc_binary(
  121. name = name,
  122. srcs = srcs,
  123. deps = deps,
  124. )
  125. def grpc_proto_library(
  126. name,
  127. srcs = [],
  128. deps = [],
  129. well_known_protos = False,
  130. has_services = True,
  131. use_external = False,
  132. generate_mocks = False):
  133. cc_grpc_library(
  134. name = name,
  135. srcs = srcs,
  136. deps = deps,
  137. well_known_protos = well_known_protos,
  138. proto_only = not has_services,
  139. use_external = use_external,
  140. generate_mocks = generate_mocks,
  141. )
  142. def ios_cc_test(
  143. name,
  144. tags = [],
  145. **kwargs):
  146. ios_test_adapter = "//third_party/objective_c/google_toolbox_for_mac:GTM_GoogleTestRunner_GTM_USING_XCTEST"
  147. test_lib_ios = name + "_test_lib_ios"
  148. ios_tags = tags + ["manual", "ios_cc_test"]
  149. if not any([t for t in tags if t.startswith("no_test_ios")]):
  150. native.objc_library(
  151. name = test_lib_ios,
  152. srcs = kwargs.get("srcs"),
  153. deps = kwargs.get("deps"),
  154. copts = kwargs.get("copts"),
  155. tags = ios_tags,
  156. alwayslink = 1,
  157. testonly = 1,
  158. )
  159. ios_test_deps = [ios_test_adapter, ":" + test_lib_ios]
  160. ios_unit_test(
  161. name = name + "_on_ios",
  162. size = kwargs.get("size"),
  163. tags = ios_tags,
  164. minimum_os_version = "9.0",
  165. deps = ios_test_deps,
  166. )
  167. 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):
  168. copts = if_mac(["-DGRPC_CFSTREAM"])
  169. if language.upper() == "C":
  170. copts = copts + if_not_windows(["-std=c99"])
  171. # NOTE: these attributes won't be used for the poller-specific versions of a test
  172. # automatically, you need to set them explicitly (if applicable)
  173. args = {
  174. "srcs": srcs,
  175. "args": args,
  176. "data": data,
  177. "deps": deps + _get_external_deps(external_deps),
  178. "copts": GRPC_DEFAULT_COPTS + copts,
  179. "linkopts": if_not_windows(["-pthread"]),
  180. "size": size,
  181. "timeout": timeout,
  182. "exec_compatible_with": exec_compatible_with,
  183. "exec_properties": exec_properties,
  184. "shard_count": shard_count,
  185. "flaky": flaky,
  186. }
  187. if uses_polling:
  188. # the vanilla version of the test should run on platforms that only
  189. # support a single poller
  190. native.cc_test(
  191. name = name,
  192. testonly = True,
  193. tags = (tags + [
  194. "no_linux", # linux supports multiple pollers
  195. ]),
  196. **args
  197. )
  198. # on linux we run the same test multiple times, once for each poller
  199. for poller in POLLERS:
  200. native.sh_test(
  201. name = name + "@poller=" + poller,
  202. data = [name] + data,
  203. srcs = [
  204. "//test/core/util:run_with_poller_sh",
  205. ],
  206. size = size,
  207. timeout = timeout,
  208. args = [
  209. poller,
  210. "$(location %s)" % name,
  211. ] + args["args"],
  212. tags = (tags + ["no_windows", "no_mac"]),
  213. exec_compatible_with = exec_compatible_with,
  214. exec_properties = exec_properties,
  215. shard_count = shard_count,
  216. flaky = flaky,
  217. )
  218. else:
  219. # the test behavior doesn't depend on polling, just generate the test
  220. native.cc_test(name = name, tags = tags + ["no_uses_polling"], **args)
  221. ios_cc_test(
  222. name = name,
  223. tags = tags,
  224. **args
  225. )
  226. def grpc_cc_binary(name, srcs = [], deps = [], external_deps = [], args = [], data = [], language = "C++", testonly = False, linkshared = False, linkopts = [], tags = [], features = []):
  227. copts = []
  228. if language.upper() == "C":
  229. copts = ["-std=c99"]
  230. native.cc_binary(
  231. name = name,
  232. srcs = srcs,
  233. args = args,
  234. data = data,
  235. testonly = testonly,
  236. linkshared = linkshared,
  237. deps = deps + _get_external_deps(external_deps),
  238. copts = GRPC_DEFAULT_COPTS + copts,
  239. linkopts = if_not_windows(["-pthread"]) + linkopts,
  240. tags = tags,
  241. features = features,
  242. )
  243. def grpc_generate_one_off_targets():
  244. # In open-source, grpc_objc* libraries depend directly on //:grpc
  245. native.alias(
  246. name = "grpc_objc",
  247. actual = "//:grpc",
  248. )
  249. def grpc_generate_objc_one_off_targets():
  250. pass
  251. def grpc_sh_test(name, srcs, args = [], data = []):
  252. native.sh_test(
  253. name = name,
  254. srcs = srcs,
  255. args = args,
  256. data = data,
  257. )
  258. def grpc_sh_binary(name, srcs, data = []):
  259. native.sh_binary(
  260. name = name,
  261. srcs = srcs,
  262. data = data,
  263. )
  264. def grpc_py_binary(
  265. name,
  266. srcs,
  267. data = [],
  268. deps = [],
  269. external_deps = [],
  270. testonly = False,
  271. python_version = "PY2",
  272. **kwargs):
  273. native.py_binary(
  274. name = name,
  275. srcs = srcs,
  276. testonly = testonly,
  277. data = data,
  278. deps = deps + _get_external_deps(external_deps),
  279. python_version = python_version,
  280. **kwargs
  281. )
  282. def grpc_package(name, visibility = "private", features = []):
  283. if visibility == "tests":
  284. visibility = ["//test:__subpackages__"]
  285. elif visibility == "public":
  286. visibility = ["//visibility:public"]
  287. elif visibility == "private":
  288. visibility = []
  289. else:
  290. fail("Unknown visibility " + visibility)
  291. if len(visibility) != 0:
  292. native.package(
  293. default_visibility = visibility,
  294. features = features,
  295. )
  296. def grpc_objc_library(
  297. name,
  298. srcs = [],
  299. hdrs = [],
  300. textual_hdrs = [],
  301. data = [],
  302. deps = [],
  303. defines = [],
  304. includes = [],
  305. visibility = ["//visibility:public"]):
  306. """The grpc version of objc_library, only used for the Objective-C library compilation
  307. Args:
  308. name: name of target
  309. hdrs: public headers
  310. srcs: all source files (.m)
  311. textual_hdrs: private headers
  312. data: any other bundle resources
  313. defines: preprocessors
  314. includes: added to search path, always [the path to objc directory]
  315. deps: dependencies
  316. visibility: visibility, default to public
  317. """
  318. native.objc_library(
  319. name = name,
  320. hdrs = hdrs,
  321. srcs = srcs,
  322. textual_hdrs = textual_hdrs,
  323. data = data,
  324. deps = deps,
  325. defines = defines,
  326. includes = includes,
  327. visibility = visibility,
  328. )
  329. def grpc_upb_proto_library(name, deps):
  330. upb_proto_library(name = name, deps = deps)
  331. def python_config_settings():
  332. native.config_setting(
  333. name = "python3",
  334. flag_values = {"@bazel_tools//tools/python:python_version": "PY3"},
  335. )