Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The compile_data attribute can now be gathered from dependencies #814

Merged
merged 8 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 12 additions & 1 deletion 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),
UebelAndre marked this conversation as resolved.
Show resolved Hide resolved
),
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 @@ -404,8 +406,15 @@ def _rust_test_common(ctx, toolchain, output):
crate_type = "bin"
if ctx.attr.crate:
# Target is building the crate in `test` config
# Build the test binary using the dependency's srcs.
crate = ctx.attr.crate[rust_common.crate_info]

# Optionally join compile data
if crate.compile_data:
compile_data = depset(ctx.files.compile_data, transitive = [crate.compile_data])
else:
compile_data = depset(ctx.files.compile_data)

# Build the test binary using the dependency's srcs.
crate_info = rust_common.create_crate_info(
name = crate_name,
type = crate_type,
Expand All @@ -418,6 +427,7 @@ def _rust_test_common(ctx, toolchain, output):
edition = crate.edition,
rustc_env = ctx.attr.rustc_env,
is_test = True,
compile_data = compile_data,
wrapped_crate_type = crate.type,
)
else:
Expand All @@ -434,6 +444,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
4 changes: 2 additions & 2 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,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 @@ -284,7 +284,6 @@ def collect_inputs(

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 @@ -295,6 +294,7 @@ def collect_inputs(
linker_depset,
crate_info.srcs,
dep_info.transitive_libs,
crate_info.compile_data,
],
)
build_env_files = getattr(files, "rustc_env_files", [])
Expand Down
5 changes: 5 additions & 0 deletions test/unit/compile_data/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
load(":compile_data_test.bzl", "compile_data_test_suite")

compile_data_test_suite(
name = "compile_data_test_suite",
)
26 changes: 26 additions & 0 deletions test/unit/compile_data/compile_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// Data loaded from compile data
pub const COMPILE_DATA: &str = include_str!("compile_data.txt");

#[cfg(test)]
mod test {
use super::*;

/// 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");
}

/// An extra module that tests the `rust_test` rule wrapping the
/// `rust_library` is able to provide it's own compile data.
#[cfg(test_compile_data)]
mod test_compile_data {
const TEST_COMPILE_DATA: &str = include_str!("test_compile_data.txt");

#[test]
fn test_compile_data_contents() {
assert_eq!(TEST_COMPILE_DATA, "test compile data contents\n");
}
}
}
1 change: 1 addition & 0 deletions test/unit/compile_data/compile_data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
compile data contents
99 changes: 99 additions & 0 deletions test/unit/compile_data/compile_data_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""Unittest to verify compile_data (attribute) propagation"""

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load("//rust:defs.bzl", "rust_common", "rust_library", "rust_test")

def _target_has_compile_data(ctx, expected):
env = analysistest.begin(ctx)
target = analysistest.target_under_test(env)

# Extract compile_data from a target expected to have a `CrateInfo` provider
crate_info = target[rust_common.crate_info]
compile_data = crate_info.compile_data.to_list()

# Ensure compile data was correctly propagated to the provider
asserts.equals(
env,
sorted([data.short_path for data in compile_data]),
expected,
)

return analysistest.end(env)

def _compile_data_propagates_to_crate_info_test_impl(ctx):
return _target_has_compile_data(
ctx,
["test/unit/compile_data/compile_data.txt"],
)

def _wrapper_rule_propagates_to_crate_info_test_impl(ctx):
return _target_has_compile_data(
ctx,
["test/unit/compile_data/compile_data.txt"],
)

def _wrapper_rule_propagates_and_joins_compile_data_test_impl(ctx):
return _target_has_compile_data(
ctx,
[
"test/unit/compile_data/compile_data.txt",
"test/unit/compile_data/test_compile_data.txt",
],
)

compile_data_propagates_to_crate_info_test = analysistest.make(_compile_data_propagates_to_crate_info_test_impl)
wrapper_rule_propagates_to_crate_info_test = analysistest.make(_wrapper_rule_propagates_to_crate_info_test_impl)
wrapper_rule_propagates_and_joins_compile_data_test = analysistest.make(_wrapper_rule_propagates_and_joins_compile_data_test_impl)

def _define_test_targets():
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 = "test_compile_data_unit_test",
compile_data = ["test_compile_data.txt"],
crate = ":compile_data",
rustc_flags = ["--cfg=test_compile_data"],
)

def compile_data_test_suite(name):
"""Entry-point macro called from the BUILD file.

Args:
name (str): Name of the macro.
"""

_define_test_targets()

compile_data_propagates_to_crate_info_test(
name = "compile_data_propagates_to_crate_info_test",
target_under_test = ":compile_data",
)

wrapper_rule_propagates_to_crate_info_test(
name = "wrapper_rule_propagates_to_crate_info_test",
target_under_test = ":compile_data_unit_test",
)

wrapper_rule_propagates_and_joins_compile_data_test(
name = "wrapper_rule_propagates_and_joins_compile_data_test",
target_under_test = ":test_compile_data_unit_test",
)

native.test_suite(
name = name,
tests = [
":compile_data_propagates_to_crate_info_test",
":wrapper_rule_propagates_to_crate_info_test",
":wrapper_rule_propagates_and_joins_compile_data_test",
],
)
1 change: 1 addition & 0 deletions test/unit/compile_data/test_compile_data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test compile data contents