#!/usr/bin/env bash
# Copyright 2020 The Dawn & Tint Authors
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# See https://clang.llvm.org/docs/SourceBasedCodeCoverage.html

if [ ! -x "$(which llvm-profdata)" ] ; then
    echo "error: llvm-profdata needs to be on \$PATH to use $0"
    exit 1
fi

if [ ! -x "$(which llvm-cov)" ] ; then
    echo "error: llvm-cov needs to be on \$PATH to use $0"
    exit 1
fi

TARGET_EXE=$1

if [ ! -x "$TARGET_EXE" ] ; then
    echo "Usage: $0 <executable-path> [optional-args]"
    echo ""
    echo "Generates a lcov.info file at the project root, which can be used by"
    echo "tools such as VSCode's Coverage Gutters extension to visualize code"
    echo "coverage in the editor".
    exit 1
fi

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd )"
ROOT_DIR="$( cd "${SCRIPT_DIR}/.." >/dev/null 2>&1 && pwd )"

PROFRAW_FILE="${ROOT_DIR}/tint.profraw"
PROFDATA_FILE="${ROOT_DIR}/tint.profdata"
LCOV_FILE="${ROOT_DIR}/lcov.info"
SUMMARY_FILE="${ROOT_DIR}/coverage.summary"

# Remove any existing coverage data and intermediate files
if [ -f "$PROFRAW_FILE"  ]; then rm ${PROFRAW_FILE};  fi
if [ -f "$PROFDATA_FILE" ]; then rm ${PROFDATA_FILE}; fi
if [ -f "$LCOV_FILE" ];     then rm ${LCOV_FILE};     fi
if [ -f "$SUMMARY_FILE" ];  then rm ${SUMMARY_FILE};  fi

# Run the executable to generate the raw coverage data
# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#running-the-instrumented-program
LLVM_PROFILE_FILE="${PROFRAW_FILE}" "$@"

# Check that coverage information was generated
if [ ! -f "$PROFRAW_FILE" ]; then
    echo "lcov.info was not generated. Is coverage generation enabled?"
    echo "To enable, run cmake with -DDAWN_EMIT_COVERAGE=1".
    exit 1
fi

# Fail on any error after running the target executable
set -e

# Index the coverage data
# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#creating-coverage-reports
llvm-profdata merge -sparse "${PROFRAW_FILE}" -o "${PROFDATA_FILE}"

# Export as lcov
# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#exporting-coverage-data
llvm-cov export --instr-profile="${PROFDATA_FILE}" --format=lcov --object=${TARGET_EXE} > "${LCOV_FILE}"

# Generate summary report
llvm-cov report --ignore-filename-regex="(.*_test\.cc|third_party/.*)" --instr-profile="${PROFDATA_FILE}" --object=${TARGET_EXE} > "${SUMMARY_FILE}"

# Clean up intermediate files
rm ${PROFRAW_FILE} ${PROFDATA_FILE}
