Skip to content

Commit 9e9bfc5

Browse files
keitherenon
authored andcommitted
Add resource_dir override
If you vendor clang-tidy in a hermetic toolchain, it likely cannot find the builtin headers path. To solve this you have to pass `-resource-dir` to the correct path. This is similar to the new gcc option but with a different flag.
1 parent 43618e3 commit 9e9bfc5

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,14 @@ label_flag(
4444
build_setting_default = ":clang_tidy_gcc_install_dir_default",
4545
visibility = ["//visibility:public"],
4646
)
47+
48+
filegroup(
49+
name = "clang_tidy_resource_dir_default",
50+
srcs = [],
51+
)
52+
53+
label_flag(
54+
name = "clang_tidy_resource_dir",
55+
build_setting_default = ":clang_tidy_resource_dir_default",
56+
visibility = ["//visibility:public"],
57+
)

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,28 @@ build:clang-tidy --@bazel_clang_tidy//:clang_tidy_gcc_install_dir=@gcc-linux-x86
118118
build:clang-tidy --@bazel_clang_tidy//:clang_tidy_additional_deps=@gcc-linux-x86_64//:toolchain_root
119119
```
120120

121+
### use with a vendored clang-tidy
122+
123+
In the case you vendor clang-tidy, potentially alongside clang itself,
124+
it's possible clang-tidy cannot automatically find the `-resource-dir`
125+
path to the builtin headers. In this case, you can use skylib's
126+
`directory` rule to create a target to the clang resource directory.
127+
128+
```bzl
129+
load("@bazel_skylib//rules/directory:directory.bzl", "directory")
130+
131+
directory(
132+
name = "resource_dir",
133+
srcs = glob(["lib/clang/*/include/**"]),
134+
visibility = ["//visibility:public"],
135+
)
136+
```
137+
138+
Then pass the `resource_dir` with a flag in your `.bazelrc`:
139+
140+
```
141+
build:clang-tidy --@bazel_clang_tidy//:clang_tidy_resource_dir=//path/to:resource_dir
142+
```
121143

122144
## Features
123145

clang_tidy/clang_tidy.bzl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def _run_tidy(
66
wrapper,
77
exe,
88
gcc_install_dir,
9+
resource_dir,
910
additional_deps,
1011
config,
1112
flags,
@@ -24,7 +25,7 @@ def _run_tidy(
2425

2526
inputs = depset(
2627
direct = direct_inputs,
27-
transitive = [additional_files, cc_toolchain.all_files],
28+
transitive = [additional_files, cc_toolchain.all_files, resource_dir.files],
2829
)
2930

3031
args = ctx.actions.args()
@@ -58,6 +59,11 @@ def _run_tidy(
5859
for dir in gcc_install_dir.files.to_list():
5960
args.add("--gcc-install-dir=%s" % dir.path)
6061

62+
resource_dir_files = resource_dir.files.to_list()
63+
if resource_dir_files:
64+
directory = resource_dir_files[0].path.split("/include/")[0]
65+
args.add("-resource-dir", directory)
66+
6167
ctx.actions.run(
6268
inputs = inputs,
6369
outputs = [outfile],
@@ -214,6 +220,7 @@ def _clang_tidy_aspect_impl(target, ctx):
214220
wrapper = ctx.attr._clang_tidy_wrapper.files_to_run
215221
exe = ctx.attr._clang_tidy_executable
216222
gcc_install_dir = ctx.attr._clang_tidy_gcc_install_dir
223+
resource_dir = ctx.attr._clang_tidy_resource_dir
217224
additional_deps = ctx.attr._clang_tidy_additional_deps
218225
config = ctx.attr._clang_tidy_config.files.to_list()[0]
219226

@@ -239,6 +246,7 @@ def _clang_tidy_aspect_impl(target, ctx):
239246
wrapper,
240247
exe,
241248
gcc_install_dir,
249+
resource_dir,
242250
additional_deps,
243251
config,
244252
c_flags if is_c_translation_unit(src, ctx.rule.attr.tags) else cxx_flags,
@@ -263,6 +271,7 @@ clang_tidy_aspect = aspect(
263271
"_clang_tidy_wrapper": attr.label(default = Label("//clang_tidy:clang_tidy")),
264272
"_clang_tidy_executable": attr.label(default = Label("//:clang_tidy_executable")),
265273
"_clang_tidy_gcc_install_dir": attr.label(default = Label("//:clang_tidy_gcc_install_dir")),
274+
"_clang_tidy_resource_dir": attr.label(default = Label("//:clang_tidy_resource_dir")),
266275
"_clang_tidy_additional_deps": attr.label(default = Label("//:clang_tidy_additional_deps")),
267276
"_clang_tidy_config": attr.label(default = Label("//:clang_tidy_config")),
268277
},

clang_tidy/clang_tidy_test.bzl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ def _clang_tidy_rule_impl(ctx):
4040
rule_conlyopts = _get_copts_attr(ctx, "conlyopts")
4141
rule_cxxopts = _get_copts_attr(ctx, "cxxopts")
4242

43+
files = ctx.attr.clang_tidy_resource_dir.files.to_list()
44+
if files:
45+
directory = files[0].path.split("/include/")[0]
46+
ccinfo_copts.extend(["-resource-dir", directory])
47+
4348
c_flags = safe_flags(toolchain_flags(ctx, ACTION_NAMES.c_compile) + rule_copts + rule_conlyopts) + ["-xc"]
4449
cxx_flags = safe_flags(toolchain_flags(ctx, ACTION_NAMES.cpp_compile) + rule_copts + rule_cxxopts) + ["-xc++"]
4550

@@ -95,7 +100,12 @@ fi
95100
ctx.files.srcs + ctx.files.hdrs + ctx.files.data,
96101
transitive_files = depset(
97102
[ctx.file.clang_tidy_config],
98-
transitive = [additional_files, find_cpp_toolchain(ctx).all_files, ctx.attr.clang_tidy_additional_deps.files],
103+
transitive = [
104+
additional_files,
105+
find_cpp_toolchain(ctx).all_files,
106+
ctx.attr.clang_tidy_additional_deps.files,
107+
ctx.attr.clang_tidy_resource_dir.files,
108+
],
99109
),
100110
)
101111
.merge(clang_tidy[DefaultInfo].default_runfiles),
@@ -110,6 +120,7 @@ clang_tidy_test = rule(
110120
"deps": attr.label_list(providers = [CcInfo]),
111121
"clang_tidy_executable": attr.label(default = Label("//:clang_tidy_executable")),
112122
"clang_tidy_additional_deps": attr.label(default = Label("//:clang_tidy_additional_deps")),
123+
"clang_tidy_resource_dir": attr.label(default = Label("//:clang_tidy_resource_dir")),
113124
"clang_tidy_config": attr.label(
114125
default = Label("//:clang_tidy_config"),
115126
allow_single_file = True,

0 commit comments

Comments
 (0)