Ulf Adams | 8829aba | 2016-11-24 12:51:35 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright 2016 The Bazel Authors. All rights reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | ROOT="$PWD" |
| 18 | if [[ $COVERAGE_OUTPUT_FILE != /* ]]; then |
| 19 | COVERAGE_OUTPUT_FILE="${ROOT}/${COVERAGE_OUTPUT_FILE}" |
| 20 | fi |
| 21 | if [[ "$COVERAGE_MANIFEST" != /* ]]; then |
| 22 | export COVERAGE_MANIFEST="${ROOT}/${COVERAGE_MANIFEST}" |
| 23 | fi |
| 24 | |
| 25 | export COVERAGE_DIR="$(mktemp -d ${TMPDIR:-/tmp}/tmp.XXXXXXXXXX)" |
| 26 | trap "{ rm -rf ${COVERAGE_DIR} }" EXIT |
| 27 | |
| 28 | # C++ env variables |
| 29 | export GCOV_PREFIX_STRIP=3 |
| 30 | export GCOV_PREFIX="${COVERAGE_DIR}" |
| 31 | |
| 32 | touch "${COVERAGE_OUTPUT_FILE}" |
| 33 | |
| 34 | DIR="$TEST_SRCDIR" |
| 35 | if [ ! -z "$TEST_WORKSPACE" ]; then |
| 36 | DIR="$DIR"/"$TEST_WORKSPACE" |
| 37 | fi |
| 38 | cd "$DIR" || { echo "Could not chdir $DIR"; exit 1; } |
| 39 | "$@" |
| 40 | TEST_STATUS=$? |
| 41 | |
| 42 | if [[ ${TEST_STATUS} -ne 0 ]]; then |
| 43 | echo "--" |
| 44 | echo "Coverage runner: Not collecting coverage for failed test." |
| 45 | echo "The following commands failed with status ${TEST_STATUS}:" |
| 46 | echo "$@" |
| 47 | exit ${TEST_STATUS} |
| 48 | fi |
| 49 | |
| 50 | echo "--" |
| 51 | echo "Post-processing coverage results:" |
| 52 | |
| 53 | cat "${COVERAGE_MANIFEST}" | grep ".gcno$" | while read path; do |
| 54 | mkdir -p "${COVERAGE_DIR}/$(dirname ${path})" |
| 55 | cp "${ROOT}/${path}" "${COVERAGE_DIR}/${path}" |
| 56 | done |
| 57 | |
| 58 | # Unfortunately, lcov messes up the source file names if it can't find the files |
| 59 | # at their relative paths. Workaround by creating empty source files according |
| 60 | # to the manifest (i.e., only for files that are supposed to be instrumented). |
| 61 | cat "${COVERAGE_MANIFEST}" | egrep ".(cc|h)$" | while read path; do |
| 62 | mkdir -p "${COVERAGE_DIR}/$(dirname ${path})" |
| 63 | touch "${COVERAGE_DIR}/${path}" |
| 64 | done |
| 65 | |
| 66 | # Run lcov over the .gcno and .gcda files to generate the lcov tracefile. |
| 67 | /usr/bin/lcov -c --no-external -d "${COVERAGE_DIR}" -o "${COVERAGE_OUTPUT_FILE}" |
| 68 | |
| 69 | # The paths are all wrong, because they point to /tmp. Fix up the paths to |
| 70 | # point to the exec root instead (${ROOT}). |
| 71 | sed -i -e "s*${COVERAGE_DIR}*${ROOT}*g" "${COVERAGE_OUTPUT_FILE}" |
| 72 | |