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