diff --git a/rust/private/providers.bzl b/rust/private/providers.bzl index f92a5fc8e7..4e180f317c 100644 --- a/rust/private/providers.bzl +++ b/rust/private/providers.bzl @@ -136,6 +136,7 @@ StdLibInfo = provider( "between_core_and_std_files": "List[File]: `.a` files related to all modules except `adler`, `alloc`, `compiler_builtins`, `core`, and `std`.", "core_files": "List[File]: `.a` files related to the `core` and `adler` modules", "dot_a_files": "Depset[File]: Generated `.a` files", + "has_profiler_builtins": "bool: Whether the sysroot contains the profiler_builtins rlib, required for -Cinstrument-coverage.", "memchr_files": "Depset[File]: `.a` files associated with the `memchr` module.", "panic_files": "Depset[File]: `.a` files associated with `panic_unwind` and `panic_abort`.", "self_contained_files": "List[File]: All `.o` files from the `self-contained` directory.", diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index 39e737adc6..cc87477b19 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -506,7 +506,7 @@ def _rust_test_impl(ctx): data, {}, ) - if toolchain.llvm_cov and ctx.configuration.coverage_enabled: + if toolchain.coverage_supported and ctx.configuration.coverage_enabled: if not toolchain.llvm_profdata: fail("toolchain.llvm_profdata is required if toolchain.llvm_cov is set.") diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index e99de85c6b..3f8f2c3892 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -1164,7 +1164,7 @@ def construct_arguments( rustc_flags.add("--extern") rustc_flags.add("proc_macro") - if toolchain.llvm_cov and ctx.configuration.coverage_enabled: + if toolchain.coverage_supported and ctx.configuration.coverage_enabled: # https://doc.rust-lang.org/rustc/instrument-coverage.html rustc_flags.add("--codegen=instrument-coverage") @@ -1651,7 +1651,7 @@ def rustc_compile_action( outputs = [crate_info.output] coverage_runfiles = [] - if toolchain.llvm_cov and ctx.configuration.coverage_enabled and crate_info.is_test: + if toolchain.coverage_supported and ctx.configuration.coverage_enabled and crate_info.is_test: coverage_runfiles = [toolchain.llvm_cov, toolchain.llvm_profdata] + toolchain.llvm_lib collect_cc_coverage = getattr(ctx.executable, "_collect_cc_coverage", None) if not collect_cc_coverage: diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl index 74883231b8..b58c9e8b1e 100644 --- a/rust/toolchain.bzl +++ b/rust/toolchain.bzl @@ -56,6 +56,7 @@ def _rust_stdlib_filegroup_impl(ctx): panic_files = [] std_rlibs = [f for f in rust_std if f.basename.endswith(".rlib")] + has_profiler_builtins = any(["profiler_builtins" in f.basename for f in std_rlibs]) if std_rlibs: # test depends on std # std depends on everything except test @@ -116,6 +117,7 @@ def _rust_stdlib_filegroup_impl(ctx): alloc_files = alloc_files, self_contained_files = self_contained_files, panic_files = panic_files, + has_profiler_builtins = has_profiler_builtins, srcs = ctx.attr.srcs, ), ] @@ -585,6 +587,7 @@ def _rust_toolchain_impl(ctx): linker = sysroot.linker, linker_preference = linker_preference, linker_type = ctx.attr.linker_type or None, + coverage_supported = bool(ctx.file.llvm_cov) and ctx.attr.rust_std[rust_common.stdlib_info].has_profiler_builtins, llvm_cov = ctx.file.llvm_cov, llvm_profdata = ctx.file.llvm_profdata, llvm_lib = ctx.files.llvm_lib,