From e856912402134d8ef00c8dd3fa3f94bc237a1fa1 Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Fri, 15 May 2026 08:46:11 -0700 Subject: [PATCH] Fix coverage with MacOS autoconf toolchains --- .bazelci/presubmit.yml | 17 ++++++++++++++++- cc/private/coverage/collect_cc_coverage.sh | 10 ++++++++++ cc/private/toolchain/unix_cc_configure.bzl | 7 +++++++ .../toolchain/unix_cc_toolchain_config.bzl | 12 ++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index e2217daa9..dab5f8b70 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -37,6 +37,11 @@ test_targets_bazel_6: &test_targets_bazel_6 - "-//tests/strip_include_prefix:test_include_textual" # Not supported in bazel <9. common_coverage_flags: &common_coverage_flags + # Test failures are not collected under `coverage` invocations. + # https://github.com/bazelbuild/continuous-integration/issues/1838 + - "--test_output=errors" + +windows_coverage_flags: &windows_coverage_flags # Starting in Bazel 9 coverage collection causes some tests to fail. # https://github.com/bazelbuild/rules_cc/issues/613 - "--test_env=IGNORE_COVERAGE_COLLECTION_FAILURES=1" @@ -45,6 +50,11 @@ common_coverage_flags: &common_coverage_flags - "--test_output=errors" coverage_flags_bazel_less_than_9: &coverage_flags_bazel_less_than_9 + # See above + - "--test_output=errors" + - "--deleted_packages=//tests/local_includes" + +windows_coverage_flags_bazel_less_than_9: &windows_coverage_flags_bazel_less_than_9 # See above - "--test_env=IGNORE_COVERAGE_COLLECTION_FAILURES=1" - "--test_output=errors" @@ -127,6 +137,8 @@ tasks: platform: windows build_targets: *build_targets test_targets: *test_targets + coverage_flags: *windows_coverage_flags + coverage_targets: *test_targets # Bazel 7 ubuntu2004_bazel_7: @@ -207,6 +219,8 @@ tasks: test_targets: *test_targets_bazel_6 build_flags: *flags_bazel_less_than_9 test_flags: *flags_bazel_less_than_9 + coverage_flags: *windows_coverage_flags_bazel_less_than_9 + coverage_targets: *test_targets_bazel_6 # Bazel 9 ubuntu2004: @@ -238,6 +252,8 @@ tasks: bazel: 9.x build_targets: *build_targets test_targets: *test_targets + coverage_flags: *windows_coverage_flags + coverage_targets: *test_targets ubuntu_bzlmod: name: Ubuntu 20.04 (Bazel 9, bzlmod) bazel: 9.x @@ -250,7 +266,6 @@ tasks: ubuntu_rule_based_toolchains_bazel_7: name: Ubuntu rule-based toolchains (Bazel 7) bazel: 7.2.1 - name: Ubuntu rule-based toolchains platform: ubuntu1804 working_directory: examples/rule_based_toolchain build_flags: diff --git a/cc/private/coverage/collect_cc_coverage.sh b/cc/private/coverage/collect_cc_coverage.sh index 5d3dc6227..609284b78 100755 --- a/cc/private/coverage/collect_cc_coverage.sh +++ b/cc/private/coverage/collect_cc_coverage.sh @@ -191,7 +191,17 @@ function main() { # format by LcovMerger. # TODO(#5881): Convert profdata reports to lcov. if uses_llvm; then + if [[ -z "${LLVM_PROFDATA:-}" ]]; then + echo "LLVM_PROFDATA is not set. The toolchain must provide llvm-profdata" \ + "in tool_paths to support coverage with clang." >&2 + exit 1 + fi if [[ "${GENERATE_LLVM_LCOV}" == "1" ]]; then + if [[ -z "${LLVM_COV:-}" ]]; then + echo "LLVM_COV is not set. The toolchain must provide llvm-cov" \ + "in tool_paths to support lcov coverage with clang." >&2 + exit 1 + fi BAZEL_CC_COVERAGE_TOOL="LLVM_LCOV" else BAZEL_CC_COVERAGE_TOOL="PROFDATA" diff --git a/cc/private/toolchain/unix_cc_configure.bzl b/cc/private/toolchain/unix_cc_configure.bzl index fbb4f1981..b85a2e8a6 100644 --- a/cc/private/toolchain/unix_cc_configure.bzl +++ b/cc/private/toolchain/unix_cc_configure.bzl @@ -395,6 +395,13 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overridden_tools): if darwin: overridden_tools["gcc"] = "cc_wrapper.sh" overridden_tools["ar"] = _find_generic(repository_ctx, "libtool", "LIBTOOL", overridden_tools) + xcrun = repository_ctx.which("xcrun") + if xcrun: + for tool_name in ["llvm-cov", "llvm-profdata"]: + if overridden_tools.get(tool_name) == None: + xcrun_result = repository_ctx.execute([xcrun, "--find", tool_name]) + if xcrun_result.return_code == 0: + overridden_tools[tool_name] = xcrun_result.stdout.strip() auto_configure_warning_maybe(repository_ctx, "CC used: " + str(cc)) tool_paths = _get_tool_paths(repository_ctx, overridden_tools) diff --git a/cc/private/toolchain/unix_cc_toolchain_config.bzl b/cc/private/toolchain/unix_cc_toolchain_config.bzl index 4fa2d2d0b..9996ccee9 100644 --- a/cc/private/toolchain/unix_cc_toolchain_config.bzl +++ b/cc/private/toolchain/unix_cc_toolchain_config.bzl @@ -258,6 +258,18 @@ def _impl(ctx): ) action_configs.append(llvm_cov_action) + llvm_profdata = ctx.attr.tool_paths.get("llvm-profdata") + if llvm_profdata: + llvm_profdata_action = action_config( + action_name = ACTION_NAMES.llvm_profdata, + tools = [ + tool( + path = llvm_profdata, + ), + ], + ) + action_configs.append(llvm_profdata_action) + objcopy = ctx.attr.tool_paths.get("objcopy") if objcopy: objcopy_action = action_config(