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

Allow true and false as options for strip option #9153

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
18 changes: 15 additions & 3 deletions src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,21 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) {
if let Some(incremental) = toml.incremental {
profile.incremental = incremental;
}
if let Some(strip) = toml.strip {
profile.strip = strip;
}
profile.strip = match toml.strip {
Some(StringOrBool::Bool(enabled)) if enabled => Strip::Symbols,
Some(StringOrBool::Bool(enabled)) if !enabled => Strip::None,
Some(StringOrBool::String(ref n)) if matches!(n.as_str(), "off" | "n" | "no" | "none") => {
Strip::None
}
Some(StringOrBool::String(ref n)) if matches!(n.as_str(), "debuginfo") => Strip::DebugInfo,
Some(StringOrBool::String(ref n)) if matches!(n.as_str(), "symbols") => Strip::Symbols,
None => Strip::None,
Some(ref strip) => panic!(
"unknown variant `{}`, expected one of `debuginfo`, `none`, `symbols` for key `strip`
",
strip
),
calavera marked this conversation as resolved.
Show resolved Hide resolved
};
}

/// The root profile (dev/release).
Expand Down
28 changes: 24 additions & 4 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use url::Url;
use crate::core::dependency::DepKind;
use crate::core::manifest::{ManifestMetadata, TargetSourcePath, Warnings};
use crate::core::nightly_features_allowed;
use crate::core::profiles::Strip;
use crate::core::resolver::ResolveBehavior;
use crate::core::{Dependency, Manifest, PackageId, Summary, Target};
use crate::core::{Edition, EitherManifest, Feature, Features, VirtualManifest, Workspace};
Expand Down Expand Up @@ -442,7 +441,7 @@ pub struct TomlProfile {
pub build_override: Option<Box<TomlProfile>>,
pub dir_name: Option<InternedString>,
pub inherits: Option<InternedString>,
pub strip: Option<Strip>,
pub strip: Option<StringOrBool>,
}

#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
Expand Down Expand Up @@ -561,6 +560,18 @@ impl TomlProfile {

if self.strip.is_some() {
features.require(Feature::strip())?;
match self.strip {
Some(StringOrBool::Bool(_)) => {}
Some(StringOrBool::String(ref n)) => match n.as_str() {
"off" | "n" | "none" | "no" | "debuginfo" | "symbols" => {}
calavera marked this conversation as resolved.
Show resolved Hide resolved
_ => bail!(
"`strip` setting of `{}` is not a valid setting,\
must be `symbols`, `debuginfo`, `none`, `true`, or `false`",
n
),
},
None => {}
}
}
Ok(())
}
Expand Down Expand Up @@ -686,8 +697,8 @@ impl TomlProfile {
self.dir_name = Some(*v);
}

if let Some(v) = profile.strip {
self.strip = Some(v);
if let Some(v) = &profile.strip {
self.strip = Some(v.clone());
}
}
}
Expand Down Expand Up @@ -769,6 +780,15 @@ impl<'de> de::Deserialize<'de> for StringOrBool {
}
}

impl fmt::Display for StringOrBool {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::String(s) => write!(f, "{}", &s),
Self::Bool(b) => write!(f, "{}", b),
}
}
}

#[derive(PartialEq, Clone, Debug, Serialize)]
#[serde(untagged)]
pub enum VecStringOrBool {
Expand Down
7 changes: 5 additions & 2 deletions src/doc/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,11 @@ cargo-features = ["strip"]
strip = "debuginfo"
```

Other possible values of `strip` are `none` and `symbols`. The default is
`none`.
Other possible string values of `strip` are `none`, `symbols`, and `off`. The default is `none`.

You can also configure this option with the two absolute boolean values
`true` and `false`. The former enables `strip` at its higher level, `symbols`,
whilst the later disables `strip` completely.

### rustdoc-map
* Tracking Issue: [#8296](https://github.com/rust-lang/cargo/issues/8296)
Expand Down
28 changes: 2 additions & 26 deletions tests/testsuite/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Tests for config settings.

use cargo::core::profiles::Strip;
use cargo::core::{enable_nightly_features, Shell};
use cargo::util::config::{self, Config, SslVersionConfig, StringList};
use cargo::util::interning::InternedString;
Expand Down Expand Up @@ -1446,7 +1445,7 @@ fn string_list_advanced_env() {
}

#[cargo_test]
fn parse_enum() {
fn parse_strip_with_string() {
write_config(
"\
[profile.release]
Expand All @@ -1458,28 +1457,5 @@ strip = 'debuginfo'

let p: toml::TomlProfile = config.get("profile.release").unwrap();
let strip = p.strip.unwrap();
assert_eq!(strip, Strip::DebugInfo);
}

#[cargo_test]
fn parse_enum_fail() {
write_config(
"\
[profile.release]
strip = 'invalid'
",
);

let config = new_config();

assert_error(
config
.get::<toml::TomlProfile>("profile.release")
.unwrap_err(),
"\
error in [..]/.cargo/config: could not load config key `profile.release.strip`

Caused by:
unknown variant `invalid`, expected one of `debuginfo`, `none`, `symbols`",
);
assert_eq!(strip, toml::StringOrBool::String("debuginfo".to_string()));
}
84 changes: 83 additions & 1 deletion tests/testsuite/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,11 @@ fn strip_works() {

#[cargo_test]
fn strip_requires_cargo_feature() {
if !is_nightly() {
// -Zstrip is unstable
return;
}

let p = project()
.file(
"Cargo.toml",
Expand Down Expand Up @@ -542,6 +547,11 @@ Caused by:

#[cargo_test]
fn strip_rejects_invalid_option() {
if !is_nightly() {
// -Zstrip is unstable
return;
}

let p = project()
.file(
"Cargo.toml",
Expand All @@ -567,7 +577,79 @@ fn strip_rejects_invalid_option() {
[ERROR] failed to parse manifest at `[CWD]/Cargo.toml`

Caused by:
unknown variant `wrong`, expected one of `debuginfo`, `none`, `symbols` for key [..]
unknown variant `wrong`, expected one of `debuginfo`, `none`, `symbols` for key `strip`
",
)
.run();
}

#[cargo_test]
fn strip_accepts_true_to_strip_symbols() {
if !is_nightly() {
// -Zstrip is unstable
return;
}

let p = project()
.file(
"Cargo.toml",
r#"
cargo-features = ["strip"]

[package]
name = "foo"
version = "0.1.0"

[profile.release]
strip = true
"#,
)
.file("src/main.rs", "fn main() {}")
.build();

p.cargo("build --release -v")
.masquerade_as_nightly_cargo()
.with_stderr(
"\
[COMPILING] foo [..]
[RUNNING] `rustc [..] -Z strip=symbols [..]`
[FINISHED] [..]
",
)
.run();
}

#[cargo_test]
fn strip_accepts_false_to_disable_strip() {
if !is_nightly() {
// -Zstrip is unstable
return;
}
let p = project()
.file(
"Cargo.toml",
r#"
cargo-features = ["strip"]

[package]
name = "foo"
version = "0.1.0"

[profile.release]
strip = false
"#,
)
.file("src/main.rs", "fn main() {}")
.build();

p.cargo("build --release -v")
.masquerade_as_nightly_cargo()
.with_stderr(
"\
[COMPILING] foo [..]
[RUNNING] `rustc [..] -Z strip=none
[..]`
[FINISHED] [..]
",
)
.run();
Expand Down