Add option and target for generating code cov info

The new `tint-generate-coverage` CMake target can be used with the clang toolchain to generate a `lcov.info` file at the root of the project, along with a `coverage.summary` human readable plain text file.

The `lcov.info` file can then be used by various tools to display code coverage information in your IDE / code editor.

Useful for ensuring decent test coverage.

Change-Id: I3d846f6da3af25d3d600d8e028f27b89e35b545f
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/31121
Commit-Queue: Ben Clayton <bclayton@google.com>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
diff --git a/tools/tint-generate-coverage b/tools/tint-generate-coverage
new file mode 100755
index 0000000..e1bef24
--- /dev/null
+++ b/tools/tint-generate-coverage
@@ -0,0 +1,65 @@
+#!/usr/bin/env bash
+# Copyright 2020 The Tint Authors
+#
+# 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.
+
+# See https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
+
+set -e # Fail on any error.
+
+if [ ! -x "$(which llvm-profdata)" ] ; then
+    echo "error: llvm-profdata needs to be on \$PATH to use $0"
+    exit 1
+fi
+
+if [ ! -x "$(which llvm-cov)" ] ; then
+    echo "error: llvm-cov needs to be on \$PATH to use $0"
+    exit 1
+fi
+
+TARGET_EXE=$1
+
+if [ ! -x "$TARGET_EXE" ] ; then
+    echo "Usage: $0 <executable-path> [optional-args]"
+    echo ""
+    echo "Generates a lcov.info file at the project root, which can be used by"
+    echo "tools such as VSCode's Coverage Gutters extension to visualize code"
+    echo "coverage in the editor".
+    exit 1
+fi
+
+SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd )"
+ROOT_DIR="$( cd "${SCRIPT_DIR}/.." >/dev/null 2>&1 && pwd )"
+
+PROFRAW_FILE="${ROOT_DIR}/tint.profraw"
+PROFDATA_FILE="${ROOT_DIR}/tint.profdata"
+LCOV_FILE="${ROOT_DIR}/lcov.info"
+SUMMARY_FILE="${ROOT_DIR}/coverage.summary"
+
+# Run the executable to generate the raw coverage data
+# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#running-the-instrumented-program
+LLVM_PROFILE_FILE="${PROFRAW_FILE}" $@
+
+# Index the coverage data
+# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#creating-coverage-reports
+llvm-profdata merge -sparse "${PROFRAW_FILE}" -o "${PROFDATA_FILE}"
+
+# Export as lcov
+# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#exporting-coverage-data
+llvm-cov export --instr-profile="${PROFDATA_FILE}" --format=lcov --object=${TARGET_EXE} > "${LCOV_FILE}"
+
+# Generate summary report
+llvm-cov report --ignore-filename-regex="(.*_test\.cc|third_party/.*)" --instr-profile="${PROFDATA_FILE}" --object=${TARGET_EXE} > "${SUMMARY_FILE}"
+
+# Clean up
+rm ${PROFRAW_FILE} ${PROFDATA_FILE}