Skip to content

Commit

Permalink
feat(trim-paths): parsing in mainfest and config
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Sep 8, 2023
1 parent 6e748f8 commit 99fafbc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::core::dependency::Artifact;
use crate::core::resolver::features::FeaturesFor;
use crate::core::{PackageId, PackageIdSpec, Resolve, Shell, Target, Workspace};
use crate::util::interning::InternedString;
use crate::util::toml::ProfileTrimPaths;
use crate::util::toml::{
ProfilePackageSpec, StringOrBool, TomlDebugInfo, TomlProfile, TomlProfiles,
};
Expand Down Expand Up @@ -555,6 +556,9 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) {
if let Some(flags) = &toml.rustflags {
profile.rustflags = flags.clone();
}
if let Some(trim_paths) = &toml.trim_paths {
profile.trim_paths = trim_paths.clone();
}
profile.strip = match toml.strip {
Some(StringOrBool::Bool(true)) => Strip::Named(InternedString::new("symbols")),
None | Some(StringOrBool::Bool(false)) => Strip::None,
Expand Down Expand Up @@ -598,6 +602,9 @@ pub struct Profile {
#[serde(skip_serializing_if = "Vec::is_empty")] // remove when `rustflags` is stablized
// Note that `rustflags` is used for the cargo-feature `profile_rustflags`
pub rustflags: Vec<InternedString>,
#[serde(skip_serializing_if = "Vec::is_empty")]
// remove when `-Ztrim-paths` is stablized
pub trim_paths: Vec<ProfileTrimPaths>,
}

impl Default for Profile {
Expand All @@ -618,6 +625,7 @@ impl Default for Profile {
panic: PanicStrategy::Unwind,
strip: Strip::None,
rustflags: vec![],
trim_paths: Vec::new(),
}
}
}
Expand Down Expand Up @@ -646,6 +654,7 @@ compact_debug! {
panic
strip
rustflags
trim_paths
)]
}
}
Expand Down Expand Up @@ -712,6 +721,7 @@ impl Profile {
self.rpath,
(self.incremental, self.panic, self.strip),
&self.rustflags,
&self.trim_paths,
)
}
}
Expand Down
39 changes: 39 additions & 0 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,36 @@ pub struct TomlProfile {
// requires all non-tables to be listed first.
pub package: Option<BTreeMap<ProfilePackageSpec, TomlProfile>>,
pub build_override: Option<Box<TomlProfile>>,
/// Unstable feature `-Ztrim-paths`.
pub trim_paths: Option<Vec<ProfileTrimPaths>>,
}

#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ProfileTrimPaths {
None,
Macro,
Diagnostics,
Object,
All,
}

impl ProfileTrimPaths {
pub fn as_str(&self) -> &str {
match self {
ProfileTrimPaths::None => "none",
ProfileTrimPaths::Macro => "macro",
ProfileTrimPaths::Diagnostics => "diagnostics",
ProfileTrimPaths::Object => "object",
ProfileTrimPaths::All => "all",
}
}
}

impl fmt::Display for ProfileTrimPaths {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}

#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
Expand Down Expand Up @@ -759,6 +789,15 @@ impl TomlProfile {
_ => {}
}
}
if self.trim_paths.is_some() {
match (
features.require(Feature::trim_paths()),
cli_unstable.trim_paths,
) {
(Err(e), false) => return Err(e),
_ => {}
}
}
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,7 @@ fn all_profile_options() {
package: None,
build_override: None,
rustflags: None,
trim_paths: None,
};
let mut overrides = BTreeMap::new();
let key = cargo_toml::ProfilePackageSpec::Spec(PackageIdSpec::parse("foo").unwrap());
Expand Down

0 comments on commit 99fafbc

Please sign in to comment.