collect_coverage.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (C) 2019 Intel Corporation. All rights reserved.
  4. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5. #
  6. readonly WORK_DIR=$PWD
  7. readonly WAMR_DIR=${WORK_DIR}/../../..
  8. readonly DST_COV_FILE=$1
  9. readonly SRC_COV_DIR=$2
  10. readonly SRC_TEMP_COV_FILE=wamr_temp.lcov
  11. readonly SRC_COV_FILE=wamr.lcov
  12. # get dest folder
  13. dir=$(dirname ${DST_COV_FILE})
  14. pushd ${dir} > /dev/null 2>&1
  15. readonly DST_COV_DIR=${PWD}
  16. popd > /dev/null 2>&1
  17. if [[ ! -d ${SRC_COV_DIR} ]]; then
  18. echo "${SRC_COV_DIR} doesn't exist, ignore code coverage collection"
  19. exit
  20. fi
  21. echo "Start to collect code coverage of ${SRC_COV_DIR} .."
  22. pushd ${SRC_COV_DIR} > /dev/null 2>&1
  23. # collect all code coverage data
  24. lcov -q -o ${SRC_TEMP_COV_FILE} -c -d . --rc lcov_branch_coverage=1
  25. # extract code coverage data of WAMR source files
  26. lcov -q -r ${SRC_TEMP_COV_FILE} -o ${SRC_TEMP_COV_FILE} \
  27. -rc lcov_branch_coverage=1 \
  28. "*/usr/*" "*/_deps/*" "*/deps/*" "*/tests/unit/*" \
  29. "*/llvm/include/*" "*/include/llvm/*" "*/samples/*" \
  30. "*/test-tools/*" "*/tests/standalone/*" "*/tests/*"
  31. if [[ -s ${SRC_TEMP_COV_FILE} ]]; then
  32. if [[ -s ${DST_COV_FILE} ]]; then
  33. # merge code coverage data
  34. lcov --rc lcov_branch_coverage=1 \
  35. --add-tracefile ${SRC_TEMP_COV_FILE} \
  36. -a ${DST_COV_FILE} -o ${SRC_COV_FILE}
  37. # backup the original lcov file
  38. cp -a ${DST_COV_FILE} "${DST_COV_FILE}.orig"
  39. # replace the lcov file
  40. cp -a ${SRC_COV_FILE} ${DST_COV_FILE}
  41. echo "Code coverage file ${DST_COV_FILE} was appended"
  42. else
  43. cp -a ${SRC_TEMP_COV_FILE} ${SRC_COV_FILE}
  44. cp -a ${SRC_COV_FILE} ${DST_COV_FILE}
  45. echo "Code coverage file ${DST_COV_FILE} was generated"
  46. fi
  47. # get ignored prefix path
  48. dir=$(dirname ${WAMR_DIR}/../..)
  49. pushd ${dir} > /dev/null 2>&1
  50. prefix_full_path=${PWD}
  51. popd > /dev/null 2>&1
  52. # generate html output for merged code coverage data
  53. rm -fr ${DST_COV_DIR}/wamr-lcov
  54. genhtml -q -t "WAMR Code Coverage" \
  55. --rc lcov_branch_coverage=1 --prefix=${prefix_full_path} \
  56. -o ${DST_COV_DIR}/wamr-lcov \
  57. ${DST_COV_FILE}
  58. cd ${DST_COV_DIR}
  59. rm -f wamr-lcov.zip
  60. zip -r -q -o wamr-lcov.zip wamr-lcov
  61. rm -fr wamr-lcov
  62. echo "Code coverage html ${DST_COV_DIR}/wamr-lcov.zip was generated"
  63. else
  64. echo "generate code coverage html failed"
  65. fi
  66. echo ""
  67. popd > /dev/null 2>&1