Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ jobs:
- name: Test Coverage
run: |
ln -s .github/workflows/.bazelrc.ci .bazelrc.ci
bazel coverage --config ${{ matrix.bzlmod_config }} //examples/hello_world_cpp:hello_world_cpp_test
bazel coverage --config ${{ matrix.bzlmod_config }} //examples/hello_world_cpp:hello_world_cpp_test //tests/coverage/...
bazel9:
strategy:
matrix:
Expand Down
37 changes: 37 additions & 0 deletions tests/coverage/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright (c) Thulio Ferraz Assis 2026
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("//rules_cc:defs.bzl", "cc_library")
load("//rules_fortran:defs.bzl", "fortran_binary")

cc_library(
name = "add_one",
srcs = ["add_one.c"],
)

fortran_binary(
name = "fortran_links_instrumented_c",
srcs = ["main.f90"],
deps = [":add_one"],
)

# Linking a coverage-instrumented C object into a Fortran executable only
# succeeds if the fortran_coverage feature mirrors --coverage onto the
# fortran-link-executable action. Run with --collect_code_coverage to exercise
# that; without it this is an ordinary mixed Fortran/C link.
build_test(
name = "fortran_coverage_link_test",
targets = [":fortran_links_instrumented_c"],
)
26 changes: 26 additions & 0 deletions tests/coverage/add_one.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Thulio Ferraz Assis 2026
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// When Bazel collects code coverage this translation unit is compiled with
// -fprofile-arcs -ftest-coverage, so its object file gains constructors and
// destructors that reference __gcov_init, __gcov_exit and __gcov_merge_add.
// Those symbols live in libgcov, which the linker only pulls in when the link
// action receives --coverage. The branch below exists so that there is more
// than one arc to instrument.
int add_one(int value) {
if (value < 0) {
return value;
}
return value + 1;
}
32 changes: 32 additions & 0 deletions tests/coverage/main.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
! Copyright (c) Thulio Ferraz Assis 2026
!
! Licensed under the Apache License, Version 2.0 (the "License");
! you may not use this file except in compliance with the License.
! You may obtain a copy of the License at
!
! http://www.apache.org/licenses/LICENSE-2.0
!
! Unless required by applicable law or agreed to in writing, software
! distributed under the License is distributed on an "AS IS" BASIS,
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
! See the License for the specific language governing permissions and
! limitations under the License.

program fortran_links_instrumented_c
use, intrinsic :: iso_c_binding, only: c_int
implicit none

interface
function add_one(value) bind(C, name='add_one')
import :: c_int
integer(c_int), value :: value
integer(c_int) :: add_one
end function add_one
end interface

if (add_one(1_c_int) /= 2_c_int) then
error stop 'add_one(1) did not return 2'
end if

write(*,'(a)') adjustl('OK')
end program fortran_links_instrumented_c
31 changes: 31 additions & 0 deletions toolchain/cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,36 @@ def _impl(ctx):
],
)

# Bazel enables the "coverage" feature on every action whenever 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 links coverage-instrumented C/C++ objects
# therefore fails to link with undefined references to __gcov_init, __gcov_exit and
# __gcov_merge_add, so the flag has to be mirrored onto the Fortran link action.
#
# Only the link action is covered. Instrumenting fortran-compile would additionally need
# rules_fortran to declare the .gcno outputs and report them through InstrumentedFilesInfo;
# without that, -ftest-coverage would emit undeclared outputs that Bazel discards, so the
# Fortran sources themselves stay uninstrumented.
fortran_coverage_feature = feature(
name = "fortran_coverage",
enabled = True,
flag_sets = [
flag_set(
actions = [FORTRAN_ACTION_NAMES.fortran_link_executable],
flag_groups = [
flag_group(
flags = ["--coverage"],
),
],
with_features = [
with_feature_set(
features = ["coverage"],
),
],
),
],
)

if enable_fortran:
action_configs.append(action_config(
action_name = FORTRAN_ACTION_NAMES.fortran_compile,
Expand Down Expand Up @@ -545,6 +575,7 @@ def _impl(ctx):
fortran_compile_flags_feature,
static_libgfortran_feature,
fortran_link_flags_feature,
fortran_coverage_feature,
extra_fflags_feature,
) + sanitizers_features

Expand Down
Loading