Skip to content

Commit e10ab21

Browse files
authored
The compile_data attribute can now be gathered from dependencies (#814)
* The `compile_data` attribute can now be gathered from dependencies * Don't transitively include compile_data from other targets * Fixed clippy defect * Added a wrapper test module * Added unittests * Fixed typo
1 parent 5ec77de commit e10ab21

File tree

9 files changed

+149
-4
lines changed

9 files changed

+149
-4
lines changed

proto/proto.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, is_gr
234234
edition = proto_toolchain.edition,
235235
rustc_env = {},
236236
is_test = False,
237+
compile_data = depset([target.files for target in getattr(ctx.attr, "compile_data", [])]),
237238
wrapped_crate_type = None,
238239
),
239240
output_hash = output_hash,

rust/private/providers.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CrateInfo = provider(
1818
doc = "A provider containing general Crate information.",
1919
fields = {
2020
"aliases": "Dict[Label, String]: Renamed and aliased crates",
21+
"compile_data": "depset[File]: Compile data required by this crate.",
2122
"deps": "depset[Provider]: This crate's (rust or cc) dependencies' providers.",
2223
"edition": "str: The edition of this crate.",
2324
"is_test": "bool: If the crate is being compiled in a test context",
@@ -39,7 +40,7 @@ DepInfo = provider(
3940
doc = "A provider containing information about a Crate's dependencies.",
4041
fields = {
4142
"dep_env": "File: File with environment variables direct dependencies build scripts rely upon.",
42-
"direct_crates": "depset[CrateInfo]",
43+
"direct_crates": "depset[AliasableDepInfo]",
4344
"transitive_build_infos": "depset[BuildInfo]",
4445
"transitive_crates": "depset[CrateInfo]",
4546
"transitive_libs": "List[File]: All transitive dependencies, not filtered by type.",

rust/private/rust.bzl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ def _rust_library_common(ctx, crate_type):
266266
edition = get_edition(ctx.attr, toolchain),
267267
rustc_env = ctx.attr.rustc_env,
268268
is_test = False,
269+
compile_data = depset(ctx.files.compile_data),
269270
),
270271
output_hash = output_hash,
271272
)
@@ -301,6 +302,7 @@ def _rust_binary_impl(ctx):
301302
edition = get_edition(ctx.attr, toolchain),
302303
rustc_env = ctx.attr.rustc_env,
303304
is_test = False,
305+
compile_data = depset(ctx.files.compile_data),
304306
),
305307
)
306308

@@ -404,8 +406,15 @@ def _rust_test_common(ctx, toolchain, output):
404406
crate_type = "bin"
405407
if ctx.attr.crate:
406408
# Target is building the crate in `test` config
407-
# Build the test binary using the dependency's srcs.
408409
crate = ctx.attr.crate[rust_common.crate_info]
410+
411+
# Optionally join compile data
412+
if crate.compile_data:
413+
compile_data = depset(ctx.files.compile_data, transitive = [crate.compile_data])
414+
else:
415+
compile_data = depset(ctx.files.compile_data)
416+
417+
# Build the test binary using the dependency's srcs.
409418
crate_info = rust_common.create_crate_info(
410419
name = crate_name,
411420
type = crate_type,
@@ -418,6 +427,7 @@ def _rust_test_common(ctx, toolchain, output):
418427
edition = crate.edition,
419428
rustc_env = ctx.attr.rustc_env,
420429
is_test = True,
430+
compile_data = compile_data,
421431
wrapped_crate_type = crate.type,
422432
)
423433
else:
@@ -434,6 +444,7 @@ def _rust_test_common(ctx, toolchain, output):
434444
edition = get_edition(ctx.attr, toolchain),
435445
rustc_env = ctx.attr.rustc_env,
436446
is_test = True,
447+
compile_data = depset(ctx.files.compile_data),
437448
)
438449

439450
providers = rustc_compile_action(

rust/private/rustc.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def collect_inputs(
268268
Args:
269269
ctx (ctx): The rule's context object.
270270
file (struct): A struct containing files defined in label type attributes marked as `allow_single_file`.
271-
files (list): A list of all inputs.
271+
files (list): A list of all inputs (`ctx.files`).
272272
toolchain (rust_toolchain): The current `rust_toolchain`.
273273
cc_toolchain (CcToolchainInfo): The current `cc_toolchain`.
274274
crate_info (CrateInfo): The Crate information of the crate to process build scripts for.
@@ -284,7 +284,6 @@ def collect_inputs(
284284

285285
compile_inputs = depset(
286286
getattr(files, "data", []) +
287-
getattr(files, "compile_data", []) +
288287
[toolchain.rustc] +
289288
toolchain.crosstool_files +
290289
([build_info.rustc_env, build_info.flags] if build_info else []) +
@@ -295,6 +294,7 @@ def collect_inputs(
295294
linker_depset,
296295
crate_info.srcs,
297296
dep_info.transitive_libs,
297+
crate_info.compile_data,
298298
],
299299
)
300300
build_env_files = getattr(files, "rustc_env_files", [])

test/unit/compile_data/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
load(":compile_data_test.bzl", "compile_data_test_suite")
2+
3+
compile_data_test_suite(
4+
name = "compile_data_test_suite",
5+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// Data loaded from compile data
2+
pub const COMPILE_DATA: &str = include_str!("compile_data.txt");
3+
4+
#[cfg(test)]
5+
mod test {
6+
use super::*;
7+
8+
/// A test that is expected to be compiled from a target that does not
9+
/// directly populate the `compile_data` attribute
10+
#[test]
11+
fn test_compile_data_contents() {
12+
assert_eq!(COMPILE_DATA, "compile data contents\n");
13+
}
14+
15+
/// An extra module that tests the `rust_test` rule wrapping the
16+
/// `rust_library` is able to provide it's own compile data.
17+
#[cfg(test_compile_data)]
18+
mod test_compile_data {
19+
const TEST_COMPILE_DATA: &str = include_str!("test_compile_data.txt");
20+
21+
#[test]
22+
fn test_compile_data_contents() {
23+
assert_eq!(TEST_COMPILE_DATA, "test compile data contents\n");
24+
}
25+
}
26+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
compile data contents
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"""Unittest to verify compile_data (attribute) propagation"""
2+
3+
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
4+
load("//rust:defs.bzl", "rust_common", "rust_library", "rust_test")
5+
6+
def _target_has_compile_data(ctx, expected):
7+
env = analysistest.begin(ctx)
8+
target = analysistest.target_under_test(env)
9+
10+
# Extract compile_data from a target expected to have a `CrateInfo` provider
11+
crate_info = target[rust_common.crate_info]
12+
compile_data = crate_info.compile_data.to_list()
13+
14+
# Ensure compile data was correctly propagated to the provider
15+
asserts.equals(
16+
env,
17+
sorted([data.short_path for data in compile_data]),
18+
expected,
19+
)
20+
21+
return analysistest.end(env)
22+
23+
def _compile_data_propagates_to_crate_info_test_impl(ctx):
24+
return _target_has_compile_data(
25+
ctx,
26+
["test/unit/compile_data/compile_data.txt"],
27+
)
28+
29+
def _wrapper_rule_propagates_to_crate_info_test_impl(ctx):
30+
return _target_has_compile_data(
31+
ctx,
32+
["test/unit/compile_data/compile_data.txt"],
33+
)
34+
35+
def _wrapper_rule_propagates_and_joins_compile_data_test_impl(ctx):
36+
return _target_has_compile_data(
37+
ctx,
38+
[
39+
"test/unit/compile_data/compile_data.txt",
40+
"test/unit/compile_data/test_compile_data.txt",
41+
],
42+
)
43+
44+
compile_data_propagates_to_crate_info_test = analysistest.make(_compile_data_propagates_to_crate_info_test_impl)
45+
wrapper_rule_propagates_to_crate_info_test = analysistest.make(_wrapper_rule_propagates_to_crate_info_test_impl)
46+
wrapper_rule_propagates_and_joins_compile_data_test = analysistest.make(_wrapper_rule_propagates_and_joins_compile_data_test_impl)
47+
48+
def _define_test_targets():
49+
rust_library(
50+
name = "compile_data",
51+
srcs = ["compile_data.rs"],
52+
compile_data = ["compile_data.txt"],
53+
edition = "2018",
54+
)
55+
56+
rust_test(
57+
name = "compile_data_unit_test",
58+
crate = ":compile_data",
59+
)
60+
61+
rust_test(
62+
name = "test_compile_data_unit_test",
63+
compile_data = ["test_compile_data.txt"],
64+
crate = ":compile_data",
65+
rustc_flags = ["--cfg=test_compile_data"],
66+
)
67+
68+
def compile_data_test_suite(name):
69+
"""Entry-point macro called from the BUILD file.
70+
71+
Args:
72+
name (str): Name of the macro.
73+
"""
74+
75+
_define_test_targets()
76+
77+
compile_data_propagates_to_crate_info_test(
78+
name = "compile_data_propagates_to_crate_info_test",
79+
target_under_test = ":compile_data",
80+
)
81+
82+
wrapper_rule_propagates_to_crate_info_test(
83+
name = "wrapper_rule_propagates_to_crate_info_test",
84+
target_under_test = ":compile_data_unit_test",
85+
)
86+
87+
wrapper_rule_propagates_and_joins_compile_data_test(
88+
name = "wrapper_rule_propagates_and_joins_compile_data_test",
89+
target_under_test = ":test_compile_data_unit_test",
90+
)
91+
92+
native.test_suite(
93+
name = name,
94+
tests = [
95+
":compile_data_propagates_to_crate_info_test",
96+
":wrapper_rule_propagates_to_crate_info_test",
97+
":wrapper_rule_propagates_and_joins_compile_data_test",
98+
],
99+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test compile data contents

0 commit comments

Comments
 (0)