blob: 284e2e9b277d5273572e008ec732877667674478 [file] [log] [blame]
Ben Claytonadb10d62020-10-27 21:04:59 +00001#!/usr/bin/env bash
2# Copyright 2020 The Tint Authors
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# See https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
17
Ben Claytonadb10d62020-10-27 21:04:59 +000018if [ ! -x "$(which llvm-profdata)" ] ; then
19 echo "error: llvm-profdata needs to be on \$PATH to use $0"
20 exit 1
21fi
22
23if [ ! -x "$(which llvm-cov)" ] ; then
24 echo "error: llvm-cov needs to be on \$PATH to use $0"
25 exit 1
26fi
27
28TARGET_EXE=$1
29
30if [ ! -x "$TARGET_EXE" ] ; then
31 echo "Usage: $0 <executable-path> [optional-args]"
32 echo ""
33 echo "Generates a lcov.info file at the project root, which can be used by"
34 echo "tools such as VSCode's Coverage Gutters extension to visualize code"
35 echo "coverage in the editor".
36 exit 1
37fi
38
39SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd )"
40ROOT_DIR="$( cd "${SCRIPT_DIR}/.." >/dev/null 2>&1 && pwd )"
41
42PROFRAW_FILE="${ROOT_DIR}/tint.profraw"
43PROFDATA_FILE="${ROOT_DIR}/tint.profdata"
44LCOV_FILE="${ROOT_DIR}/lcov.info"
45SUMMARY_FILE="${ROOT_DIR}/coverage.summary"
46
Ben Clayton5af571b2021-11-23 17:57:37 +000047# Remove any existing coverage data and intermediate files
48if [ -f "$PROFRAW_FILE" ]; then rm ${PROFRAW_FILE}; fi
49if [ -f "$PROFDATA_FILE" ]; then rm ${PROFDATA_FILE}; fi
50if [ -f "$LCOV_FILE" ]; then rm ${LCOV_FILE}; fi
51if [ -f "$SUMMARY_FILE" ]; then rm ${SUMMARY_FILE}; fi
52
Ben Claytonadb10d62020-10-27 21:04:59 +000053# Run the executable to generate the raw coverage data
54# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#running-the-instrumented-program
Ben Clayton5af571b2021-11-23 17:57:37 +000055LLVM_PROFILE_FILE="${PROFRAW_FILE}" "$@"
56
57# Check that coverage information was generated
58if [ ! -f "$PROFRAW_FILE" ]; then
59 echo "lcov.info was not generated. Is coverage generation enabled?"
James Pricefe0c2a72023-01-04 18:06:49 +000060 echo "To enable, run cmake with -DDAWN_EMIT_COVERAGE=1".
Ben Clayton5af571b2021-11-23 17:57:37 +000061 exit 1
62fi
Ben Claytonadb10d62020-10-27 21:04:59 +000063
Ben Clayton2abecbb2020-12-12 16:19:54 +000064# Fail on any error after running the target executable
65set -e
66
Ben Claytonadb10d62020-10-27 21:04:59 +000067# Index the coverage data
68# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#creating-coverage-reports
69llvm-profdata merge -sparse "${PROFRAW_FILE}" -o "${PROFDATA_FILE}"
70
71# Export as lcov
72# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#exporting-coverage-data
73llvm-cov export --instr-profile="${PROFDATA_FILE}" --format=lcov --object=${TARGET_EXE} > "${LCOV_FILE}"
74
75# Generate summary report
76llvm-cov report --ignore-filename-regex="(.*_test\.cc|third_party/.*)" --instr-profile="${PROFDATA_FILE}" --object=${TARGET_EXE} > "${SUMMARY_FILE}"
77
Ben Clayton5af571b2021-11-23 17:57:37 +000078# Clean up intermediate files
Ben Claytonadb10d62020-10-27 21:04:59 +000079rm ${PROFRAW_FILE} ${PROFDATA_FILE}