diff --git a/proto/proto.bzl b/proto/proto.bzl index e79521cc65..e1f14a9425 100644 --- a/proto/proto.bzl +++ b/proto/proto.bzl @@ -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", [])]), ), output_hash = output_hash, ) diff --git a/rust/private/providers.bzl b/rust/private/providers.bzl index 53bfbc8e14..c627cae5ef 100644 --- a/rust/private/providers.bzl +++ b/rust/private/providers.bzl @@ -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", @@ -35,7 +36,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.", diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index cf9b700f6e..754001e22c 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -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, ) @@ -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), ), ) @@ -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), ) else: # Target is a standalone crate. Build the test binary as its own crate. @@ -433,6 +436,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( diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 367474d403..b324b25ed4 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -265,7 +265,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. @@ -279,9 +279,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 []) + @@ -292,6 +294,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", []) diff --git a/test/compile_data/BUILD.bazel b/test/compile_data/BUILD.bazel new file mode 100644 index 0000000000..274c8a3467 --- /dev/null +++ b/test/compile_data/BUILD.bazel @@ -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"], +) diff --git a/test/compile_data/compile_data.rs b/test/compile_data/compile_data.rs new file mode 100644 index 0000000000..ccd7e64f63 --- /dev/null +++ b/test/compile_data/compile_data.rs @@ -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"); +} diff --git a/test/compile_data/compile_data.txt b/test/compile_data/compile_data.txt new file mode 100644 index 0000000000..6d8daa7138 --- /dev/null +++ b/test/compile_data/compile_data.txt @@ -0,0 +1 @@ +compile data contents diff --git a/test/compile_data/compile_data_test.rs b/test/compile_data/compile_data_test.rs new file mode 100644 index 0000000000..ac640633b3 --- /dev/null +++ b/test/compile_data/compile_data_test.rs @@ -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")); +}