From 5534dd2fcb539006163a875fe5258a6e7120f52d Mon Sep 17 00:00:00 2001 From: Ulf Adams Date: Tue, 17 Apr 2018 10:36:41 +0200 Subject: [PATCH] Prototype support for llvm coverage https://clang.llvm.org/docs/SourceBasedCodeCoverage.html Usage: BAZEL_USE_LLVM_NATIVE_COVERAGE=1 GCOV=llvm-profdata-3.9 CC=clang-3.9 bazel coverage --instrumentation_filter=src/main/cpp src/test/cpp:all --- tools/cpp/cc_configure.bzl | 3 ++- tools/cpp/unix_cc_configure.bzl | 11 ++++++++--- tools/test/collect_coverage.sh | 17 ++++++++++++----- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/tools/cpp/cc_configure.bzl b/tools/cpp/cc_configure.bzl index cf121260f47563..88c16cca1d5922 100644 --- a/tools/cpp/cc_configure.bzl +++ b/tools/cpp/cc_configure.bzl @@ -53,6 +53,7 @@ cc_autoconf = repository_rule( "BAZEL_TARGET_LIBC", "BAZEL_TARGET_SYSTEM", "BAZEL_USE_CPP_ONLY_TOOLCHAIN", + "BAZEL_USE_LLVM_NATIVE_COVERAGE", "BAZEL_VC", "BAZEL_VS", "CC", @@ -64,9 +65,9 @@ cc_autoconf = repository_rule( "GCOV", "HOMEBREW_RUBY_PATH", "NO_WHOLE_ARCHIVE_OPTION", + "SYSTEMROOT", "USE_DYNAMIC_CRT", "USE_MSVC_WRAPPER", - "SYSTEMROOT", "VS90COMNTOOLS", "VS100COMNTOOLS", "VS110COMNTOOLS", diff --git a/tools/cpp/unix_cc_configure.bzl b/tools/cpp/unix_cc_configure.bzl index a74edf80b0fc27..bcfe2569775782 100644 --- a/tools/cpp/unix_cc_configure.bzl +++ b/tools/cpp/unix_cc_configure.bzl @@ -338,8 +338,13 @@ def get_env(repository_ctx): return "" -def _coverage_feature(darwin): - if darwin: +def _coverage_feature(repository_ctx, darwin): + use_llvm_cov = "1" == get_env_var( + repository_ctx, + "BAZEL_USE_LLVM_NATIVE_COVERAGE", + default="0", + enable_warning=False) + if darwin or use_llvm_cov: compile_flags = """flag_group { flag: '-fprofile-instr-generate' flag: '-fcoverage-mapping' @@ -450,7 +455,7 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools): "%{opt_content}": _build_crosstool(opt_content, " "), "%{dbg_content}": _build_crosstool(dbg_content, " "), "%{cxx_builtin_include_directory}": "", - "%{coverage}": _coverage_feature(darwin), + "%{coverage}": _coverage_feature(repository_ctx, darwin), "%{msvc_env_tmp}": "", "%{msvc_env_path}": "", "%{msvc_env_include}": "", diff --git a/tools/test/collect_coverage.sh b/tools/test/collect_coverage.sh index b9579cac993739..20410a6ccc6478 100755 --- a/tools/test/collect_coverage.sh +++ b/tools/test/collect_coverage.sh @@ -88,6 +88,7 @@ fi if [[ "$COVERAGE_LEGACY_MODE" ]]; then export GCOV_PREFIX_STRIP=3 export GCOV_PREFIX="${COVERAGE_DIR}" + export LLVM_PROFILE_FILE="${COVERAGE_DIR}/%h-%p-%m.profraw" fi cd "$TEST_SRCDIR/$TEST_WORKSPACE" @@ -107,11 +108,17 @@ fi cd $ROOT -# If LCOV_MERGER is not set, use the legacy, awful, C++-only method to convert -# coverage files. -# NB: This is here just so that we don't regress. Do not add support for new -# coverage features here. Implement it instead properly in LcovMerger. -if [[ "$COVERAGE_LEGACY_MODE" ]]; then +USES_LLVM_COV= +if stat --printf='' "${COVERAGE_DIR}"/*.profraw 2>/dev/null; then + USES_LLVM_COV=1 +fi + +if [[ "$USES_LLVM_COV" ]]; then + "${GCOV}" merge -output "${COVERAGE_OUTPUT_FILE}" "${COVERAGE_DIR}"/*.profraw + exit $TEST_STATUS + +# If LCOV_MERGER is not set, use the legacy C++-only method to convert coverage files. +elif [[ "$COVERAGE_LEGACY_MODE" ]]; then cat "${COVERAGE_MANIFEST}" | grep ".gcno$" | while read path; do mkdir -p "${COVERAGE_DIR}/$(dirname ${path})" cp "${ROOT}/${path}" "${COVERAGE_DIR}/${path}"