Skip to content

Commit 3a13af4

Browse files
authored
Remove LCOV merger dependency of cc_test without coverage (bazelbuild#17004)
When coverage is disabled, `cc_test` should not depend on the Java LCOV merger tool. This is achieved by using `configuration_field`. Also depend on coverage tools in `exec` rather than `target` configuration, matching Java rules as well as the once-per-build coverage report generator. Fixes bazelbuild#16961 Closes bazelbuild#16994. PiperOrigin-RevId: 494941601 Change-Id: Ie614de713b87bb66d869515676698f1a71cb106e
1 parent 7e80d02 commit 3a13af4

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

src/main/starlark/builtins_bzl/common/cc/cc_test.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def make_cc_test(with_linkstatic = False, with_aspects = False):
137137
"stripped_binary": "%{name}.stripped",
138138
"dwp_file": "%{name}.dwp",
139139
},
140-
fragments = ["google_cpp", "cpp"],
140+
fragments = ["google_cpp", "cpp", "coverage"],
141141
exec_groups = {
142142
"cpp_link": exec_group(copy_from_rule = True),
143143
},

src/main/starlark/builtins_bzl/common/cc/semantics.bzl

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ def _get_test_malloc_attr():
8080
def _get_coverage_attrs():
8181
return {
8282
"_lcov_merger": attr.label(
83-
default = "@bazel_tools//tools/test:lcov_merger",
83+
default = configuration_field(fragment = "coverage", name = "output_generator"),
8484
executable = True,
85-
cfg = "target",
85+
cfg = "exec",
8686
),
8787
"_collect_cc_coverage": attr.label(
8888
default = "@bazel_tools//tools/test:collect_cc_coverage",
8989
executable = True,
90-
cfg = "target",
90+
cfg = "exec",
9191
),
9292
}
9393

src/test/shell/bazel/cc_integration_test.sh

+42
Original file line numberDiff line numberDiff line change
@@ -1752,4 +1752,46 @@ EOF
17521752
bazel build //:main --repo_env=CC=clang || fail "Expected compiler flag to have value 'clang'"
17531753
}
17541754

1755+
function test_cc_test_no_target_coverage_dep() {
1756+
# Regression test for https://github.com/bazelbuild/bazel/issues/16961
1757+
local package="${FUNCNAME[0]}"
1758+
mkdir -p "${package}"
1759+
1760+
cat > "${package}"/BUILD.bazel <<'EOF'
1761+
cc_test(
1762+
name = "test",
1763+
srcs = ["test.cc"],
1764+
)
1765+
EOF
1766+
touch "${package}"/test.cc
1767+
1768+
out=$(bazel cquery --collect_code_coverage \
1769+
"deps(//${package}:test) intersect config(@remote_coverage_tools//:all, target)")
1770+
if [[ -n "$out" ]]; then
1771+
fail "Expected no dependency on lcov_merger in the target configuration, but got: $out"
1772+
fi
1773+
}
1774+
1775+
function test_cc_test_no_lcov_merger_dep_without_coverage() {
1776+
# Regression test for https://github.com/bazelbuild/bazel/issues/16961
1777+
local package="${FUNCNAME[0]}"
1778+
mkdir -p "${package}"
1779+
1780+
cat > "${package}"/BUILD.bazel <<'EOF'
1781+
cc_test(
1782+
name = "test",
1783+
srcs = ["test.cc"],
1784+
)
1785+
EOF
1786+
touch "${package}"/test.cc
1787+
1788+
# FIXME: cc_test still unconditionally depends on the LCOV merger binary through
1789+
# @remote_coverage_tools//:coverage_output_generator, which is also unnecessary:
1790+
# https://github.com/bazelbuild/bazel/issues/15088
1791+
out=$(bazel cquery "somepath(//${package}:test,@remote_coverage_tools//:lcov_merger)")
1792+
if [[ -n "$out" ]]; then
1793+
fail "Expected no dependency on lcov_merger, but got: $out"
1794+
fi
1795+
}
1796+
17551797
run_suite "cc_integration_test"

0 commit comments

Comments
 (0)