1#!/usr/bin/env bash
2# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3
4# Exit on error.
5set -e
6
7if [ -n "$USE_CLANG" ]; then
8  echo "Error: Coverage test is supported only for gcc."
9  exit 1
10fi
11
12ROOT=".."
13# Fetch right version of gcov
14if [ -d /mnt/gvfs/third-party -a -z "$CXX" ]; then
15  source $ROOT/build_tools/fbcode_config.sh
16  GCOV=$GCC_BASE/bin/gcov
17else
18  GCOV=$(which gcov)
19fi
20echo -e "Using $GCOV"
21
22COVERAGE_DIR="$PWD/COVERAGE_REPORT"
23mkdir -p $COVERAGE_DIR
24
25# Find all gcno files to generate the coverage report
26
27PYTHON=${1:-`which python`}
28echo -e "Using $PYTHON"
29GCNO_FILES=`find $ROOT -name "*.gcno"`
30$GCOV --preserve-paths --relative-only --no-output $GCNO_FILES 2>/dev/null |
31  # Parse the raw gcov report to more human readable form.
32  $PYTHON $ROOT/coverage/parse_gcov_output.py |
33  # Write the output to both stdout and report file.
34  tee $COVERAGE_DIR/coverage_report_all.txt &&
35echo -e "Generated coverage report for all files: $COVERAGE_DIR/coverage_report_all.txt\n"
36
37# TODO: we also need to get the files of the latest commits.
38# Get the most recently committed files.
39LATEST_FILES=`
40  git show --pretty="format:" --name-only HEAD |
41  grep -v "^$" |
42  paste -s -d,`
43RECENT_REPORT=$COVERAGE_DIR/coverage_report_recent.txt
44
45echo -e "Recently updated files: $LATEST_FILES\n" > $RECENT_REPORT
46$GCOV --preserve-paths --relative-only --no-output $GCNO_FILES 2>/dev/null |
47  $PYTHON $ROOT/coverage/parse_gcov_output.py -interested-files $LATEST_FILES |
48  tee -a $RECENT_REPORT &&
49echo -e "Generated coverage report for recently updated files: $RECENT_REPORT\n"
50
51# Unless otherwise specified, we'll not generate html report by default
52if [ -z "$HTML" ]; then
53  exit 0
54fi
55
56# Generate the html report. If we cannot find lcov in this machine, we'll simply
57# skip this step.
58echo "Generating the html coverage report..."
59
60LCOV=$(which lcov || true 2>/dev/null)
61if [ -z $LCOV ]
62then
63  echo "Skip: Cannot find lcov to generate the html report."
64  exit 0
65fi
66
67LCOV_VERSION=$(lcov -v | grep 1.1 || true)
68if [ $LCOV_VERSION ]
69then
70  echo "Not supported lcov version. Expect lcov 1.1."
71  exit 0
72fi
73
74(cd $ROOT; lcov --no-external \
75     --capture  \
76     --directory $PWD \
77     --gcov-tool $GCOV \
78     --output-file $COVERAGE_DIR/coverage.info)
79
80genhtml $COVERAGE_DIR/coverage.info -o $COVERAGE_DIR
81
82echo "HTML Coverage report is generated in $COVERAGE_DIR"
83