Ben Clayton | adb10d6 | 2020-10-27 21:04:59 +0000 | [diff] [blame] | 1 | #!/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 Clayton | adb10d6 | 2020-10-27 21:04:59 +0000 | [diff] [blame] | 18 | if [ ! -x "$(which llvm-profdata)" ] ; then |
| 19 | echo "error: llvm-profdata needs to be on \$PATH to use $0" |
| 20 | exit 1 |
| 21 | fi |
| 22 | |
| 23 | if [ ! -x "$(which llvm-cov)" ] ; then |
| 24 | echo "error: llvm-cov needs to be on \$PATH to use $0" |
| 25 | exit 1 |
| 26 | fi |
| 27 | |
| 28 | TARGET_EXE=$1 |
| 29 | |
| 30 | if [ ! -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 |
| 37 | fi |
| 38 | |
| 39 | SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd )" |
| 40 | ROOT_DIR="$( cd "${SCRIPT_DIR}/.." >/dev/null 2>&1 && pwd )" |
| 41 | |
| 42 | PROFRAW_FILE="${ROOT_DIR}/tint.profraw" |
| 43 | PROFDATA_FILE="${ROOT_DIR}/tint.profdata" |
| 44 | LCOV_FILE="${ROOT_DIR}/lcov.info" |
| 45 | SUMMARY_FILE="${ROOT_DIR}/coverage.summary" |
| 46 | |
Ben Clayton | 5af571b | 2021-11-23 17:57:37 +0000 | [diff] [blame] | 47 | # Remove any existing coverage data and intermediate files |
| 48 | if [ -f "$PROFRAW_FILE" ]; then rm ${PROFRAW_FILE}; fi |
| 49 | if [ -f "$PROFDATA_FILE" ]; then rm ${PROFDATA_FILE}; fi |
| 50 | if [ -f "$LCOV_FILE" ]; then rm ${LCOV_FILE}; fi |
| 51 | if [ -f "$SUMMARY_FILE" ]; then rm ${SUMMARY_FILE}; fi |
| 52 | |
Ben Clayton | adb10d6 | 2020-10-27 21:04:59 +0000 | [diff] [blame] | 53 | # Run the executable to generate the raw coverage data |
| 54 | # https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#running-the-instrumented-program |
Ben Clayton | 5af571b | 2021-11-23 17:57:37 +0000 | [diff] [blame] | 55 | LLVM_PROFILE_FILE="${PROFRAW_FILE}" "$@" |
| 56 | |
| 57 | # Check that coverage information was generated |
| 58 | if [ ! -f "$PROFRAW_FILE" ]; then |
| 59 | echo "lcov.info was not generated. Is coverage generation enabled?" |
James Price | fe0c2a7 | 2023-01-04 18:06:49 +0000 | [diff] [blame] | 60 | echo "To enable, run cmake with -DDAWN_EMIT_COVERAGE=1". |
Ben Clayton | 5af571b | 2021-11-23 17:57:37 +0000 | [diff] [blame] | 61 | exit 1 |
| 62 | fi |
Ben Clayton | adb10d6 | 2020-10-27 21:04:59 +0000 | [diff] [blame] | 63 | |
Ben Clayton | 2abecbb | 2020-12-12 16:19:54 +0000 | [diff] [blame] | 64 | # Fail on any error after running the target executable |
| 65 | set -e |
| 66 | |
Ben Clayton | adb10d6 | 2020-10-27 21:04:59 +0000 | [diff] [blame] | 67 | # Index the coverage data |
| 68 | # https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#creating-coverage-reports |
| 69 | llvm-profdata merge -sparse "${PROFRAW_FILE}" -o "${PROFDATA_FILE}" |
| 70 | |
| 71 | # Export as lcov |
| 72 | # https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#exporting-coverage-data |
| 73 | llvm-cov export --instr-profile="${PROFDATA_FILE}" --format=lcov --object=${TARGET_EXE} > "${LCOV_FILE}" |
| 74 | |
| 75 | # Generate summary report |
| 76 | llvm-cov report --ignore-filename-regex="(.*_test\.cc|third_party/.*)" --instr-profile="${PROFDATA_FILE}" --object=${TARGET_EXE} > "${SUMMARY_FILE}" |
| 77 | |
Ben Clayton | 5af571b | 2021-11-23 17:57:37 +0000 | [diff] [blame] | 78 | # Clean up intermediate files |
Ben Clayton | adb10d6 | 2020-10-27 21:04:59 +0000 | [diff] [blame] | 79 | rm ${PROFRAW_FILE} ${PROFDATA_FILE} |