From 9b6c2eb468339096ebf90339559d0cb0147c35cf Mon Sep 17 00:00:00 2001 From: mla Date: Fri, 28 Mar 2025 10:38:51 +0100 Subject: [PATCH 1/2] clang-tidy emit action per file to lint and merge stdout and error codes --- example/src/cpp/main/BUILD | 7 ++++- lint/clang_tidy.bzl | 52 ++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/example/src/cpp/main/BUILD b/example/src/cpp/main/BUILD index 6c89bff7..bbaeed9b 100644 --- a/example/src/cpp/main/BUILD +++ b/example/src/cpp/main/BUILD @@ -1,5 +1,5 @@ load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") - +load("//tools/lint:linters.bzl", "clang_tidy_test") cc_library( name = "hello-greet", srcs = ["hello-greet.cc"], @@ -17,6 +17,11 @@ cc_binary( ], ) +clang_tidy_test( + name = "hello-world-clang-tidy-test", + srcs = ["hello-world"], +) + cc_library( name = "hello-greet-with-error", srcs = ["hello-greet-with-error.cc"], diff --git a/lint/clang_tidy.bzl b/lint/clang_tidy.bzl index 59c0bc34..c822f04c 100644 --- a/lint/clang_tidy.bzl +++ b/lint/clang_tidy.bzl @@ -309,29 +309,53 @@ def clang_tidy_action(ctx, compilation_context, executable, srcs, stdout, exit_c outputs = [stdout] env = _get_env(ctx, srcs) - env["CLANG_TIDY__STDOUT_STDERR_OUTPUT_FILE"] = stdout.path if exit_code: - env["CLANG_TIDY__EXIT_CODE_OUTPUT_FILE"] = exit_code.path outputs.append(exit_code) # pass compiler args via a params file. The command line may already be long due to # sources, which can't go the params file, so materialize it always. - clang_tidy_args = _get_args(ctx, compilation_context, srcs) - compiler_args = ctx.actions.args() - compiler_args.add_all(_get_compiler_args(ctx, compilation_context, srcs)) - compiler_args.use_param_file("--config %s", use_always = True) + intermediate_outputs_stdout = [] + intermediate_outputs_exit_code = [] + # create an action for each file + for src in srcs: + out_intermediate_stdout = ctx.actions.declare_file(stdout.short_path+".{}.stdout".format(len(intermediate_outputs_stdout))) + env["CLANG_TIDY__STDOUT_STDERR_OUTPUT_FILE"] = out_intermediate_stdout.path + if exit_code: + out_intermediate_exit_code = ctx.actions.declare_file(exit_code.short_path+".{}.exit_code".format(len(intermediate_outputs_exit_code))) + env["CLANG_TIDY__EXIT_CODE_OUTPUT_FILE"] = out_intermediate_exit_code.path + clang_tidy_args = _get_args(ctx, compilation_context, [src]) + compiler_args = ctx.actions.args() + compiler_args.add_all(_get_compiler_args(ctx, compilation_context, [src])) + compiler_args.use_param_file("--config %s", use_always = True) + + ctx.actions.run_shell( + inputs = _gather_inputs(ctx, compilation_context, [src]), + outputs = [out_intermediate_stdout,]+([out_intermediate_exit_code] if exit_code else []), + tools = [executable._clang_tidy_wrapper, executable._clang_tidy, find_cpp_toolchain(ctx).all_files], + command = executable._clang_tidy_wrapper.path + " $@", + arguments = [executable._clang_tidy.path] + clang_tidy_args + ["--", compiler_args], + env = env, + mnemonic = _MNEMONIC, + progress_message = "Linting %{label} with clang-tidy", + ) + intermediate_outputs_stdout.append(out_intermediate_stdout) + if exit_code: + intermediate_outputs_exit_code.append(out_intermediate_exit_code) + + # emit ctx.actions.run_shell( - inputs = _gather_inputs(ctx, compilation_context, srcs), - outputs = outputs, - tools = [executable._clang_tidy_wrapper, executable._clang_tidy, find_cpp_toolchain(ctx).all_files], - command = executable._clang_tidy_wrapper.path + " $@", - arguments = [executable._clang_tidy.path] + clang_tidy_args + ["--", compiler_args], - env = env, - mnemonic = _MNEMONIC, - progress_message = "Linting %{label} with clang-tidy", + inputs = intermediate_outputs_stdout, + outputs = [stdout], + command = "cat {} > {}".format(" ".join([f.path for f in intermediate_outputs_stdout]),stdout.path) ) + if exit_code: + ctx.actions.run_shell( + inputs = intermediate_outputs_exit_code, + outputs = [exit_code], + command = "cat {} | sort -nr | head -n 1 > {}".format(" ".join([f.path for f in intermediate_outputs_exit_code]),exit_code.path) + ) def clang_tidy_fix(ctx, compilation_context, executable, srcs, patch, stdout, exit_code): """Create a Bazel Action that spawns clang-tidy with --fix. From 075dd8707c1a6ec298dfd4051c060a6a778636da Mon Sep 17 00:00:00 2001 From: mla Date: Thu, 3 Apr 2025 14:24:15 +0200 Subject: [PATCH 2/2] remove test target --- example/src/cpp/main/BUILD | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/example/src/cpp/main/BUILD b/example/src/cpp/main/BUILD index bbaeed9b..6c89bff7 100644 --- a/example/src/cpp/main/BUILD +++ b/example/src/cpp/main/BUILD @@ -1,5 +1,5 @@ load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") -load("//tools/lint:linters.bzl", "clang_tidy_test") + cc_library( name = "hello-greet", srcs = ["hello-greet.cc"], @@ -17,11 +17,6 @@ cc_binary( ], ) -clang_tidy_test( - name = "hello-world-clang-tidy-test", - srcs = ["hello-world"], -) - cc_library( name = "hello-greet-with-error", srcs = ["hello-greet-with-error.cc"],