|
| 1 | +"""Clang tidy aspect. |
| 2 | +
|
| 3 | +The aspect, when enabled runs clang_tidy on every compiled c++ file. |
| 4 | +""" |
| 5 | + |
| 6 | +load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES") |
| 7 | +load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain") |
| 8 | +load("@rules_cc//cc/common:cc_common.bzl", "cc_common") |
| 9 | +load("@rules_cc//cc/common:cc_info.bzl", "CcInfo") |
| 10 | + |
| 11 | +def _clang_tidy_aspect_impl(target, ctx): |
| 12 | + # not a c++ target |
| 13 | + if not CcInfo in target: |
| 14 | + return [] |
| 15 | + |
| 16 | + cc_toolchain = find_cc_toolchain(ctx) |
| 17 | + feature_configuration = cc_common.configure_features( |
| 18 | + ctx = ctx, |
| 19 | + cc_toolchain = cc_toolchain, |
| 20 | + ) |
| 21 | + compile_variables = cc_common.create_compile_variables( |
| 22 | + feature_configuration = feature_configuration, |
| 23 | + cc_toolchain = cc_toolchain, |
| 24 | + user_compile_flags = ctx.fragments.cpp.cxxopts + ctx.fragments.cpp.copts, |
| 25 | + ) |
| 26 | + toolchain_flags = cc_common.get_memory_inefficient_command_line( |
| 27 | + feature_configuration = feature_configuration, |
| 28 | + action_name = ACTION_NAMES.cpp_compile, |
| 29 | + variables = compile_variables, |
| 30 | + ) |
| 31 | + |
| 32 | + compilation_context = target[CcInfo].compilation_context |
| 33 | + |
| 34 | + rule_copts = getattr(ctx.rule.attr, "copts", []) |
| 35 | + |
| 36 | + # we use $location in our copts, expand it |
| 37 | + rule_copts = [ctx.expand_location(opt) for opt in rule_copts] |
| 38 | + |
| 39 | + srcs = [] |
| 40 | + if hasattr(ctx.rule.attr, "srcs"): |
| 41 | + for src in ctx.rule.attr.srcs: |
| 42 | + srcs += [ |
| 43 | + src |
| 44 | + for src in src.files.to_list() |
| 45 | + if src.is_source and src.short_path.endswith((".c++", ".c", ".h")) |
| 46 | + ] |
| 47 | + if hasattr(ctx.rule.attr, "hdrs"): |
| 48 | + for src in ctx.rule.attr.hdrs: |
| 49 | + srcs += [ |
| 50 | + src |
| 51 | + for src in src.files.to_list() |
| 52 | + if src.is_source and src.short_path.endswith((".c++", ".c", ".h")) |
| 53 | + ] |
| 54 | + |
| 55 | + defines = compilation_context.defines.to_list() |
| 56 | + local_defines = compilation_context.local_defines.to_list() |
| 57 | + includes = compilation_context.includes.to_list() |
| 58 | + quote_includes = compilation_context.quote_includes.to_list() |
| 59 | + system_includes = compilation_context.system_includes.to_list() |
| 60 | + headers = compilation_context.headers |
| 61 | + |
| 62 | + # disable clang tidy if no-clang-tidy tag is defined. |
| 63 | + # todo: figure out a better way to control clang tidy on a per-target basis. |
| 64 | + if "no-clang-tidy" in ctx.rule.attr.tags: |
| 65 | + return [] |
| 66 | + |
| 67 | + # bazel doesn't expose implementation deps through compilation context |
| 68 | + # https://github.com/bazelbuild/bazel/issues/19663 |
| 69 | + if hasattr(ctx.rule.attr, "implementation_deps"): |
| 70 | + deps = [dep[CcInfo].compilation_context for dep in ctx.rule.attr.implementation_deps if CcInfo in dep] |
| 71 | + defines = depset( |
| 72 | + defines, |
| 73 | + transitive = [dep.defines for dep in deps], |
| 74 | + ) |
| 75 | + includes = depset( |
| 76 | + includes, |
| 77 | + transitive = [dep.includes for dep in deps], |
| 78 | + ) |
| 79 | + system_includes = depset( |
| 80 | + system_includes, |
| 81 | + transitive = [dep.system_includes for dep in deps], |
| 82 | + ) |
| 83 | + quote_includes = depset( |
| 84 | + quote_includes, |
| 85 | + transitive = [dep.quote_includes for dep in deps], |
| 86 | + ) |
| 87 | + headers = depset( |
| 88 | + headers.to_list(), |
| 89 | + transitive = [dep.headers for dep in deps], |
| 90 | + ) |
| 91 | + |
| 92 | + tools = [ |
| 93 | + ctx.attr._clang_tidy_executable.files, |
| 94 | + ctx.attr._clang_tidy_wrapper.files, |
| 95 | + ctx.attr._clang_tidy_config.files, |
| 96 | + ] |
| 97 | + |
| 98 | + outs = [] |
| 99 | + for src in srcs: |
| 100 | + # run actions need to produce something, declare a dummy file |
| 101 | + # multiple labels can use the same path, so disambiguate. |
| 102 | + out = ctx.actions.declare_file(src.path + "." + ctx.label.name + ".clang_tidy") |
| 103 | + outs.append(out) |
| 104 | + |
| 105 | + args = ctx.actions.args() |
| 106 | + |
| 107 | + # these are consumed by clang_tidy_wrapper,sh |
| 108 | + args.add(ctx.attr._clang_tidy_executable.files_to_run.executable) |
| 109 | + args.add(out) |
| 110 | + |
| 111 | + # clang-tidy arguments |
| 112 | + # do not print statistics |
| 113 | + args.add("--quiet") |
| 114 | + args.add("--config-file=" + ctx.attr._clang_tidy_config.files.to_list()[0].short_path) |
| 115 | + |
| 116 | + if ctx.attr.clang_tidy_args: |
| 117 | + args.add_all(ctx.attr.clang_tidy_args.split(" ")) |
| 118 | + |
| 119 | + args.add(src.path) |
| 120 | + |
| 121 | + # compiler arguments |
| 122 | + args.add("--") |
| 123 | + |
| 124 | + args.add("-xc++") |
| 125 | + |
| 126 | + args.add_all(ctx.attr._clang_tidy_compiler_flags) |
| 127 | + args.add_all(rule_copts) |
| 128 | + args.add_all(defines, before_each = "-D") |
| 129 | + args.add_all(local_defines, before_each = "-D") |
| 130 | + args.add_all(includes, before_each = "-I") |
| 131 | + args.add_all(quote_includes, before_each = "-iquote") |
| 132 | + args.add_all(system_includes, before_each = "-isystem") |
| 133 | + |
| 134 | + args.add_all(toolchain_flags) |
| 135 | + |
| 136 | + # Silence warnings about unused functions or #pragma once being present in header files. For |
| 137 | + # source files, we already cover these warnings in regular compilation |
| 138 | + args.add("-Wno-pragma-once-outside-header") |
| 139 | + args.add("-Wno-unused") |
| 140 | + |
| 141 | + # TODO(cleanup): These paths provide required includes, but if the toolchain was working |
| 142 | + # properly we wouldn't need them in the first place... |
| 143 | + # Linux includes |
| 144 | + args.add("-isystem/usr/lib/llvm-19/include/c++/v1") |
| 145 | + args.add("-isystem/usr/lib/llvm-19/lib/clang/19/include") |
| 146 | + args.add("-isystem/usr/include") |
| 147 | + args.add("-isystem/usr/include/x86_64-linux-gnu") |
| 148 | + |
| 149 | + # macOS includes |
| 150 | + args.add("-isystem/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1") |
| 151 | + args.add("-isystem/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/17/include") |
| 152 | + args.add("-isystem/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include") |
| 153 | + |
| 154 | + inputs = depset( |
| 155 | + direct = [src], |
| 156 | + transitive = [headers], |
| 157 | + ) |
| 158 | + |
| 159 | + ctx.actions.run( |
| 160 | + outputs = [out], |
| 161 | + arguments = [args], |
| 162 | + executable = ctx.attr._clang_tidy_wrapper.files_to_run.executable, |
| 163 | + progress_message = "Run clang-tidy on {}".format(src.short_path), |
| 164 | + tools = tools, |
| 165 | + mnemonic = "ClangTidy", |
| 166 | + inputs = inputs, |
| 167 | + ) |
| 168 | + |
| 169 | + return [ |
| 170 | + OutputGroupInfo(clang_tidy_checks = depset(direct = outs)), |
| 171 | + ] |
| 172 | + |
| 173 | +clang_tidy_aspect = aspect( |
| 174 | + implementation = _clang_tidy_aspect_impl, |
| 175 | + fragments = ["cpp"], |
| 176 | + attrs = { |
| 177 | + "_clang_tidy_wrapper": attr.label( |
| 178 | + default = Label("@//build/tools/clang_tidy:clang_tidy_wrapper.sh"), |
| 179 | + allow_single_file = True, |
| 180 | + ), |
| 181 | + "_clang_tidy_executable": attr.label( |
| 182 | + default = Label("//tools:clang-tidy"), |
| 183 | + allow_single_file = True, |
| 184 | + ), |
| 185 | + "_clang_tidy_config": attr.label( |
| 186 | + default = Label("//:clang_tidy_config"), |
| 187 | + allow_single_file = True, |
| 188 | + ), |
| 189 | + "_clang_tidy_compiler_flags": attr.string_list( |
| 190 | + default = [], |
| 191 | + ), |
| 192 | + "clang_tidy_args": attr.string(default = ""), |
| 193 | + }, |
| 194 | + toolchains = ["@bazel_tools//tools/cpp:toolchain_type"], |
| 195 | +) |
0 commit comments