build_coverage.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (c) 2022 Project CHIP Authors
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. set -e
  18. _install_lcov() {
  19. if ! lcov --version >/dev/null 2>&1; then
  20. echo "lcov not installed. Installing..."
  21. case "$(uname)" in
  22. "Darwin")
  23. brew install lcov
  24. ;;
  25. "Linux")
  26. sudo apt-get update
  27. sudo apt-get install -y lcov
  28. ;;
  29. *)
  30. die
  31. ;;
  32. esac
  33. fi
  34. }
  35. _install_lcov
  36. _normpath() {
  37. python3 -c "import os.path; print(os.path.normpath('$@'))"
  38. }
  39. CHIP_ROOT=$(_normpath "$(dirname "$0")/..")
  40. OUTPUT_ROOT="$CHIP_ROOT/out/coverage"
  41. COVERAGE_ROOT="$OUTPUT_ROOT/coverage"
  42. SUPPORTED_CODE=(core clusters all)
  43. SUPPORTED_TESTS=(unit yaml all)
  44. CODE="core"
  45. TESTS="unit"
  46. skip_gn=false
  47. help() {
  48. echo "Usage: $file_name [--output_root=<output_root>] [--code=<core|clusters|all>] [--tests=<unit|yaml|all>]"
  49. echo
  50. echo "Misc:
  51. -h, --help Print this help, then exit."
  52. echo
  53. echo "Options:
  54. -o, --output_root Set the build output directory. When set manually, performs only lcov stage
  55. on provided build output. Assumes output_root has been built with 'use_coverage=true'
  56. and that 'ninja check' was run.
  57. -c, --code Specify which scope to collect coverage data.
  58. 'core': collect coverage data from core stack in Matter SDK. --default
  59. 'clusters': collect coverage data from clusters implementation in Matter SDK.
  60. 'all': collect coverage data from Matter SDK.
  61. -t, --tests Specify which tools to run the coverage check.
  62. 'unit': Run unit test to drive the coverage check. --default
  63. 'yaml': Run yaml test to drive the coverage check.
  64. 'all': Run unit & yaml test to drive the coverage check.
  65. "
  66. }
  67. file_name=${0##*/}
  68. for i in "$@"; do
  69. case $i in
  70. -h | --help)
  71. help
  72. exit 1
  73. ;;
  74. -c=* | --code=*)
  75. CODE="${i#*=}"
  76. shift
  77. ;;
  78. -t=* | --tests=*)
  79. TESTS="${i#*=}"
  80. shift
  81. ;;
  82. -o=* | --output_root=*)
  83. OUTPUT_ROOT="${i#*=}"
  84. COVERAGE_ROOT="$OUTPUT_ROOT/coverage"
  85. skip_gn=true
  86. shift
  87. ;;
  88. *)
  89. echo "Unknown Option \"$1\""
  90. echo
  91. help
  92. exit 1
  93. ;;
  94. esac
  95. done
  96. if [[ ! " ${SUPPORTED_CODE[@]} " =~ " ${CODE} " ]]; then
  97. echo "ERROR: Code $CODE not supported"
  98. exit 1
  99. fi
  100. if [[ ! " ${SUPPORTED_TESTS[@]} " =~ " ${TESTS} " ]]; then
  101. echo "ERROR: Tests $TESTS not supported"
  102. exit 1
  103. fi
  104. if [ "$skip_gn" == false ]; then
  105. # Ensure we have a compilation environment
  106. source "$CHIP_ROOT/scripts/activate.sh"
  107. # Generates ninja files
  108. EXTRA_GN_ARGS=""
  109. if [[ "$TESTS" == "yaml" || "$TESTS" == "all" ]]; then
  110. EXTRA_GN_ARGS="$EXTRA_GN_ARGS chip_build_all_clusters_app=true"
  111. else
  112. EXTRA_GN_ARGS="$EXTRA_GN_ARGS chip_build_tools=false"
  113. fi
  114. gn --root="$CHIP_ROOT" gen "$OUTPUT_ROOT" --args="use_coverage=true$EXTRA_GN_ARGS"
  115. ninja -C "$OUTPUT_ROOT"
  116. # Run unit tests
  117. if [[ "$TESTS" == "unit" || "$TESTS" == "all" ]]; then
  118. ninja -C "$OUTPUT_ROOT" check
  119. fi
  120. # Run yaml tests
  121. if [[ "$TESTS" == "yaml" || "$TESTS" == "all" ]]; then
  122. scripts/run_in_build_env.sh \
  123. "./scripts/tests/run_test_suite.py \
  124. --chip-tool ""$OUTPUT_ROOT/chip-tool \
  125. run \
  126. --iterations 1 \
  127. --test-timeout-seconds 120 \
  128. --all-clusters-app ""$OUTPUT_ROOT/chip-all-clusters-app
  129. "
  130. fi
  131. # Remove misc support components from coverage statistics
  132. rm -rf "$OUTPUT_ROOT/obj/src/app/app-platform"
  133. rm -rf "$OUTPUT_ROOT/obj/src/app/common"
  134. rm -rf "$OUTPUT_ROOT/obj/src/app/util/mock"
  135. rm -rf "$OUTPUT_ROOT/obj/src/controller/python"
  136. rm -rf "$OUTPUT_ROOT/obj/src/lib/dnssd/platform"
  137. rm -rf "$OUTPUT_ROOT/obj/src/lib/shell"
  138. rm -rf "$OUTPUT_ROOT/obj/src/lwip"
  139. rm -rf "$OUTPUT_ROOT/obj/src/platform"
  140. rm -rf "$OUTPUT_ROOT/obj/src/tools"
  141. # Remove unit test itself from coverage statistics
  142. find "$OUTPUT_ROOT/obj/src/" -depth -name 'tests' -exec rm -rf {} \;
  143. if [ "$CODE" == "core" ]; then
  144. rm -rf "$OUTPUT_ROOT/obj/src/app/clusters"
  145. elif [ "$CODE" == "clusters" ]; then
  146. mv "$OUTPUT_ROOT/obj/src/app/clusters" "$OUTPUT_ROOT/obj/clusters"
  147. rm -rf "$OUTPUT_ROOT/obj/src"
  148. mkdir "$OUTPUT_ROOT/obj/src"
  149. mv "$OUTPUT_ROOT/obj/clusters" "$OUTPUT_ROOT/obj/src/clusters"
  150. fi
  151. fi
  152. mkdir -p "$COVERAGE_ROOT"
  153. lcov --initial --capture --directory "$OUTPUT_ROOT/obj/src" --exclude="$PWD"/zzz_generated/* --exclude="$PWD"/third_party/* --exclude=/usr/include/* --output-file "$COVERAGE_ROOT/lcov_base.info"
  154. lcov --capture --directory "$OUTPUT_ROOT/obj/src" --exclude="$PWD"/zzz_generated/* --exclude="$PWD"/third_party/* --exclude=/usr/include/* --output-file "$COVERAGE_ROOT/lcov_test.info"
  155. lcov --add-tracefile "$COVERAGE_ROOT/lcov_base.info" --add-tracefile "$COVERAGE_ROOT/lcov_test.info" --output-file "$COVERAGE_ROOT/lcov_final.info"
  156. genhtml "$COVERAGE_ROOT/lcov_final.info" --output-directory "$COVERAGE_ROOT/html"
  157. # Copy webapp's YAML file to the coverage output directory
  158. cp "$CHIP_ROOT/integrations/appengine/webapp_config.yaml" "$COVERAGE_ROOT/webapp_config.yaml"