Skip to content

Add resource_dir override #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ label_flag(
build_setting_default = ":clang_tidy_gcc_install_dir_default",
visibility = ["//visibility:public"],
)

filegroup(
name = "clang_tidy_resource_dir_default",
srcs = [],
)

label_flag(
name = "clang_tidy_resource_dir",
build_setting_default = ":clang_tidy_resource_dir_default",
visibility = ["//visibility:public"],
)
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ build:clang-tidy --@bazel_clang_tidy//:clang_tidy_gcc_install_dir=@gcc-linux-x86
build:clang-tidy --@bazel_clang_tidy//:clang_tidy_additional_deps=@gcc-linux-x86_64//:toolchain_root
```

### use with a vendored clang-tidy

In the case you vendor clang-tidy, potentially alongside clang itself,
it's possible clang-tidy cannot automatically find the `-resource-dir`
path to the builtin headers. In this case, you can use skylib's
`directory` rule to create a target to the clang resource directory.

```bzl
load("@bazel_skylib//rules/directory:directory.bzl", "directory")

directory(
name = "resource_dir",
srcs = glob(["lib/clang/*/include/**"]),
visibility = ["//visibility:public"],
)
```

Then pass the `resource_dir` with a flag in your `.bazelrc`:

```
build:clang-tidy --@bazel_clang_tidy//:clang_tidy_resource_dir=//path/to:resource_dir
```

## Features

Expand Down
11 changes: 10 additions & 1 deletion clang_tidy/clang_tidy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def _run_tidy(
wrapper,
exe,
gcc_install_dir,
resource_dir,
additional_deps,
config,
flags,
Expand All @@ -24,7 +25,7 @@ def _run_tidy(

inputs = depset(
direct = direct_inputs,
transitive = [additional_files, cc_toolchain.all_files],
transitive = [additional_files, cc_toolchain.all_files, resource_dir.files],
)

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

resource_dir_files = resource_dir.files.to_list()
if resource_dir_files:
directory = resource_dir_files[0].path.split("/include/")[0]
args.add("-resource-dir", directory)

ctx.actions.run(
inputs = inputs,
outputs = [outfile],
Expand Down Expand Up @@ -214,6 +220,7 @@ def _clang_tidy_aspect_impl(target, ctx):
wrapper = ctx.attr._clang_tidy_wrapper.files_to_run
exe = ctx.attr._clang_tidy_executable
gcc_install_dir = ctx.attr._clang_tidy_gcc_install_dir
resource_dir = ctx.attr._clang_tidy_resource_dir
additional_deps = ctx.attr._clang_tidy_additional_deps
config = ctx.attr._clang_tidy_config.files.to_list()[0]

Expand All @@ -239,6 +246,7 @@ def _clang_tidy_aspect_impl(target, ctx):
wrapper,
exe,
gcc_install_dir,
resource_dir,
additional_deps,
config,
c_flags if is_c_translation_unit(src, ctx.rule.attr.tags) else cxx_flags,
Expand All @@ -263,6 +271,7 @@ clang_tidy_aspect = aspect(
"_clang_tidy_wrapper": attr.label(default = Label("//clang_tidy:clang_tidy")),
"_clang_tidy_executable": attr.label(default = Label("//:clang_tidy_executable")),
"_clang_tidy_gcc_install_dir": attr.label(default = Label("//:clang_tidy_gcc_install_dir")),
"_clang_tidy_resource_dir": attr.label(default = Label("//:clang_tidy_resource_dir")),
"_clang_tidy_additional_deps": attr.label(default = Label("//:clang_tidy_additional_deps")),
"_clang_tidy_config": attr.label(default = Label("//:clang_tidy_config")),
},
Expand Down
13 changes: 12 additions & 1 deletion clang_tidy/clang_tidy_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def _clang_tidy_rule_impl(ctx):
rule_conlyopts = _get_copts_attr(ctx, "conlyopts")
rule_cxxopts = _get_copts_attr(ctx, "cxxopts")

files = ctx.attr.clang_tidy_resource_dir.files.to_list()
if files:
directory = files[0].path.split("/include/")[0]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is ugly but i think we'd have to use skylib directly to access a better api from the provider to improve this

ccinfo_copts.extend(["-resource-dir", directory])

c_flags = safe_flags(toolchain_flags(ctx, ACTION_NAMES.c_compile) + rule_copts + rule_conlyopts) + ["-xc"]
cxx_flags = safe_flags(toolchain_flags(ctx, ACTION_NAMES.cpp_compile) + rule_copts + rule_cxxopts) + ["-xc++"]

Expand Down Expand Up @@ -95,7 +100,12 @@ fi
ctx.files.srcs + ctx.files.hdrs + ctx.files.data,
transitive_files = depset(
[ctx.file.clang_tidy_config],
transitive = [additional_files, find_cpp_toolchain(ctx).all_files, ctx.attr.clang_tidy_additional_deps.files],
transitive = [
additional_files,
find_cpp_toolchain(ctx).all_files,
ctx.attr.clang_tidy_additional_deps.files,
ctx.attr.clang_tidy_resource_dir.files,
],
),
)
.merge(clang_tidy[DefaultInfo].default_runfiles),
Expand All @@ -110,6 +120,7 @@ clang_tidy_test = rule(
"deps": attr.label_list(providers = [CcInfo]),
"clang_tidy_executable": attr.label(default = Label("//:clang_tidy_executable")),
"clang_tidy_additional_deps": attr.label(default = Label("//:clang_tidy_additional_deps")),
"clang_tidy_resource_dir": attr.label(default = Label("//:clang_tidy_resource_dir")),
"clang_tidy_config": attr.label(
default = Label("//:clang_tidy_config"),
allow_single_file = True,
Expand Down