collect_coverage.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. # for lcov 2.x: ignore-errors mismatch,negative
  25. lcov -q -o ${SRC_TEMP_COV_FILE} -c -d . --rc lcov_branch_coverage=1 --rc geninfo_unexecuted_blocks=1
  26. # extract code coverage data of WAMR source files
  27. # for lcov 2.x: ignore-errors unused
  28. lcov -q -r ${SRC_TEMP_COV_FILE} -o ${SRC_TEMP_COV_FILE} \
  29. -rc lcov_branch_coverage=1\
  30. "*/usr/*" "*/_deps/*" "*/deps/*" "*/tests/unit/*" \
  31. "*/llvm/include/*" "*/include/llvm/*" "*/samples/*" \
  32. "*/test-tools/*" "*/tests/standalone/*" "*/tests/*"
  33. if [[ -s ${SRC_TEMP_COV_FILE} ]]; then
  34. if [[ -s ${DST_COV_FILE} ]]; then
  35. # merge code coverage data
  36. lcov --rc lcov_branch_coverage=1 \
  37. --add-tracefile ${SRC_TEMP_COV_FILE} \
  38. -a ${DST_COV_FILE} -o ${SRC_COV_FILE}
  39. # backup the original lcov file
  40. cp -a ${DST_COV_FILE} "${DST_COV_FILE}.orig"
  41. # replace the lcov file
  42. cp -a ${SRC_COV_FILE} ${DST_COV_FILE}
  43. echo "Code coverage file ${DST_COV_FILE} was appended"
  44. else
  45. cp -a ${SRC_TEMP_COV_FILE} ${SRC_COV_FILE}
  46. cp -a ${SRC_COV_FILE} ${DST_COV_FILE}
  47. echo "Code coverage file ${DST_COV_FILE} was generated"
  48. fi
  49. # get ignored prefix path
  50. dir=$(dirname ${WAMR_DIR}/../..)
  51. pushd ${dir} > /dev/null 2>&1
  52. prefix_full_path=${PWD}
  53. popd > /dev/null 2>&1
  54. # generate html output for merged code coverage data
  55. rm -fr ${DST_COV_DIR}/wamr-lcov
  56. genhtml -q -t "WAMR Code Coverage" \
  57. --rc lcov_branch_coverage=1 --prefix=${prefix_full_path} \
  58. -o ${DST_COV_DIR}/wamr-lcov \
  59. ${DST_COV_FILE}
  60. cd ${DST_COV_DIR}
  61. rm -f wamr-lcov.zip
  62. zip -r -q -o wamr-lcov.zip wamr-lcov
  63. rm -fr wamr-lcov
  64. echo "Code coverage html ${DST_COV_DIR}/wamr-lcov.zip was generated"
  65. else
  66. echo "generate code coverage html failed"
  67. fi
  68. echo ""
  69. popd > /dev/null 2>&1