Ben Clayton | adb10d6 | 2020-10-27 21:04:59 +0000 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 2 | # Copyright 2020 The Dawn & Tint Authors |
Ben Clayton | adb10d6 | 2020-10-27 21:04:59 +0000 | [diff] [blame] | 3 | # |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions are met: |
Ben Clayton | adb10d6 | 2020-10-27 21:04:59 +0000 | [diff] [blame] | 6 | # |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 7 | # 1. Redistributions of source code must retain the above copyright notice, this |
| 8 | # list of conditions and the following disclaimer. |
Ben Clayton | adb10d6 | 2020-10-27 21:04:59 +0000 | [diff] [blame] | 9 | # |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 10 | # 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 Clayton | adb10d6 | 2020-10-27 21:04:59 +0000 | [diff] [blame] | 28 | |
| 29 | # See https://clang.llvm.org/docs/SourceBasedCodeCoverage.html |
| 30 | |
Ben Clayton | adb10d6 | 2020-10-27 21:04:59 +0000 | [diff] [blame] | 31 | if [ ! -x "$(which llvm-profdata)" ] ; then |
| 32 | echo "error: llvm-profdata needs to be on \$PATH to use $0" |
| 33 | exit 1 |
| 34 | fi |
| 35 | |
| 36 | if [ ! -x "$(which llvm-cov)" ] ; then |
| 37 | echo "error: llvm-cov needs to be on \$PATH to use $0" |
| 38 | exit 1 |
| 39 | fi |
| 40 | |
| 41 | TARGET_EXE=$1 |
| 42 | |
| 43 | if [ ! -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 |
| 50 | fi |
| 51 | |
| 52 | SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd )" |
| 53 | ROOT_DIR="$( cd "${SCRIPT_DIR}/.." >/dev/null 2>&1 && pwd )" |
| 54 | |
| 55 | PROFRAW_FILE="${ROOT_DIR}/tint.profraw" |
| 56 | PROFDATA_FILE="${ROOT_DIR}/tint.profdata" |
| 57 | LCOV_FILE="${ROOT_DIR}/lcov.info" |
| 58 | SUMMARY_FILE="${ROOT_DIR}/coverage.summary" |
| 59 | |
Ben Clayton | 5af571b | 2021-11-23 17:57:37 +0000 | [diff] [blame] | 60 | # Remove any existing coverage data and intermediate files |
| 61 | if [ -f "$PROFRAW_FILE" ]; then rm ${PROFRAW_FILE}; fi |
| 62 | if [ -f "$PROFDATA_FILE" ]; then rm ${PROFDATA_FILE}; fi |
| 63 | if [ -f "$LCOV_FILE" ]; then rm ${LCOV_FILE}; fi |
| 64 | if [ -f "$SUMMARY_FILE" ]; then rm ${SUMMARY_FILE}; fi |
| 65 | |
Ben Clayton | adb10d6 | 2020-10-27 21:04:59 +0000 | [diff] [blame] | 66 | # Run the executable to generate the raw coverage data |
| 67 | # https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#running-the-instrumented-program |
Ben Clayton | 5af571b | 2021-11-23 17:57:37 +0000 | [diff] [blame] | 68 | LLVM_PROFILE_FILE="${PROFRAW_FILE}" "$@" |
| 69 | |
| 70 | # Check that coverage information was generated |
| 71 | if [ ! -f "$PROFRAW_FILE" ]; then |
| 72 | echo "lcov.info was not generated. Is coverage generation enabled?" |
James Price | fe0c2a7 | 2023-01-04 18:06:49 +0000 | [diff] [blame] | 73 | echo "To enable, run cmake with -DDAWN_EMIT_COVERAGE=1". |
Ben Clayton | 5af571b | 2021-11-23 17:57:37 +0000 | [diff] [blame] | 74 | exit 1 |
| 75 | fi |
Ben Clayton | adb10d6 | 2020-10-27 21:04:59 +0000 | [diff] [blame] | 76 | |
Ben Clayton | 2abecbb | 2020-12-12 16:19:54 +0000 | [diff] [blame] | 77 | # Fail on any error after running the target executable |
| 78 | set -e |
| 79 | |
Ben Clayton | adb10d6 | 2020-10-27 21:04:59 +0000 | [diff] [blame] | 80 | # Index the coverage data |
| 81 | # https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#creating-coverage-reports |
| 82 | llvm-profdata merge -sparse "${PROFRAW_FILE}" -o "${PROFDATA_FILE}" |
| 83 | |
| 84 | # Export as lcov |
| 85 | # https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#exporting-coverage-data |
| 86 | llvm-cov export --instr-profile="${PROFDATA_FILE}" --format=lcov --object=${TARGET_EXE} > "${LCOV_FILE}" |
| 87 | |
| 88 | # Generate summary report |
| 89 | llvm-cov report --ignore-filename-regex="(.*_test\.cc|third_party/.*)" --instr-profile="${PROFDATA_FILE}" --object=${TARGET_EXE} > "${SUMMARY_FILE}" |
| 90 | |
Ben Clayton | 5af571b | 2021-11-23 17:57:37 +0000 | [diff] [blame] | 91 | # Clean up intermediate files |
Ben Clayton | adb10d6 | 2020-10-27 21:04:59 +0000 | [diff] [blame] | 92 | rm ${PROFRAW_FILE} ${PROFDATA_FILE} |