Skip to content

Commit cf291a9

Browse files
authored
Add check_attributes aspect [BUILD-638] (#63)
* Add check_attributes aspect [BUILD-638] * Unify get_cc_srcs and get_cc_hdrs
1 parent 5dcf5e8 commit cf291a9

File tree

5 files changed

+87
-40
lines changed

5 files changed

+87
-40
lines changed

check_attributes/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
filegroup(
2+
name = "excluded_default",
3+
)
4+
5+
label_flag(
6+
name = "excluded",
7+
build_setting_default = ":excluded_default",
8+
visibility = ["//visibility:public"],
9+
)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
load("//tools:get_cc_files.bzl", "get_cc_files")
2+
3+
def _run_check_attributes(ctx, infile):
4+
output = ctx.actions.declare_file(infile.path + ".check-attributes.txt")
5+
6+
ctx.actions.run_shell(
7+
inputs = [infile],
8+
outputs = [output],
9+
command = """
10+
result=0
11+
12+
matches=$(grep -Hn __attribute__ "{infile}")
13+
14+
touch {output}
15+
16+
if [ -n "$matches" ];
17+
then
18+
while read -r line;
19+
do
20+
echo "error: Do not use __attribute__, prefer one of the macros from swiftnav/macros.h" | tee {output}
21+
echo "$line" | tee {output}
22+
result=1
23+
done <<< $matches
24+
fi
25+
26+
exit $result
27+
""".format(infile = infile.path, output = output.path),
28+
)
29+
return output
30+
31+
def _check_attributes_impl(target, ctx):
32+
# if not a C/C++ target, we are not interested
33+
if not CcInfo in target:
34+
return []
35+
36+
outputs = []
37+
38+
for file in get_cc_files(ctx):
39+
if file in ctx.files._excluded:
40+
continue
41+
outputs.append(_run_check_attributes(ctx, file))
42+
43+
return [
44+
OutputGroupInfo(report = depset(direct = outputs)),
45+
]
46+
47+
check_attributes = aspect(
48+
implementation = _check_attributes_impl,
49+
fragments = ["cpp"],
50+
attrs = {
51+
"_excluded": attr.label(default = Label("//check_attributes:excluded")),
52+
},
53+
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
54+
)

clang_format/clang_format_check.bzl

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
load("//tools:get_cc_files.bzl", "get_cc_files")
2+
13
def _check_format(ctx, exe, config, infile, clang_format_bin):
24
output = ctx.actions.declare_file(infile.path + ".clang-format.txt")
35

@@ -19,18 +21,6 @@ def _check_format(ctx, exe, config, infile, clang_format_bin):
1921
)
2022
return output
2123

22-
def _extract_files(ctx):
23-
files = []
24-
if hasattr(ctx.rule.attr, "srcs"):
25-
for src in ctx.rule.attr.srcs:
26-
files += [src for src in src.files.to_list() if src.is_source]
27-
28-
if hasattr(ctx.rule.attr, "hdrs"):
29-
for hdr in ctx.rule.attr.hdrs:
30-
files += [hdr for hdr in hdr.files.to_list() if hdr.is_source]
31-
32-
return files
33-
3424
def _clang_format_check_aspect_impl(target, ctx):
3525
# if not a C/C++ target, we are not interested
3626
if not CcInfo in target:
@@ -39,7 +29,7 @@ def _clang_format_check_aspect_impl(target, ctx):
3929
exe = ctx.attr._clang_format.files_to_run
4030
config = ctx.attr._clang_format_config.files.to_list()[0]
4131
clang_format_bin = ctx.attr._clang_format_bin.files.to_list()[0]
42-
files = _extract_files(ctx)
32+
files = get_cc_files(ctx)
4333

4434
outputs = []
4535
for file in files:

clang_tidy/clang_tidy.bzl

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
22
load("//cc:defs.bzl", "BINARY", "LIBRARY")
3+
load("//tools:get_cc_files.bzl", "get_cc_srcs")
34

45
def _flatten(input_list):
56
return [item for sublist in input_list for item in sublist]
@@ -71,13 +72,6 @@ def _run_tidy(ctx, wrapper, exe, additional_deps, config, flags, compilation_con
7172
)
7273
return outfile
7374

74-
def _rule_sources(ctx):
75-
srcs = []
76-
if hasattr(ctx.rule.attr, "srcs"):
77-
for src in ctx.rule.attr.srcs:
78-
srcs += [src for src in src.files.to_list() if src.is_source]
79-
return srcs
80-
8175
def _toolchain_flags(ctx):
8276
cc_toolchain = find_cpp_toolchain(ctx)
8377
feature_configuration = cc_common.configure_features(
@@ -142,7 +136,7 @@ def _clang_tidy_aspect_impl(target, ctx):
142136
final_flags = _replace_gendir(safe_flags, ctx)
143137
compilation_contexts = _get_compilation_contexts(target, ctx)
144138

145-
srcs = _rule_sources(ctx)
139+
srcs = get_cc_srcs(ctx)
146140

147141
# We exclude headers because we shouldn't run clang-tidy directly with them.
148142
# Headers will be linted if included in a source file.

tools/get_cc_files.bzl

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,41 @@ FilesInfo = provider(
66
},
77
)
88

9-
def _get_hdrs(ctx):
10-
files = []
9+
def get_cc_hdrs(ctx):
10+
hdrs = []
1111

1212
if hasattr(ctx.rule.attr, "hdrs"):
1313
for hdr in ctx.rule.attr.hdrs:
14-
for file in hdr.files.to_list():
15-
if not file.path.startswith(ctx.genfiles_dir.path):
16-
files.append(file)
17-
return files
14+
hdrs += [hdr for hdr in hdr.files.to_list() if hdr.is_source]
1815

19-
def _get_srcs(ctx):
20-
files = []
16+
return hdrs
17+
18+
def get_cc_srcs(ctx):
19+
srcs = []
2120

2221
if hasattr(ctx.rule.attr, "srcs"):
2322
for src in ctx.rule.attr.srcs:
24-
for file in src.files.to_list():
25-
if file.is_source and not file.path.startswith(ctx.genfiles_dir.path):
26-
files.append(file)
27-
return files
23+
srcs += [src for src in src.files.to_list() if src.is_source]
2824

29-
def _get_cc_target_files_impl(target, ctx):
25+
return srcs
26+
27+
def get_cc_files(ctx):
3028
files = []
3129

30+
files.extend(get_cc_srcs(ctx))
31+
files.extend(get_cc_hdrs(ctx))
32+
33+
return files
34+
35+
def _get_cc_target_files_impl(target, ctx):
3236
if not CcInfo in target:
3337
return [FilesInfo(files = [])]
3438

3539
tags = getattr(ctx.rule.attr, "tags", [])
3640
if not LIBRARY in tags and not TEST_LIBRARY in tags and not BINARY in tags:
3741
return [FilesInfo(files = [])]
3842

39-
files.extend(_get_srcs(ctx))
40-
41-
files.extend(_get_hdrs(ctx))
42-
43-
return [FilesInfo(files = files)]
43+
return [FilesInfo(files = get_cc_files(ctx))]
4444

4545
get_cc_target_files = aspect(
4646
implementation = _get_cc_target_files_impl,
@@ -50,7 +50,7 @@ def _get_cc_target_hdrs_impl(target, ctx):
5050
if not CcInfo in target:
5151
return [FilesInfo(files = [])]
5252

53-
return [FilesInfo(files = _get_hdrs(ctx))]
53+
return [FilesInfo(files = get_cc_hdrs(ctx))]
5454

5555
get_cc_target_hdrs = aspect(
5656
implementation = _get_cc_target_hdrs_impl,

0 commit comments

Comments
 (0)