From 5e7e310d61018965d57e79f076512182f9004939 Mon Sep 17 00:00:00 2001 From: Andrew Werner Date: Sat, 16 Nov 2024 20:46:50 -0500 Subject: [PATCH] provide flag to exclude data at compile time Fixes #2977 --- crate_universe/BUILD.bazel | 1 + rust/private/rustc.bzl | 8 +++++++- rust/settings/BUILD.bazel | 8 ++++++++ rust/toolchain.bzl | 5 +++++ test/build_env/BUILD.bazel | 2 +- 5 files changed, 22 insertions(+), 2 deletions(-) diff --git a/crate_universe/BUILD.bazel b/crate_universe/BUILD.bazel index 782b0005be..b6da14961a 100644 --- a/crate_universe/BUILD.bazel +++ b/crate_universe/BUILD.bazel @@ -93,6 +93,7 @@ alias( rust_test( name = "unit_test", aliases = aliases(), + compile_data = glob(["test_data/**"]), crate = ":cargo_bazel", data = glob(["test_data/**"]) + [ "//crate_universe/test_data/serialized_configs", diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index d2a810f099..d23da7dce5 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -715,8 +715,14 @@ def collect_inputs( if build_info.flags: build_info_inputs.append(build_info.flags) + # The old default behavior was to include data files at compile time. + # This flag controls whether to include data files in compile_data. + data_included_in_inputs = [] + if not toolchain._incompatible_do_not_include_data_in_compile_data: + data_included_in_inputs = getattr(files, "data", []) + nolinkstamp_compile_inputs = depset( - getattr(files, "data", []) + + data_included_in_inputs + build_info_inputs + ([toolchain.target_json] if toolchain.target_json else []) + ([] if linker_script == None else [linker_script]), diff --git a/rust/settings/BUILD.bazel b/rust/settings/BUILD.bazel index f063fb2508..ac7a784402 100644 --- a/rust/settings/BUILD.bazel +++ b/rust/settings/BUILD.bazel @@ -253,3 +253,11 @@ per_crate_rustc_flag( build_setting_default = [], visibility = ["//visibility:public"], ) + +# A flag to control whether to include data files in compile_data. +incompatible_flag( + name = "incompatible_do_not_include_data_in_compile_data", + build_setting_default = False, + issue = "https://github.com/bazelbuild/rules_rust/issues/2977", + visibility = ["//visibility:public"], +) diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl index f252e22042..ce0bb5d457 100644 --- a/rust/toolchain.bzl +++ b/rust/toolchain.bzl @@ -699,6 +699,7 @@ def _rust_toolchain_impl(ctx): _experimental_use_coverage_metadata_files = ctx.attr._experimental_use_coverage_metadata_files[BuildSettingInfo].value, _incompatible_change_rust_test_compilation_output_directory = ctx.attr._incompatible_change_rust_test_compilation_output_directory[IncompatibleFlagInfo].enabled, _toolchain_generated_sysroot = ctx.attr._toolchain_generated_sysroot[BuildSettingInfo].value, + _incompatible_do_not_include_data_in_compile_data = ctx.attr._incompatible_do_not_include_data_in_compile_data[IncompatibleFlagInfo].enabled, _no_std = no_std, ) return [ @@ -886,6 +887,10 @@ rust_toolchain = rule( "_incompatible_change_rust_test_compilation_output_directory": attr.label( default = Label("//rust/settings:incompatible_change_rust_test_compilation_output_directory"), ), + "_incompatible_do_not_include_data_in_compile_data": attr.label( + default = Label("//rust/settings:incompatible_do_not_include_data_in_compile_data"), + doc = "Label to a boolean build setting that controls whether to include data files in compile_data.", + ), "_no_std": attr.label( default = Label("//:no_std"), ), diff --git a/test/build_env/BUILD.bazel b/test/build_env/BUILD.bazel index 12fd5fa805..ffd49b1234 100644 --- a/test/build_env/BUILD.bazel +++ b/test/build_env/BUILD.bazel @@ -9,7 +9,7 @@ package(default_visibility = ["//visibility:public"]) rust_test( name = "conflicting_deps_test", srcs = ["tests/manifest_dir.rs"], - data = ["src/manifest_dir_file.txt"], + compile_data = ["src/manifest_dir_file.txt"], edition = "2018", )