diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaTestRule.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaTestRule.java
index 6495561dd081b8..6ba2788b1ba406 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaTestRule.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaTestRule.java
@@ -62,6 +62,13 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
attr(":lcov_merger", LABEL)
.cfg(ExecutionTransitionFactory.create())
.value(BaseRuleClasses.getCoverageOutputGeneratorLabel()))
+ // Add the script as an attribute in order for java_test to output code coverage results for
+ // code covered by CC binaries invocations.
+ .add(
+ attr("$collect_cc_coverage", LABEL)
+ .cfg(ExecutionTransitionFactory.create())
+ .singleArtifact()
+ .value(env.getToolsLabel("//tools/test:collect_cc_coverage")))
/*
The Java class to be loaded by the test runner.
diff --git a/src/test/shell/bazel/bazel_coverage_java_test.sh b/src/test/shell/bazel/bazel_coverage_java_test.sh
index d8104ced1a5cbf..27f86db018b32c 100755
--- a/src/test/shell/bazel/bazel_coverage_java_test.sh
+++ b/src/test/shell/bazel/bazel_coverage_java_test.sh
@@ -89,6 +89,25 @@ EOF
cat $(rlocation io_bazel/src/test/shell/bazel/testdata/jdk_http_archives) >> WORKSPACE
}
+# Returns 0 if gcov is not installed or if a version before 7.0 was found.
+# Returns 1 otherwise.
+function is_gcov_missing_or_wrong_version() {
+ local -r gcov_location=$(which gcov)
+ if [[ ! -x ${gcov_location:-/usr/bin/gcov} ]]; then
+ echo "gcov not installed."
+ return 0
+ fi
+
+ "$gcov_location" -version | grep "LLVM" && \
+ echo "gcov LLVM version not supported." && return 0
+ # gcov -v | grep "gcov" outputs a line that looks like this:
+ # gcov (Debian 7.3.0-5) 7.3.0
+ local gcov_version="$(gcov -v | grep "gcov" | cut -d " " -f 4 | cut -d "." -f 1)"
+ [ "$gcov_version" -lt 7 ] \
+ && echo "gcov versions before 7.0 is not supported." && return 0
+ return 1
+}
+
# Asserts if the given expected coverage result is included in the given output
# file.
#
@@ -990,4 +1009,138 @@ BRH:5"
assert_coverage_result "$expected_result" "$coverage_file_path"
}
+function test_java_test_coverage_cc_binary() {
+ if is_gcov_missing_or_wrong_version; then
+ echo "Skipping test." && return
+ fi
+
+ ########### Setup source files and BUILD file ###########
+ cat <