Skip to content

Commit

Permalink
feat(cli.rs): strip release binaries [TRI-031] (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Jan 9, 2022
1 parent 153a6a4 commit 2f3a582
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changes/strip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cli.rs": "patch"
---

Automatically `strip` the built binary on Linux and macOS if `--debug` is not specified.
7 changes: 7 additions & 0 deletions tooling/cli.rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tooling/cli.rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ url = { version = "2.2", features = [ "serde" ] }
[target."cfg(windows)".dependencies]
encode_unicode = "0.3"

[target."cfg(not(windows))".dependencies]
humansize = "1.1"

[target."cfg(target_os = \"linux\")".build-dependencies]
heck = "0.4"

Expand Down
12 changes: 6 additions & 6 deletions tooling/cli.rs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -952,9 +952,6 @@
"FsAllowlistConfig": {
"description": "Allowlist for the file system APIs.",
"type": "object",
"required": [
"scope"
],
"properties": {
"all": {
"description": "Use this flag to enable all file system API features.",
Expand Down Expand Up @@ -1003,6 +1000,9 @@
},
"scope": {
"description": "The access scope for the filesystem APIs.",
"default": [
"$APP/**"
],
"allOf": [
{
"$ref": "#/definitions/FsAllowlistScope"
Expand Down Expand Up @@ -1203,9 +1203,6 @@
"ProtocolAllowlistConfig": {
"description": "Allowlist for the custom protocols.",
"type": "object",
"required": [
"assetScope"
],
"properties": {
"all": {
"description": "Use this flag to enable all custom protocols.",
Expand All @@ -1219,6 +1216,9 @@
},
"assetScope": {
"description": "The access scope for the asset protocol.",
"default": [
"$APP/**"
],
"allOf": [
{
"$ref": "#/definitions/FsAllowlistScope"
Expand Down
68 changes: 50 additions & 18 deletions tooling/cli.rs/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,28 +140,29 @@ pub fn command(options: Options) -> Result<()> {
let out_dir = app_settings
.get_out_dir(options.target.clone(), options.debug)
.with_context(|| "failed to get project out directory")?;

let bin_name = app_settings
.cargo_package_settings()
.name
.clone()
.expect("Cargo manifest must have the `package.name` field");
#[cfg(windows)]
let bin_path = out_dir.join(format!("{}.exe", bin_name));
#[cfg(not(windows))]
let bin_path = out_dir.join(&bin_name);

#[cfg(unix)]
if !options.debug {
strip(&bin_path, &logger)?;
}

if let Some(product_name) = config_.package.product_name.clone() {
let bin_name = app_settings
.cargo_package_settings()
.name
.clone()
.expect("Cargo manifest must have the `package.name` field");
#[cfg(windows)]
let (bin_path, product_path) = {
(
out_dir.join(format!("{}.exe", bin_name)),
out_dir.join(format!("{}.exe", product_name)),
)
};
let product_path = out_dir.join(format!("{}.exe", product_name));
#[cfg(target_os = "macos")]
let (bin_path, product_path) = { (out_dir.join(bin_name), out_dir.join(product_name)) };
let product_path = out_dir.join(product_name);
#[cfg(target_os = "linux")]
let (bin_path, product_path) = {
(
out_dir.join(bin_name),
out_dir.join(product_name.to_kebab_case()),
)
};
let product_path = out_dir.join(product_name.to_kebab_case());
rename(&bin_path, &product_path).with_context(|| {
format!(
"failed to rename `{}` to `{}`",
Expand Down Expand Up @@ -308,3 +309,34 @@ fn print_signed_updater_archive(output_paths: &[PathBuf]) -> crate::Result<()> {
}
Ok(())
}

// TODO: drop this when https://github.com/rust-lang/rust/issues/72110 is stabilized
#[cfg(unix)]
fn strip(path: &std::path::Path, logger: &Logger) -> crate::Result<()> {
use humansize::{file_size_opts, FileSize};

let filesize_before = std::fs::metadata(&path)
.with_context(|| "failed to get executable file size")?
.len();

// Strip the binary
Command::new("strip")
.arg(&path)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.with_context(|| "failed to execute strip")?;

let filesize_after = std::fs::metadata(&path)
.with_context(|| "failed to get executable file size")?
.len();

logger.log(format!(
"Binary stripped, size reduced by {}",
(filesize_before - filesize_after)
.file_size(file_size_opts::CONVENTIONAL)
.unwrap(),
));

Ok(())
}

0 comments on commit 2f3a582

Please sign in to comment.