Sync upstream coverage changes

Copybara Import from https://github.com/bazelbuild/rules_cc/pull/795

BEGIN_PUBLIC
Sync upstream coverage changes (#795)

Fixes https://github.com/bazelbuild/rules_cc/issues/786

Pulls in https://github.com/bazelbuild/bazel/pull/29109 and https://github.com/bazelbuild/bazel/pull/29091

Closes #795
END_PUBLIC

Tested:
    TAP --sample ran all affected tests and none failed
    []
COPYBARA_INTEGRATE_REVIEW=https://github.com/bazelbuild/rules_cc/pull/795 from keith:ks/sync-upstream-coverage-changes 20199fe6eda1de6580c077f8d95ff875ac8e59e9
PiperOrigin-RevId: 958084427
Change-Id: I8403908a6cc2a2bd5b63acf6b3664c071d3d89f4
diff --git a/cc/private/coverage/collect_cc_coverage.sh b/cc/private/coverage/collect_cc_coverage.sh
index 609284b..5773109 100755
--- a/cc/private/coverage/collect_cc_coverage.sh
+++ b/cc/private/coverage/collect_cc_coverage.sh
@@ -37,7 +37,9 @@
 # gcda or profraw) and uses either lcov or gcov to get the coverage data.
 # The coverage data is placed in $COVERAGE_OUTPUT_FILE.
 
-if [[ -n "$VERBOSE_COVERAGE" ]]; then
+set -u
+
+if [[ -n "${VERBOSE_COVERAGE:-}" ]]; then
   set -x
 fi
 
@@ -49,12 +51,6 @@
   return 1
 }
 
-# Returns 0 if gcov must be used, 1 otherwise.
-function uses_gcov() {
-  [[ "$GCOV_COVERAGE" -eq "1"  ]] && return 0
-  return 1
-}
-
 function init_gcov() {
   # Symlink the gcov tool such with a link called gcov. Clang comes with a tool
   # called llvm-cov, which behaves like gcov if symlinked in this way (otherwise
@@ -109,6 +105,8 @@
 # - output_file     The location of the file where the generated code coverage
 #                   report is written.
 function gcov_coverage() {
+  init_gcov
+
   local output_file="${1}"; shift
 
   # We'll save the standard output of each the gcov command in this log.
@@ -161,9 +159,9 @@
           # because of a gcov issue that segfaults when both -i and -b are used
           # (see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84879).
           if [[ $gcov_major_version -le 7 ]]; then
-              "${GCOV}" -i $COVERAGE_GCOV_OPTIONS -o "$(dirname ${gcda})" "${gcda}"
+              "${GCOV}" -i ${COVERAGE_GCOV_OPTIONS:-} -o "$(dirname ${gcda})" "${gcda}"
           else
-              "${GCOV}" -i -b $COVERAGE_GCOV_OPTIONS -o "$(dirname ${gcda})" "${gcda}"
+              "${GCOV}" -i -b ${COVERAGE_GCOV_OPTIONS:-} -o "$(dirname ${gcda})" "${gcda}"
           fi
 
           # Check the type of output: gcov 9 or later outputs compressed JSON
@@ -181,27 +179,17 @@
       fi
     fi
   done < "${COVERAGE_MANIFEST}"
+
+  rm -f "${GCOV}"
 }
 
 function main() {
-  init_gcov
-
   # If llvm code coverage is used, we output the raw code coverage report in
   # the $COVERAGE_OUTPUT_FILE. This report will not be converted to any other
   # format by LcovMerger.
   # TODO(#5881): Convert profdata reports to lcov.
   if uses_llvm; then
-    if [[ -z "${LLVM_PROFDATA:-}" ]]; then
-        echo "LLVM_PROFDATA is not set. The toolchain must provide llvm-profdata" \
-             "in tool_paths to support coverage with clang." >&2
-        exit 1
-    fi
     if [[ "${GENERATE_LLVM_LCOV}" == "1" ]]; then
-        if [[ -z "${LLVM_COV:-}" ]]; then
-            echo "LLVM_COV is not set. The toolchain must provide llvm-cov" \
-                 "in tool_paths to support lcov coverage with clang." >&2
-            exit 1
-        fi
         BAZEL_CC_COVERAGE_TOOL="LLVM_LCOV"
     else
         BAZEL_CC_COVERAGE_TOOL="PROFDATA"