Skip to content

Commit

Permalink
Update crate universe to generate build script targets with compile data
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Sep 11, 2024
1 parent 56facca commit be4be44
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions crate_universe/private/crate.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def _annotation(
additive_build_file = None,
additive_build_file_content = None,
alias_rule = None,
build_script_compile_data = None,
build_script_data = None,
build_script_tools = None,
build_script_data_glob = None,
Expand Down Expand Up @@ -125,6 +126,7 @@ def _annotation(
generated BUILD files.
alias_rule (str, optional): Alias rule to use instead of `native.alias()`. Overrides [render_config](#render_config)'s
'default_alias_rule'.
build_script_compile_data (list, optional): A list of labels to add to a crate's `cargo_build_script::compile_data` attribute.
build_script_data (list, optional): A list of labels to add to a crate's `cargo_build_script::data` attribute.
build_script_tools (list, optional): A list of labels to add to a crate's `cargo_build_script::tools` attribute.
build_script_data_glob (list, optional): A list of glob patterns to add to a crate's `cargo_build_script::data`
Expand Down Expand Up @@ -185,6 +187,7 @@ def _annotation(
additive_build_file = _stringify_label(additive_build_file),
additive_build_file_content = additive_build_file_content,
alias_rule = parse_alias_rule(alias_rule),
build_script_compile_data = _stringify_list(build_script_compile_data),
build_script_data = _stringify_list(build_script_data),
build_script_tools = _stringify_list(build_script_tools),
build_script_data_glob = build_script_data_glob,
Expand Down
5 changes: 5 additions & 0 deletions crate_universe/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ pub(crate) struct CrateAnnotations {
/// [proc_macro_deps](https://bazelbuild.github.io/rules_rust/cargo.html#cargo_build_script-proc_macro_deps) attribute.
pub(crate) build_script_proc_macro_deps: Option<Select<BTreeSet<Label>>>,

/// Additional compile-only data to pass to a build script's
/// [compile_data](https://bazelbuild.github.io/rules_rust/cargo.html#cargo_build_script-compile_data) attribute.
pub(crate) build_script_compile_data: Option<Select<BTreeSet<Label>>>,

/// Additional data to pass to a build script's
/// [build_script_data](https://bazelbuild.github.io/rules_rust/cargo.html#cargo_build_script-data) attribute.
pub(crate) build_script_data: Option<Select<BTreeSet<Label>>>,
Expand Down Expand Up @@ -387,6 +391,7 @@ impl Add for CrateAnnotations {
rustc_flags: select_merge(self.rustc_flags, rhs.rustc_flags),
build_script_deps: select_merge(self.build_script_deps, rhs.build_script_deps),
build_script_proc_macro_deps: select_merge(self.build_script_proc_macro_deps, rhs.build_script_proc_macro_deps),
build_script_compile_data: select_merge(self.build_script_compile_data, rhs.build_script_compile_data),
build_script_data: select_merge(self.build_script_data, rhs.build_script_data),
build_script_tools: select_merge(self.build_script_tools, rhs.build_script_tools),
build_script_data_glob: joined_extra_member!(self.build_script_data_glob, rhs.build_script_data_glob, BTreeSet::new, BTreeSet::extend),
Expand Down
11 changes: 11 additions & 0 deletions crate_universe/src/context/crate_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ pub(crate) struct BuildScriptAttributes {
#[serde(skip_serializing_if = "Select::is_empty")]
pub(crate) compile_data: Select<BTreeSet<Label>>,

#[serde(skip_serializing_if = "BTreeSet::is_empty")]
pub(crate) compile_data_glob: BTreeSet<String>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub(crate) data: Select<BTreeSet<Label>>,

Expand Down Expand Up @@ -245,6 +248,9 @@ impl Default for BuildScriptAttributes {
fn default() -> Self {
Self {
compile_data: Default::default(),
// The build script itself also has access to all
// source files by default.
compile_data_glob: BTreeSet::from(["**".to_owned()]),
data: Default::default(),
// Build scripts include all sources by default
data_glob: BTreeSet::from(["**".to_owned()]),
Expand Down Expand Up @@ -619,6 +625,11 @@ impl CrateContext {
attrs.data = Select::merge(attrs.data.clone(), extra.clone());
}

// Compile Data
if let Some(extra) = &crate_extra.build_script_compile_data {
attrs.compile_data = Select::merge(attrs.compile_data.clone(), extra.clone());
}

// Tools
if let Some(extra) = &crate_extra.build_script_tools {
attrs.tools = Select::merge(attrs.tools.clone(), extra.clone());
Expand Down
2 changes: 1 addition & 1 deletion crate_universe/src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ mod test {
);

assert_eq!(
Digest("610cbb406b7452d32ae31c45ec82cd3b3b1fb184c3411ef613c948d88492441b".to_owned()),
Digest("5e0fd9106767c43deb77bb1024ca24e99685110a1085c03abc028c75e180831f".to_owned()),
digest,
);
}
Expand Down
6 changes: 5 additions & 1 deletion crate_universe/src/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,9 @@ impl Renderer {
),
compile_data: make_data(
platforms,
Default::default(),
attrs
.map(|attrs| attrs.compile_data_glob.clone())
.unwrap_or_default(),
attrs
.map(|attrs| attrs.compile_data.clone())
.unwrap_or_default(),
Expand Down Expand Up @@ -1060,6 +1062,7 @@ mod test {
},
);

eprintln!("{:#?}", context);
let renderer = Renderer::new(mock_render_config(None), mock_supported_platform_triples());
let output = renderer.render(&context).unwrap();

Expand All @@ -1070,6 +1073,7 @@ mod test {
assert!(build_file_content.contains("cargo_build_script("));
assert!(build_file_content.contains("name = \"build_script_build\""));
assert!(build_file_content.contains("\"crate-name=mock_crate\""));
assert!(build_file_content.contains("compile_data = glob("));

// Ensure `cargo_build_script` requirements are met
assert!(build_file_content.contains("name = \"_bs\""));
Expand Down
3 changes: 2 additions & 1 deletion crate_universe/src/utils/starlark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub(crate) struct Alias {
pub(crate) tags: Set<String>,
}

#[derive(Serialize)]
#[derive(Debug, Serialize)]
#[serde(rename = "cargo_build_script")]
pub(crate) struct CargoBuildScript {
pub(crate) name: String,
Expand Down Expand Up @@ -205,6 +205,7 @@ pub(crate) struct CommonAttrs {
pub(crate) version: String,
}

#[derive(Debug)]
pub(crate) struct Data {
pub(crate) glob: Glob,
pub(crate) select: SelectSet<Label>,
Expand Down

0 comments on commit be4be44

Please sign in to comment.