|
| 1 | +""" |
| 2 | +clang-tidy apply fixes rule |
| 3 | +""" |
| 4 | + |
| 5 | +def _clang_tidy_apply_fixes_impl(ctx): |
| 6 | + apply_fixes = ctx.actions.declare_file( |
| 7 | + "clang_tidy.{}.sh".format(ctx.attr.name), |
| 8 | + ) |
| 9 | + |
| 10 | + config = ctx.attr._tidy_config.files.to_list() |
| 11 | + if len(config) != 1: |
| 12 | + fail(":config ({}) must contain a single file".format(config)) |
| 13 | + |
| 14 | + apply_bin = ctx.attr._apply_replacements_binary.files_to_run.executable |
| 15 | + apply_path = apply_bin.path if apply_bin else "clang-apply-replacements" |
| 16 | + |
| 17 | + ctx.actions.expand_template( |
| 18 | + template = ctx.attr._template.files.to_list()[0], |
| 19 | + output = apply_fixes, |
| 20 | + substitutions = { |
| 21 | + "@APPLY_REPLACEMENTS_BINARY@": apply_path, |
| 22 | + "@TIDY_BINARY@": str(ctx.attr._tidy_binary.label), |
| 23 | + "@TIDY_CONFIG@": str(ctx.attr._tidy_config.label), |
| 24 | + "@WORKSPACE@": ctx.label.workspace_name, |
| 25 | + }, |
| 26 | + ) |
| 27 | + |
| 28 | + tidy_bin = ctx.attr._tidy_binary.files_to_run.executable |
| 29 | + runfiles = ctx.runfiles( |
| 30 | + ( |
| 31 | + [apply_bin] if apply_bin else [] + |
| 32 | + [tidy_bin] if tidy_bin else [] + |
| 33 | + config |
| 34 | + ), |
| 35 | + ) |
| 36 | + |
| 37 | + return [ |
| 38 | + DefaultInfo( |
| 39 | + executable = apply_fixes, |
| 40 | + runfiles = runfiles, |
| 41 | + ), |
| 42 | + # support use of a .bazelrc config containing `--output_groups=report` |
| 43 | + # for example, bazel run @bazel_clang_tidy//:apply-fixes --config=clang-tidy ... |
| 44 | + # with |
| 45 | + # build:clang-tidy --aspects @bazel_clang_tidy... |
| 46 | + # build:clang-tidy --@bazel_clang_tidy//:clang_tidy_config=... |
| 47 | + # build:clang-tidy --output_groups=report |
| 48 | + OutputGroupInfo(report = depset(direct = [apply_fixes])), |
| 49 | + ] |
| 50 | + |
| 51 | +clang_tidy_apply_fixes = rule( |
| 52 | + implementation = _clang_tidy_apply_fixes_impl, |
| 53 | + fragments = ["cpp"], |
| 54 | + attrs = { |
| 55 | + "_template": attr.label(default = Label("//clang_tidy:apply_fixes_template")), |
| 56 | + "_tidy_config": attr.label(default = Label("//:clang_tidy_config")), |
| 57 | + "_tidy_binary": attr.label(default = Label("//:clang_tidy_executable")), |
| 58 | + "_apply_replacements_binary": attr.label( |
| 59 | + default = Label("//:clang_apply_replacements_executable"), |
| 60 | + ), |
| 61 | + }, |
| 62 | + toolchains = ["@bazel_tools//tools/cpp:toolchain_type"], |
| 63 | + executable = True, |
| 64 | +) |
0 commit comments