blob: 85875c08a2b54145708e9abb5063d6231a496a77 [file] [log] [blame]
Ulf Adams8829aba2016-11-24 12:51:35 +00001#!/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
17ROOT="$PWD"
18if [[ $COVERAGE_OUTPUT_FILE != /* ]]; then
19 COVERAGE_OUTPUT_FILE="${ROOT}/${COVERAGE_OUTPUT_FILE}"
20fi
21if [[ "$COVERAGE_MANIFEST" != /* ]]; then
22 export COVERAGE_MANIFEST="${ROOT}/${COVERAGE_MANIFEST}"
23fi
24
25export COVERAGE_DIR="$(mktemp -d ${TMPDIR:-/tmp}/tmp.XXXXXXXXXX)"
26trap "{ rm -rf ${COVERAGE_DIR} }" EXIT
27
28# C++ env variables
29export GCOV_PREFIX_STRIP=3
30export GCOV_PREFIX="${COVERAGE_DIR}"
31
32touch "${COVERAGE_OUTPUT_FILE}"
33
34DIR="$TEST_SRCDIR"
35if [ ! -z "$TEST_WORKSPACE" ]; then
36 DIR="$DIR"/"$TEST_WORKSPACE"
37fi
38cd "$DIR" || { echo "Could not chdir $DIR"; exit 1; }
39"$@"
40TEST_STATUS=$?
41
42if [[ ${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}
48fi
49
50echo "--"
51echo "Post-processing coverage results:"
52
53cat "${COVERAGE_MANIFEST}" | grep ".gcno$" | while read path; do
54 mkdir -p "${COVERAGE_DIR}/$(dirname ${path})"
55 cp "${ROOT}/${path}" "${COVERAGE_DIR}/${path}"
56done
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).
61cat "${COVERAGE_MANIFEST}" | egrep ".(cc|h)$" | while read path; do
62 mkdir -p "${COVERAGE_DIR}/$(dirname ${path})"
63 touch "${COVERAGE_DIR}/${path}"
64done
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}).
71sed -i -e "s*${COVERAGE_DIR}*${ROOT}*g" "${COVERAGE_OUTPUT_FILE}"
72