Skip to content

Split coverage features into coverage_enabled and coverage_instrumented - #842

Open
fmeum wants to merge 2 commits into
bazelbuild:mainfrom
fmeum:cc-coverage-features
Open

Split coverage features into coverage_enabled and coverage_instrumented#842
fmeum wants to merge 2 commits into
bazelbuild:mainfrom
fmeum:cc-coverage-features

Conversation

@fmeum

@fmeum fmeum commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Before this change, rules_cc only enabled a single coverage feature when building with coverage, which doesn't directly allow toolchains to distinguish between targets that are instrumented for coverage and those which are not. The default toolchain thus ends up instrumenting everything even with a narrow --instrumentation_filter, which is wasteful. Blaze relies on the presence of the gcov_gcno_file variable to avoid this, but that's really more of a hack and undocumented (and also undocumentable).

This change lets rules_cc request two new coverage-related features: coverage_enabled is requested whenever coverage is generally enabled (matching coverage), whereas coverage_instrumented is only requested when the current target is instrumented. This provides a clean way for toolchains to instrument only those targets that require it (see #424 for an example of such a toolchain).

Along the way, implementation_deps is added to the check for an instrumented dependency, which apparently wasn't updated when this attribute was added.

Older versions of rules_cc only request coverage, so toolchains that want to support them as well can keep their existing coverage flag sets and guard them with with_feature_set(not_features = ["coverage_enabled"]).

Fixes bazelbuild/bazel#15627
Fixes bazelbuild/bazel#21911

@fmeum
fmeum marked this pull request as ready for review August 11, 2026 09:18
@fmeum

fmeum commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator Author

The coverage failure is preexisting, will send a fix to Bazel.

@fmeum

fmeum commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator Author

I pushed a new commit with further cleanup enabled by this change.

fmeum added 2 commits August 12, 2026 11:44
…ented`

`cc_common.configure_features` only requested a single `coverage` feature when building with coverage, which doesn't allow toolchains to distinguish between targets that are instrumented for coverage and those that aren't. The default toolchains thus end up instrumenting everything even with a narrow `--instrumentation_filter`, which is wasteful.

`coverage_enabled` is now requested whenever coverage is generally enabled (matching `coverage`), whereas `coverage_instrumented` is only requested if the current target is instrumented. This gives toolchains a clean way to only instrument the targets that require it, see bazelbuild#424 for an example.

Along the way, `implementation_deps` is added to the check for an instrumented dependency, which apparently wasn't updated when this attribute was added.

This is a port of bazelbuild/bazel#26355, with the coverage feature computation kept in `configure_features` instead of moved into the individual rules: unlike Bazel's Java implementation, the Starlark one has access to `ctx`, so every rule calling `cc_common.configure_features` benefits without losing the existing `coverage` and `*_coverage_map_format` features.
Derive coverage outputs and build variables directly from the coverage_instrumented feature. Inject legacy coverage marker features independently to avoid partial-definition conflicts.
@fmeum
fmeum force-pushed the cc-coverage-features branch from 66eaf86 to c2c13a3 Compare August 12, 2026 09:44
@fmeum

fmeum commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator Author

CI is green now @pzembrod

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Documentation on how to get c++ toolchain to work with bazel converage instrumentation_filter doesn't control what files to be instrumented

1 participant