Skip to content

Commit fc00eac

Browse files
authored
Avoid adding data (vs compile_data) to Rustc actions (#3916)
relates to #3915 closes #3609
1 parent 43b039a commit fc00eac

7 files changed

Lines changed: 298 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
load(":transitive_data_test.bzl", "transitive_cbs_data_test_suite")
2+
3+
transitive_cbs_data_test_suite(
4+
name = "transitive_cbs_data_test_suite",
5+
)
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
"""Tests documenting that cargo_build_script `data` files currently act as both
2+
compile_data and runtime data -- they appear in Rustc compile action inputs for
3+
both the direct library and transitive dependents.
4+
5+
See https://github.com/bazelbuild/rules_rust/issues/3609 for context."""
6+
7+
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
8+
load("@bazel_skylib//rules:write_file.bzl", "write_file")
9+
load("//cargo:defs.bzl", "cargo_build_script")
10+
load("//rust:defs.bzl", "rust_library")
11+
12+
def _cbs_data_in_rustc_inputs_impl(ctx):
13+
env = analysistest.begin(ctx)
14+
target = analysistest.target_under_test(env)
15+
16+
rustc_action = None
17+
for action in target.actions:
18+
if action.mnemonic == "Rustc":
19+
rustc_action = action
20+
break
21+
22+
asserts.false(env, rustc_action == None, "Expected a Rustc action")
23+
24+
# cargo_build_script `data` currently flows into BuildInfo.compile_data
25+
# via script_data, so it appears in Rustc action inputs for both the
26+
# direct dependent and transitive dependents. This documents that
27+
# CBS `data` effectively acts as both compile_data and data today.
28+
data_inputs = [i for i in rustc_action.inputs.to_list() if "cbs_data_dep.txt" in i.path]
29+
asserts.true(
30+
env,
31+
len(data_inputs) > 0,
32+
"Expected CBS data file to appear in Rustc action inputs (CBS data currently acts as compile_data)",
33+
)
34+
35+
return analysistest.end(env)
36+
37+
cbs_data_in_rustc_inputs_test = analysistest.make(
38+
_cbs_data_in_rustc_inputs_impl,
39+
)
40+
41+
def _define_test_targets():
42+
write_file(
43+
name = "cbs_data_dep_file",
44+
out = "cbs_data_dep.txt",
45+
content = ["data for build script", ""],
46+
newline = "unix",
47+
)
48+
49+
write_file(
50+
name = "build_rs_src",
51+
out = "build.rs",
52+
content = ["fn main() {}", ""],
53+
newline = "unix",
54+
)
55+
56+
cargo_build_script(
57+
name = "build_script",
58+
srcs = [":build.rs"],
59+
data = [":cbs_data_dep.txt"],
60+
edition = "2021",
61+
)
62+
63+
write_file(
64+
name = "lib_src",
65+
out = "lib.rs",
66+
content = ["pub fn hello() {}", ""],
67+
newline = "unix",
68+
)
69+
70+
rust_library(
71+
name = "lib",
72+
srcs = [":lib.rs"],
73+
deps = [":build_script"],
74+
edition = "2021",
75+
)
76+
77+
write_file(
78+
name = "bin_src",
79+
out = "bin.rs",
80+
content = ["extern crate lib;", ""],
81+
newline = "unix",
82+
)
83+
84+
rust_library(
85+
name = "bin",
86+
srcs = [":bin.rs"],
87+
deps = [":lib"],
88+
edition = "2021",
89+
)
90+
91+
def transitive_cbs_data_test_suite(name):
92+
"""Entry-point macro called from the BUILD file.
93+
94+
Args:
95+
name (str): Name of the macro.
96+
"""
97+
_define_test_targets()
98+
99+
cbs_data_in_rustc_inputs_test(
100+
name = "cbs_data_in_lib_compile_inputs_test",
101+
target_under_test = ":lib",
102+
)
103+
104+
cbs_data_in_rustc_inputs_test(
105+
name = "cbs_data_in_bin_compile_inputs_test",
106+
target_under_test = ":bin",
107+
)
108+
109+
native.test_suite(
110+
name = name,
111+
tests = [
112+
":cbs_data_in_lib_compile_inputs_test",
113+
":cbs_data_in_bin_compile_inputs_test",
114+
],
115+
)

rust/private/rustc.bzl

Lines changed: 8 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
@@ -2037,6 +2042,7 @@ def _process_build_scripts(
20372042
for dep_build_info in dep_info.transitive_build_infos.to_list():
20382043
if dep_build_info.out_dir:
20392044
direct_inputs.append(dep_build_info.out_dir)
2045+
transitive_inputs.append(dep_build_info.compile_data)
20402046

20412047
out_dir_compile_inputs = depset(
20422048
direct_inputs,

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
),

0 commit comments

Comments
 (0)