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