Skip to content

Commit

Permalink
provide flag to exclude data at compile time (#3005)
Browse files Browse the repository at this point in the history
Fixes #2977

---------

Co-authored-by: Daniel Wagner-Hall <[email protected]>
  • Loading branch information
ajwerner and illicitonion authored Nov 23, 2024
1 parent d9bbb1d commit e61e6e1
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions crate_universe/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion examples/cargo_manifest_dir/external_crate/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ load("@rules_rust//rust:defs.bzl", "rust_library")
rust_library(
name = "external_crate",
srcs = ["src/lib.rs"],
data = ["include/included_file.rs.inc"],
compile_data = ["include/included_file.rs.inc"],
visibility = ["//visibility:public"],
)
2 changes: 1 addition & 1 deletion examples/crate_universe/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ rust_binary(
name = "cxxbridge-cmd",
srcs = glob(["src/**/*.rs"]),
aliases = aliases(),
data = [
compile_data = [
"src/gen/include/cxx.h",
],
edition = "2021",
Expand Down
2 changes: 1 addition & 1 deletion examples/flag_locations/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ rust_test(
srcs = [
"main.rs",
],
data = [":flag_generator"],
compile_data = [":flag_generator"],
edition = "2018",
rustc_flags = [
"@$(location :flag_generator)",
Expand Down
8 changes: 7 additions & 1 deletion rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down
8 changes: 8 additions & 0 deletions rust/settings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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 = True,
issue = "https://github.com/bazelbuild/rules_rust/issues/2977",
visibility = ["//visibility:public"],
)
5 changes: 5 additions & 0 deletions rust/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down Expand Up @@ -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"),
),
Expand Down
2 changes: 1 addition & 1 deletion test/build_env/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)

Expand Down

0 comments on commit e61e6e1

Please sign in to comment.