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 32readonly OT_MERGED_PROFILES=merged_profiles 33readonly OT_GCOV_PREFIX_BASE=ot-run 34 35merge_profiles() 36{ 37 local profile_current="$1" 38 local profile_merged="$2" 39 40 [[ -d ${profile_current} ]] || return 0 41 42 local profile_temporary="${profile_merged}.tmp" 43 44 gcov-tool merge "${profile_current}" "${profile_merged}" -o "${profile_temporary}" || return 0 45 cp -r "${profile_temporary}"/* "${profile_merged}" 46 rm -rf "${profile_temporary}" 47} 48 49do_clean() 50{ 51 rm -rfv "${OT_MERGED_PROFILES}" "${OT_GCOV_PREFIX_BASE}" || sudo rm -rfv "${OT_MERGED_PROFILES}" "${OT_GCOV_PREFIX_BASE}" 52} 53 54do_collect() 55{ 56 [[ -d ${OT_GCOV_PREFIX_BASE} ]] || return 0 57 sudo chown -R "$USER" "${OT_GCOV_PREFIX_BASE}" 58 59 while read -r node_gcda; do 60 while read -r build_name; do 61 local profile_current="${node_gcda}/$PWD/build/${build_name}" 62 local profile_merged="${OT_MERGED_PROFILES}/build/${build_name}" 63 if [[ -d ${profile_merged} ]]; then 64 merge_profiles "${profile_current}" "${profile_merged}" 65 rm -rf "${profile_current}" 66 else 67 mkdir -p "$(dirname "${profile_merged}")" 68 mv "${profile_current}" "${profile_merged}" 69 fi 70 done < <(ls "${node_gcda}/$PWD/build") 71 done < <(find "${OT_GCOV_PREFIX_BASE}" -type d -name 'ot-gcda.*') 72} 73 74do_install() 75{ 76 [[ -d ${OT_MERGED_PROFILES} ]] || return 0 77 78 while read -r build_name; do 79 local profile_current="build/${build_name}" 80 local profile_merged="${OT_MERGED_PROFILES}/build/${build_name}" 81 merge_profiles "${profile_current}" "${profile_merged}" 82 done < <(ls "build") 83 84 sudo chown -R "$USER" build 85 cp -r "${OT_MERGED_PROFILES}"/build/* build 86 rm -rf "${OT_MERGED_PROFILES}" "${OT_GCOV_PREFIX_BASE}" 87} 88 89main() 90{ 91 case $1 in 92 collect) 93 do_collect 94 ;; 95 install) 96 do_install 97 ;; 98 clean) 99 do_clean 100 ;; 101 *) 102 false 103 ;; 104 esac 105} 106 107main "$@" 108