Skip to content

cargo_build_script: don't configure cc coverage features for build scripts - #4433

Open
TrapsterDK wants to merge 1 commit into
bazelbuild:mainfrom
TrapsterDK:cargo-build-script-no-coverage
Open

TrapsterDK wants to merge 1 commit into
bazelbuild:mainfrom
TrapsterDK:cargo-build-script-no-coverage

Conversation

@TrapsterDK

Copy link
Copy Markdown
Contributor

Fixes #4432

cargo_build_script now passes coverage, coverage_enabled, coverage_instrumented, gcc_coverage_map_format and llvm_coverage_map_format as extra_unsupported_features when it calls find_cc_toolchain. Bazel requests coverage and one of the map formats whenever coverage is enabled, whatever --instrumentation_filter says, and newer rules_cc also requests coverage_enabled/coverage_instrumented. So today every build script that uses the cc toolchain gets coverage flags under bazel coverage.

That feature configuration is only used to build the build script's cc environment (CC/CXX/AR, CFLAGS, CXXFLAGS, LDFLAGS, the linker env and INCLUDE). Turning the features off there keeps coverage flags out of the build script and out of whatever it compiles through cc-rs. It also means variables those features expect, such as gcov_gcno_file, are never needed. That variable should break bazel coverage on macOS with apple_support >= 2.10.0 and the default gcc covmap: its gcc_coverage_map_format feature sets GCOV_GCNO_FILE=%{gcov_gcno_file}, and a build script action has no gcno path. I haven't reproduced that on a Mac; it follows from reading the apple_support toolchain config. The companion apple_support PR, bazelbuild/apple_support#693, guards that env entry on its side.

find_cc_toolchain already took extra_unsupported_features, so the change is one call site. Coverage of Rust code itself is unaffected: that goes through rustc's -Cinstrument-coverage and doesn't use this feature configuration.

Tests, in cargo/tests/cargo_build_script/cc_args_and_env:

  • The test cc toolchain there now has coverage, gcc_coverage_map_format and llvm_coverage_map_format features that add flags to compile and link actions. They're off unless requested, so the existing tests don't change. The transition used by those tests can now also set --collect_code_coverage.
  • coverage_features_disabled_test analyzes a build script with --collect_code_coverage and checks that no coverage flags show up in CFLAGS, CXXFLAGS or LDFLAGS. Without the fix it fails: CFLAGS and CXXFLAGS contain --coverage -fprofile-arcs -ftest-coverage, and LDFLAGS contains --coverage.
  • coverage_features_disabled_test_control is a check on the test itself. In the same configuration with the same toolchain, a plain feature configuration does get --coverage. That way the first test can't pass just because coverage was never requested. I checked that it fails when the transition doesn't turn coverage on.

bazel test //cargo/tests/cargo_build_script/... passes (72 tests).

Assisted by Claude.

…ripts

Whenever coverage is enabled, Bazel requests the `coverage` feature and a
coverage map format (`gcc_coverage_map_format` by default,
`llvm_coverage_map_format` with `--experimental_use_llvm_covmap`) when
configuring cc features; newer rules_cc also requests `coverage_enabled`
and `coverage_instrumented`. This doesn't depend on
`--instrumentation_filter`. `cargo_build_script` used that feature
configuration to compute CC/CFLAGS/CXXFLAGS/LDFLAGS and the linker env, so
every build script with `use_cc_toolchain` picked up the toolchain's
coverage flags, and so did the C code it compiles through cc-rs.

On macOS this breaks `bazel coverage`: apple_support's
`gcc_coverage_map_format` feature sets `GCOV_GCNO_FILE=%{gcov_gcno_file}`
on compile actions, and a build script action has no gcno path, so
analysis fails on the unexpanded variable.

The build script now passes these five features as extra unsupported
features to `find_cc_toolchain`.

Test: new `coverage_features_disabled_test` in
`cargo/tests/cargo_build_script/cc_args_and_env`. It gives the test cc
toolchain coverage features, analyzes the build script with
`--collect_code_coverage`, and checks CFLAGS, CXXFLAGS and LDFLAGS carry
no coverage flags. It fails without the change.
`coverage_features_disabled_test_control` checks that in the same
configuration a plain feature configuration for the same toolchain does
get `--coverage`, so the first test can't pass just because coverage was
never requested.

Assisted-by: Claude

This branch has not been deployed

No deployments
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.

cargo_build_script picks up cc coverage features when coverage is enabled

1 participant