From adb10d60de324986020310dfbb730b3f3b50f3c0 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Tue, 27 Oct 2020 21:04:59 +0000 Subject: [PATCH] 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 Commit-Queue: dan sinclair Reviewed-by: dan sinclair --- .gitignore | 2 ++ CMakeLists.txt | 29 ++++++++++++++++ tools/tint-generate-coverage | 65 ++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100755 tools/tint-generate-coverage diff --git a/.gitignore b/.gitignore index 5fcdc87b9b..9d6a3b11bb 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,8 @@ .vscode build buildtools +coverage.summary +lcov.info out testing third_party/cpplint diff --git a/CMakeLists.txt b/CMakeLists.txt index 528671b851..1d23434864 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 $ + DEPENDS tint_unittests + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + COMMENT "Generating tint coverage data" + VERBATIM) +endif() diff --git a/tools/tint-generate-coverage b/tools/tint-generate-coverage new file mode 100755 index 0000000000..e1bef24723 --- /dev/null +++ b/tools/tint-generate-coverage @@ -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 [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}