blob: 9e7b4a0fe8a6d679eec735cb29720bfc9c4b2d61 [file] [log] [blame]
Martin Turon9cc13b52022-05-09 20:40:36 -07001#!/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
19set -e
20
Yufeng Wang185fb492022-09-13 06:28:50 -070021_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 Turon9cc13b52022-05-09 20:40:36 -070041_normpath() {
Yufeng Wang185fb492022-09-13 06:28:50 -070042 python3 -c "import os.path; print(os.path.normpath('$@'))"
Martin Turon9cc13b52022-05-09 20:40:36 -070043}
44
45CHIP_ROOT=$(_normpath "$(dirname "$0")/..")
46OUTPUT_ROOT="$CHIP_ROOT/out/coverage"
47COVERAGE_ROOT="$OUTPUT_ROOT/coverage"
Yufeng Wang92504fc2022-10-03 10:08:51 -070048SUPPORTED_CODE=(core clusters all)
49SUPPORTED_TESTS=(unit yaml all)
50CODE="core"
51TESTS="unit"
Martin Turon9cc13b52022-05-09 20:40:36 -070052skip_gn=false
53
54help() {
55
Yufeng Wang92504fc2022-10-03 10:08:51 -070056 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 Turon9cc13b52022-05-09 20:40:36 -070062 -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 Wang92504fc2022-10-03 10:08:51 -070065 -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 Turon9cc13b52022-05-09 20:40:36 -070073 "
74}
75
76file_name=${0##*/}
77
Yufeng Wang92504fc2022-10-03 10:08:51 -070078for i in "$@"; do
79 case $i in
80 -h | --help)
Martin Turon9cc13b52022-05-09 20:40:36 -070081 help
82 exit 1
83 ;;
Yufeng Wang92504fc2022-10-03 10:08:51 -070084 -c=* | --code=*)
85 CODE="${i#*=}"
86 shift
Yufeng Wangd5d61fc2022-09-28 08:13:36 -070087 ;;
Yufeng Wang92504fc2022-10-03 10:08:51 -070088 -t=* | --tests=*)
89 TESTS="${i#*=}"
90 shift
91 ;;
92 -o=* | --output_root=*)
93 OUTPUT_ROOT="${i#*=}"
Martin Turon9cc13b52022-05-09 20:40:36 -070094 COVERAGE_ROOT="$OUTPUT_ROOT/coverage"
95 skip_gn=true
96 shift
97 ;;
Yufeng Wang92504fc2022-10-03 10:08:51 -070098 *)
Martin Turon9cc13b52022-05-09 20:40:36 -070099 echo "Unknown Option \"$1\""
Yufeng Wang92504fc2022-10-03 10:08:51 -0700100 echo
101 help
Martin Turon9cc13b52022-05-09 20:40:36 -0700102 exit 1
103 ;;
104 esac
Martin Turon9cc13b52022-05-09 20:40:36 -0700105done
106
Yufeng Wang92504fc2022-10-03 10:08:51 -0700107if [[ ! " ${SUPPORTED_CODE[@]} " =~ " ${CODE} " ]]; then
108 echo "ERROR: Code $CODE not supported"
109 exit 1
110fi
Martin Turon9cc13b52022-05-09 20:40:36 -0700111
Yufeng Wang92504fc2022-10-03 10:08:51 -0700112if [[ ! " ${SUPPORTED_TESTS[@]} " =~ " ${TESTS} " ]]; then
113 echo "ERROR: Tests $TESTS not supported"
114 exit 1
115fi
116
Martin Turon9cc13b52022-05-09 20:40:36 -0700117if [ "$skip_gn" == false ]; then
Yufeng Wang92504fc2022-10-03 10:08:51 -0700118 # Ensure we have a compilation environment
119 source "$CHIP_ROOT/scripts/activate.sh"
120
121 # Generates ninja files
dependabot[bot]683d5572023-07-31 18:56:10 +0000122 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 Wangd5d61fc2022-09-28 08:13:36 -0700129 ninja -C "$OUTPUT_ROOT"
Yufeng Wangd5d61fc2022-09-28 08:13:36 -0700130
Yufeng Wang92504fc2022-10-03 10:08:51 -0700131 # Run unit tests
132 if [[ "$TESTS" == "unit" || "$TESTS" == "all" ]]; then
133 ninja -C "$OUTPUT_ROOT" check
134 fi
Martin Turon9cc13b52022-05-09 20:40:36 -0700135
Yufeng Wang92504fc2022-10-03 10:08:51 -0700136 # 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 Wangd5d61fc2022-09-28 08:13:36 -0700147
Yufeng Wang92504fc2022-10-03 10:08:51 -0700148 # 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
170fi
Yufeng Wang8ac8ec72022-09-23 14:34:30 -0700171
Martin Turon9cc13b52022-05-09 20:40:36 -0700172mkdir -p "$COVERAGE_ROOT"
Yufeng Wang8ac8ec72022-09-23 14:34:30 -0700173lcov --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"
174lcov --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 Turon9cc13b52022-05-09 20:40:36 -0700175lcov --add-tracefile "$COVERAGE_ROOT/lcov_base.info" --add-tracefile "$COVERAGE_ROOT/lcov_test.info" --output-file "$COVERAGE_ROOT/lcov_final.info"
176genhtml "$COVERAGE_ROOT/lcov_final.info" --output-directory "$COVERAGE_ROOT/html"
Yufeng Wang7e3d7122023-01-20 10:21:55 -0800177
178# Copy webapp's YAML file to the coverage output directory
179cp "$CHIP_ROOT/integrations/appengine/webapp_config.yaml" "$COVERAGE_ROOT/webapp_config.yaml"