lcov.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/bin/sh
  2. help () {
  3. cat <<EOF
  4. Usage: $0 [-r]
  5. Collect coverage statistics of library code into an HTML report.
  6. General instructions:
  7. 1. Build the library with CFLAGS="--coverage -O0 -g3" and link the test
  8. programs with LDFLAGS="--coverage".
  9. This can be an out-of-tree build.
  10. For example (in-tree):
  11. make CFLAGS="--coverage -O0 -g3" LDFLAGS="--coverage"
  12. Or (out-of-tree):
  13. mkdir build-coverage && cd build-coverage &&
  14. cmake -D CMAKE_BUILD_TYPE=Coverage .. && make
  15. 2. Run whatever tests you want.
  16. 3. Run this script from the parent of the directory containing the library
  17. object files and coverage statistics files.
  18. 4. Browse the coverage report in Coverage/index.html.
  19. 5. After rework, run "$0 -r", then re-test and run "$0" to get a fresh report.
  20. Options
  21. -r Reset traces. Run this before re-testing to get fresh measurements.
  22. EOF
  23. }
  24. # Copyright The Mbed TLS Contributors
  25. # SPDX-License-Identifier: Apache-2.0
  26. #
  27. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  28. # not use this file except in compliance with the License.
  29. # You may obtain a copy of the License at
  30. #
  31. # http://www.apache.org/licenses/LICENSE-2.0
  32. #
  33. # Unless required by applicable law or agreed to in writing, software
  34. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  35. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  36. # See the License for the specific language governing permissions and
  37. # limitations under the License.
  38. set -eu
  39. # Collect stats and build a HTML report.
  40. lcov_library_report () {
  41. rm -rf Coverage
  42. mkdir Coverage Coverage/tmp
  43. lcov --capture --initial --directory library -o Coverage/tmp/files.info
  44. lcov --rc lcov_branch_coverage=1 --capture --directory library -o Coverage/tmp/tests.info
  45. lcov --rc lcov_branch_coverage=1 --add-tracefile Coverage/tmp/files.info --add-tracefile Coverage/tmp/tests.info -o Coverage/tmp/all.info
  46. lcov --rc lcov_branch_coverage=1 --remove Coverage/tmp/all.info -o Coverage/tmp/final.info '*.h'
  47. gendesc tests/Descriptions.txt -o Coverage/tmp/descriptions
  48. genhtml --title "mbed TLS" --description-file Coverage/tmp/descriptions --keep-descriptions --legend --branch-coverage -o Coverage Coverage/tmp/final.info
  49. rm -f Coverage/tmp/*.info Coverage/tmp/descriptions
  50. echo "Coverage report in: Coverage/index.html"
  51. }
  52. # Reset the traces to 0.
  53. lcov_reset_traces () {
  54. # Location with plain make
  55. rm -f library/*.gcda
  56. # Location with CMake
  57. rm -f library/CMakeFiles/*.dir/*.gcda
  58. }
  59. if [ $# -gt 0 ] && [ "$1" = "--help" ]; then
  60. help
  61. exit
  62. fi
  63. main=lcov_library_report
  64. while getopts r OPTLET; do
  65. case $OPTLET in
  66. r) main=lcov_reset_traces;;
  67. *) help 2>&1; exit 120;;
  68. esac
  69. done
  70. shift $((OPTIND - 1))
  71. "$main" "$@"