• 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
26# The set of pollers to test against if a test exercises polling
27POLLERS = ["epollex", "epollsig", "epoll1", "poll", "poll-cv"]
28
29def if_not_windows(a):
30    return select({
31        "//:windows": [],
32        "//:windows_msvc": [],
33        "//conditions:default": a,
34    })
35
36def _get_external_deps(external_deps):
37    ret = []
38    for dep in external_deps:
39        if dep == "address_sorting":
40            ret += ["//third_party/address_sorting"]
41        elif dep == "cares":
42            ret += select({
43                "//:grpc_no_ares": [],
44                "//conditions:default": ["//external:cares"],
45            })
46        else:
47            ret += ["//external:" + dep]
48    return ret
49
50def _maybe_update_cc_library_hdrs(hdrs):
51    ret = []
52    hdrs_to_update = {
53        "third_party/objective_c/Cronet/bidirectional_stream_c.h": "//third_party:objective_c/Cronet/bidirectional_stream_c.h",
54    }
55    for h in hdrs:
56        if h in hdrs_to_update.keys():
57            ret.append(hdrs_to_update[h])
58        else:
59            ret.append(h)
60    return ret
61
62def grpc_cc_library(
63        name,
64        srcs = [],
65        public_hdrs = [],
66        hdrs = [],
67        external_deps = [],
68        deps = [],
69        standalone = False,
70        language = "C++",
71        testonly = False,
72        visibility = None,
73        alwayslink = 0,
74        data = []):
75    copts = []
76    if language.upper() == "C":
77        copts = if_not_windows(["-std=c99"])
78    native.cc_library(
79        name = name,
80        srcs = srcs,
81        defines = select({
82                      "//:grpc_no_ares": ["GRPC_ARES=0"],
83                      "//conditions:default": [],
84                  }) +
85                  select({
86                      "//:remote_execution": ["GRPC_PORT_ISOLATED_RUNTIME=1"],
87                      "//conditions:default": [],
88                  }) +
89                  select({
90                      "//:grpc_allow_exceptions": ["GRPC_ALLOW_EXCEPTIONS=1"],
91                      "//:grpc_disallow_exceptions": ["GRPC_ALLOW_EXCEPTIONS=0"],
92                      "//conditions:default": [],
93                  }),
94        hdrs = _maybe_update_cc_library_hdrs(hdrs + public_hdrs),
95        deps = deps + _get_external_deps(external_deps),
96        copts = copts,
97        visibility = visibility,
98        testonly = testonly,
99        linkopts = if_not_windows(["-pthread"]),
100        includes = [
101            "include",
102        ],
103        alwayslink = alwayslink,
104        data = data,
105    )
106
107def grpc_proto_plugin(name, srcs = [], deps = []):
108    native.cc_binary(
109        name = name,
110        srcs = srcs,
111        deps = deps,
112    )
113
114load("//:bazel/cc_grpc_library.bzl", "cc_grpc_library")
115
116def 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
134def grpc_cc_test(name, srcs = [], deps = [], external_deps = [], args = [], data = [], uses_polling = True, language = "C++", size = "medium", timeout = "moderate", tags = []):
135    copts = []
136    if language.upper() == "C":
137        copts = if_not_windows(["-std=c99"])
138    args = {
139        "name": name,
140        "srcs": srcs,
141        "args": args,
142        "data": data,
143        "deps": deps + _get_external_deps(external_deps),
144        "copts": copts,
145        "linkopts": if_not_windows(["-pthread"]),
146        "size": size,
147        "timeout": timeout,
148    }
149    if uses_polling:
150        native.cc_test(testonly = True, tags = ["manual"], **args)
151        for poller in POLLERS:
152            native.sh_test(
153                name = name + "@poller=" + poller,
154                data = [name] + data,
155                srcs = [
156                    "//test/core/util:run_with_poller_sh",
157                ],
158                size = size,
159                timeout = timeout,
160                args = [
161                    poller,
162                    "$(location %s)" % name,
163                ] + args["args"],
164                tags = tags,
165            )
166    else:
167        native.cc_test(**args)
168
169def grpc_cc_binary(name, srcs = [], deps = [], external_deps = [], args = [], data = [], language = "C++", testonly = False, linkshared = False, linkopts = []):
170    copts = []
171    if language.upper() == "C":
172        copts = ["-std=c99"]
173    native.cc_binary(
174        name = name,
175        srcs = srcs,
176        args = args,
177        data = data,
178        testonly = testonly,
179        linkshared = linkshared,
180        deps = deps + _get_external_deps(external_deps),
181        copts = copts,
182        linkopts = if_not_windows(["-pthread"]) + linkopts,
183    )
184
185def grpc_generate_one_off_targets():
186    pass
187
188def grpc_sh_test(name, srcs, args = [], data = []):
189    native.sh_test(
190        name = name,
191        srcs = srcs,
192        args = args,
193        data = data,
194    )
195
196def grpc_sh_binary(name, srcs, data = []):
197    native.sh_binary(
198        name = name,
199        srcs = srcs,
200        data = data,
201    )
202
203def grpc_py_binary(name, srcs, data = [], deps = [], external_deps = [], testonly = False):
204    native.py_binary(
205        name = name,
206        srcs = srcs,
207        testonly = testonly,
208        data = data,
209        deps = deps + _get_external_deps(external_deps),
210    )
211
212def grpc_package(name, visibility = "private", features = []):
213    if visibility == "tests":
214        visibility = ["//test:__subpackages__"]
215    elif visibility == "public":
216        visibility = ["//visibility:public"]
217    elif visibility == "private":
218        visibility = []
219    else:
220        fail("Unknown visibility " + visibility)
221
222    if len(visibility) != 0:
223        native.package(
224            default_visibility = visibility,
225            features = features,
226        )
227