1#!/bin/bash
2#
3#  Copyright (c) 2020, The OpenThread 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#  1. Redistributions of source code must retain the above copyright
9#     notice, this list of conditions and the following disclaimer.
10#  2. Redistributions in binary form must reproduce the above copyright
11#     notice, this list of conditions and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#  3. Neither the name of the copyright holder nor the
14#     names of its contributors may be used to endorse or promote products
15#     derived from this software without specific prior written permission.
16#
17#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27#  POSSIBILITY OF SUCH DAMAGE.
28#
29
30set -euxo pipefail
31
32OT_MERGED_PROFILES=merged_profiles
33readonly OT_MERGED_PROFILES
34
35OT_GCOV_PREFIX_BASE=ot-run
36readonly OT_GCOV_PREFIX_BASE
37
38merge_profiles()
39{
40    local profile_current="$1"
41    local profile_merged="$2"
42
43    [[ -d ${profile_current} ]] || return 0
44
45    local profile_temporary="${profile_merged}.tmp"
46
47    gcov-tool merge "${profile_current}" "${profile_merged}" -o "${profile_temporary}" || return 0
48    cp -r "${profile_temporary}"/* "${profile_merged}"
49    rm -rf "${profile_temporary}"
50}
51
52do_clean()
53{
54    rm -rfv "${OT_MERGED_PROFILES}" "${OT_GCOV_PREFIX_BASE}" || sudo rm -rfv "${OT_MERGED_PROFILES}" "${OT_GCOV_PREFIX_BASE}"
55}
56
57do_collect()
58{
59    [[ -d ${OT_GCOV_PREFIX_BASE} ]] || return 0
60    sudo chown -R "$USER" "${OT_GCOV_PREFIX_BASE}"
61
62    while read -r node_gcda; do
63        while read -r build_name; do
64            local profile_current="${node_gcda}/$PWD/build/${build_name}"
65            local profile_merged="${OT_MERGED_PROFILES}/build/${build_name}"
66            if [[ -d ${profile_merged} ]]; then
67                merge_profiles "${profile_current}" "${profile_merged}"
68                rm -rf "${profile_current}"
69            else
70                mkdir -p "$(dirname "${profile_merged}")"
71                mv "${profile_current}" "${profile_merged}"
72            fi
73        done < <(ls "${node_gcda}/$PWD/build")
74    done < <(find "${OT_GCOV_PREFIX_BASE}" -type d -name 'ot-gcda.*')
75}
76
77do_install()
78{
79    [[ -d ${OT_MERGED_PROFILES} ]] || return 0
80
81    while read -r build_name; do
82        local profile_current="build/${build_name}"
83        local profile_merged="${OT_MERGED_PROFILES}/build/${build_name}"
84        merge_profiles "${profile_current}" "${profile_merged}"
85    done < <(ls "build")
86
87    sudo chown -R "$USER" build
88    cp -r "${OT_MERGED_PROFILES}"/build/* build
89    rm -rf "${OT_MERGED_PROFILES}" "${OT_GCOV_PREFIX_BASE}"
90}
91
92main()
93{
94    case $1 in
95        collect)
96            do_collect
97            ;;
98        install)
99            do_install
100            ;;
101        clean)
102            do_clean
103            ;;
104        *)
105            false
106            ;;
107    esac
108}
109
110main "$@"
111