-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcodechecker.bzl
More file actions
433 lines (410 loc) · 13.9 KB
/
codechecker.bzl
File metadata and controls
433 lines (410 loc) · 13.9 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# Copyright 2023 Ericsson AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Rulesets for running codechecker in a single Bazel job.
"""
load(
"@default_codechecker_tools//:defs.bzl",
"CODECHECKER_BIN_PATH",
)
load(
"codechecker_config.bzl",
"codechecker_config_internal",
"get_config_file",
)
load(
"common.bzl",
"python_path",
"python_toolchain_type",
"version_specific_attributes",
)
load(
"compile_commands.bzl",
"compile_commands_aspect",
"compile_commands_impl",
"platforms_transition",
)
load(
"per_file.bzl",
"per_file_test",
)
def get_platform_alias(platform):
"""
Get platform alias for full platform names being used
Args:
platform: A string containing the platform
Returns:
string: If the full platform name is consistent with
valid syntax, returns the short alias to represent it.
Returns the original platform passed otherwise
"""
if platform.startswith("@platforms"):
(_, _, shortname) = platform.partition(":")
platform = shortname
return platform
def _codechecker_impl(ctx):
# Get compile_commands.json file and source files
compile_commands = None
source_files = None
for output in compile_commands_impl(ctx):
if type(output) == "DefaultInfo":
compile_commands = output.files.to_list()[0]
source_files = output.default_runfiles.files.to_list()
if not compile_commands:
fail("Failed to generate compile_commands.json file!")
if not source_files:
fail("Failed to collect source files!")
if compile_commands != ctx.outputs.compile_commands:
fail("Seems compile_commands.json file is incorrect!")
# Convert flacc calls to clang in compile_commands.json
# and save to codechecker_commands.json
ctx.actions.run(
inputs = [ctx.outputs.compile_commands],
outputs = [ctx.outputs.codechecker_commands],
executable = ctx.executable._compile_commands_filter,
arguments = [
# "-v", # -vv for debug
"--input=" + ctx.outputs.compile_commands.path,
"--output=" + ctx.outputs.codechecker_commands.path,
],
mnemonic = "CodeCheckerConvertFlaccToClang",
progress_message = "Filtering %s" % str(ctx.label),
# use_default_shell_env = True,
)
# Create CodeChecker skip (ignore) file
ctx.actions.write(
output = ctx.outputs.codechecker_skipfile,
content = "\n".join(ctx.attr.skip),
is_executable = False,
)
config_file, codechecker_env = get_config_file(ctx)
codechecker_files = ctx.actions.declare_directory(ctx.label.name + "/codechecker-files")
ctx.actions.expand_template(
template = ctx.file._codechecker_script_template,
output = ctx.outputs.codechecker_script,
is_executable = True,
substitutions = {
"{Mode}": "Run",
"{Verbosity}": "DEBUG",
"{PythonPath}": python_path(ctx), # "/usr/bin/env python3",
"{codechecker_bin}": CODECHECKER_BIN_PATH,
"{compile_commands}": ctx.outputs.codechecker_commands.path,
"{codechecker_skipfile}": ctx.outputs.codechecker_skipfile.path,
"{codechecker_config}": config_file.path,
"{codechecker_analyze}": " ".join(ctx.attr.analyze),
"{codechecker_files}": codechecker_files.path,
"{codechecker_log}": ctx.outputs.codechecker_log.path,
"{codechecker_env}": codechecker_env,
},
)
ctx.actions.run(
inputs = depset(
[
ctx.outputs.codechecker_script,
ctx.outputs.codechecker_commands,
ctx.outputs.codechecker_skipfile,
config_file,
] + source_files,
),
outputs = [
codechecker_files,
ctx.outputs.codechecker_log,
],
executable = ctx.outputs.codechecker_script,
arguments = [],
# executable = python_path(ctx),
# arguments = [ctx.outputs.codechecker_script.path],
mnemonic = "CodeChecker",
progress_message = "CodeChecker %s" % str(ctx.label),
# use_default_shell_env = True,
)
# List all files required at build and run (test) time
all_files = [
ctx.outputs.compile_commands,
ctx.outputs.codechecker_commands,
ctx.outputs.codechecker_skipfile,
config_file,
codechecker_files,
ctx.outputs.codechecker_script,
ctx.outputs.codechecker_log,
] + source_files
# List files required for test
run_files = [
codechecker_files,
] + source_files
# Return all files
return [
DefaultInfo(
files = depset(all_files),
runfiles = ctx.runfiles(files = run_files),
),
OutputGroupInfo(
codechecker_files = depset([codechecker_files]),
),
]
codechecker = rule(
implementation = _codechecker_impl,
attrs = {
"targets": attr.label_list(
aspects = [
compile_commands_aspect,
],
doc = "List of compilable targets which should be checked.",
),
"skip": attr.string_list(
default = [],
doc = "List of skip/ignore file rules. " +
"See https://codechecker.readthedocs.io/en/latest/analyzer/user_guide/#skip-file",
),
"config": attr.label(
default = None,
doc = "CodeChecker configuration",
),
"analyze": attr.string_list(
default = [],
doc = "List of analyze command arguments, e.g.; --ctu.",
),
"_compile_commands_filter": attr.label(
allow_files = True,
executable = True,
cfg = "exec",
default = ":compile_commands_filter",
),
"_codechecker_script_template": attr.label(
default = ":codechecker_script.py",
allow_single_file = True,
),
},
outputs = {
"compile_commands": "%{name}/compile_commands.json",
"codechecker_commands": "%{name}/codechecker_commands.json",
"codechecker_skipfile": "%{name}/codechecker_skipfile.cfg",
"codechecker_script": "%{name}/codechecker_script.py",
"codechecker_log": "%{name}/codechecker.log",
},
toolchains = [python_toolchain_type()],
)
def _codechecker_test_impl(ctx):
# Run CodeChecker at build step
info = _codechecker_impl(ctx)
all_files = []
default_runfiles = []
codechecker_files = []
for output in info:
if type(output) == "DefaultInfo":
all_files = output.files.to_list()
default_runfiles = output.default_runfiles.files.to_list()
if type(output) == "OutputGroupInfo":
codechecker_files = output.codechecker_files.to_list()[0]
if not all_files:
fail("Files required for codechecker test are not available")
if not codechecker_files:
fail("Execution results required for codechecker test are not available")
# Create test script from template
ctx.actions.expand_template(
template = ctx.file._codechecker_script_template,
output = ctx.outputs.codechecker_test_script,
is_executable = True,
substitutions = {
"{Mode}": "Test",
"{Verbosity}": "INFO",
"{PythonPath}": python_path(ctx), # "/usr/bin/env python3",
"{codechecker_bin}": CODECHECKER_BIN_PATH,
"{codechecker_files}": codechecker_files.short_path,
"{Severities}": " ".join(ctx.attr.severities),
},
)
# Return test script and all required files
run_files = default_runfiles + [ctx.outputs.codechecker_test_script]
return [
DefaultInfo(
files = depset(all_files),
runfiles = ctx.runfiles(files = run_files),
executable = ctx.outputs.codechecker_test_script,
),
]
_codechecker_test = rule(
implementation = _codechecker_test_impl,
attrs = {
"platform": attr.string(
default = "", #"@platforms//os:linux",
doc = "Platform to build for",
),
"targets": attr.label_list(
aspects = [
compile_commands_aspect,
],
cfg = platforms_transition,
doc = "List of compilable targets which should be checked.",
),
"_compile_commands_filter": attr.label(
allow_files = True,
executable = True,
cfg = "exec",
default = ":compile_commands_filter",
),
"_codechecker_script_template": attr.label(
default = ":codechecker_script.py",
allow_single_file = True,
),
"severities": attr.string_list(
default = ["HIGH"],
doc = "List of defect severities: HIGH, MEDIUM, LOW, STYLE etc",
),
"skip": attr.string_list(
default = [],
doc = "List of skip/ignore file rules. " +
"See https://codechecker.readthedocs.io/en/latest/analyzer/user_guide/#skip-file",
),
"config": attr.label(
default = None,
cfg = platforms_transition,
doc = "CodeChecker configuration",
),
"analyze": attr.string_list(
default = [],
doc = "List of analyze command arguments, e.g. --ctu",
),
} | version_specific_attributes(),
outputs = {
"compile_commands": "%{name}/compile_commands.json",
"codechecker_commands": "%{name}/codechecker_commands.json",
"codechecker_skipfile": "%{name}/codechecker_skipfile.cfg",
"codechecker_script": "%{name}/codechecker_script.py",
"codechecker_log": "%{name}/codechecker.log",
"codechecker_test_script": "%{name}/codechecker_test_script.py",
},
toolchains = [python_toolchain_type()],
test = True,
)
def codechecker_test(
name,
targets,
platform = "", #"@platforms//os:linux",
severities = ["HIGH"],
skip = [],
config = None,
analyze = [],
tags = [],
per_file = False,
**kwargs):
"""
Macro to choose the appropriate codechecker rule
Args:
name: Name of the target invoking CodeChecker.
targets: List of targets to be analyzed by CodeChecker.
platform: Platform to be analyzed on.
severities: List of warning levels that should be considered failing.
skip: Skip patterns for CodeChecker.
config: Config target for CodeChecker.
analyze: List of analyze command arguments.
tags: Bazel tags
per_file: Boolean value, toggles wether to run the analysis
with the experimental per_file rule.
**kwargs: Other miscellaneous arguments.
Returns:
none
"""
codechecker_tags = [] + tags
if "codechecker" not in tags:
codechecker_tags.append("codechecker")
if per_file:
per_file_test(
name = name,
targets = targets,
options = analyze,
config = config,
tags = tags,
**kwargs
)
else:
_codechecker_test(
name = name,
platform = platform,
targets = targets,
severities = severities,
skip = skip,
config = config,
analyze = analyze,
tags = codechecker_tags,
**kwargs
)
def codechecker_suite(
name,
targets,
platforms = [""], #["@platforms//os:linux"],
severities = ["HIGH"],
skip = [],
config = None,
analyze = [],
tags = [],
**kwargs):
"""
Bazel test suite to run CodeChecker for different platforms
Args:
name: Name of the target invoking CodeChecker.
targets: List of targets to be analyzed by CodeChecker.
platforms: List of platform to be analyzed on.
severities: List of warning levels that should be considered failing.
skip: Skip patterns for CodeChecker.
config: Config target for CodeChecker.
analyze: List of analyze command arguments.
tags: Bazel tags
**kwargs: Other miscellaneous arguments.
Returns:
none
"""
tests = []
for platform in platforms:
shortname = get_platform_alias(platform)
if not shortname:
shortname = "default"
test_name = name + "." + shortname
tests.append(test_name)
codechecker_test(
name = test_name,
platform = platform,
targets = targets,
severities = severities,
skip = skip,
config = config,
analyze = analyze,
tags = tags,
)
native.test_suite(
name = name,
tests = tests,
tags = tags,
**kwargs
)
# This rule definition is here for compatibility reasons
# in the earliest versions, the entire codechecker_config definition was here,
# but was later moved to its own .bzl file.
# This macro is left here so that early adopters
# don't need to change where the rule is loaded from.
def codechecker_config(
name,
analyze = [],
parse = [],
config_file = None,
env = [],
**kwargs):
codechecker_config_internal(
name = name,
analyze = analyze,
parse = parse,
config_file = config_file,
env = env,
**kwargs
)