Skip to content

Commit 0d2cc0c

Browse files
committed
Optionally pass --gcc-install-dir option to clang
This option allows the selection of a particular toolchain, including an hermetic one. See also: https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-gcc-install-dir Without this option, clang selects the latest toolchain installed on the system, which is not necessarily the one used to build the code and can lead to inconsistencies. This patch is related to issue erenon#19
1 parent a965fb4 commit 0d2cc0c

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@ label_flag(
3333
build_setting_default = ":clang_tidy_additional_deps_default",
3434
visibility = ["//visibility:public"],
3535
)
36+
37+
filegroup(
38+
name = "clang_tidy_gcc_install_dir_default",
39+
srcs = [],
40+
)
41+
42+
label_flag(
43+
name = "clang_tidy_gcc_install_dir",
44+
build_setting_default = ":clang_tidy_gcc_install_dir_default",
45+
visibility = ["//visibility:public"],
46+
)

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,40 @@ build:clang-tidy --@bazel_clang_tidy//:clang_tidy_executable=@local_config_cc//:
8585
This aspect is not executed on external targets. To exclude other targets,
8686
users may tag a target with `no-clang-tidy` or `noclangtidy`.
8787

88+
### use with non-system gcc
89+
90+
Create a label to the installation dir of your gcc toolchain, for example with
91+
skylib's `directory`.
92+
93+
```py
94+
# BUILD file for gcc
95+
load("@bazel_skylib//rules/directory:directory.bzl", "directory")
96+
97+
package(default_visibility = ["//visibility:public"])
98+
99+
directory(
100+
name = "toolchain_root",
101+
srcs = glob([
102+
"lib/**",
103+
"x86_64-buildroot-linux-gnu/include/**",
104+
]),
105+
)
106+
107+
directory(
108+
name = "x86_64-buildroot-linux-gnu",
109+
srcs = ["lib/gcc/x86_64-buildroot-linux-gnu/13.3.0"],
110+
)
111+
112+
```
113+
114+
then add the toolchain as an additional dependency and set the `clang_tidy_gcc_install_dir` option
115+
116+
```text
117+
build:clang-tidy --@bazel_clang_tidy//:clang_tidy_gcc_install_dir=@gcc-linux-x86_64//:x86_64-buildroot-linux-gnu
118+
build:clang-tidy --@bazel_clang_tidy//:clang_tidy_additional_deps=@gcc-linux-x86_64//:toolchain_root
119+
```
120+
121+
88122
## Features
89123

90124
- Run clang-tidy on any C/C++ target

clang_tidy/clang_tidy.bzl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ def _run_tidy(
55
ctx,
66
wrapper,
77
exe,
8+
gcc_install_dir,
89
additional_deps,
910
config,
1011
flags,
@@ -51,6 +52,12 @@ def _run_tidy(
5152
# start args passed to the compiler
5253
args.add("--")
5354

55+
if len(gcc_install_dir.files.to_list()) >= 2:
56+
fail("clang_tidy_gcc_install_dir must contain at most one directory")
57+
58+
for dir in gcc_install_dir.files.to_list():
59+
args.add("--gcc-install-dir=%s" % dir.path)
60+
5461
ctx.actions.run(
5562
inputs = inputs,
5663
outputs = [outfile],
@@ -206,6 +213,7 @@ def _clang_tidy_aspect_impl(target, ctx):
206213

207214
wrapper = ctx.attr._clang_tidy_wrapper.files_to_run
208215
exe = ctx.attr._clang_tidy_executable
216+
gcc_install_dir = ctx.attr._clang_tidy_gcc_install_dir
209217
additional_deps = ctx.attr._clang_tidy_additional_deps
210218
config = ctx.attr._clang_tidy_config.files.to_list()[0]
211219

@@ -230,6 +238,7 @@ def _clang_tidy_aspect_impl(target, ctx):
230238
ctx,
231239
wrapper,
232240
exe,
241+
gcc_install_dir,
233242
additional_deps,
234243
config,
235244
c_flags if is_c_translation_unit(src, ctx.rule.attr.tags) else cxx_flags,
@@ -253,6 +262,7 @@ clang_tidy_aspect = aspect(
253262
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
254263
"_clang_tidy_wrapper": attr.label(default = Label("//clang_tidy:clang_tidy")),
255264
"_clang_tidy_executable": attr.label(default = Label("//:clang_tidy_executable")),
265+
"_clang_tidy_gcc_install_dir": attr.label(default = Label("//:clang_tidy_gcc_install_dir")),
256266
"_clang_tidy_additional_deps": attr.label(default = Label("//:clang_tidy_additional_deps")),
257267
"_clang_tidy_config": attr.label(default = Label("//:clang_tidy_config")),
258268
},

0 commit comments

Comments
 (0)