Skip to content

Commit

Permalink
The compile_data attribute can now be gathered from dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Jul 2, 2021
1 parent c9345d3 commit 41370be
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions proto/proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, is_gr
edition = proto_toolchain.edition,
rustc_env = {},
is_test = False,
compile_data = depset([target.files for target in getattr(ctx.attr, "compile_data", [])]),
wrapped_crate_type = None,
),
output_hash = output_hash,
Expand Down
3 changes: 2 additions & 1 deletion rust/private/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CrateInfo = provider(
doc = "A provider containing general Crate information.",
fields = {
"aliases": "Dict[Label, String]: Renamed and aliased crates",
"compile_data": "depset[File]: Compile data required by this crate.",
"deps": "depset[Provider]: This crate's (rust or cc) dependencies' providers.",
"edition": "str: The edition of this crate.",
"is_test": "bool: If the crate is being compiled in a test context",
Expand All @@ -39,7 +40,7 @@ DepInfo = provider(
doc = "A provider containing information about a Crate's dependencies.",
fields = {
"dep_env": "File: File with environment variables direct dependencies build scripts rely upon.",
"direct_crates": "depset[CrateInfo]",
"direct_crates": "depset[AliasableDepInfo]",
"transitive_build_infos": "depset[BuildInfo]",
"transitive_crates": "depset[CrateInfo]",
"transitive_libs": "List[File]: All transitive dependencies, not filtered by type.",
Expand Down
4 changes: 4 additions & 0 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def _rust_library_common(ctx, crate_type):
edition = get_edition(ctx.attr, toolchain),
rustc_env = ctx.attr.rustc_env,
is_test = False,
compile_data = depset(ctx.files.compile_data),
),
output_hash = output_hash,
)
Expand Down Expand Up @@ -301,6 +302,7 @@ def _rust_binary_impl(ctx):
edition = get_edition(ctx.attr, toolchain),
rustc_env = ctx.attr.rustc_env,
is_test = False,
compile_data = depset(ctx.files.compile_data),
),
)

Expand Down Expand Up @@ -418,6 +420,7 @@ def _rust_test_common(ctx, toolchain, output):
edition = crate.edition,
rustc_env = ctx.attr.rustc_env,
is_test = True,
compile_data = depset(crate.compile_data),
wrapped_crate_type = crate.type,
)
else:
Expand All @@ -434,6 +437,7 @@ def _rust_test_common(ctx, toolchain, output):
edition = get_edition(ctx.attr, toolchain),
rustc_env = ctx.attr.rustc_env,
is_test = True,
compile_data = depset(ctx.files.compile_data),
)

providers = rustc_compile_action(
Expand Down
8 changes: 6 additions & 2 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def collect_inputs(
Args:
ctx (ctx): The rule's context object.
file (struct): A struct containing files defined in label type attributes marked as `allow_single_file`.
files (list): A list of all inputs.
files (list): A list of all inputs (`ctx.files`).
toolchain (rust_toolchain): The current `rust_toolchain`.
cc_toolchain (CcToolchainInfo): The current `cc_toolchain`.
crate_info (CrateInfo): The Crate information of the crate to process build scripts for.
Expand All @@ -280,9 +280,11 @@ def collect_inputs(

linker_depset = cc_toolchain.all_files

# Collect compile data from the crate's dependencies
transitive_compile_data = depset(transitive = [info.compile_data for info in dep_info.transitive_crates.to_list()])

compile_inputs = depset(
getattr(files, "data", []) +
getattr(files, "compile_data", []) +
[toolchain.rustc] +
toolchain.crosstool_files +
([build_info.rustc_env, build_info.flags] if build_info else []) +
Expand All @@ -293,6 +295,8 @@ def collect_inputs(
linker_depset,
crate_info.srcs,
dep_info.transitive_libs,
transitive_compile_data,
crate_info.compile_data,
],
)
build_env_files = getattr(files, "rustc_env_files", [])
Expand Down
21 changes: 21 additions & 0 deletions test/compile_data/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load("//rust:defs.bzl", "rust_library", "rust_test")

# Note that this is the only target which assigns the `compile_data` attribute
rust_library(
name = "compile_data",
srcs = ["compile_data.rs"],
compile_data = ["compile_data.txt"],
edition = "2018",
)

rust_test(
name = "compile_data_unit_test",
crate = ":compile_data",
)

rust_test(
name = "compile_data_integration_test",
srcs = ["compile_data_test.rs"],
edition = "2018",
deps = [":compile_data"],
)
9 changes: 9 additions & 0 deletions test/compile_data/compile_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// Data loaded from compile data
pub const COMPILE_DATA: &'static str = include_str!("compile_data.txt");

/// A test that is expected to be compiled from a target that does not
/// directly populate the `compile_data` attribute
#[test]
fn test_compile_data_contents() {
assert_eq!(COMPILE_DATA, "compile data contents\n");
}
1 change: 1 addition & 0 deletions test/compile_data/compile_data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
compile data contents
9 changes: 9 additions & 0 deletions test/compile_data/compile_data_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use compile_data::COMPILE_DATA;

#[test]
fn test_compile_data_contents() {
// This would confirm that the constant that's compiled into the `compile_data`
// crate matches the data loaded at compile time here. Where `compile_data.txt`
// would have only been provided by the `compile_data` crate itself.
assert_eq!(COMPILE_DATA, include_str!("compile_data.txt"));
}

0 comments on commit 41370be

Please sign in to comment.