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