Skip to content

Commit 7e50b0e

Browse files
committed
[feat] clang-tidy: add optional gcc-install-dir arg
Without the argument, clang (clang-tidy) discovers a gcc installation automatically on the system, for example, ``` % clang -v Ubuntu clang version 18.1.8 (++20240731024944+3b5b5c1ec4a3-1~exp1~20240731145000.144) Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/13 Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/13 ``` However, users may want to switch off the discovery and specify a version, exactly. ``` % clang -v --gcc-install-dir=/usr/lib/gcc/x86_64-linux-gnu/12 Ubuntu clang version 18.1.8 (++20240731024944+3b5b5c1ec4a3-1~exp1~20240731145000.144) Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/12 ``` This is especially relevant for, but not limited to, hermetic toolchains. See also This patch ports erenon/bazel_clang_tidy#88 to rules_lint.
1 parent 5f965fe commit 7e50b0e

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

lint/clang_tidy.bzl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ clang_tidy = lint_clang_tidy_aspect(
3737
```
3838
"""
3939

40+
load("@bazel_skylib//rules/directory:providers.bzl", "DirectoryInfo")
4041
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
4142
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
4243
load("//lint/private:lint_aspect.bzl", "LintOptionsInfo", "OPTIONAL_SARIF_PARSER_TOOLCHAIN", "OUTFILE_FORMAT", "noop_lint_action", "output_files", "parse_to_sarif_action", "patch_and_output_files")
@@ -262,6 +263,14 @@ def _get_args(ctx, compilation_context, srcs):
262263
def _get_compiler_args(ctx, compilation_context, srcs):
263264
# add args specified by the toolchain, on the command line and rule copts
264265
args = []
266+
267+
if ctx.attr._gcc_install_dir:
268+
gcc_install_dir = ctx.attr._gcc_install_dir[0].files.to_list()
269+
if len(gcc_install_dir) > 1:
270+
fail("gcc_install_dir must contain at most one directory")
271+
for dir in gcc_install_dir:
272+
args.append("--gcc-install-dir=" + dir.path)
273+
265274
rule_flags = ctx.rule.attr.copts if hasattr(ctx.rule.attr, "copts") else []
266275
sources_are_cxx = _is_cxx(srcs[0])
267276
if (sources_are_cxx):
@@ -409,7 +418,15 @@ def _clang_tidy_aspect_impl(target, ctx):
409418
parse_to_sarif_action(ctx, _MNEMONIC, raw_machine_report, outputs.machine.out)
410419
return [info]
411420

412-
def lint_clang_tidy_aspect(binary, configs = [], global_config = [], header_filter = "", lint_target_headers = False, angle_includes_are_system = True, verbose = False):
421+
def lint_clang_tidy_aspect(
422+
binary,
423+
configs = [],
424+
global_config = [],
425+
gcc_install_dir = [],
426+
header_filter = "",
427+
lint_target_headers = False,
428+
angle_includes_are_system = True,
429+
verbose = False):
413430
"""A factory function to create a linter aspect.
414431
415432
Args:
@@ -427,6 +444,8 @@ def lint_clang_tidy_aspect(binary, configs = [], global_config = [], header_filt
427444
files which may be used for formatting fixes.
428445
global_config: label of a single global .clang-tidy file to pass to clang-tidy on the command line. This
429446
will cause clang-tidy to ignore any other config files in the source directories.
447+
gcc_install_dir: optional, label of a `Directory` from the skylib library pointing to the gcc install
448+
directory. The argument is passed to the underlying clang as `--gcc-install-dir`.
430449
header_filter: optional, set to a posix regex to supply to clang-tidy with the -header-filter option
431450
lint_target_headers: optional, set to True to pass a pattern that includes all headers with the target's
432451
directory prefix. This crude control may include headers from the linted target in the results. If
@@ -455,6 +474,10 @@ def lint_clang_tidy_aspect(binary, configs = [], global_config = [], header_filt
455474
default = global_config,
456475
allow_files = True,
457476
),
477+
"_gcc_install_dir": attr.label_list(
478+
default = gcc_install_dir,
479+
providers = [DirectoryInfo],
480+
),
458481
"_lint_target_headers": attr.bool(
459482
default = lint_target_headers,
460483
),

0 commit comments

Comments
 (0)