Skip to content

Commit eb280fa

Browse files
authored
Update rust_stdlib_filegroup to detect profiler_builtins (bazelbuild#3958)
This change adds a `coverage_enabled` to `rust_toolchain` that should be used to identify if the toolchain supports coverage. This would allow repositories which use MUSL to run `bazel coverage //...` without running into a linker error. The downside is that they will get no coverage but this would already be the case due to the linker error. More details on rust-lang/rust#79556
1 parent 3357b18 commit eb280fa

4 files changed

Lines changed: 7 additions & 3 deletions

File tree

rust/private/providers.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ StdLibInfo = provider(
136136
"between_core_and_std_files": "List[File]: `.a` files related to all modules except `adler`, `alloc`, `compiler_builtins`, `core`, and `std`.",
137137
"core_files": "List[File]: `.a` files related to the `core` and `adler` modules",
138138
"dot_a_files": "Depset[File]: Generated `.a` files",
139+
"has_profiler_builtins": "bool: Whether the sysroot contains the profiler_builtins rlib, required for -Cinstrument-coverage.",
139140
"memchr_files": "Depset[File]: `.a` files associated with the `memchr` module.",
140141
"panic_files": "Depset[File]: `.a` files associated with `panic_unwind` and `panic_abort`.",
141142
"self_contained_files": "List[File]: All `.o` files from the `self-contained` directory.",

rust/private/rust.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ def _rust_test_impl(ctx):
506506
data,
507507
{},
508508
)
509-
if toolchain.llvm_cov and ctx.configuration.coverage_enabled:
509+
if toolchain.coverage_supported and ctx.configuration.coverage_enabled:
510510
if not toolchain.llvm_profdata:
511511
fail("toolchain.llvm_profdata is required if toolchain.llvm_cov is set.")
512512

rust/private/rustc.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ def construct_arguments(
12001200
rustc_flags.add("--extern")
12011201
rustc_flags.add("proc_macro")
12021202

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

@@ -1687,7 +1687,7 @@ def rustc_compile_action(
16871687
outputs = [crate_info.output]
16881688

16891689
coverage_runfiles = []
1690-
if toolchain.llvm_cov and ctx.configuration.coverage_enabled and crate_info.is_test:
1690+
if toolchain.coverage_supported and ctx.configuration.coverage_enabled and crate_info.is_test:
16911691
coverage_runfiles = [toolchain.llvm_cov, toolchain.llvm_profdata] + toolchain.llvm_lib
16921692
collect_cc_coverage = getattr(ctx.executable, "_collect_cc_coverage", None)
16931693
if not collect_cc_coverage:

rust/toolchain.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def _rust_stdlib_filegroup_impl(ctx):
5656
panic_files = []
5757

5858
std_rlibs = [f for f in rust_std if f.basename.endswith(".rlib")]
59+
has_profiler_builtins = any(["profiler_builtins" in f.basename for f in std_rlibs])
5960
if std_rlibs:
6061
# test depends on std
6162
# std depends on everything except test
@@ -116,6 +117,7 @@ def _rust_stdlib_filegroup_impl(ctx):
116117
alloc_files = alloc_files,
117118
self_contained_files = self_contained_files,
118119
panic_files = panic_files,
120+
has_profiler_builtins = has_profiler_builtins,
119121
srcs = ctx.attr.srcs,
120122
),
121123
]
@@ -587,6 +589,7 @@ def _rust_toolchain_impl(ctx):
587589
linker = sysroot.linker,
588590
linker_preference = linker_preference,
589591
linker_type = ctx.attr.linker_type or None,
592+
coverage_supported = bool(ctx.file.llvm_cov) and ctx.attr.rust_std[rust_common.stdlib_info].has_profiler_builtins,
590593
llvm_cov = ctx.file.llvm_cov,
591594
llvm_profdata = ctx.file.llvm_profdata,
592595
llvm_lib = ctx.files.llvm_lib,

0 commit comments

Comments
 (0)