fix: link Fortran executables with --coverage under code coverage - #235
Merged
Conversation
Bazel enables the "coverage" feature on every action when code coverage
is being collected, but rules_cc's legacy gcc_coverage_map_format
feature only attaches --coverage to the C/C++ link actions. A
fortran_binary that statically links coverage-instrumented C/C++ objects
therefore failed to link:
libxwin.a(Xwin2.pic.o): undefined reference to `__gcov_init'
libxwin.a(Xwin2.pic.o): undefined reference to `__gcov_exit'
libxwin.a(Xwin2.pic.o): undefined reference to `__gcov_merge_add'
Add a fortran_coverage feature that mirrors --coverage onto
fortran-link-executable, gated on the coverage feature so non-coverage
builds are unchanged.
Only the link action is covered. Instrumenting fortran-compile would
also require rules_fortran to declare the .gcno outputs and report them
through InstrumentedFilesInfo; without that, -ftest-coverage emits
undeclared outputs that Bazel discards, so Fortran sources stay
uninstrumented.
The coverage CI job only exercised a C++ test, which is why this went
unnoticed. It now also builds tests/coverage, where a fortran_binary
links an instrumented C library; that target fails to link without this
change.
There was a problem hiding this comment.
Pull request overview
This PR fixes bazel coverage link failures for mixed Fortran/C(/C++) builds by ensuring the Fortran link action also receives coverage link flags when Bazel enables the coverage feature during coverage collection.
Changes:
- Adds a
fortran_coveragetoolchain feature that mirrors--coverageonto thefortran-link-executableaction when thecoveragefeature is enabled. - Introduces a coverage regression test that links a
fortran_binaryagainst a coverage-instrumentedcc_library. - Expands the CI coverage workflow to include the new
//tests/coverage/...targets.
Reviewed changes
Copilot reviewed 2 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| toolchain/cc_toolchain_config.bzl | Adds a fortran_coverage feature to apply --coverage to the Fortran link action under Bazel coverage. |
| tests/coverage/main.f90 | Adds a minimal Fortran program calling a C function via iso_c_binding to validate mixed-language linking. |
| tests/coverage/BUILD.bazel | Defines the mixed Fortran/C targets and a build_test used by coverage CI to exercise the link behavior. |
| tests/coverage/add_one.c | Adds a small C function intended to be coverage-instrumented and linked into the Fortran executable. |
| .github/workflows/default.yaml | Updates the coverage job to run coverage over //tests/coverage/... in addition to the existing C++ example test. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A
fortran_binarythat statically links coverage-instrumented C/C++ objects fails to link underbazel coverage:Cause
Bazel enables the
coveragefeature on every action whenever code coverage is being collected (configure_features, viactx.configuration.coverage_enabled). This toolchain does not setno_legacy_features, so rules_cc injects the legacycoverage/gcc_coverage_map_formatfeatures — and those attach--coverageonly to the standard C/C++ link actions (cpp_link_executable,cpp_link_dynamic_library,cpp_link_nodeps_dynamic_library,lto_index_for_*).fortran-link-executableis a custom action name defined by this repo, so no rules_cc feature enumerates it._fortran_binary_implbuilds its link line fromcc_common.get_memory_inefficient_command_line(action_name = fortran_link_executable, ...), which therefore contributes nothing coverage-related. The C half of the link is instrumented and emits__gcov_*references, so the link fails.The asymmetry only shows up in the target configuration — the same binaries link fine in the exec configuration, where coverage is off, so a Fortran binary used purely as a genrule tool never trips it.
Fix
A
fortran_coveragefeature that mirrors--coverageontofortran-link-executable, gated withwith_feature_set(features = ["coverage"])so non-coverage builds are byte-identical to before.This lives in the toolchain rather than in
_fortran_binary_implbecause every other Fortran link flag here is already expressed as a feature (fortran_link_flags,static_libgfortran,linker-lld, the sanitizers), which keeps the flag out of the rule implementation and lets a consumer opt out per target withfeatures = ["-fortran_coverage"].Why link-only
Instrumenting
fortran-compileas well would additionally requirerules_fortranto declare the.gcnooutputs and report them throughInstrumentedFilesInfo. Without that,-ftest-coverageemits undeclared outputs that Bazel discards — cost with no usable data — so Fortran sources deliberately stay uninstrumented. Doing it properly is a larger, separate change.Test
tests/coverage/adds afortran_binarylinking an instrumented Ccc_library, plus abuild_test. It is notmanual: without coverage it is an ordinary mixed Fortran/C link check in the regular suite, and under--collect_code_coverageit exercises the fix.The existing
coverageCI job only ran//examples/hello_world_cpp:hello_world_cpp_test— pure C++ — which is why this gap survived. It now also builds//tests/coverage/....Verification
bazel test --collect_code_coverage //tests/coverage/...reproduces the exact__gcov_init/__gcov_exit/__gcov_merge_addfailure; with it, the test passes.libadd_one.acarries 5__gcovreferences,add_one.pic.gcnois emitted, and the linked binary runs.aqueryconfirms--coverageappears on theFortranLinkcommand line under coverage and is absent without it.bazel test //...passes (12/12), as does the exact command the coverage CI job runs.bazel coverage(blocked by AVL and XFOIL, bothfortran_binarytargets linking an instrumentedxwinC library) now build and pass, and a combined LCOV report is produced.