Skip to content

Commit 4687688

Browse files
authored
Make Buildifier Sanity Test Strict (grpc#27807)
* Fix all lint errors in repo. * Use strict buildifier by default * Whoops. That file does not exist * Attempt fix to buildifier invocation * Add missing copyright
1 parent cb95e9f commit 4687688

106 files changed

Lines changed: 471 additions & 258 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bazel/BUILD

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
"""
16+
Contains build targets used by Starlark files in the bazel/ directory.
17+
"""
18+
1519
licenses(["notice"])
1620

1721
package(default_visibility = ["//:__subpackages__"])
1822

19-
load(":cc_grpc_library.bzl", "cc_grpc_library")
20-
2123
filegroup(
2224
name = "_gevent_test_main",
2325
srcs = ["_gevent_test_main.py"],

bazel/copts.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
"""
16+
Includes default copts.
17+
"""
18+
1519
# This is a list of llvm flags to be used when being built with use_strict_warning=1
1620
GRPC_LLVM_WARNING_FLAGS = [
1721
# Enable all & extra warnings

bazel/custom_exec_properties.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
"""
16+
Reimports constants from the grpc_custom_exec_properties repo.
17+
"""
18+
1519
load("@grpc_custom_exec_properties//:constants.bzl", _LARGE_MACHINE = "LARGE_MACHINE")
1620

1721
LARGE_MACHINE = _LARGE_MACHINE

bazel/generate_cc.bzl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ def _join_directories(directories):
5454
return "/".join(massaged_directories)
5555

5656
def generate_cc_impl(ctx):
57-
"""Implementation of the generate_cc rule."""
57+
"""Implementation of the generate_cc rule.
58+
59+
Args:
60+
ctx: The context object.
61+
Returns:
62+
The provider for the generated files.
63+
"""
5864
protos = [f for src in ctx.attr.srcs for f in src[ProtoInfo].check_deps_sources.to_list()]
5965
includes = [
6066
f
@@ -118,7 +124,7 @@ def generate_cc_impl(ctx):
118124
)
119125
tools = [ctx.executable.plugin]
120126
else:
121-
arguments += ["--cpp_out=" + ",".join(ctx.attr.flags) + ":" + dir_out]
127+
arguments.append("--cpp_out=" + ",".join(ctx.attr.flags) + ":" + dir_out)
122128
tools = []
123129

124130
arguments += [
@@ -128,7 +134,7 @@ def generate_cc_impl(ctx):
128134

129135
# Include the output directory so that protoc puts the generated code in the
130136
# right directory.
131-
arguments += ["--proto_path={0}{1}".format(dir_out, proto_root)]
137+
arguments.append("--proto_path={0}{1}".format(dir_out, proto_root))
132138
arguments += [_get_srcs_file_path(proto) for proto in protos]
133139

134140
# create a list of well known proto files if the argument is non-None
@@ -138,11 +144,11 @@ def generate_cc_impl(ctx):
138144
if f != "external/com_google_protobuf/src/google/protobuf":
139145
print(
140146
"Error: Only @com_google_protobuf//:well_known_protos is supported",
141-
)
147+
) # buildifier: disable=print
142148
else:
143149
# f points to "external/com_google_protobuf/src/google/protobuf"
144150
# add -I argument to protoc so it knows where to look for the proto files.
145-
arguments += ["-I{0}".format(f + "/../..")]
151+
arguments.append("-I{0}".format(f + "/../.."))
146152
well_known_proto_files = [
147153
f
148154
for f in ctx.attr.well_known_protos.files.to_list()
@@ -157,7 +163,7 @@ def generate_cc_impl(ctx):
157163
use_default_shell_env = True,
158164
)
159165

160-
return struct(files = depset(out_files))
166+
return struct(files = depset(out_files)) # buildifier: disable=rule-impl-return
161167

162168
_generate_cc = rule(
163169
attrs = {

bazel/generate_objc.bzl

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
15+
"""
16+
This module contains build rules relating to gRPC Objective-C.
17+
"""
18+
1419
load("@rules_proto//proto:defs.bzl", "ProtoInfo")
1520
load(
1621
"//bazel:protobuf.bzl",
@@ -44,13 +49,13 @@ def _generate_objc_impl(ctx):
4449

4550
outs = []
4651
for proto in protos:
47-
outs += [_get_output_file_name_from_proto(proto, _PROTO_HEADER_FMT)]
48-
outs += [_get_output_file_name_from_proto(proto, _PROTO_SRC_FMT)]
52+
outs.append(_get_output_file_name_from_proto(proto, _PROTO_HEADER_FMT))
53+
outs.append(_get_output_file_name_from_proto(proto, _PROTO_SRC_FMT))
4954

5055
file_path = _get_full_path_from_file(proto)
5156
if file_path in files_with_rpc:
52-
outs += [_get_output_file_name_from_proto(proto, _GRPC_PROTO_HEADER_FMT)]
53-
outs += [_get_output_file_name_from_proto(proto, _GRPC_PROTO_SRC_FMT)]
57+
outs.append(_get_output_file_name_from_proto(proto, _GRPC_PROTO_HEADER_FMT))
58+
outs.append(_get_output_file_name_from_proto(proto, _GRPC_PROTO_SRC_FMT))
5459

5560
out_files = [ctx.actions.declare_file(out) for out in outs]
5661
dir_out = _join_directories([
@@ -60,6 +65,7 @@ def _generate_objc_impl(ctx):
6065
])
6166

6267
arguments = []
68+
tools = []
6369
if ctx.executable.plugin:
6470
arguments += get_plugin_args(
6571
ctx.executable.plugin,
@@ -68,17 +74,17 @@ def _generate_objc_impl(ctx):
6874
False,
6975
)
7076
tools = [ctx.executable.plugin]
71-
arguments += ["--objc_out=" + dir_out]
77+
arguments.append("--objc_out=" + dir_out)
7278

73-
arguments += ["--proto_path=."]
79+
arguments.append("--proto_path=.")
7480
arguments += [
7581
"--proto_path={}".format(get_include_directory(i))
7682
for i in protos
7783
]
7884

7985
# Include the output directory so that protoc puts the generated code in the
8086
# right directory.
81-
arguments += ["--proto_path={}".format(dir_out)]
87+
arguments.append("--proto_path={}".format(dir_out))
8288
arguments += ["--proto_path={}".format(_get_directory_from_proto(proto)) for proto in protos]
8389
arguments += [_get_full_path_from_file(proto) for proto in protos]
8490

@@ -88,7 +94,7 @@ def _generate_objc_impl(ctx):
8894
f = ctx.attr.well_known_protos.files.to_list()[0].dirname
8995

9096
# go two levels up so that #import "google/protobuf/..." is correct
91-
arguments += ["-I{0}".format(f + "/../..")]
97+
arguments.append("-I{0}".format(f + "/../.."))
9298
well_known_proto_files = ctx.attr.well_known_protos.files.to_list()
9399
ctx.actions.run(
94100
inputs = protos + well_known_proto_files,
@@ -98,7 +104,7 @@ def _generate_objc_impl(ctx):
98104
arguments = arguments,
99105
)
100106

101-
return struct(files = depset(out_files))
107+
return struct(files = depset(out_files)) # buildifier: disable=rule-impl-return
102108

103109
def _label_to_full_file_path(src, package):
104110
if not src.startswith("//"):
@@ -198,7 +204,7 @@ def _group_objc_files_impl(ctx):
198204
for file in ctx.attr.src.files.to_list()
199205
if file.basename.endswith(suffix)
200206
]
201-
return struct(files = depset(out_files))
207+
return struct(files = depset(out_files)) # buildifier: disable=rule-impl-return
202208

203209
generate_objc_hdrs = rule(
204210
attrs = {

bazel/gevent_test.bzl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
15+
"""
16+
Houses py_grpc_gevent_test.
17+
"""
18+
1419
load("@grpc_python_dependencies//:requirements.bzl", "requirement")
1520

1621
_GRPC_LIB = "//src/python/grpcio/grpc:grpcio"
@@ -24,6 +29,16 @@ def py_grpc_gevent_test(
2429
deps = None,
2530
data = None,
2631
**kwargs):
32+
"""Runs a Python test with gevent monkeypatched in.
33+
34+
Args:
35+
name: The name of the test.
36+
srcs: The source files.
37+
main: The main file of the test.
38+
deps: The dependencies of the test.
39+
data: The data dependencies of the test.
40+
**kwargs: Any other test arguments.
41+
"""
2742
if main == None:
2843
if len(srcs) != 1:
2944
fail("When main is not provided, srcs must be of size 1.")

bazel/grpc_build_system.bzl

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
# each change must be ported from one to the other.
2424
#
2525

26+
"""
27+
Contains macros used throughout the repo.
28+
"""
29+
2630
load("//bazel:cc_grpc_library.bzl", "cc_grpc_library")
2731
load("//bazel:copts.bzl", "GRPC_DEFAULT_COPTS")
2832
load("@upb//bazel:upb_proto_library.bzl", "upb_proto_library", "upb_proto_reflection_library")
@@ -48,20 +52,20 @@ def _get_external_deps(external_deps):
4852
ret = []
4953
for dep in external_deps:
5054
if dep == "address_sorting":
51-
ret += ["//third_party/address_sorting"]
55+
ret.append("//third_party/address_sorting")
5256
elif dep == "xxhash":
53-
ret += ["//third_party/xxhash"]
57+
ret.append("//third_party/xxhash")
5458
elif dep == "cares":
5559
ret += select({
5660
"//:grpc_no_ares": [],
5761
"//conditions:default": ["//external:cares"],
5862
})
5963
elif dep == "cronet_c_for_grpc":
60-
ret += ["//third_party/objective_c/Cronet:cronet_c_for_grpc"]
64+
ret.append("//third_party/objective_c/Cronet:cronet_c_for_grpc")
6165
elif dep.startswith("absl/"):
62-
ret += ["@com_google_absl//" + dep]
66+
ret.append("@com_google_absl//" + dep)
6367
else:
64-
ret += ["//external:" + dep]
68+
ret.append("//external:" + dep)
6569
return ret
6670

6771
def _update_visibility(visibility):
@@ -120,6 +124,27 @@ def grpc_cc_library(
120124
use_cfstream = False,
121125
tags = [],
122126
linkstatic = False):
127+
"""An internal wrapper around cc_library.
128+
129+
Args:
130+
name: The name of the library.
131+
srcs: The source files.
132+
public_hdrs: The public headers.
133+
hdrs: The headers.
134+
external_deps: External depdendencies to be resolved.
135+
defines: Build defines to use.
136+
deps: cc_library deps.
137+
select_deps: deps included conditionally.
138+
standalone: Unused.
139+
language: The language of the library, e.g. C, C++.
140+
testonly: Whether the target is for tests only.
141+
visibility: The visibility of the target.
142+
alwayslink: Whether to enable alwayslink on the cc_library.
143+
data: Data dependencies.
144+
use_cfstream: Whether to use cfstream.
145+
tags: Tags to apply to the rule.
146+
linkstatic: Whether to enable linkstatic on the cc_library.
147+
"""
123148
visibility = _update_visibility(visibility)
124149
copts = []
125150
if use_cfstream:
@@ -197,6 +222,13 @@ def ios_cc_test(
197222
name,
198223
tags = [],
199224
**kwargs):
225+
"""An ios C++ test target.
226+
227+
Args:
228+
name: The name of the test.
229+
tags: The tags to apply to the test.
230+
**kwargs: All other arguments to apply.
231+
"""
200232
ios_test_adapter = "//third_party/objective_c/google_toolbox_for_mac:GTM_GoogleTestRunner_GTM_USING_XCTEST"
201233

202234
test_lib_ios = name + "_test_lib_ios"
@@ -221,6 +253,28 @@ def ios_cc_test(
221253
)
222254

223255
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 = []):
256+
"""A cc_test target for use in the gRPC repo.
257+
258+
Args:
259+
name: The name of the test.
260+
srcs: The source files.
261+
deps: The target deps.
262+
external_deps: The external deps.
263+
args: The args to supply to the test binary.
264+
data: Data dependencies.
265+
uses_polling: Whether the test uses polling.
266+
language: The language of the test, e.g C, C++.
267+
size: The size of the test.
268+
timeout: The test timeout.
269+
tags: The tags for the test.
270+
exec_compatible_with: A list of constraint values that must be
271+
satisifed for the platform.
272+
exec_properties: A dictionary of strings that will be added to the
273+
exec_properties of a platform selected for this target.
274+
shard_count: The number of shards for this test.
275+
flaky: Whether this test is flaky.
276+
copts: Add these to the compiler invocation.
277+
"""
224278
copts = copts + if_mac(["-DGRPC_CFSTREAM"])
225279
if language.upper() == "C":
226280
copts = copts + if_not_windows(["-std=c99"])
@@ -283,6 +337,22 @@ def grpc_cc_test(name, srcs = [], deps = [], external_deps = [], args = [], data
283337
)
284338

285339
def grpc_cc_binary(name, srcs = [], deps = [], external_deps = [], args = [], data = [], language = "C++", testonly = False, linkshared = False, linkopts = [], tags = [], features = []):
340+
"""Generates a cc_binary for use in the gRPC repo.
341+
342+
Args:
343+
name: The name of the target.
344+
srcs: The source files.
345+
deps: The dependencies.
346+
external_deps: The external dependencies.
347+
args: The arguments to supply to the binary.
348+
data: The data dependencies.
349+
language: The language of the binary, e.g. C, C++.
350+
testonly: Whether the binary is for tests only.
351+
linkshared: Enables linkshared on the binary.
352+
linkopts: linkopts to supply to the cc_binary.
353+
tags: Tags to apply to the target.
354+
features: features to be supplied to the cc_binary.
355+
"""
286356
copts = []
287357
if language.upper() == "C":
288358
copts = ["-std=c99"]
@@ -300,6 +370,7 @@ def grpc_cc_binary(name, srcs = [], deps = [], external_deps = [], args = [], da
300370
features = features,
301371
)
302372

373+
# buildifier: disable=unnamed-macro
303374
def grpc_generate_one_off_targets():
304375
# In open-source, grpc_objc* libraries depend directly on //:grpc
305376
native.alias(
@@ -345,6 +416,13 @@ def grpc_py_binary(
345416
)
346417

347418
def grpc_package(name, visibility = "private", features = []):
419+
"""Creates a package.
420+
421+
Args:
422+
name: The name of the target
423+
visibility: The visibility of the target.
424+
features: The features to enable.
425+
"""
348426
if visibility == "tests":
349427
visibility = ["//test:__subpackages__"]
350428
elif visibility == "public":
@@ -355,6 +433,7 @@ def grpc_package(name, visibility = "private", features = []):
355433
fail("Unknown visibility " + visibility)
356434

357435
if len(visibility) != 0:
436+
# buildifier: disable=native-package
358437
native.package(
359438
default_visibility = visibility,
360439
features = features,
@@ -402,6 +481,7 @@ def grpc_upb_proto_library(name, deps):
402481
def grpc_upb_proto_reflection_library(name, deps):
403482
upb_proto_reflection_library(name = name, deps = deps)
404483

484+
# buildifier: disable=unnamed-macro
405485
def python_config_settings():
406486
native.config_setting(
407487
name = "python3",

0 commit comments

Comments
 (0)