Skip to content

Commit

Permalink
Prototype support for llvm coverage
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ulfjack committed Apr 25, 2018
1 parent 5b79098 commit 5534dd2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
3 changes: 2 additions & 1 deletion tools/cpp/cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
11 changes: 8 additions & 3 deletions tools/cpp/unix_cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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}": "",
Expand Down
17 changes: 12 additions & 5 deletions tools/test/collect_coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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}"
Expand Down

0 comments on commit 5534dd2

Please sign in to comment.