1 # Copyright 2016 gRPC authors.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 # This is for the gRPC build system. This isn't intended to be used outsite of
17 # the BUILD file for gRPC. It contains the mapping for the template system we
18 # use to generate other platform's build system files.
20 # Please consider that there should be a high bar for additions and changes to
22 # Each rule listed must be re-written for Google's internal build system, and
23 # each change must be ported from one to the other.
26 load("//bazel:cc_grpc_library.bzl", "cc_grpc_library")
27 load("//bazel:copts.bzl", "GRPC_DEFAULT_COPTS")
28 load("@upb//bazel:upb_proto_library.bzl", "upb_proto_library")
29 load("@build_bazel_rules_apple//apple:ios.bzl", "ios_unit_test")
31 # The set of pollers to test against if a test exercises polling
32 POLLERS = ["epollex", "epoll1", "poll"]
34 def if_not_windows(a):
37 "//:windows_msvc": [],
38 "//conditions:default": a,
44 "//conditions:default": [],
47 def _get_external_deps(external_deps):
49 for dep in external_deps:
50 if dep == "address_sorting":
51 ret += ["//third_party/address_sorting"]
53 ret += ["//third_party/xxhash"]
56 "//:grpc_no_ares": [],
57 "//conditions:default": ["//external:cares"],
59 elif dep == "cronet_c_for_grpc":
60 ret += ["//third_party/objective_c/Cronet:cronet_c_for_grpc"]
61 elif dep.startswith("absl/"):
62 ret += ["@com_google_absl//" + dep]
64 ret += ["//external:" + dep]
67 def _update_visibility(visibility):
68 if visibility == None:
71 # Visibility rules prefixed with '@grpc_' are used to flag different visibility rule
73 PUBLIC = ["//visibility:public"]
74 PRIVATE = ["//:__subpackages__"]
75 VISIBILITY_TARGETS = {
76 "alt_gpr_base_legacy": PRIVATE,
77 "alt_grpc++_base_legacy": PRIVATE,
78 "alt_grpc_base_legacy": PRIVATE,
79 "alt_grpc++_base_unsecure_legacy": PRIVATE,
80 "alts_frame_protector": PRIVATE,
82 "client_channel": PRIVATE,
83 "debug_location": PRIVATE,
84 "endpoint_tests": PRIVATE,
86 "grpc_opencensus_plugin": PUBLIC,
87 "grpc_resolver_fake": PRIVATE,
88 "grpc++_test": PRIVATE,
90 "ref_counted_ptr": PRIVATE,
92 "tsi_interface": PRIVATE,
97 for rule in visibility:
98 if rule.startswith("@grpc:"):
99 for replacement in VISIBILITY_TARGETS[rule[len("@grpc:"):]]:
100 final_visibility.append(replacement)
102 final_visibility.append(rule)
103 return [x for x in final_visibility]
120 use_cfstream = False,
123 visibility = _update_visibility(visibility)
126 copts = if_mac(["-DGRPC_CFSTREAM"])
127 if language.upper() == "C":
128 copts = copts + if_not_windows(["-std=c99"])
129 linkopts = if_not_windows(["-pthread"])
131 linkopts = linkopts + if_mac(["-framework CoreFoundation"])
134 deps += select(select_deps)
141 "//:grpc_no_ares": ["GRPC_ARES=0"],
142 "//conditions:default": [],
145 "//:remote_execution": ["GRPC_PORT_ISOLATED_RUNTIME=1"],
146 "//conditions:default": [],
149 "//:grpc_allow_exceptions": ["GRPC_ALLOW_EXCEPTIONS=1"],
150 "//:grpc_disallow_exceptions": ["GRPC_ALLOW_EXCEPTIONS=0"],
151 "//conditions:default": [],
153 hdrs = hdrs + public_hdrs,
154 deps = deps + _get_external_deps(external_deps),
155 copts = GRPC_DEFAULT_COPTS + copts,
156 visibility = visibility,
161 "src/core/ext/upb-generated", # Once upb code-gen issue is resolved, remove this.
162 "src/core/ext/upbdefs-generated", # Once upb code-gen issue is resolved, remove this.
164 alwayslink = alwayslink,
167 linkstatic = linkstatic,
170 def grpc_proto_plugin(name, srcs = [], deps = []):
177 def grpc_proto_library(
181 well_known_protos = False,
183 use_external = False,
184 generate_mocks = False):
189 well_known_protos = well_known_protos,
190 proto_only = not has_services,
191 use_external = use_external,
192 generate_mocks = generate_mocks,
199 ios_test_adapter = "//third_party/objective_c/google_toolbox_for_mac:GTM_GoogleTestRunner_GTM_USING_XCTEST"
201 test_lib_ios = name + "_test_lib_ios"
202 ios_tags = tags + ["manual", "ios_cc_test"]
203 if not any([t for t in tags if t.startswith("no_test_ios")]):
206 srcs = kwargs.get("srcs"),
207 deps = kwargs.get("deps"),
208 copts = kwargs.get("copts"),
213 ios_test_deps = [ios_test_adapter, ":" + test_lib_ios]
215 name = name + "_on_ios",
216 size = kwargs.get("size"),
218 minimum_os_version = "9.0",
219 deps = ios_test_deps,
222 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 = []):
223 copts = copts + if_mac(["-DGRPC_CFSTREAM"])
224 if language.upper() == "C":
225 copts = copts + if_not_windows(["-std=c99"])
227 # NOTE: these attributes won't be used for the poller-specific versions of a test
228 # automatically, you need to set them explicitly (if applicable)
233 "deps": deps + _get_external_deps(external_deps),
234 "copts": GRPC_DEFAULT_COPTS + copts,
235 "linkopts": if_not_windows(["-pthread"]),
238 "exec_compatible_with": exec_compatible_with,
239 "exec_properties": exec_properties,
240 "shard_count": shard_count,
244 # the vanilla version of the test should run on platforms that only
245 # support a single poller
250 "no_linux", # linux supports multiple pollers
255 # on linux we run the same test multiple times, once for each poller
256 for poller in POLLERS:
258 name = name + "@poller=" + poller,
259 data = [name] + data,
261 "//test/core/util:run_with_poller_sh",
267 "$(location %s)" % name,
269 tags = (tags + ["no_windows", "no_mac"]),
270 exec_compatible_with = exec_compatible_with,
271 exec_properties = exec_properties,
272 shard_count = shard_count,
276 # the test behavior doesn't depend on polling, just generate the test
277 native.cc_test(name = name, tags = tags + ["no_uses_polling"], **args)
284 def grpc_cc_binary(name, srcs = [], deps = [], external_deps = [], args = [], data = [], language = "C++", testonly = False, linkshared = False, linkopts = [], tags = [], features = []):
286 if language.upper() == "C":
294 linkshared = linkshared,
295 deps = deps + _get_external_deps(external_deps),
296 copts = GRPC_DEFAULT_COPTS + copts,
297 linkopts = if_not_windows(["-pthread"]) + linkopts,
302 def grpc_generate_one_off_targets():
303 # In open-source, grpc_objc* libraries depend directly on //:grpc
309 def grpc_generate_objc_one_off_targets():
312 def grpc_sh_test(name, srcs, args = [], data = []):
320 def grpc_sh_binary(name, srcs, data = []):
334 python_version = "PY2",
341 deps = deps + _get_external_deps(external_deps),
342 python_version = python_version,
346 def grpc_package(name, visibility = "private", features = []):
347 if visibility == "tests":
348 visibility = ["//test:__subpackages__"]
349 elif visibility == "public":
350 visibility = ["//visibility:public"]
351 elif visibility == "private":
354 fail("Unknown visibility " + visibility)
356 if len(visibility) != 0:
358 default_visibility = visibility,
362 def grpc_objc_library(
371 visibility = ["//visibility:public"]):
372 """The grpc version of objc_library, only used for the Objective-C library compilation
377 srcs: all source files (.m)
378 textual_hdrs: private headers
379 data: any other bundle resources
380 defines: preprocessors
381 includes: added to search path, always [the path to objc directory]
383 visibility: visibility, default to public
390 textual_hdrs = textual_hdrs,
395 visibility = visibility,
398 def grpc_upb_proto_library(name, deps):
399 upb_proto_library(name = name, deps = deps)
401 def python_config_settings():
402 native.config_setting(
404 flag_values = {"@bazel_tools//tools/python:python_version": "PY3"},