Skip to content

Commit ab64d13

Browse files
committed
Auto merge of #9153 - calavera:strip_options, r=ehuss
Allow `true` and `false` as options for `strip` option This follows the convention of `lto` and `debug` that allow `true` for the highest level, and `false` for disabled. Signed-off-by: David Calavera <[email protected]>
2 parents 46bac2d + 0608fcd commit ab64d13

File tree

5 files changed

+106
-50
lines changed

5 files changed

+106
-50
lines changed

src/cargo/core/profiles.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,7 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) {
552552
}
553553
match toml.lto {
554554
Some(StringOrBool::Bool(b)) => profile.lto = Lto::Bool(b),
555-
Some(StringOrBool::String(ref n)) if matches!(n.as_str(), "off" | "n" | "no") => {
556-
profile.lto = Lto::Off
557-
}
555+
Some(StringOrBool::String(ref n)) if is_off(n.as_str()) => profile.lto = Lto::Off,
558556
Some(StringOrBool::String(ref n)) => profile.lto = Lto::Named(InternedString::new(n)),
559557
None => {}
560558
}
@@ -590,9 +588,12 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) {
590588
if let Some(incremental) = toml.incremental {
591589
profile.incremental = incremental;
592590
}
593-
if let Some(strip) = toml.strip {
594-
profile.strip = strip;
595-
}
591+
profile.strip = match toml.strip {
592+
Some(StringOrBool::Bool(true)) => Strip::Named(InternedString::new("symbols")),
593+
None | Some(StringOrBool::Bool(false)) => Strip::None,
594+
Some(StringOrBool::String(ref n)) if is_off(n.as_str()) => Strip::None,
595+
Some(StringOrBool::String(ref n)) => Strip::Named(InternedString::new(n)),
596+
};
596597
}
597598

598599
/// The root profile (dev/release).
@@ -809,24 +810,22 @@ impl fmt::Display for PanicStrategy {
809810
)]
810811
#[serde(rename_all = "lowercase")]
811812
pub enum Strip {
812-
/// Only strip debugging symbols
813-
DebugInfo,
814813
/// Don't remove any symbols
815814
None,
816-
/// Strip all non-exported symbols from the final binary
817-
Symbols,
815+
/// Named Strip settings
816+
Named(InternedString),
818817
}
819818

820819
impl fmt::Display for Strip {
821820
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
822821
match *self {
823-
Strip::DebugInfo => "debuginfo",
824822
Strip::None => "none",
825-
Strip::Symbols => "symbols",
823+
Strip::Named(s) => s.as_str(),
826824
}
827825
.fmt(f)
828826
}
829827
}
828+
830829
/// Flags used in creating `Unit`s to indicate the purpose for the target, and
831830
/// to ensure the target's dependencies have the correct settings.
832831
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
@@ -1249,3 +1248,8 @@ fn validate_packages_unmatched(
12491248
}
12501249
Ok(())
12511250
}
1251+
1252+
/// Returns `true` if a string is a toggle that turns an option off.
1253+
fn is_off(s: &str) -> bool {
1254+
matches!(s, "off" | "n" | "no" | "none")
1255+
}

src/cargo/util/toml/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use url::Url;
1616
use crate::core::dependency::DepKind;
1717
use crate::core::manifest::{ManifestMetadata, TargetSourcePath, Warnings};
1818
use crate::core::nightly_features_allowed;
19-
use crate::core::profiles::Strip;
2019
use crate::core::resolver::ResolveBehavior;
2120
use crate::core::{Dependency, Manifest, PackageId, Summary, Target};
2221
use crate::core::{Edition, EitherManifest, Feature, Features, VirtualManifest, Workspace};
@@ -442,7 +441,7 @@ pub struct TomlProfile {
442441
pub build_override: Option<Box<TomlProfile>>,
443442
pub dir_name: Option<InternedString>,
444443
pub inherits: Option<InternedString>,
445-
pub strip: Option<Strip>,
444+
pub strip: Option<StringOrBool>,
446445
}
447446

448447
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
@@ -686,8 +685,8 @@ impl TomlProfile {
686685
self.dir_name = Some(*v);
687686
}
688687

689-
if let Some(v) = profile.strip {
690-
self.strip = Some(v);
688+
if let Some(v) = &profile.strip {
689+
self.strip = Some(v.clone());
691690
}
692691
}
693692
}

src/doc/src/reference/unstable.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -765,8 +765,11 @@ cargo-features = ["strip"]
765765
strip = "debuginfo"
766766
```
767767

768-
Other possible values of `strip` are `none` and `symbols`. The default is
769-
`none`.
768+
Other possible string values of `strip` are `none`, `symbols`, and `off`. The default is `none`.
769+
770+
You can also configure this option with the two absolute boolean values
771+
`true` and `false`. The former enables `strip` at its higher level, `symbols`,
772+
whilst the later disables `strip` completely.
770773

771774
### rustdoc-map
772775
* Tracking Issue: [#8296](https://github.com/rust-lang/cargo/issues/8296)

tests/testsuite/config.rs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Tests for config settings.
22
3-
use cargo::core::profiles::Strip;
43
use cargo::core::{enable_nightly_features, Shell};
54
use cargo::util::config::{self, Config, SslVersionConfig, StringList};
65
use cargo::util::interning::InternedString;
@@ -1446,7 +1445,7 @@ fn string_list_advanced_env() {
14461445
}
14471446

14481447
#[cargo_test]
1449-
fn parse_enum() {
1448+
fn parse_strip_with_string() {
14501449
write_config(
14511450
"\
14521451
[profile.release]
@@ -1458,28 +1457,5 @@ strip = 'debuginfo'
14581457

14591458
let p: toml::TomlProfile = config.get("profile.release").unwrap();
14601459
let strip = p.strip.unwrap();
1461-
assert_eq!(strip, Strip::DebugInfo);
1462-
}
1463-
1464-
#[cargo_test]
1465-
fn parse_enum_fail() {
1466-
write_config(
1467-
"\
1468-
[profile.release]
1469-
strip = 'invalid'
1470-
",
1471-
);
1472-
1473-
let config = new_config();
1474-
1475-
assert_error(
1476-
config
1477-
.get::<toml::TomlProfile>("profile.release")
1478-
.unwrap_err(),
1479-
"\
1480-
error in [..]/.cargo/config: could not load config key `profile.release.strip`
1481-
1482-
Caused by:
1483-
unknown variant `invalid`, expected one of `debuginfo`, `none`, `symbols`",
1484-
);
1460+
assert_eq!(strip, toml::StringOrBool::String("debuginfo".to_string()));
14851461
}

tests/testsuite/profiles.rs

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,11 @@ fn strip_works() {
509509

510510
#[cargo_test]
511511
fn strip_requires_cargo_feature() {
512+
if !is_nightly() {
513+
// -Zstrip is unstable
514+
return;
515+
}
516+
512517
let p = project()
513518
.file(
514519
"Cargo.toml",
@@ -541,7 +546,12 @@ Caused by:
541546
}
542547

543548
#[cargo_test]
544-
fn strip_rejects_invalid_option() {
549+
fn strip_passes_unknown_option_to_rustc() {
550+
if !is_nightly() {
551+
// -Zstrip is unstable
552+
return;
553+
}
554+
545555
let p = project()
546556
.file(
547557
"Cargo.toml",
@@ -553,7 +563,7 @@ fn strip_rejects_invalid_option() {
553563
version = "0.1.0"
554564
555565
[profile.release]
556-
strip = 'wrong'
566+
strip = 'unknown'
557567
"#,
558568
)
559569
.file("src/main.rs", "fn main() {}")
@@ -562,13 +572,77 @@ fn strip_rejects_invalid_option() {
562572
p.cargo("build --release -v")
563573
.masquerade_as_nightly_cargo()
564574
.with_status(101)
565-
.with_stderr(
575+
.with_stderr_contains(
566576
"\
567-
[ERROR] failed to parse manifest at `[CWD]/Cargo.toml`
577+
[COMPILING] foo [..]
578+
[RUNNING] `rustc [..] -Z strip=unknown [..]`
579+
error: incorrect value `unknown` for debugging option `strip` - either `none`, `debuginfo`, or `symbols` was expected
580+
",
581+
)
582+
.run();
583+
}
568584

569-
Caused by:
570-
unknown variant `wrong`, expected one of `debuginfo`, `none`, `symbols` for key [..]
585+
#[cargo_test]
586+
fn strip_accepts_true_to_strip_symbols() {
587+
if !is_nightly() {
588+
// -Zstrip is unstable
589+
return;
590+
}
591+
592+
let p = project()
593+
.file(
594+
"Cargo.toml",
595+
r#"
596+
cargo-features = ["strip"]
597+
598+
[package]
599+
name = "foo"
600+
version = "0.1.0"
601+
602+
[profile.release]
603+
strip = true
604+
"#,
605+
)
606+
.file("src/main.rs", "fn main() {}")
607+
.build();
608+
609+
p.cargo("build --release -v")
610+
.masquerade_as_nightly_cargo()
611+
.with_stderr(
612+
"\
613+
[COMPILING] foo [..]
614+
[RUNNING] `rustc [..] -Z strip=symbols [..]`
615+
[FINISHED] [..]
571616
",
572617
)
573618
.run();
574619
}
620+
621+
#[cargo_test]
622+
fn strip_accepts_false_to_disable_strip() {
623+
if !is_nightly() {
624+
// -Zstrip is unstable
625+
return;
626+
}
627+
let p = project()
628+
.file(
629+
"Cargo.toml",
630+
r#"
631+
cargo-features = ["strip"]
632+
633+
[package]
634+
name = "foo"
635+
version = "0.1.0"
636+
637+
[profile.release]
638+
strip = false
639+
"#,
640+
)
641+
.file("src/main.rs", "fn main() {}")
642+
.build();
643+
644+
p.cargo("build --release -v")
645+
.masquerade_as_nightly_cargo()
646+
.with_stderr_does_not_contain("-Z strip")
647+
.run();
648+
}

0 commit comments

Comments
 (0)