Skip to content

Commit 3c61927

Browse files
authored
Add Buildifier (#187)
1 parent b860ec2 commit 3c61927

16 files changed

Lines changed: 223 additions & 107 deletions

File tree

.github/workflows/lint.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,20 @@ jobs:
1515
with:
1616
python-version: "3.10"
1717

18+
- name: Setup Bazel
19+
uses: bazel-contrib/setup-bazel@0.15.0
20+
1821
- name: Install dependencies
1922
run: |
2023
python -m pip install --upgrade pip
2124
pip install pylint
25+
echo "6.5.0" > .bazelversion
2226
2327
- name: Analysing the code with pylint
28+
run: pylint .
29+
30+
- name: Analyzing the code with Buildifier
2431
run: |
25-
pylint .
32+
bazel test //:format_test --test_output=errors
2633
2734
# TODO: Add more linters here

BUILD

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
load("@aspect_rules_lint//format:defs.bzl", "format_test")
2+
load("@buildifier_prebuilt//:rules.bzl", "buildifier_test")
3+
4+
buildifier_test(
5+
name = "buildifier_native",
6+
diff_command = "diff -u",
7+
exclude_patterns = [
8+
"./.git/*",
9+
],
10+
lint_mode = "warn",
11+
mode = "diff",
12+
no_sandbox = True,
13+
workspace = "//:WORKSPACE",
14+
)
15+
16+
format_test(
17+
name = "format_test",
18+
# Temporary workaround for not being able to use -diff_command
19+
env = ["BUILDIFIER_DIFF='diff -u'"],
20+
no_sandbox = True,
21+
# TODO: extend with pylint
22+
starlark = "@buildifier_prebuilt//:buildifier",
23+
starlark_check_args = [
24+
"-lint=warn",
25+
"-warnings=all",
26+
"-mode=diff",
27+
# -u will always get passed to buildifier not diff_command
28+
#"-diff_command=\"diff -u\"",
29+
],
30+
workspace = "//:WORKSPACE",
31+
)

MODULE.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ module(name = "rules_codechecker")
1919
bazel_dep(name = "rules_python", version = "0.32.0")
2020
bazel_dep(name = "rules_cc", version = "0.2.3")
2121

22+
bazel_dep(
23+
name = "buildifier_prebuilt",
24+
version = "6.4.0",
25+
dev_dependency = True,
26+
)
27+
bazel_dep(name = "aspect_rules_lint", version = "1.11.0", dev_dependency = True)
28+
2229
codechecker_extension = use_extension(
2330
"//src:tools.bzl",
2431
"module_register_default_codechecker",

WORKSPACE

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,54 @@ register_default_python_toolchain()
2525
register_toolchains("@default_python_tools//:python_toolchain")
2626

2727
register_default_codechecker()
28+
29+
# Dev dependencies
30+
31+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
32+
33+
http_archive(
34+
name = "buildifier_prebuilt",
35+
sha256 = "8ada9d88e51ebf5a1fdff37d75ed41d51f5e677cdbeafb0a22dda54747d6e07e",
36+
strip_prefix = "buildifier-prebuilt-6.4.0",
37+
urls = [
38+
"http://github.com/keith/buildifier-prebuilt/archive/6.4.0.tar.gz",
39+
],
40+
)
41+
42+
load("@buildifier_prebuilt//:deps.bzl", "buildifier_prebuilt_deps")
43+
44+
buildifier_prebuilt_deps()
45+
46+
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
47+
48+
bazel_skylib_workspace()
49+
50+
load("@buildifier_prebuilt//:defs.bzl", "buildifier_prebuilt_register_toolchains")
51+
52+
buildifier_prebuilt_register_toolchains()
53+
54+
http_archive(
55+
name = "aspect_rules_lint",
56+
sha256 = "329cf5ba776a75b70049a5695e9ca29a25113230f4f447aff7102b62afe7c24a",
57+
strip_prefix = "rules_lint-1.11.0",
58+
url = "https://github.com/aspect-build/rules_lint/releases/download/v1.11.0/rules_lint-v1.11.0.tar.gz",
59+
)
60+
61+
http_archive(
62+
name = "bazel_lib",
63+
sha256 = "0758ace949a93f709230a8e08ef35c5f0aacae2ff5d219b27da1d21d8233a709",
64+
strip_prefix = "bazel-lib-3.0.0-rc.0",
65+
url = "https://github.com/bazel-contrib/bazel-lib/releases/download/v3.0.0-rc.0/bazel-lib-v3.0.0-rc.0.tar.gz",
66+
)
67+
68+
load("@bazel_lib//lib:repositories.bzl", "bazel_lib_dependencies")
69+
70+
bazel_lib_dependencies()
71+
72+
load(
73+
"@aspect_rules_lint//format:repositories.bzl",
74+
# Fetch additional formatter binaries you need:
75+
"rules_lint_dependencies",
76+
)
77+
78+
rules_lint_dependencies()

src/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
# limitations under the License.
1414

1515
# Tool filter compile_commands.json file
16+
# In bazel 6 we use our own python toolchain,
17+
# therefore we cannot load from rules_python.
18+
# buildifier: disable=native-py
1619
py_binary(
1720
name = "compile_commands_filter",
1821
srcs = ["compile_commands_filter.py"],

src/clang.bzl

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -446,21 +446,10 @@ def _clang_tidy_test_impl(ctx):
446446
clang_tidy_test = rule(
447447
implementation = _clang_tidy_test_impl,
448448
attrs = {
449-
"platform": attr.string(
450-
default = "", #"@platforms//os:linux",
451-
doc = "Platform to build for",
452-
),
453-
"targets": attr.label_list(
454-
aspects = [
455-
compile_info_aspect,
456-
],
457-
cfg = platforms_transition,
458-
doc = "List of compilable targets which should be checked.",
459-
),
460-
"options": attr.string_list(
461-
# Since clang-tidy-22 clang-tidy fails if no checkers are enabled
462-
default = ["--checks=bugprone-*"],
463-
doc = "List of clang-tidy options, e.g.: --checks=",
449+
"config_file": attr.label(
450+
default = None,
451+
allow_single_file = True,
452+
doc = "Clang-tidy config file (usually .clang-tidy)",
464453
),
465454
"default_options": attr.string_list(
466455
default = [
@@ -472,18 +461,29 @@ clang_tidy_test = rule(
472461
],
473462
doc = "List of default clang-tidy options",
474463
),
475-
"config_file": attr.label(
476-
default = None,
477-
allow_single_file = True,
478-
doc = "Clang-tidy config file (usually .clang-tidy)",
479-
),
480464
"executable": attr.label(
481465
default = None,
482466
allow_single_file = True,
483467
executable = True,
484468
cfg = "exec",
485469
doc = "Clang-tidy executable",
486470
),
471+
"options": attr.string_list(
472+
# Since clang-tidy-22 clang-tidy fails if no checkers are enabled
473+
default = ["--checks=bugprone-*"],
474+
doc = "List of clang-tidy options, e.g.: --checks=",
475+
),
476+
"platform": attr.string(
477+
default = "", #"@platforms//os:linux",
478+
doc = "Platform to build for",
479+
),
480+
"targets": attr.label_list(
481+
aspects = [
482+
compile_info_aspect,
483+
],
484+
cfg = platforms_transition,
485+
doc = "List of compilable targets which should be checked.",
486+
),
487487
} | version_specific_attributes(),
488488
outputs = {
489489
"test_script": "%{name}.test_script.sh",
@@ -497,20 +497,10 @@ def _clang_analyze_test_impl(ctx):
497497
clang_analyze_test = rule(
498498
implementation = _clang_analyze_test_impl,
499499
attrs = {
500-
"platform": attr.string(
501-
default = "", #"@platforms//os:linux",
502-
doc = "Platform to build for",
503-
),
504-
"targets": attr.label_list(
505-
aspects = [
506-
compile_info_aspect,
507-
],
508-
cfg = platforms_transition,
509-
doc = "List of compilable targets which should be checked.",
510-
),
511-
"options": attr.string_list(
512-
default = [],
513-
doc = "List of clang options, e.g.: -fcolor-diagnostics",
500+
"config_file": attr.label(
501+
default = None,
502+
allow_single_file = True,
503+
doc = "?", # FIXME: configuration file for clang -analyze?
514504
),
515505
"default_options": attr.string_list(
516506
default = [
@@ -527,18 +517,28 @@ clang_analyze_test = rule(
527517
],
528518
doc = "List of default clang options",
529519
),
530-
"config_file": attr.label(
531-
default = None,
532-
allow_single_file = True,
533-
doc = "?", # FIXME: configuration file for clang -analyze?
534-
),
535520
"executable": attr.label(
536521
default = None,
537522
allow_single_file = True,
538523
executable = True,
539524
cfg = "exec",
540525
doc = "Clang executable",
541526
),
527+
"options": attr.string_list(
528+
default = [],
529+
doc = "List of clang options, e.g.: -fcolor-diagnostics",
530+
),
531+
"platform": attr.string(
532+
default = "", #"@platforms//os:linux",
533+
doc = "Platform to build for",
534+
),
535+
"targets": attr.label_list(
536+
aspects = [
537+
compile_info_aspect,
538+
],
539+
cfg = platforms_transition,
540+
doc = "List of compilable targets which should be checked.",
541+
),
542542
} | version_specific_attributes(),
543543
outputs = {
544544
"test_script": "%{name}.test_script.sh",

src/clang_ctu.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,6 @@ def _clang_ctu_impl(ctx):
413413
clang_ctu_test = rule(
414414
implementation = _clang_ctu_impl,
415415
attrs = {
416-
"options": attr.string_list(
417-
default = [],
418-
doc = "List of clang --analyze options",
419-
),
420416
"default_options": attr.string_list(
421417
default = [
422418
"-fcolor-diagnostics",
@@ -425,6 +421,10 @@ clang_ctu_test = rule(
425421
# Use: clang -cc1 -analyzer-config-help
426422
doc = "List of default analyze options",
427423
),
424+
"options": attr.string_list(
425+
default = [],
426+
doc = "List of clang --analyze options",
427+
),
428428
"targets": attr.label_list(
429429
aspects = [
430430
compile_info_aspect,

0 commit comments

Comments
 (0)