Skip to content

Commit 3c39ecb

Browse files
committed
Avoid adding data (vs compile_data) to Rustc actions
1 parent a9565ff commit 3c39ecb

5 files changed

Lines changed: 89 additions & 2 deletions

File tree

rust/private/rustc.bzl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,7 @@ def collect_inputs(
858858
build_info = build_info,
859859
dep_info = dep_info,
860860
include_link_flags = include_link_flags,
861+
include_transitive_data = not toolchain._incompatible_do_not_include_transitive_data_in_compile_inputs,
861862
)
862863

863864
# TODO(parkmycar): Cleanup the handling of lint_files here.
@@ -1994,13 +1995,15 @@ def add_edition_flags(args, crate):
19941995
def _process_build_scripts(
19951996
build_info,
19961997
dep_info,
1997-
include_link_flags = True):
1998+
include_link_flags = True,
1999+
include_transitive_data = False):
19982000
"""Gathers the outputs from a target's `cargo_build_script` action.
19992001
20002002
Args:
20012003
build_info (BuildInfo): The target Build's dependency info.
20022004
dep_info (DepInfo): The Depinfo provider form the target Crate's set of inputs.
20032005
include_link_flags (bool, optional): Whether to include flags like `-l` that instruct the linker to search for a library.
2006+
include_transitive_data (bool, optional): Whether to include transitive data dependencies in compile inputs.
20042007
20052008
Returns:
20062009
tuple: A tuple: A tuple of the following items:
@@ -2010,7 +2013,9 @@ def _process_build_scripts(
20102013
- (depset[File]): All direct and transitive build flags from the current build info.
20112014
"""
20122015
direct_inputs = []
2013-
transitive_inputs = [dep_info.link_search_path_files, dep_info.transitive_data]
2016+
transitive_inputs = [dep_info.link_search_path_files]
2017+
if include_transitive_data:
2018+
transitive_inputs.append(dep_info.transitive_data)
20142019

20152020
# Arguments to the commandline line wrapper that are going to be used
20162021
# to create the final command line

rust/settings/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ load(
2727
"extra_rustc_flags",
2828
"incompatible_change_clippy_error_format",
2929
"incompatible_do_not_include_data_in_compile_data",
30+
"incompatible_do_not_include_transitive_data_in_compile_inputs",
3031
"lto",
3132
"no_std",
3233
"pipelined_compilation",
@@ -111,6 +112,8 @@ incompatible_change_clippy_error_format()
111112

112113
incompatible_do_not_include_data_in_compile_data()
113114

115+
incompatible_do_not_include_transitive_data_in_compile_inputs()
116+
114117
lto()
115118

116119
no_std()

rust/settings/settings.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,15 @@ def incompatible_do_not_include_data_in_compile_data():
535535
issue = "https://github.com/bazelbuild/rules_rust/issues/2977",
536536
)
537537

538+
# buildifier: disable=unnamed-macro
539+
def incompatible_do_not_include_transitive_data_in_compile_inputs():
540+
"""A flag to control whether transitive data dependencies are included in compile inputs."""
541+
incompatible_flag(
542+
name = "incompatible_do_not_include_transitive_data_in_compile_inputs",
543+
build_setting_default = True,
544+
issue = "https://github.com/bazelbuild/rules_rust/issues/3915",
545+
)
546+
538547
def codegen_units():
539548
"""The default value for `--codegen-units` which also affects resource allocation for rustc actions.
540549

rust/toolchain.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ def _rust_toolchain_impl(ctx):
623623
_experimental_use_coverage_metadata_files = ctx.attr._experimental_use_coverage_metadata_files[BuildSettingInfo].value,
624624
_toolchain_generated_sysroot = ctx.attr._toolchain_generated_sysroot[BuildSettingInfo].value,
625625
_incompatible_do_not_include_data_in_compile_data = ctx.attr._incompatible_do_not_include_data_in_compile_data[IncompatibleFlagInfo].enabled,
626+
_incompatible_do_not_include_transitive_data_in_compile_inputs = ctx.attr._incompatible_do_not_include_transitive_data_in_compile_inputs[IncompatibleFlagInfo].enabled,
626627
_no_std = no_std,
627628
_codegen_units = ctx.attr._codegen_units[BuildSettingInfo].value,
628629
_experimental_use_allocator_libraries_with_mangled_symbols = ctx.attr.experimental_use_allocator_libraries_with_mangled_symbols,
@@ -866,6 +867,10 @@ rust_toolchain = rule(
866867
default = Label("//rust/settings:incompatible_do_not_include_data_in_compile_data"),
867868
doc = "Label to a boolean build setting that controls whether to include data files in compile_data.",
868869
),
870+
"_incompatible_do_not_include_transitive_data_in_compile_inputs": attr.label(
871+
default = Label("//rust/settings:incompatible_do_not_include_transitive_data_in_compile_inputs"),
872+
doc = "Label to a boolean build setting that controls whether to include transitive data dependencies in compile inputs.",
873+
),
869874
"_linker_preference": attr.label(
870875
default = Label("//rust/settings:toolchain_linker_preference"),
871876
),

test/unit/compile_data/compile_data_test.bzl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,34 @@ def _compile_data_propagates_to_rust_doc_test_impl(ctx):
5656

5757
return analysistest.end(env)
5858

59+
def _transitive_data_not_in_compile_inputs_test_impl(ctx):
60+
env = analysistest.begin(ctx)
61+
target = analysistest.target_under_test(env)
62+
63+
rustc_action = None
64+
for action in target.actions:
65+
if action.mnemonic == "Rustc":
66+
rustc_action = action
67+
break
68+
69+
asserts.false(env, rustc_action == None, "Expected a Rustc action")
70+
71+
data_inputs = [i for i in rustc_action.inputs.to_list() if "transitive_data_dep.txt" in i.path]
72+
asserts.equals(
73+
env,
74+
0,
75+
len(data_inputs),
76+
"Expected transitive data dep file to NOT appear in Rustc action inputs, but found: " +
77+
str([i.path for i in data_inputs]),
78+
)
79+
80+
return analysistest.end(env)
81+
5982
compile_data_propagates_to_crate_info_test = analysistest.make(_compile_data_propagates_to_crate_info_test_impl)
6083
wrapper_rule_propagates_to_crate_info_test = analysistest.make(_wrapper_rule_propagates_to_crate_info_test_impl)
6184
wrapper_rule_propagates_and_joins_compile_data_test = analysistest.make(_wrapper_rule_propagates_and_joins_compile_data_test_impl)
6285
compile_data_propagates_to_rust_doc_test = analysistest.make(_compile_data_propagates_to_rust_doc_test_impl)
86+
transitive_data_not_in_compile_inputs_test = analysistest.make(_transitive_data_not_in_compile_inputs_test_impl)
6387

6488
def _define_test_targets():
6589
rust_library(
@@ -134,6 +158,41 @@ def _define_test_targets():
134158
crate = ":compile_data_gen_srcs",
135159
)
136160

161+
write_file(
162+
name = "transitive_data_dep_file",
163+
out = "transitive_data_dep.txt",
164+
content = ["transitive data dep", ""],
165+
newline = "unix",
166+
)
167+
168+
write_file(
169+
name = "lib_with_data_src",
170+
out = "lib_with_data.rs",
171+
content = ["pub fn hello() {}", ""],
172+
newline = "unix",
173+
)
174+
175+
rust_library(
176+
name = "lib_with_data",
177+
srcs = [":lib_with_data.rs"],
178+
data = [":transitive_data_dep.txt"],
179+
edition = "2021",
180+
)
181+
182+
write_file(
183+
name = "lib_depending_on_data_src",
184+
out = "lib_depending_on_data.rs",
185+
content = ["extern crate lib_with_data;", ""],
186+
newline = "unix",
187+
)
188+
189+
rust_library(
190+
name = "lib_depending_on_data",
191+
srcs = [":lib_depending_on_data.rs"],
192+
deps = [":lib_with_data"],
193+
edition = "2021",
194+
)
195+
137196
def compile_data_test_suite(name):
138197
"""Entry-point macro called from the BUILD file.
139198
@@ -163,12 +222,18 @@ def compile_data_test_suite(name):
163222
target_under_test = ":compile_data_env_rust_doc",
164223
)
165224

225+
transitive_data_not_in_compile_inputs_test(
226+
name = "transitive_data_not_in_compile_inputs_test",
227+
target_under_test = ":lib_depending_on_data",
228+
)
229+
166230
native.test_suite(
167231
name = name,
168232
tests = [
169233
":compile_data_propagates_to_crate_info_test",
170234
":wrapper_rule_propagates_to_crate_info_test",
171235
":wrapper_rule_propagates_and_joins_compile_data_test",
172236
":compile_data_propagates_to_rust_doc_test",
237+
":transitive_data_not_in_compile_inputs_test",
173238
],
174239
)

0 commit comments

Comments
 (0)