Partially fix C++ coverage on Linux

gcov 8.3.0 no longer prints "Creating <output>" messages, so we have to
fall back to collecting all .gcov files in the current directory. Also,
provide a mechanism to pass additional options to gcov, e.g.:

--test_env=COVERAGE_GCOV_OPTIONS="-p -x"

We cannot always pass these flags, as not all versions of gcov support
them.

This also solves one issue on MacOS, where gcov output emits lower-case
"creating <output>" messages.

Progress on #9406.

PiperOrigin-RevId: 291172263
diff --git a/tools/test/collect_cc_coverage.sh b/tools/test/collect_cc_coverage.sh
index c55b8e9..3e95c57 100755
--- a/tools/test/collect_cc_coverage.sh
+++ b/tools/test/collect_cc_coverage.sh
@@ -28,6 +28,7 @@
 #                           coverage collection (e.g. gcda files, profraw).
 # - COVERAGE_MANIFEST       Location of the instrumented file manifest.
 # - COVERAGE_GCOV_PATH      Location of gcov. This is set by the TestRunner.
+# - COVERAGE_GCOV_OPTIONS   Additional options to pass to gcov.
 # - ROOT                    Location from where the code coverage collection
 #                           was invoked.
 #
@@ -114,25 +115,12 @@
           # Don't generate branch coverage (-b) 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).
-          "${GCOV}" -i -o "$(dirname ${gcda})" "${gcda}" &> "$gcov_log"
+          "${GCOV}" -i $COVERAGE_GCOV_OPTIONS -o "$(dirname ${gcda})" "${gcda}"
 
-          # Go through all the files that were created by the gcov command above
-          # and append their content to the output .gcov file.
-          #
-          # For each source file gcov outputs to stdout something like this:
-          #
-          # File 'examples/cpp/hello-lib.cc'
-          # Lines executed:100.00% of 8
-          # Creating 'hello-lib.cc.gcov'
-          #
-          # We grep the names of the files that were created from that output.
-          cat "$gcov_log" | grep "Creating" | cut -d " " -f 2 | cut -d"'" -f2 | \
-              while read gcov_file; do
-            echo "Processing $gcov_file"
-            cat "$gcov_file" >> "$output_file"
-            # Remove the intermediate gcov file because it is not useful anymore.
-            rm -f "$gcov_file"
-          done
+          # Append all .gcov files in the current directory to the output file.
+          cat *.gcov >> "$output_file"
+          # Delete the .gcov files.
+          rm *.gcov
       fi
     fi
   done < "${COVERAGE_MANIFEST}"