grpc_build_system.bzl 11 KB

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