-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_clang_tidy_aspect.bzl
252 lines (218 loc) · 7.82 KB
/
make_clang_tidy_aspect.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"""
make_clang_tidy_aspect is a macro that creates an aspect to run ClangTidy. It
provides attributes to customize options passed to ClangTidy (e.g. with
different configurations or verbosity).
"""
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES")
load("@rules_cc//cc:find_cc_toolchain.bzl", "CC_TOOLCHAIN_TYPE")
load("//private:functional.bzl", fn = "functional")
def _source_files_in(ctx, attr):
if not hasattr(ctx.rule.attr, attr):
return []
files = []
for f in getattr(ctx.rule.attr, attr):
files += [g for g in f.files.to_list() if g.is_source]
return files
def _compilation_ctx_args(compilation_ctx):
prepend = lambda front, attr: fn.map(
fn.bind_front(fn.add, front),
getattr(compilation_ctx, attr).to_list(),
)
return (
prepend("-D", "defines") +
prepend("-isystem", "external_includes") +
prepend("-F", "framework_includes") +
prepend("-I", "includes") +
prepend("-D", "local_defines") +
prepend("-iquote", "quote_includes") +
prepend("-isystem", "system_includes")
)
def _toolchain_args(ctx):
cc_toolchain = ctx.toolchains[CC_TOOLCHAIN_TYPE].cc
feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
)
compile_variables = cc_common.create_compile_variables(
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
user_compile_flags = ctx.fragments.cpp.cxxopts + ctx.fragments.cpp.copts,
)
return cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = ACTION_NAMES.cpp_compile,
variables = compile_variables,
)
def _do_tidy(ctx, compilation_ctx, source_file, **kwargs):
clang_tidy = ctx.attr._clang_tidy.files_to_run.executable
sanitize = lambda s: "".join(fn.filter(
lambda c: not c in ["@", "/", ":", "%"],
s.elems(),
)).replace(".bzl", "", 1)
outbase = ".".join([
source_file.short_path,
"_".join(fn.map(sanitize, ctx.aspect_ids)),
])
# This output file may be used by clang-apply-replacements which requires a
# yaml extension.
fixes = ctx.actions.declare_file(outbase + ".yaml")
result = ctx.actions.declare_file(outbase + ".result")
stdout = ctx.actions.declare_file(outbase + ".stdout")
stderr = ctx.actions.declare_file(outbase + ".stderr")
phony = ctx.actions.declare_file(outbase + ".phony")
# This action should succeed so that the fixes file is generated and cached.
ctx.actions.run_shell(
inputs = depset(
direct = [
ctx.file._config,
source_file,
clang_tidy,
],
transitive = [compilation_ctx.headers],
),
outputs = [fixes, result, stdout, stderr],
command = """\
#!/usr/bin/env bash
set -euo pipefail
({clang_tidy} \
--config-file={config} \
{tidy_options} \
{extra_options} \
--export-fixes="{fixes}" \
{infile} \
-- {compiler_command} > {stdout} 2> {stderr} && echo "true" > {result}) \
|| (cat {stderr} >&2 && echo "false" > {result})
touch {fixes}
# replace sandbox path prefix from file paths and
# hope `+` isn't used anywhere
if [ "$(uname)" == "Darwin" ]; then
SED_INPLACE="-i''"
else
SED_INPLACE="--in-place"
fi
sed "$SED_INPLACE" -e "s+$(pwd)+%workspace%+g" {fixes}
""".format(
clang_tidy = clang_tidy.path,
config = ctx.file._config.path,
tidy_options = " ".join(kwargs["tidy_options"]),
extra_options = " ".join(ctx.attr._extra_options[BuildSettingInfo].value),
infile = source_file.path,
fixes = fixes.path,
result = result.path,
stdout = stdout.path,
stderr = stderr.path,
compiler_command = " ".join(
_toolchain_args(ctx) +
_compilation_ctx_args(compilation_ctx) +
ctx.rule.attr.copts,
# TODO add options from cpp fragment
),
),
mnemonic = "ClangTidy",
progress_message = "Linting {}".format(source_file.short_path),
execution_requirements = kwargs["execution_requirements"],
use_default_shell_env = True,
)
# use result to conditionally fail the action
ctx.actions.run_shell(
inputs = depset(direct = [result, stdout, stderr]),
outputs = [phony],
command = """\
#!/usr/bin/env bash
set -euo pipefail
if ! $(cat {result}); then
cat {stdout}
>&2 cat {stderr}
false
fi
touch {phony}
""".format(
result = result.path,
stdout = stdout.path,
stderr = stderr.path,
phony = phony.path,
),
)
return struct(fixes = fixes, phony = phony)
def _check_attr(ctx):
config_files = ctx.attr._config.files.to_list()
if len(config_files) != 1:
fail("{label} may only contain a single file but it has: {files}".format(
label = ctx.attr._config.label,
files = config_files,
))
if not config_files[0].is_source:
fail("{} must be a source file".format(config_files[0]))
def _clang_tidy_aspect_impl(**kwargs):
def impl(target, ctx):
# https://github.com/bazelbuild/bazel/issues/19609
if not CcInfo in target:
return []
_check_attr(ctx)
fixes = []
phony = []
for source_file in (
_source_files_in(ctx, "hdrs") +
_source_files_in(ctx, "srcs")
):
out = _do_tidy(
ctx,
target[CcInfo].compilation_context,
source_file,
**kwargs
)
fixes.append(out.fixes)
phony.append(out.phony)
return [
OutputGroupInfo(
report = depset(fixes + phony),
fixes = depset(fixes),
),
]
return impl
def make_clang_tidy_aspect(
clang_tidy = None,
config = None,
options = [],
execution_requirements = None):
"""
Creates an aspect to run ClangTidy.
Args:
clang_tidy: `label`; or `None`; default is `None`
A label specifying a `clang-tidy` binary. If `None`, the binary is
determined by `label_flag` `//:clang-tidy`. If `//:clang-tidy` is
not overriden to specify a binary, `clang-tidy` must be available
in `PATH`.
config: `label`; or `None`; default is `None`
A single file filegroup passed to ClangTidy with `--config-file`.
If `None`, the config file used by ClangTidy is determined by
`label_flag` `//:config`.
options: List of strings; default is []
A list of options passed to ClangTidy.
execution_requirements: `dict`; or `None`; default is `None`
Information for scheduling the action.
https://bazel.build/reference/be/common-definitions#common.tags
The aspect produces a single output file in the `report` output group.
Options can refer to the output file with `$@`.
"""
return aspect(
implementation = _clang_tidy_aspect_impl(
tidy_options = options,
execution_requirements = execution_requirements,
),
fragments = ["cpp"],
attrs = {
"_clang_tidy": attr.label(
default = Label(clang_tidy or "//:clang-tidy"),
),
"_config": attr.label(
default = Label(config or "//:config"),
allow_single_file = True,
),
"_extra_options": attr.label(
default = Label("//:extra-options"),
),
},
toolchains = [CC_TOOLCHAIN_TYPE],
)