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

Remove CrateFeatures::Legacy #2338

Merged
merged 1 commit into from
Dec 27, 2023
Merged
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
2 changes: 1 addition & 1 deletion crate_universe/src/context.rs
Original file line number Diff line number Diff line change
@@ -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,
);
71 changes: 15 additions & 56 deletions crate_universe/src/context/crate_context.rs
Original file line number Diff line number Diff line change
@@ -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)]
@@ -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>>,
@@ -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 {
@@ -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(),
@@ -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
@@ -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,
);
@@ -832,7 +791,7 @@ mod test {
&annotations.metadata.packages,
&annotations.lockfile.crates,
&pairred_extras,
&annotations.features,
&annotations.crate_features,
include_binaries,
include_build_scripts,
);
@@ -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,
);
@@ -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,
);
@@ -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,
);
6 changes: 3 additions & 3 deletions crate_universe/src/metadata/metadata_annotation.rs
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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,
})
}
}
60 changes: 7 additions & 53 deletions crate_universe/src/rendering.rs
Original file line number Diff line number Diff line change
@@ -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(
@@ -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,
@@ -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;

@@ -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 {
@@ -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()