Skip to content

Commit f2ec612

Browse files
authored
Crate universe compile_data_glob_excludes (#3210)
Right now there is no way to exclude specific paths from compile_data. And in a very rare cases this can be required, such example could be what crate have generated filenames which incompatible with bazel (in my case it was `:` symbol), usualy these files are used for test and not required for compile. I added new field to `annotation` called `compile_data_glob_excludes`, it allows specify excludes on top of default ones, following same logic as `compile_data_glob`. --------- Signed-off-by: ianisimov <ianisimov@nvidia.com>
1 parent a29ade9 commit f2ec612

22 files changed

+391
-24
lines changed

crate_universe/extensions.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,9 @@ _annotation = tag_class(
11091109
"compile_data_glob": attr.string_list(
11101110
doc = "A list of glob patterns to add to a crate's `rust_library::compile_data` attribute.",
11111111
),
1112+
"compile_data_glob_excludes": attr.string_list(
1113+
doc = "A list of glob patterns to be excllued from a crate's `rust_library::compile_data` attribute.",
1114+
),
11121115
"crate": attr.string(
11131116
doc = "The name of the crate the annotation is applied to",
11141117
mandatory = True,

crate_universe/private/crate.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def _annotation(
101101
build_script_use_default_shell_env = None,
102102
compile_data = None,
103103
compile_data_glob = None,
104+
compile_data_glob_excludes = None,
104105
crate_features = None,
105106
data = None,
106107
data_glob = None,
@@ -148,6 +149,8 @@ def _annotation(
148149
compile_data (list, optional): A list of labels to add to a crate's `rust_library::compile_data` attribute.
149150
compile_data_glob (list, optional): A list of glob patterns to add to a crate's `rust_library::compile_data`
150151
attribute.
152+
compile_data_glob_excludes (list, optional): A list of glob patterns to be excluded from a crate's `rust_library::compile_data`
153+
attribute.
151154
crate_features (optional): A list of strings to add to a crate's `rust_library::crate_features`
152155
attribute.
153156
data (list, optional): A list of labels to add to a crate's `rust_library::data` attribute.
@@ -206,6 +209,7 @@ def _annotation(
206209
build_script_use_default_shell_env = build_script_use_default_shell_env,
207210
compile_data = _stringify_list(compile_data),
208211
compile_data_glob = compile_data_glob,
212+
compile_data_glob_excludes = compile_data_glob_excludes,
209213
crate_features = crate_features,
210214
data = _stringify_list(data),
211215
data_glob = data_glob,

crate_universe/src/config.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ pub(crate) struct CrateAnnotations {
262262
/// [compile_data](https://bazelbuild.github.io/rules_rust/defs.html#rust_library-compile_data) attribute.
263263
pub(crate) compile_data_glob: Option<BTreeSet<String>>,
264264

265+
/// An optional glob pattern to set on the
266+
/// [compile_data](https://bazelbuild.github.io/rules_rust/defs.html#rust_library-compile_data) excludes attribute.
267+
pub(crate) compile_data_glob_excludes: Option<BTreeSet<String>>,
268+
265269
/// If true, disables pipelining for library targets generated for this crate.
266270
pub(crate) disable_pipelining: bool,
267271

@@ -404,6 +408,7 @@ impl Add for CrateAnnotations {
404408
disable_pipelining: self.disable_pipelining || rhs.disable_pipelining,
405409
compile_data: select_merge(self.compile_data, rhs.compile_data),
406410
compile_data_glob: joined_extra_member!(self.compile_data_glob, rhs.compile_data_glob, BTreeSet::new, BTreeSet::extend),
411+
compile_data_glob_excludes: joined_extra_member!(self.compile_data_glob_excludes, rhs.compile_data_glob_excludes, BTreeSet::new, BTreeSet::extend),
407412
rustc_env: select_merge(self.rustc_env, rhs.rustc_env),
408413
rustc_env_files: select_merge(self.rustc_env_files, rhs.rustc_env_files),
409414
rustc_flags: select_merge(self.rustc_flags, rhs.rustc_flags),
@@ -463,6 +468,7 @@ pub(crate) struct AnnotationsProvidedByPackage {
463468
pub(crate) deps: Option<Select<BTreeSet<Label>>>,
464469
pub(crate) compile_data: Option<Select<BTreeSet<Label>>>,
465470
pub(crate) compile_data_glob: Option<BTreeSet<String>>,
471+
pub(crate) compile_data_glob_excludes: Option<BTreeSet<String>>,
466472
pub(crate) rustc_env: Option<Select<BTreeMap<String, String>>>,
467473
pub(crate) rustc_env_files: Option<Select<BTreeSet<String>>>,
468474
pub(crate) rustc_flags: Option<Select<Vec<String>>>,
@@ -486,6 +492,7 @@ impl CrateAnnotations {
486492
deps,
487493
compile_data,
488494
compile_data_glob,
495+
compile_data_glob_excludes,
489496
rustc_env,
490497
rustc_env_files,
491498
rustc_flags,
@@ -517,6 +524,10 @@ impl CrateAnnotations {
517524
default(&mut self.deps, deps);
518525
default(&mut self.compile_data, compile_data);
519526
default(&mut self.compile_data_glob, compile_data_glob);
527+
default(
528+
&mut self.compile_data_glob_excludes,
529+
compile_data_glob_excludes,
530+
);
520531
default(&mut self.rustc_env, rustc_env);
521532
default(&mut self.rustc_env_files, rustc_env_files);
522533
default(&mut self.rustc_flags, rustc_flags);

crate_universe/src/context/crate_context.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ pub(crate) struct CommonAttributes {
9898
#[serde(skip_serializing_if = "BTreeSet::is_empty")]
9999
pub(crate) compile_data_glob: BTreeSet<String>,
100100

101+
#[serde(skip_serializing_if = "BTreeSet::is_empty")]
102+
pub(crate) compile_data_glob_excludes: BTreeSet<String>,
103+
101104
#[serde(skip_serializing_if = "Select::is_empty")]
102105
pub(crate) crate_features: Select<BTreeSet<String>>,
103106

@@ -151,6 +154,7 @@ impl Default for CommonAttributes {
151154
compile_data: Default::default(),
152155
// Generated targets include all files in their package by default
153156
compile_data_glob: BTreeSet::from(["**".to_owned()]),
157+
compile_data_glob_excludes: Default::default(),
154158
crate_features: Default::default(),
155159
data: Default::default(),
156160
data_glob: Default::default(),
@@ -182,6 +186,9 @@ pub(crate) struct BuildScriptAttributes {
182186
#[serde(skip_serializing_if = "BTreeSet::is_empty")]
183187
pub(crate) compile_data_glob: BTreeSet<String>,
184188

189+
#[serde(skip_serializing_if = "BTreeSet::is_empty")]
190+
pub(crate) compile_data_glob_excludes: BTreeSet<String>,
191+
185192
#[serde(skip_serializing_if = "Select::is_empty")]
186193
pub(crate) data: Select<BTreeSet<Label>>,
187194

@@ -258,6 +265,7 @@ impl Default for BuildScriptAttributes {
258265
// The build script itself also has access to all
259266
// source files by default.
260267
compile_data_glob: BTreeSet::from(["**".to_owned()]),
268+
compile_data_glob_excludes: BTreeSet::from(["**/*.rs".to_owned()]),
261269
data: Default::default(),
262270
// Build scripts include all sources by default
263271
data_glob: BTreeSet::from(["**".to_owned()]),
@@ -578,6 +586,13 @@ impl CrateContext {
578586
self.common_attrs.compile_data_glob.extend(extra.clone());
579587
}
580588

589+
// Compile data glob excludes
590+
if let Some(extra) = &crate_extra.compile_data_glob_excludes {
591+
self.common_attrs
592+
.compile_data_glob_excludes
593+
.extend(extra.clone());
594+
}
595+
581596
// Crate features
582597
if let Some(extra) = &crate_extra.crate_features {
583598
self.common_attrs.crate_features =

crate_universe/src/lockfile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ mod test {
292292
);
293293

294294
assert_eq!(
295-
Digest("2c4ee31fbefac161d2f3c6cae7fb8995c6311e292386a0500ac84e2586589dae".to_owned()),
295+
Digest("d4899058bbb3a94af5f09c1d029bb6b7f152bd9a580a10563ac5211a842d1169".to_owned()),
296296
digest,
297297
);
298298
}

crate_universe/src/rendering.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,6 @@ impl Renderer {
499499
) -> Result<CargoBuildScript> {
500500
let attrs = krate.build_script_attrs.as_ref();
501501

502-
const COMPILE_DATA_GLOB_EXCLUDES: &[&str] = &["**/*.rs"];
503-
504502
Ok(CargoBuildScript {
505503
// Because `cargo_build_script` does some invisible target name
506504
// mutating to determine the package and crate name for a build
@@ -528,10 +526,9 @@ impl Renderer {
528526
attrs
529527
.map(|attrs| attrs.compile_data_glob.clone())
530528
.unwrap_or_default(),
531-
COMPILE_DATA_GLOB_EXCLUDES
532-
.iter()
533-
.map(|&pattern| pattern.to_owned())
534-
.collect(),
529+
attrs
530+
.map(|attrs| attrs.compile_data_glob_excludes.clone())
531+
.unwrap_or_default(),
535532
attrs
536533
.map(|attrs| attrs.compile_data.clone())
537534
.unwrap_or_default(),
@@ -544,6 +541,7 @@ impl Renderer {
544541
attrs
545542
.map(|attrs| attrs.data_glob.clone())
546543
.unwrap_or_default(),
544+
Default::default(),
547545
attrs.map(|attrs| attrs.data.clone()).unwrap_or_default(),
548546
),
549547
deps: SelectSet::new(
@@ -728,13 +726,15 @@ impl Renderer {
728726
compile_data: make_data(
729727
platforms,
730728
krate.common_attrs.compile_data_glob.clone(),
729+
krate.common_attrs.compile_data_glob_excludes.clone(),
731730
krate.common_attrs.compile_data.clone(),
732731
),
733732
crate_features: SelectSet::new(krate.common_attrs.crate_features.clone(), platforms),
734733
crate_root: target.crate_root.clone(),
735734
data: make_data(
736735
platforms,
737736
krate.common_attrs.data_glob.clone(),
737+
Default::default(),
738738
krate.common_attrs.data.clone(),
739739
),
740740
edition: krate.common_attrs.edition.clone(),
@@ -984,9 +984,10 @@ fn make_data_with_exclude(
984984
fn make_data(
985985
platforms: &Platforms,
986986
glob: BTreeSet<String>,
987+
excludes: BTreeSet<String>,
987988
select: Select<BTreeSet<Label>>,
988989
) -> Data {
989-
make_data_with_exclude(platforms, glob, BTreeSet::new(), select)
990+
make_data_with_exclude(platforms, glob, excludes, select)
990991
}
991992

992993
#[cfg(test)]

examples/crate_universe/alias_rule/cargo-bazel-lock_global_alias_annotation_none.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"checksum": "bd90d955bdf1d6179d17e03bed1ee31c230f203539fc0f383b731949682d153f",
2+
"checksum": "38545303789a26523d07ca189a42892a106f1bad909fcd30de9eff4a62732f8c",
33
"crates": {
44
"direct-cargo-bazel-deps 0.0.1": {
55
"name": "direct-cargo-bazel-deps",
@@ -110,6 +110,9 @@
110110
"compile_data_glob": [
111111
"**"
112112
],
113+
"compile_data_glob_excludes": [
114+
"**/*.rs"
115+
],
113116
"data_glob": [
114117
"**"
115118
]

examples/crate_universe/alias_rule/cargo-bazel-lock_global_alias_annotation_opt.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"checksum": "185b85996af206151d0ac61700bbd27a5c6ba56e10d2bdeedeb229ec2d3620dc",
2+
"checksum": "4f32204f9e97b48770d63da9b37612966dcd220ca27b67209c481412045615b3",
33
"crates": {
44
"direct-cargo-bazel-deps 0.0.1": {
55
"name": "direct-cargo-bazel-deps",
@@ -110,6 +110,9 @@
110110
"compile_data_glob": [
111111
"**"
112112
],
113+
"compile_data_glob_excludes": [
114+
"**/*.rs"
115+
],
113116
"data_glob": [
114117
"**"
115118
]

examples/crate_universe/alias_rule/cargo-bazel-lock_global_custom_annotation_none.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"checksum": "4c18272a8dc6d1bc60827cc21b7b36418ce31b319d36280ceea4ae781a9228fc",
2+
"checksum": "b1f2020829b8d898974c39d885673f4141ba6d5a483122ce7146f168445e7bc6",
33
"crates": {
44
"direct-cargo-bazel-deps 0.0.1": {
55
"name": "direct-cargo-bazel-deps",
@@ -110,6 +110,9 @@
110110
"compile_data_glob": [
111111
"**"
112112
],
113+
"compile_data_glob_excludes": [
114+
"**/*.rs"
115+
],
113116
"data_glob": [
114117
"**"
115118
]

examples/crate_universe/alias_rule/cargo-bazel-lock_global_dbg_annotation_fastbuild.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"checksum": "5f88c0ea36c3f51f9d60943927b89fd8d801bccac2a62bea1a4130614e63120d",
2+
"checksum": "a763b2dd2109e2a32a81e03f15a0ff5e74493e54e4436ac735ff73b9f311ae2a",
33
"crates": {
44
"direct-cargo-bazel-deps 0.0.1": {
55
"name": "direct-cargo-bazel-deps",
@@ -110,6 +110,9 @@
110110
"compile_data_glob": [
111111
"**"
112112
],
113+
"compile_data_glob_excludes": [
114+
"**/*.rs"
115+
],
113116
"data_glob": [
114117
"**"
115118
]

0 commit comments

Comments
 (0)