Add option and target for generating code cov info

The new `tint-generate-coverage` CMake target can be used with the clang toolchain to generate a `lcov.info` file at the root of the project, along with a `coverage.summary` human readable plain text file.

The `lcov.info` file can then be used by various tools to display code coverage information in your IDE / code editor.

Useful for ensuring decent test coverage.

Change-Id: I3d846f6da3af25d3d600d8e028f27b89e35b545f
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/31121
Commit-Queue: Ben Clayton <bclayton@google.com>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-10-27 21:04:59 +00:00 committed by Commit Bot service account
parent bfd81096a5
commit adb10d60de
3 changed files with 96 additions and 0 deletions

2
.gitignore vendored
View File

@ -6,6 +6,8 @@
.vscode
build
buildtools
coverage.summary
lcov.info
out
testing
third_party/cpplint

View File

@ -39,6 +39,8 @@ option(TINT_ENABLE_MSAN "Enable memory sanitizer" OFF)
option(TINT_ENABLE_ASAN "Enable address sanitizer" OFF)
option(TINT_ENABLE_UBSAN "Enable undefined behaviour sanitizer" OFF)
option(TINT_EMIT_COVERAGE "Emit code coverage information" OFF)
option(TINT_CHECK_CHROMIUM_STYLE "Check for [chromium-style] issues during build" OFF)
if(WIN32)
@ -173,6 +175,18 @@ function(tint_default_compile_options TARGET)
endif()
endif()
if (${TINT_EMIT_COVERAGE})
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_compile_options(${TARGET} PRIVATE "--coverage")
target_link_options(${TARGET} PRIVATE "gcov")
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(${TARGET} PRIVATE "-fprofile-instr-generate" "-fcoverage-mapping")
target_link_options(${TARGET} PRIVATE "-fprofile-instr-generate" "-fcoverage-mapping")
else()
message(FATAL_ERROR "Coverage generation not supported for the ${CMAKE_CXX_COMPILER_ID} toolchain")
endif()
endif()
if (MSVC)
# Specify /EHs for exception handling.
target_compile_options(${TARGET} PRIVATE
@ -228,3 +242,18 @@ add_custom_target(tint-format
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMENT "Running formatter"
VERBATIM)
if (${TINT_EMIT_COVERAGE} AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# Generates a lcov.info file at the project root.
# This can be used by tools such as VSCode's Coverage Gutters extension to
# visualize code coverage in the editor.
get_filename_component(CLANG_BIN_DIR ${CMAKE_C_COMPILER} DIRECTORY)
set(PATH_WITH_CLANG "${CLANG_BIN_DIR}:$ENV{PATH}")
add_custom_target(tint-generate-coverage
COMMAND ${CMAKE_COMMAND} -E env PATH=${PATH_WITH_CLANG} ./tools/tint-generate-coverage $<TARGET_FILE:tint_unittests>
DEPENDS tint_unittests
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMENT "Generating tint coverage data"
VERBATIM)
endif()

65
tools/tint-generate-coverage Executable file
View File

@ -0,0 +1,65 @@
#!/usr/bin/env bash
# Copyright 2020 The Tint 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.
# See https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
set -e # Fail on any error.
if [ ! -x "$(which llvm-profdata)" ] ; then
echo "error: llvm-profdata needs to be on \$PATH to use $0"
exit 1
fi
if [ ! -x "$(which llvm-cov)" ] ; then
echo "error: llvm-cov needs to be on \$PATH to use $0"
exit 1
fi
TARGET_EXE=$1
if [ ! -x "$TARGET_EXE" ] ; then
echo "Usage: $0 <executable-path> [optional-args]"
echo ""
echo "Generates a lcov.info file at the project root, which can be used by"
echo "tools such as VSCode's Coverage Gutters extension to visualize code"
echo "coverage in the editor".
exit 1
fi
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd )"
ROOT_DIR="$( cd "${SCRIPT_DIR}/.." >/dev/null 2>&1 && pwd )"
PROFRAW_FILE="${ROOT_DIR}/tint.profraw"
PROFDATA_FILE="${ROOT_DIR}/tint.profdata"
LCOV_FILE="${ROOT_DIR}/lcov.info"
SUMMARY_FILE="${ROOT_DIR}/coverage.summary"
# Run the executable to generate the raw coverage data
# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#running-the-instrumented-program
LLVM_PROFILE_FILE="${PROFRAW_FILE}" $@
# Index the coverage data
# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#creating-coverage-reports
llvm-profdata merge -sparse "${PROFRAW_FILE}" -o "${PROFDATA_FILE}"
# Export as lcov
# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#exporting-coverage-data
llvm-cov export --instr-profile="${PROFDATA_FILE}" --format=lcov --object=${TARGET_EXE} > "${LCOV_FILE}"
# Generate summary report
llvm-cov report --ignore-filename-regex="(.*_test\.cc|third_party/.*)" --instr-profile="${PROFDATA_FILE}" --object=${TARGET_EXE} > "${SUMMARY_FILE}"
# Clean up
rm ${PROFRAW_FILE} ${PROFDATA_FILE}