|
| 1 | +"""API for calling declaring a cppcheck lint aspect. |
| 2 | +""" |
| 3 | + |
| 4 | +load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") |
| 5 | +load("//lint/private:lint_aspect.bzl", "LintOptionsInfo", "OPTIONAL_SARIF_PARSER_TOOLCHAIN", "OUTFILE_FORMAT", "noop_lint_action", "output_files", "parse_to_sarif_action") |
| 6 | + |
| 7 | +_MNEMONIC = "AspectRulesLintCppCheck" |
| 8 | + |
| 9 | +def _gather_inputs(compilation_context, srcs): |
| 10 | + inputs = srcs |
| 11 | + return depset(inputs, transitive = [compilation_context.headers]) |
| 12 | + |
| 13 | +# taken over from clang_tidy.bzl |
| 14 | +def _is_source(file): |
| 15 | + permitted_source_types = [ |
| 16 | + "c", |
| 17 | + "cc", |
| 18 | + "cpp", |
| 19 | + "cxx", |
| 20 | + "c++", |
| 21 | + "C", |
| 22 | + ] |
| 23 | + return (file.is_source and file.extension in permitted_source_types) |
| 24 | + |
| 25 | +# taken over from clang_tidy.bzl |
| 26 | +# modification of filter_srcs in lint_aspect.bzl that filters out header files |
| 27 | +def _filter_srcs(rule): |
| 28 | + # some rules can return a CcInfo without having a srcs attribute |
| 29 | + if not hasattr(rule.attr, "srcs"): |
| 30 | + return [] |
| 31 | + if "lint-genfiles" in rule.attr.tags: |
| 32 | + return rule.files.srcs |
| 33 | + else: |
| 34 | + return [s for s in rule.files.srcs if _is_source(s)] |
| 35 | + |
| 36 | +def _prefixed(list, prefix): |
| 37 | + array = [] |
| 38 | + for arg in list: |
| 39 | + array.append("{} {}".format(prefix, arg)) |
| 40 | + return array |
| 41 | + |
| 42 | +def _get_compiler_args(compilation_context): |
| 43 | + # add includes |
| 44 | + args = [] |
| 45 | + args.extend(_prefixed(compilation_context.framework_includes.to_list(), "-I")) |
| 46 | + args.extend(_prefixed(compilation_context.includes.to_list(), "-I")) |
| 47 | + args.extend(_prefixed(compilation_context.quote_includes.to_list(), "-I")) |
| 48 | + args.extend(_prefixed(compilation_context.system_includes.to_list(), "-I")) |
| 49 | + args.extend(_prefixed(compilation_context.external_includes.to_list(), "-I")) |
| 50 | + return args |
| 51 | + |
| 52 | +def cppcheck_action(ctx, compilation_context, executable, srcs, stdout, exit_code, do_xml = False): |
| 53 | + """Create a Bazel Action that spawns a cppcheck process. |
| 54 | +
|
| 55 | + Args: |
| 56 | + ctx: an action context OR aspect context |
| 57 | + compilation_context: from target |
| 58 | + executable: struct with a cppcheck field |
| 59 | + srcs: file objects to lint |
| 60 | + stdout: output file containing the stdout or --output-file of cppcheck |
| 61 | + exit_code: output file containing the exit code of cppcheck. |
| 62 | + If None, then fail the build when cppcheck exits non-zero. |
| 63 | + do_xml: If true, xml output is generated |
| 64 | + """ |
| 65 | + |
| 66 | + outputs = [stdout] |
| 67 | + env = {} |
| 68 | + env["CPPCHECK__STDOUT_STDERR_OUTPUT_FILE"] = stdout.path |
| 69 | + |
| 70 | + if exit_code: |
| 71 | + env["CPPCHECK__EXIT_CODE_OUTPUT_FILE"] = exit_code.path |
| 72 | + outputs.append(exit_code) |
| 73 | + |
| 74 | + env["CPPCHECK__VERBOSE"] = "1" if ctx.attr._verbose else "" |
| 75 | + |
| 76 | + cppcheck_args = [] |
| 77 | + |
| 78 | + # cppcheck shall fail with exit code != 0 if issues found |
| 79 | + cppcheck_args.append("--error-exitcode=31") |
| 80 | + |
| 81 | + # add include paths |
| 82 | + cppcheck_args.extend(_get_compiler_args(compilation_context)) |
| 83 | + |
| 84 | + if do_xml: |
| 85 | + cppcheck_args.append("--xml-version=3") |
| 86 | + |
| 87 | + for f in srcs: |
| 88 | + cppcheck_args.append(f.short_path) |
| 89 | + |
| 90 | + ctx.actions.run_shell( |
| 91 | + inputs = _gather_inputs(compilation_context, srcs), |
| 92 | + outputs = outputs, |
| 93 | + tools = [executable._cppcheck_wrapper, executable._cppcheck, find_cpp_toolchain(ctx).all_files], |
| 94 | + command = executable._cppcheck_wrapper.path + " $@", |
| 95 | + arguments = [executable._cppcheck.path] + cppcheck_args, |
| 96 | + env = env, |
| 97 | + mnemonic = _MNEMONIC, |
| 98 | + progress_message = "Linting %{label} with cppcheck", |
| 99 | + ) |
| 100 | + |
| 101 | +def _cppcheck_aspect_impl(target, ctx): |
| 102 | + if not CcInfo in target: |
| 103 | + return [] |
| 104 | + |
| 105 | + files_to_lint = _filter_srcs(ctx.rule) |
| 106 | + compilation_context = target[CcInfo].compilation_context |
| 107 | + if hasattr(ctx.rule.attr, "implementation_deps"): |
| 108 | + compilation_context = cc_common.merge_compilation_contexts( |
| 109 | + compilation_contexts = [compilation_context] + |
| 110 | + [implementation_dep[CcInfo].compilation_context for implementation_dep in ctx.rule.attr.implementation_deps], |
| 111 | + ) |
| 112 | + |
| 113 | + outputs, info = output_files(_MNEMONIC, target, ctx) |
| 114 | + |
| 115 | + if len(files_to_lint) == 0: |
| 116 | + noop_lint_action(ctx, outputs) |
| 117 | + return [info] |
| 118 | + |
| 119 | + cppcheck_action(ctx, compilation_context, ctx.executable, files_to_lint, outputs.human.out, outputs.human.exit_code) |
| 120 | + |
| 121 | + # report: |
| 122 | + raw_machine_report = ctx.actions.declare_file(OUTFILE_FORMAT.format(label = target.label.name, mnemonic = _MNEMONIC, suffix = "raw_machine_report")) |
| 123 | + cppcheck_action(ctx, compilation_context, ctx.executable, files_to_lint, raw_machine_report, outputs.machine.exit_code, do_xml = True) |
| 124 | + parse_to_sarif_action(ctx, _MNEMONIC, raw_machine_report, outputs.machine.out) |
| 125 | + |
| 126 | + # xml_output = ctx.actions.declare_file(OUTFILE_FORMAT.format(label = target.label.name, mnemonic = _MNEMONIC, suffix = "xml")) |
| 127 | + # xml_exit_code = ctx.actions.declare_file(OUTFILE_FORMAT.format(label = target.label.name, mnemonic = _MNEMONIC, suffix = "xml_exit_code")) |
| 128 | + # cppcheck_action(ctx, compilation_context, ctx.executable, files_to_lint, xml_output, xml_exit_code, do_xml = True) |
| 129 | + |
| 130 | + # # Create new OutputGroupInfo with xml_output added to machine outputs |
| 131 | + # info = OutputGroupInfo( |
| 132 | + # rules_lint_human = info.rules_lint_human, |
| 133 | + # rules_lint_machine = depset([xml_output], transitive = [info.rules_lint_machine]), |
| 134 | + # rules_lint_report = depset([xml_output], transitive = [info.rules_lint_report]), |
| 135 | + # _validation = info._validation, |
| 136 | + # ) |
| 137 | + return [info] |
| 138 | + |
| 139 | +def lint_cppcheck_aspect(binary, verbose = False): |
| 140 | + """A factory function to create a linter aspect. |
| 141 | +
|
| 142 | + Args: |
| 143 | + binary: the cppcheck binary, typically a rule like |
| 144 | +
|
| 145 | + ```starlark |
| 146 | + native_binary( |
| 147 | + name = "cppcheck", |
| 148 | + src = "cppcheck_wrapper.sh" |
| 149 | + ) |
| 150 | + ``` |
| 151 | + As cppcheck does not support any configuration files so far, all arguments |
| 152 | + shall be directly implemented in the wrapper script. This file can also directly |
| 153 | + pass the license file to cppcheck, if needed. |
| 154 | +
|
| 155 | + verbose: print debug messages including clang-tidy command lines being invoked. |
| 156 | + """ |
| 157 | + |
| 158 | + return aspect( |
| 159 | + implementation = _cppcheck_aspect_impl, |
| 160 | + attrs = { |
| 161 | + "_options": attr.label( |
| 162 | + default = "//lint:options", |
| 163 | + providers = [LintOptionsInfo], |
| 164 | + ), |
| 165 | + "_verbose": attr.bool( |
| 166 | + default = verbose, |
| 167 | + ), |
| 168 | + "_cppcheck": attr.label( |
| 169 | + default = binary, |
| 170 | + executable = True, |
| 171 | + cfg = "exec", |
| 172 | + ), |
| 173 | + "_cppcheck_wrapper": attr.label( |
| 174 | + default = Label("@aspect_rules_lint//lint:cppcheck_wrapper"), |
| 175 | + executable = True, |
| 176 | + cfg = "exec", |
| 177 | + ), |
| 178 | + "_patcher": attr.label( |
| 179 | + default = "@aspect_rules_lint//lint/private:patcher", |
| 180 | + executable = True, |
| 181 | + cfg = "exec", |
| 182 | + ), |
| 183 | + "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")), |
| 184 | + }, |
| 185 | + toolchains = [ |
| 186 | + OPTIONAL_SARIF_PARSER_TOOLCHAIN, |
| 187 | + "@bazel_tools//tools/cpp:toolchain_type", |
| 188 | + ], |
| 189 | + fragments = ["cpp"], |
| 190 | + ) |
0 commit comments