Skip to content

Commit

Permalink
Removed CrateFeatures::Legacy.
Browse files Browse the repository at this point in the history
  • Loading branch information
rickvanprim committed Dec 27, 2023
1 parent fed7e8f commit a63ffe4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 113 deletions.
2 changes: 1 addition & 1 deletion crate_universe/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Context {
&annotations.metadata.packages,
&annotations.lockfile.crates,
&annotations.pairred_extras,
&annotations.features,
&annotations.crate_features,
annotations.config.generate_binaries,
annotations.config.generate_build_scripts,
);
Expand Down
71 changes: 15 additions & 56 deletions crate_universe/src/context/crate_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,41 +59,6 @@ pub enum Rule {
BuildScript(TargetAttributes),
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CrateFeatures {
// Not populated going forward. This just exists for backward compatiblity
// with old lock files.
LegacySet(BTreeSet<String>),

/// Map from target triplet to set of features.
SelectSet(Select<BTreeSet<String>>),
}

impl Default for CrateFeatures {
fn default() -> Self {
CrateFeatures::SelectSet(Default::default())
}
}

impl From<&CrateFeatures> for Select<BTreeSet<String>> {
fn from(value: &CrateFeatures) -> Self {
match value {
CrateFeatures::LegacySet(s) => Select::from_value(s.clone()),
CrateFeatures::SelectSet(sl) => sl.clone(),
}
}
}

impl CrateFeatures {
pub fn is_empty(&self) -> bool {
match self {
CrateFeatures::LegacySet(s) => s.is_empty(),
CrateFeatures::SelectSet(sl) => sl.is_empty(),
}
}
}

/// A set of attributes common to most `rust_library`, `rust_proc_macro`, and other
/// [core rules of `rules_rust`](https://bazelbuild.github.io/rules_rust/defs.html).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand All @@ -105,8 +70,8 @@ pub struct CommonAttributes {
#[serde(skip_serializing_if = "BTreeSet::is_empty")]
pub compile_data_glob: BTreeSet<String>,

#[serde(skip_serializing_if = "CrateFeatures::is_empty")]
pub crate_features: CrateFeatures,
#[serde(skip_serializing_if = "Select::is_empty")]
pub crate_features: Select<BTreeSet<String>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub data: Select<BTreeSet<String>>,
Expand Down Expand Up @@ -332,7 +297,7 @@ impl CrateContext {
packages: &BTreeMap<PackageId, Package>,
source_annotations: &BTreeMap<PackageId, SourceAnnotation>,
extras: &BTreeMap<CrateId, PairedExtras>,
features: &BTreeMap<CrateId, Select<BTreeSet<String>>>,
crate_features: &BTreeMap<CrateId, Select<BTreeSet<String>>>,
include_binaries: bool,
include_build_scripts: bool,
) -> Self {
Expand Down Expand Up @@ -366,9 +331,11 @@ impl CrateContext {

// Gather all "common" attributes
let mut common_attrs = CommonAttributes {
crate_features: CrateFeatures::SelectSet(
features.get(&current_crate_id).cloned().unwrap_or_default(),
),
crate_features: crate_features
.get(&current_crate_id)
.cloned()
.unwrap_or_default(),

deps,
deps_dev,
edition: package.edition.as_str().to_string(),
Expand Down Expand Up @@ -515,16 +482,8 @@ impl CrateContext {

// Crate features
if let Some(extra) = &crate_extra.crate_features {
match &mut self.common_attrs.crate_features {
CrateFeatures::LegacySet(s) => s.extend(
extra
.items()
.into_iter()
.filter(|(configuration, _)| configuration.is_none())
.map(|(_, value)| value),
),
CrateFeatures::SelectSet(sl) => *sl = Select::merge(sl.clone(), extra.clone()),
}
self.common_attrs.crate_features =
Select::merge(self.common_attrs.crate_features, extra.clone());
}

// Data
Expand Down Expand Up @@ -786,7 +745,7 @@ mod test {
&annotations.metadata.packages,
&annotations.lockfile.crates,
&annotations.pairred_extras,
&annotations.features,
&annotations.crate_features,
include_binaries,
include_build_scripts,
);
Expand Down Expand Up @@ -832,7 +791,7 @@ mod test {
&annotations.metadata.packages,
&annotations.lockfile.crates,
&pairred_extras,
&annotations.features,
&annotations.crate_features,
include_binaries,
include_build_scripts,
);
Expand Down Expand Up @@ -895,7 +854,7 @@ mod test {
&annotations.metadata.packages,
&annotations.lockfile.crates,
&annotations.pairred_extras,
&annotations.features,
&annotations.crate_features,
include_binaries,
include_build_scripts,
);
Expand Down Expand Up @@ -940,7 +899,7 @@ mod test {
&annotations.metadata.packages,
&annotations.lockfile.crates,
&annotations.pairred_extras,
&annotations.features,
&annotations.crate_features,
include_binaries,
include_build_scripts,
);
Expand Down Expand Up @@ -975,7 +934,7 @@ mod test {
&annotations.metadata.packages,
&annotations.lockfile.crates,
&annotations.pairred_extras,
&annotations.features,
&annotations.crate_features,
include_binaries,
include_build_scripts,
);
Expand Down
6 changes: 3 additions & 3 deletions crate_universe/src/metadata/metadata_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ pub struct Annotations {
pub pairred_extras: BTreeMap<CrateId, PairedExtras>,

/// Feature set for each target triplet and crate.
pub features: BTreeMap<CrateId, Select<BTreeSet<String>>>,
pub crate_features: BTreeMap<CrateId, Select<BTreeSet<String>>>,
}

impl Annotations {
Expand Down Expand Up @@ -421,15 +421,15 @@ impl Annotations {
);
}

let features = metadata_annotation.workspace_metadata.features.clone();
let crate_features = metadata_annotation.workspace_metadata.features.clone();

// Annotate metadata
Ok(Annotations {
metadata: metadata_annotation,
lockfile: lockfile_annotation,
config,
pairred_extras,
features,
crate_features,
})
}
}
Expand Down
60 changes: 7 additions & 53 deletions crate_universe/src/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,7 @@ impl Renderer {
.map(|attrs| attrs.compile_data.clone())
.unwrap_or_default(),
),
crate_features: SelectSet::new(
Select::<BTreeSet<String>>::from(&krate.common_attrs.crate_features),
platforms,
),
crate_features: SelectSet::new(krate.common_attrs.crate_features.clone(), platforms),
crate_name: utils::sanitize_module_name(&target.crate_name),
crate_root: target.crate_root.clone(),
data: make_data(
Expand Down Expand Up @@ -597,10 +594,7 @@ impl Renderer {
krate.common_attrs.compile_data_glob.clone(),
krate.common_attrs.compile_data.clone(),
),
crate_features: SelectSet::new(
Select::<BTreeSet<String>>::from(&krate.common_attrs.crate_features),
platforms,
),
crate_features: SelectSet::new(krate.common_attrs.crate_features.clone(), platforms),
crate_root: target.crate_root.clone(),
data: make_data(
platforms,
Expand Down Expand Up @@ -855,9 +849,7 @@ mod test {

use crate::config::{Config, CrateId, VendorMode};
use crate::context::crate_context::{CrateContext, Rule};
use crate::context::{
BuildScriptAttributes, CommonAttributes, Context, CrateFeatures, TargetAttributes,
};
use crate::context::{BuildScriptAttributes, CommonAttributes, Context, TargetAttributes};
use crate::metadata::Annotations;
use crate::test;

Expand Down Expand Up @@ -1267,44 +1259,6 @@ mod test {
);
}

#[test]
fn legacy_crate_features() {
let mut context = Context::default();
let crate_id = CrateId::new("mock_crate".to_owned(), "0.1.0".to_owned());
context.crates.insert(
crate_id.clone(),
CrateContext {
name: crate_id.name,
version: crate_id.version,
targets: BTreeSet::from([Rule::Library(mock_target_attributes())]),
common_attrs: CommonAttributes {
crate_features: CrateFeatures::LegacySet(BTreeSet::from([
"foo".to_owned(),
"bar".to_owned(),
])),
..CommonAttributes::default()
},
..CrateContext::default()
},
);

let renderer = Renderer::new(mock_render_config(None), mock_supported_platform_triples());
let output = renderer.render(&context).unwrap();

let build_file_content = output
.get(&PathBuf::from("BUILD.mock_crate-0.1.0.bazel"))
.unwrap();
let expected = indoc! {r#"
crate_features = [
"bar",
"foo",
],
"#};
assert!(build_file_content
.replace(' ', "")
.contains(&expected.replace(' ', "")));
}

#[test]
fn crate_features_by_target() {
let mut context = Context {
Expand All @@ -1315,17 +1269,17 @@ mod test {
..Context::default()
};
let crate_id = CrateId::new("mock_crate".to_owned(), "0.1.0".to_owned());
let mut features: Select<BTreeSet<String>> = Select::default();
features.insert("foo".to_owned(), Some("aarch64-apple-darwin".to_owned()));
features.insert("bar".to_owned(), None);
let mut crate_features: Select<BTreeSet<String>> = Select::default();
crate_features.insert("foo".to_owned(), Some("aarch64-apple-darwin".to_owned()));
crate_features.insert("bar".to_owned(), None);
context.crates.insert(
crate_id.clone(),
CrateContext {
name: crate_id.name,
version: crate_id.version,
targets: BTreeSet::from([Rule::Library(mock_target_attributes())]),
common_attrs: CommonAttributes {
crate_features: CrateFeatures::SelectSet(features),
crate_features,
..CommonAttributes::default()
},
..CrateContext::default()
Expand Down

0 comments on commit a63ffe4

Please sign in to comment.