Skip to content

Commit

Permalink
[coverage] Add use_coverage flag to add --coverage to clang builds. (#…
Browse files Browse the repository at this point in the history
…17935)

* [coverage] Add use_coverage flag to add --coverage to clang builds.

* [coverage] Add -o option to run lcov in a prebuilt output directory.

* [coverage] Fixes for review comments.

* [coverage] Rephrase use_coverage comment.
  • Loading branch information
turon authored and pull[bot] committed Jan 19, 2024
1 parent 34456f2 commit b478bf8
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
12 changes: 12 additions & 0 deletions build/config/compiler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,18 @@ config("fuzzing_default") {
}
}

config("coverage") {
cflags = [ "--coverage" ]
ldflags = cflags
}

config("coverage_default") {
configs = []
if (use_coverage) {
configs += [ ":coverage" ]
}
}

declare_args() {
# Enable Runtime Type Information (RTTI)
enable_rtti = false
Expand Down
3 changes: 3 additions & 0 deletions build/config/compiler/compiler.gni
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@ declare_args() {

# enable libfuzzer
is_libfuzzer = false

# Generate code coverage analysis artifacts when enabled.
use_coverage = false
}
5 changes: 5 additions & 0 deletions build/config/defaults.gni
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ declare_args() {
# Defaults fuzzing configs.
default_configs_fuzzing = [ "${build_root}/config/compiler:fuzzing_default" ]

# Defaults coverage configs.
default_configs_coverage =
[ "${build_root}/config/compiler:coverage_default" ]

# Defaults target-specific configs.
default_configs_target = [ "${build_root}/config/compiler:target_default" ]

Expand Down Expand Up @@ -108,6 +112,7 @@ default_configs += default_configs_runtime
default_configs += default_configs_aliasing
default_configs += default_configs_sanitize
default_configs += default_configs_fuzzing
default_configs += default_configs_coverage
default_configs += default_configs_target
default_configs += default_configs_dead_code
default_configs += default_configs_extra
Expand Down
79 changes: 79 additions & 0 deletions scripts/build_coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env bash

#
# Copyright (c) 2022 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

set -e

_normpath() {
python -c "import os.path; print(os.path.normpath('$@'))"
}

CHIP_ROOT=$(_normpath "$(dirname "$0")/..")
OUTPUT_ROOT="$CHIP_ROOT/out/coverage"
COVERAGE_ROOT="$OUTPUT_ROOT/coverage"
skip_gn=false

help() {

echo "Usage: $file_name [ options ... ]"

echo "General Options:
-h, --help Display this information.
Input Options:
-o, --output_root Set the build output directory. When set manually, performs only lcov stage
on provided build output. Assumes output_root has been built with 'use_coverage=true'
and that 'ninja check' was run.
"
}

file_name=${0##*/}

while (($#)); do
case $1 in
--help | -h)
help
exit 1
;;
--output_root | -o)
OUTPUT_ROOT=$2
COVERAGE_ROOT="$OUTPUT_ROOT/coverage"
skip_gn=true
shift
;;
-*)
help
echo "Unknown Option \"$1\""
exit 1
;;
esac
shift
done

# Ensure we have a compilation environment
source "$CHIP_ROOT/scripts/activate.sh"

# Generates ninja files
if [ "$skip_gn" == false ]; then
gn --root="$CHIP_ROOT" gen "$OUTPUT_ROOT" --args='use_coverage=true'
ninja -C "$OUTPUT_ROOT" check
fi

mkdir -p "$COVERAGE_ROOT"
lcov --initial --capture --directory "$OUTPUT_ROOT/obj" --output-file "$COVERAGE_ROOT/lcov_base.info"
lcov --capture --directory "$OUTPUT_ROOT"/obj --output-file "$COVERAGE_ROOT/lcov_test.info"
lcov --add-tracefile "$COVERAGE_ROOT/lcov_base.info" --add-tracefile "$COVERAGE_ROOT/lcov_test.info" --output-file "$COVERAGE_ROOT/lcov_final.info"
genhtml "$COVERAGE_ROOT/lcov_final.info" --output-directory "$COVERAGE_ROOT/html"

0 comments on commit b478bf8

Please sign in to comment.