Skip to content

Commit

Permalink
Added support for split_coverage_post_processing
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Jul 27, 2023
1 parent 05a3c44 commit d222758
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 51 deletions.
16 changes: 16 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ coverage_validation_post_shell_commands: &coverage_validation_post_shell_command
; 1>&2 cat bazel-out/_coverage/_coverage_report.dat \
; exit 1 \
; }
split_coverage_postprocessing_shell_commands: &split_coverage_postprocessing_shell_commands
- echo "coverage --experimental_fetch_all_coverage_outputs" >> user.bazelrc
- echo "coverage --experimental_split_coverage_postprocessing" >> user.bazelrc
- echo "build --//rust/settings:experimental_use_coverage_metadata_files" >> user.bazelrc
tasks:
ubuntu2004:
build_targets: *default_linux_targets
Expand Down Expand Up @@ -69,6 +73,18 @@ tasks:
windows:
build_targets: *default_windows_targets
test_targets: *default_windows_targets
ubuntu2004_split_coverage_postprocessing:
name: Split Coverage Postprocessing
platform: ubuntu2004
shell_commands: *split_coverage_postprocessing_shell_commands
coverage_targets: *default_linux_targets
post_shell_commands: *coverage_validation_post_shell_commands
macos_split_coverage_postprocessing:
name: Split Coverage Postprocessing
platform: macos
shell_commands: *split_coverage_postprocessing_shell_commands
coverage_targets: *default_macos_targets
post_shell_commands: *coverage_validation_post_shell_commands
ubuntu2004_opt:
name: Opt Mode
platform: ubuntu2004
Expand Down
18 changes: 11 additions & 7 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,17 @@ def _rust_test_impl(ctx):
if not toolchain.llvm_profdata:
fail("toolchain.llvm_profdata is required if toolchain.llvm_cov is set.")

llvm_cov_path = toolchain.llvm_cov.short_path
if llvm_cov_path.startswith("../"):
llvm_cov_path = llvm_cov_path[len("../"):]
if toolchain._experimental_use_coverage_metadata_files:
llvm_cov_path = toolchain.llvm_cov.path
llvm_profdata_path = toolchain.llvm_profdata.path
else:
llvm_cov_path = toolchain.llvm_cov.path
if llvm_cov_path.startswith("../"):
llvm_cov_path = llvm_cov_path[len("../"):]

llvm_profdata_path = toolchain.llvm_profdata.short_path
if llvm_profdata_path.startswith("../"):
llvm_profdata_path = llvm_profdata_path[len("../"):]
llvm_profdata_path = toolchain.llvm_profdata.short_path
if llvm_profdata_path.startswith("../"):
llvm_profdata_path = llvm_profdata_path[len("../"):]

env["RUST_LLVM_COV"] = llvm_cov_path
env["RUST_LLVM_PROFDATA"] = llvm_profdata_path
Expand Down Expand Up @@ -742,7 +746,7 @@ _common_attrs = {

_coverage_attrs = {
"_collect_cc_coverage": attr.label(
default = Label("//util:collect_coverage"),
default = Label("//util/collect_coverage"),
executable = True,
cfg = "exec",
),
Expand Down
24 changes: 19 additions & 5 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,7 @@ def construct_arguments(
rustc_flags.add("proc_macro")

if toolchain.llvm_cov and ctx.configuration.coverage_enabled:
# https://doc.rust-lang.org/rustc/instrument-coverage.html
rustc_flags.add("--codegen=instrument-coverage")

# Make bin crate data deps available to tests.
Expand Down Expand Up @@ -1364,8 +1365,10 @@ def rustc_compile_action(
if toolchain.llvm_cov and ctx.configuration.coverage_enabled and crate_info.is_test:
coverage_runfiles = [toolchain.llvm_cov, toolchain.llvm_profdata]

experimental_use_coverage_metadata_files = toolchain._experimental_use_coverage_metadata_files

runfiles = ctx.runfiles(
files = getattr(ctx.files, "data", []) + coverage_runfiles,
files = getattr(ctx.files, "data", []) + ([] if experimental_use_coverage_metadata_files else coverage_runfiles),
collect_data = True,
)
if getattr(ctx.attr, "crate", None):
Expand All @@ -1376,18 +1379,29 @@ def rustc_compile_action(
# https://github.com/bazelbuild/rules_rust/issues/771
out_binary = getattr(attr, "out_binary", False)

executable = crate_info.output if crate_info.type == "bin" or crate_info.is_test or out_binary else None

instrumented_files_kwargs = {
"dependency_attributes": ["deps", "crate"],
"extensions": ["rs"],
"source_attributes": ["srcs"],
}

if experimental_use_coverage_metadata_files:
instrumented_files_kwargs.update({
"metadata_files": coverage_runfiles + [executable] if executable else [],
})

providers = [
DefaultInfo(
# nb. This field is required for cc_library to depend on our output.
files = depset(outputs),
runfiles = runfiles,
executable = crate_info.output if crate_info.type == "bin" or crate_info.is_test or out_binary else None,
executable = executable,
),
coverage_common.instrumented_files_info(
ctx,
dependency_attributes = ["deps", "crate"],
extensions = ["rs"],
source_attributes = ["srcs"],
**instrumented_files_kwargs
),
]

Expand Down
6 changes: 3 additions & 3 deletions rust/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ def rules_rust_dependencies():
maybe(
http_archive,
name = "bazel_skylib",
sha256 = "66ffd9315665bfaafc96b52278f57c7e2dd09f5ede279ea6d39b2be471e7e3aa",
urls = [
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.2.0/bazel-skylib-1.2.0.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.2.0/bazel-skylib-1.2.0.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.4.2/bazel-skylib-1.4.2.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.4.2/bazel-skylib-1.4.2.tar.gz",
],
sha256 = "af87959afe497dc8dfd4c6cb66e1279cb98ccc84284619ebfec27d9c09a903de",
)

# Make the iOS simulator constraint available, which is referenced in abi_to_constraints()
Expand Down
7 changes: 7 additions & 0 deletions rust/settings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ bool_flag(
build_setting_default = False,
)

# A flag to have coverage tooling added as `coverage_common.instrumented_files_info.metadata_files` instead of
# reporting tools like `llvm-cov` and `llvm-profdata` as runfiles to each test.
bool_flag(
name = "experimental_use_coverage_metadata_files",
build_setting_default = False,
)

bzl_library(
name = "bzl_lib",
srcs = glob(["**/*.bzl"]),
Expand Down
4 changes: 4 additions & 0 deletions rust/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ def _rust_toolchain_impl(ctx):
_pipelined_compilation = pipelined_compilation,
_experimental_use_cc_common_link = experimental_use_cc_common_link,
_experimental_use_global_allocator = experimental_use_global_allocator,
_experimental_use_coverage_metadata_files = ctx.attr._experimental_use_coverage_metadata_files[BuildSettingInfo].value,
_no_std = no_std,
)
return [
Expand Down Expand Up @@ -784,6 +785,9 @@ rust_toolchain = rule(
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
),
"_experimental_use_coverage_metadata_files": attr.label(
default = Label("//rust/settings:experimental_use_coverage_metadata_files"),
),
"_experimental_use_global_allocator": attr.label(
default = Label("//rust/settings:experimental_use_global_allocator"),
doc = (
Expand Down
4 changes: 2 additions & 2 deletions util/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ sh_binary(
tags = ["manual"],
)

filegroup(
alias(
name = "collect_coverage",
srcs = ["collect_coverage.sh"],
actual = "//util/collect_coverage",
visibility = ["//visibility:public"],
)
34 changes: 0 additions & 34 deletions util/collect_coverage.sh

This file was deleted.

8 changes: 8 additions & 0 deletions util/collect_coverage/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("//rust:defs.bzl", "rust_binary")

rust_binary(
name = "collect_coverage",
srcs = ["collect_coverage.rs"],
edition = "2018",
visibility = ["//visibility:public"],
)
Loading

0 comments on commit d222758

Please sign in to comment.