1b8021494Sopenharmony_ci#!/bin/bash
2b8021494Sopenharmony_ci
3b8021494Sopenharmony_ci# Copyright 2021, VIXL authors
4b8021494Sopenharmony_ci# All rights reserved.
5b8021494Sopenharmony_ci#
6b8021494Sopenharmony_ci# Redistribution and use in source and binary forms, with or without
7b8021494Sopenharmony_ci# modification, are permitted provided that the following conditions are met:
8b8021494Sopenharmony_ci#
9b8021494Sopenharmony_ci#   * Redistributions of source code must retain the above copyright notice,
10b8021494Sopenharmony_ci#     this list of conditions and the following disclaimer.
11b8021494Sopenharmony_ci#   * Redistributions in binary form must reproduce the above copyright notice,
12b8021494Sopenharmony_ci#     this list of conditions and the following disclaimer in the documentation
13b8021494Sopenharmony_ci#     and/or other materials provided with the distribution.
14b8021494Sopenharmony_ci#   * Neither the name of ARM Limited nor the names of its contributors may be
15b8021494Sopenharmony_ci#     used to endorse or promote products derived from this software without
16b8021494Sopenharmony_ci#     specific prior written permission.
17b8021494Sopenharmony_ci#
18b8021494Sopenharmony_ci# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
19b8021494Sopenharmony_ci# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20b8021494Sopenharmony_ci# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21b8021494Sopenharmony_ci# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22b8021494Sopenharmony_ci# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23b8021494Sopenharmony_ci# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24b8021494Sopenharmony_ci# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25b8021494Sopenharmony_ci# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26b8021494Sopenharmony_ci# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27b8021494Sopenharmony_ci# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28b8021494Sopenharmony_ci
29b8021494Sopenharmony_ci# This code coverage script assumes a Linux-like environment, and has been
30b8021494Sopenharmony_ci# tested on Ubuntu 18.04.
31b8021494Sopenharmony_ci
32b8021494Sopenharmony_ciif ! hash pv 2>/dev/null ; then
33b8021494Sopenharmony_ci  echo "This script requires 'pv'"
34b8021494Sopenharmony_ci  echo "On Ubuntu, install it with 'sudo apt-get install pv'"
35b8021494Sopenharmony_ci  exit 1;
36b8021494Sopenharmony_cifi
37b8021494Sopenharmony_ci
38b8021494Sopenharmony_ciexport CXX=clang++
39b8021494Sopenharmony_ciexport LLVM_PROFILE_FILE=$(mktemp)
40b8021494Sopenharmony_ciPROFDATA=$(mktemp)
41b8021494Sopenharmony_ciBUILDDIR="obj/target_a64/mode_debug/symbols_on/compiler_clang++/std_c++17/simulator_aarch64/negative_testing_off/code_buffer_allocator_mmap"
42b8021494Sopenharmony_ciRUNNER="$BUILDDIR/test/test-runner"
43b8021494Sopenharmony_ci
44b8021494Sopenharmony_ci# Build with code coverage instrumentation enabled.
45b8021494Sopenharmony_ciscons mode=debug coverage=on target=a64 all -j8
46b8021494Sopenharmony_ci
47b8021494Sopenharmony_ciif [ ! -f "$RUNNER" ]; then
48b8021494Sopenharmony_ci    echo "$RUNNER not found."
49b8021494Sopenharmony_ci    echo "No test-runner for profiling."
50b8021494Sopenharmony_ci    exit 1;
51b8021494Sopenharmony_cifi
52b8021494Sopenharmony_ci
53b8021494Sopenharmony_ci# Count the number of tests.
54b8021494Sopenharmony_citests=`$RUNNER --list | wc -l`
55b8021494Sopenharmony_ci
56b8021494Sopenharmony_ci# Generate a raw profile for a run using all tests.
57b8021494Sopenharmony_ciecho "Running $tests tests. This may take a while..."
58b8021494Sopenharmony_ci$RUNNER --run-all 2>&1 | grep -P "^Running [A-Z0-9]{3,}_" | pv -lbp -w 40 -s $tests >/dev/null
59b8021494Sopenharmony_ci
60b8021494Sopenharmony_ci# Process the raw profile data for reporting.
61b8021494Sopenharmony_cillvm-profdata merge -sparse $LLVM_PROFILE_FILE -o $PROFDATA
62b8021494Sopenharmony_ci
63b8021494Sopenharmony_ci# Print a coverage report for the source files in src/
64b8021494Sopenharmony_ciREPORT="llvm-cov report $RUNNER -instr-profile=$PROFDATA $BUILDDIR/src/"
65b8021494Sopenharmony_cieval $REPORT
66b8021494Sopenharmony_ci
67b8021494Sopenharmony_ci# Log the report summary line.
68b8021494Sopenharmony_cieval $REPORT | tail -n1 | tr -s " " | cut -d" " -f4,7,10 | xargs -i printf "%s %s\n" `date +%s` {} >>tools/code_coverage.log
69b8021494Sopenharmony_ci
70b8021494Sopenharmony_ci# Clean up.
71b8021494Sopenharmony_cirm -f $LLVM_PROFILE_FILE
72b8021494Sopenharmony_cirm -f $PROFDATA
73