Skip to content

Commit e7475ca

Browse files
committed
Add option to strip binaries
1 parent 13bded9 commit e7475ca

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use self::output_depinfo::output_depinfo;
4646
use self::unit_graph::UnitDep;
4747
pub use crate::core::compiler::unit::{Unit, UnitInterner};
4848
use crate::core::manifest::TargetSourcePath;
49-
use crate::core::profiles::{PanicStrategy, Profile};
49+
use crate::core::profiles::{PanicStrategy, Profile, Strip};
5050
use crate::core::{Edition, Feature, InternedString, PackageId, Target};
5151
use crate::util::errors::{self, CargoResult, CargoResultExt, ProcessError, VerboseError};
5252
use crate::util::machine_message::Message;
@@ -732,6 +732,7 @@ fn build_base_args(
732732
rpath,
733733
ref panic,
734734
incremental,
735+
strip,
735736
..
736737
} = unit.profile;
737738
let test = unit.mode.is_any_test();
@@ -910,6 +911,11 @@ fn build_base_args(
910911
opt(cmd, "-C", "incremental=", Some(dir));
911912
}
912913

914+
if strip != Strip::None {
915+
cmd.arg("-Z").arg(format!("strip={}", strip));
916+
}
917+
918+
913919
if unit.is_std {
914920
// -Zforce-unstable-if-unmarked prevents the accidental use of
915921
// unstable crates within the sysroot (such as "extern crate libc" or

src/cargo/core/profiles.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,14 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) {
565565
if let Some(incremental) = toml.incremental {
566566
profile.incremental = incremental;
567567
}
568+
if let Some(strip) = toml.strip {
569+
profile.strip = match strip.as_str() {
570+
"debuginfo" => Strip::DebugInfo,
571+
"none" => Strip::None,
572+
"symbols" => Strip::Symbols,
573+
_ => panic!("Unexpected strip option `{}`", strip),
574+
};
575+
}
568576
}
569577

570578
/// The root profile (dev/release).
@@ -595,6 +603,7 @@ pub struct Profile {
595603
pub rpath: bool,
596604
pub incremental: bool,
597605
pub panic: PanicStrategy,
606+
pub strip: Strip,
598607
}
599608

600609
impl Default for Profile {
@@ -611,6 +620,7 @@ impl Default for Profile {
611620
rpath: false,
612621
incremental: false,
613622
panic: PanicStrategy::Unwind,
623+
strip: Strip::None,
614624
}
615625
}
616626
}
@@ -635,6 +645,7 @@ compact_debug! {
635645
rpath
636646
incremental
637647
panic
648+
strip
638649
)]
639650
}
640651
}
@@ -721,6 +732,7 @@ impl Profile {
721732
bool,
722733
bool,
723734
PanicStrategy,
735+
Strip,
724736
) {
725737
(
726738
self.opt_level,
@@ -732,6 +744,7 @@ impl Profile {
732744
self.rpath,
733745
self.incremental,
734746
self.panic,
747+
self.strip,
735748
)
736749
}
737750
}
@@ -776,6 +789,28 @@ impl fmt::Display for PanicStrategy {
776789
}
777790
}
778791

792+
/// The setting for choosing which symbols to strip
793+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, PartialOrd, Ord, serde::Serialize)]
794+
#[serde(rename_all = "lowercase")]
795+
pub enum Strip {
796+
/// Only strip debugging symbols
797+
DebugInfo,
798+
/// Don't remove any symbols
799+
None,
800+
/// Strip all non-exported symbols from the final binary
801+
Symbols,
802+
}
803+
804+
impl fmt::Display for Strip {
805+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
806+
match *self {
807+
Strip::DebugInfo => "unwind",
808+
Strip::None => "abort",
809+
Strip::Symbols => "symbols",
810+
}
811+
.fmt(f)
812+
}
813+
}
779814
/// Flags used in creating `Unit`s to indicate the purpose for the target, and
780815
/// to ensure the target's dependencies have the correct settings.
781816
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]

src/cargo/util/toml/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ pub struct TomlProfile {
407407
pub build_override: Option<Box<TomlProfile>>,
408408
pub dir_name: Option<InternedString>,
409409
pub inherits: Option<InternedString>,
410+
pub strip: Option<InternedString>,
410411
}
411412

412413
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
@@ -522,6 +523,16 @@ impl TomlProfile {
522523
);
523524
}
524525
}
526+
527+
if let Some(strip) = &self.strip {
528+
if strip != "debuginfo" && strip != "none" && strip != "symbols" {
529+
bail!(
530+
"`strip` setting of `{}` is not a valid setting,\
531+
must be `debuginfo`, `none` or `symbols`",
532+
strip
533+
);
534+
}
535+
}
525536
Ok(())
526537
}
527538

@@ -641,6 +652,10 @@ impl TomlProfile {
641652
if let Some(v) = &profile.dir_name {
642653
self.dir_name = Some(*v);
643654
}
655+
656+
if let Some(v) = profile.strip {
657+
self.strip = Some(v);
658+
}
644659
}
645660
}
646661

0 commit comments

Comments
 (0)