diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index 74d458eef9..dae1880f93 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -424,6 +424,7 @@ def _rust_test_impl(ctx): owner = ctx.label, ) + coverage = toolchain.llvm_cov and ctx.coverage_instrumented(target = ctx.attr.crate) providers = rustc_compile_action( ctx = ctx, attr = ctx.attr, @@ -431,6 +432,7 @@ def _rust_test_impl(ctx): crate_info_dict = crate_info_dict, rust_flags = get_rust_test_flags(ctx.attr), skip_expanding_rustc_env = True, + include_coverage = coverage, ) data = getattr(ctx.attr, "data", []) @@ -440,7 +442,7 @@ def _rust_test_impl(ctx): data, {}, ) - if toolchain.llvm_cov and ctx.configuration.coverage_enabled: + if coverage: 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 d78c28902c..1bc25d29ba 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -833,7 +833,8 @@ def construct_arguments( use_json_output = False, build_metadata = False, force_depend_on_objects = False, - skip_expanding_rustc_env = False): + skip_expanding_rustc_env = False, + include_coverage = False): """Builds an Args object containing common rustc flags Args: @@ -865,6 +866,7 @@ def construct_arguments( build_metadata (bool): Generate CLI arguments for building *only* .rmeta files. This requires use_json_output. force_depend_on_objects (bool): Force using `.rlib` object files instead of metadata (`.rmeta`) files even if they are available. skip_expanding_rustc_env (bool): Whether to skip expanding CrateInfo.rustc_env_attr + include_coverage: (bool): Whether we should include coverage flags for this crate. Returns: tuple: A tuple of the following items @@ -1057,7 +1059,7 @@ def construct_arguments( rustc_flags.add("--extern") rustc_flags.add("proc_macro") - if toolchain.llvm_cov and ctx.configuration.coverage_enabled: + if toolchain.llvm_cov and include_coverage: # https://doc.rust-lang.org/rustc/instrument-coverage.html rustc_flags.add("--codegen=instrument-coverage") @@ -1157,7 +1159,8 @@ def rustc_compile_action( force_all_deps_direct = False, crate_info_dict = None, skip_expanding_rustc_env = False, - include_coverage = True): + include_coverage = None, + include_coverage_provider = True): """Create and run a rustc compile action based on the current rule's attributes Args: @@ -1171,6 +1174,7 @@ def rustc_compile_action( crate_info_dict: A mutable dict used to create CrateInfo provider skip_expanding_rustc_env (bool, optional): Whether to expand CrateInfo.rustc_env include_coverage (bool, optional): Whether to generate coverage information or not. + include_coverage_provider: (bool, optional): Whether to the coverage provider or not. Returns: list: A list of the following providers: @@ -1180,6 +1184,9 @@ def rustc_compile_action( """ crate_info = rust_common.create_crate_info(**crate_info_dict) + if include_coverage == None: + include_coverage = toolchain.llvm_cov and ctx.coverage_instrumented() + build_metadata = crate_info_dict.get("metadata", None) rustc_output = crate_info_dict.get("rustc_output", None) rustc_rmeta_output = crate_info_dict.get("rustc_rmeta_output", None) @@ -1277,6 +1284,7 @@ def rustc_compile_action( stamp = stamp, use_json_output = bool(build_metadata) or bool(rustc_output) or bool(rustc_rmeta_output), skip_expanding_rustc_env = skip_expanding_rustc_env, + include_coverage = include_coverage, ) args_metadata = None @@ -1496,7 +1504,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 include_coverage: coverage_runfiles = [toolchain.llvm_cov, toolchain.llvm_profdata] experimental_use_coverage_metadata_files = toolchain._experimental_use_coverage_metadata_files @@ -1549,7 +1557,7 @@ def rustc_compile_action( # baseline_coverage.dat created here will conflict with the baseline_coverage.dat of the # underlying target, which is a build failure. So we add an option to disable it so that this # function can be invoked from aspects for rules that have its own InstrumentedFilesInfo. - if include_coverage: + if include_coverage and include_coverage_provider: providers.append( coverage_common.instrumented_files_info( ctx,