blob: 6a5cb9bfb21ab6d9bff9eb8cc5b9f3948a103cf7 [file] [log] [blame]
Ben Claytonadb10d62020-10-27 21:04:59 +00001#!/usr/bin/env bash
Austin Engcc2516a2023-10-17 20:57:54 +00002# Copyright 2020 The Dawn & Tint Authors
Ben Claytonadb10d62020-10-27 21:04:59 +00003#
Austin Engcc2516a2023-10-17 20:57:54 +00004# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are met:
Ben Claytonadb10d62020-10-27 21:04:59 +00006#
Austin Engcc2516a2023-10-17 20:57:54 +00007# 1. Redistributions of source code must retain the above copyright notice, this
8# list of conditions and the following disclaimer.
Ben Claytonadb10d62020-10-27 21:04:59 +00009#
Austin Engcc2516a2023-10-17 20:57:54 +000010# 2. Redistributions in binary form must reproduce the above copyright notice,
11# this list of conditions and the following disclaimer in the documentation
12# and/or other materials provided with the distribution.
13#
14# 3. Neither the name of the copyright holder nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Ben Claytonadb10d62020-10-27 21:04:59 +000028
29# See https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
30
Ben Claytonadb10d62020-10-27 21:04:59 +000031if [ ! -x "$(which llvm-profdata)" ] ; then
32 echo "error: llvm-profdata needs to be on \$PATH to use $0"
33 exit 1
34fi
35
36if [ ! -x "$(which llvm-cov)" ] ; then
37 echo "error: llvm-cov needs to be on \$PATH to use $0"
38 exit 1
39fi
40
41TARGET_EXE=$1
42
43if [ ! -x "$TARGET_EXE" ] ; then
44 echo "Usage: $0 <executable-path> [optional-args]"
45 echo ""
46 echo "Generates a lcov.info file at the project root, which can be used by"
47 echo "tools such as VSCode's Coverage Gutters extension to visualize code"
48 echo "coverage in the editor".
49 exit 1
50fi
51
52SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd )"
53ROOT_DIR="$( cd "${SCRIPT_DIR}/.." >/dev/null 2>&1 && pwd )"
54
55PROFRAW_FILE="${ROOT_DIR}/tint.profraw"
56PROFDATA_FILE="${ROOT_DIR}/tint.profdata"
57LCOV_FILE="${ROOT_DIR}/lcov.info"
58SUMMARY_FILE="${ROOT_DIR}/coverage.summary"
59
Ben Clayton5af571b2021-11-23 17:57:37 +000060# Remove any existing coverage data and intermediate files
61if [ -f "$PROFRAW_FILE" ]; then rm ${PROFRAW_FILE}; fi
62if [ -f "$PROFDATA_FILE" ]; then rm ${PROFDATA_FILE}; fi
63if [ -f "$LCOV_FILE" ]; then rm ${LCOV_FILE}; fi
64if [ -f "$SUMMARY_FILE" ]; then rm ${SUMMARY_FILE}; fi
65
Ben Claytonadb10d62020-10-27 21:04:59 +000066# Run the executable to generate the raw coverage data
67# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#running-the-instrumented-program
Ben Clayton5af571b2021-11-23 17:57:37 +000068LLVM_PROFILE_FILE="${PROFRAW_FILE}" "$@"
69
70# Check that coverage information was generated
71if [ ! -f "$PROFRAW_FILE" ]; then
72 echo "lcov.info was not generated. Is coverage generation enabled?"
James Pricefe0c2a72023-01-04 18:06:49 +000073 echo "To enable, run cmake with -DDAWN_EMIT_COVERAGE=1".
Ben Clayton5af571b2021-11-23 17:57:37 +000074 exit 1
75fi
Ben Claytonadb10d62020-10-27 21:04:59 +000076
Ben Clayton2abecbb2020-12-12 16:19:54 +000077# Fail on any error after running the target executable
78set -e
79
Ben Claytonadb10d62020-10-27 21:04:59 +000080# Index the coverage data
81# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#creating-coverage-reports
82llvm-profdata merge -sparse "${PROFRAW_FILE}" -o "${PROFDATA_FILE}"
83
84# Export as lcov
85# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#exporting-coverage-data
86llvm-cov export --instr-profile="${PROFDATA_FILE}" --format=lcov --object=${TARGET_EXE} > "${LCOV_FILE}"
87
88# Generate summary report
89llvm-cov report --ignore-filename-regex="(.*_test\.cc|third_party/.*)" --instr-profile="${PROFDATA_FILE}" --object=${TARGET_EXE} > "${SUMMARY_FILE}"
90
Ben Clayton5af571b2021-11-23 17:57:37 +000091# Clean up intermediate files
Ben Claytonadb10d62020-10-27 21:04:59 +000092rm ${PROFRAW_FILE} ${PROFDATA_FILE}