grpc_build_system.bzl 10 KB

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