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

Add option to strip binaries #8246

Merged
merged 9 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 6 additions & 1 deletion src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use self::output_depinfo::output_depinfo;
use self::unit_graph::UnitDep;
pub use crate::core::compiler::unit::{Unit, UnitInterner};
use crate::core::manifest::TargetSourcePath;
use crate::core::profiles::{PanicStrategy, Profile};
use crate::core::profiles::{PanicStrategy, Profile, Strip};
use crate::core::{Edition, Feature, InternedString, PackageId, Target};
use crate::util::errors::{self, CargoResult, CargoResultExt, ProcessError, VerboseError};
use crate::util::machine_message::Message;
Expand Down Expand Up @@ -732,6 +732,7 @@ fn build_base_args(
rpath,
ref panic,
incremental,
strip,
..
} = unit.profile;
let test = unit.mode.is_any_test();
Expand Down Expand Up @@ -910,6 +911,10 @@ fn build_base_args(
opt(cmd, "-C", "incremental=", Some(dir));
}

if strip != Strip::None {
cmd.arg("-Z").arg(format!("strip={}", strip));
}

if unit.is_std {
// -Zforce-unstable-if-unmarked prevents the accidental use of
// unstable crates within the sysroot (such as "extern crate libc" or
Expand Down
35 changes: 35 additions & 0 deletions src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,14 @@ 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 = match strip.as_str() {
"debuginfo" => Strip::DebugInfo,
"none" => Strip::None,
"symbols" => Strip::Symbols,
_ => panic!("Unexpected strip option `{}`", strip),
};
}
}

/// The root profile (dev/release).
Expand Down Expand Up @@ -595,6 +603,7 @@ pub struct Profile {
pub rpath: bool,
pub incremental: bool,
pub panic: PanicStrategy,
pub strip: Strip,
}

impl Default for Profile {
Expand All @@ -611,6 +620,7 @@ impl Default for Profile {
rpath: false,
incremental: false,
panic: PanicStrategy::Unwind,
strip: Strip::None,
}
}
}
Expand All @@ -635,6 +645,7 @@ compact_debug! {
rpath
incremental
panic
strip
)]
}
}
Expand Down Expand Up @@ -721,6 +732,7 @@ impl Profile {
bool,
bool,
PanicStrategy,
Strip,
) {
(
self.opt_level,
Expand All @@ -732,6 +744,7 @@ impl Profile {
self.rpath,
self.incremental,
self.panic,
self.strip,
)
}
}
Expand Down Expand Up @@ -776,6 +789,28 @@ impl fmt::Display for PanicStrategy {
}
}

/// The setting for choosing which symbols to strip
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, PartialOrd, Ord, serde::Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Strip {
/// Only strip debugging symbols
DebugInfo,
/// Don't remove any symbols
None,
/// Strip all non-exported symbols from the final binary
Symbols,
}

impl fmt::Display for Strip {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Strip::DebugInfo => "unwind",
Strip::None => "abort",
Strip::Symbols => "symbols",
GabrielMajeri marked this conversation as resolved.
Show resolved Hide resolved
}
.fmt(f)
}
}
/// Flags used in creating `Unit`s to indicate the purpose for the target, and
/// to ensure the target's dependencies have the correct settings.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
Expand Down
15 changes: 15 additions & 0 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ pub struct TomlProfile {
pub build_override: Option<Box<TomlProfile>>,
pub dir_name: Option<InternedString>,
pub inherits: Option<InternedString>,
pub strip: Option<InternedString>,
GabrielMajeri marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
Expand Down Expand Up @@ -522,6 +523,16 @@ impl TomlProfile {
);
}
}

if let Some(strip) = &self.strip {
if strip != "debuginfo" && strip != "none" && strip != "symbols" {
bail!(
"`strip` setting of `{}` is not a valid setting,\
must be `debuginfo`, `none` or `symbols`",
strip
);
}
}
Ok(())
}

Expand Down Expand Up @@ -641,6 +652,10 @@ impl TomlProfile {
if let Some(v) = &profile.dir_name {
self.dir_name = Some(*v);
}

if let Some(v) = profile.strip {
self.strip = Some(v);
}
}
}

Expand Down
12 changes: 8 additions & 4 deletions tests/testsuite/unit_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ fn simple() {
"overflow_checks": true,
"rpath": false,
"incremental": false,
"panic": "unwind"
"panic": "unwind",
"strip": "none"
},
"platform": null,
"mode": "build",
Expand Down Expand Up @@ -115,7 +116,8 @@ fn simple() {
"overflow_checks": true,
"rpath": false,
"incremental": false,
"panic": "unwind"
"panic": "unwind",
"strip": "none"
},
"platform": null,
"mode": "build",
Expand Down Expand Up @@ -155,7 +157,8 @@ fn simple() {
"overflow_checks": true,
"rpath": false,
"incremental": false,
"panic": "unwind"
"panic": "unwind",
"strip": "none"
},
"platform": null,
"mode": "build",
Expand Down Expand Up @@ -188,7 +191,8 @@ fn simple() {
"overflow_checks": true,
"rpath": false,
"incremental": false,
"panic": "unwind"
"panic": "unwind",
"strip": "none"
},
"platform": null,
"mode": "build",
Expand Down