build_coverage.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. _normpath() {
  19. python -c "import os.path; print(os.path.normpath('$@'))"
  20. }
  21. CHIP_ROOT=$(_normpath "$(dirname "$0")/..")
  22. OUTPUT_ROOT="$CHIP_ROOT/out/coverage"
  23. COVERAGE_ROOT="$OUTPUT_ROOT/coverage"
  24. skip_gn=false
  25. help() {
  26. echo "Usage: $file_name [ options ... ]"
  27. echo "General Options:
  28. -h, --help Display this information.
  29. Input Options:
  30. -o, --output_root Set the build output directory. When set manually, performs only lcov stage
  31. on provided build output. Assumes output_root has been built with 'use_coverage=true'
  32. and that 'ninja check' was run.
  33. "
  34. }
  35. file_name=${0##*/}
  36. while (($#)); do
  37. case $1 in
  38. --help | -h)
  39. help
  40. exit 1
  41. ;;
  42. --output_root | -o)
  43. OUTPUT_ROOT=$2
  44. COVERAGE_ROOT="$OUTPUT_ROOT/coverage"
  45. skip_gn=true
  46. shift
  47. ;;
  48. -*)
  49. help
  50. echo "Unknown Option \"$1\""
  51. exit 1
  52. ;;
  53. esac
  54. shift
  55. done
  56. # Ensure we have a compilation environment
  57. source "$CHIP_ROOT/scripts/activate.sh"
  58. # Generates ninja files
  59. if [ "$skip_gn" == false ]; then
  60. gn --root="$CHIP_ROOT" gen "$OUTPUT_ROOT" --args='use_coverage=true'
  61. ninja -C "$OUTPUT_ROOT" check
  62. fi
  63. mkdir -p "$COVERAGE_ROOT"
  64. lcov --initial --capture --directory "$OUTPUT_ROOT/obj" --output-file "$COVERAGE_ROOT/lcov_base.info"
  65. lcov --capture --directory "$OUTPUT_ROOT"/obj --output-file "$COVERAGE_ROOT/lcov_test.info"
  66. lcov --add-tracefile "$COVERAGE_ROOT/lcov_base.info" --add-tracefile "$COVERAGE_ROOT/lcov_test.info" --output-file "$COVERAGE_ROOT/lcov_final.info"
  67. genhtml "$COVERAGE_ROOT/lcov_final.info" --output-directory "$COVERAGE_ROOT/html"