From 2f3a582c69994d66f2035bbe62825eafc869d90f Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Sun, 9 Jan 2022 15:46:43 -0300 Subject: [PATCH] feat(cli.rs): strip release binaries [TRI-031] (#22) --- .changes/strip.md | 5 +++ tooling/cli.rs/Cargo.lock | 7 ++++ tooling/cli.rs/Cargo.toml | 3 ++ tooling/cli.rs/schema.json | 12 +++---- tooling/cli.rs/src/build.rs | 68 +++++++++++++++++++++++++++---------- 5 files changed, 71 insertions(+), 24 deletions(-) create mode 100644 .changes/strip.md diff --git a/.changes/strip.md b/.changes/strip.md new file mode 100644 index 000000000..f49c954b1 --- /dev/null +++ b/.changes/strip.md @@ -0,0 +1,5 @@ +--- +"cli.rs": "patch" +--- + +Automatically `strip` the built binary on Linux and macOS if `--debug` is not specified. diff --git a/tooling/cli.rs/Cargo.lock b/tooling/cli.rs/Cargo.lock index cd9c6d8d4..69182de28 100644 --- a/tooling/cli.rs/Cargo.lock +++ b/tooling/cli.rs/Cargo.lock @@ -811,6 +811,12 @@ dependencies = [ "itoa 1.0.1", ] +[[package]] +name = "humansize" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02296996cb8796d7c6e3bc2d9211b7802812d36999a51bb754123ead7d37d026" + [[package]] name = "icns" version = "0.3.1" @@ -2281,6 +2287,7 @@ dependencies = [ "glob", "handlebars", "heck", + "humansize", "include_dir", "json-patch", "lazy_static", diff --git a/tooling/cli.rs/Cargo.toml b/tooling/cli.rs/Cargo.toml index d47391d6d..bf065051a 100644 --- a/tooling/cli.rs/Cargo.toml +++ b/tooling/cli.rs/Cargo.toml @@ -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" diff --git a/tooling/cli.rs/schema.json b/tooling/cli.rs/schema.json index c3ccfb3f4..ead30d093 100644 --- a/tooling/cli.rs/schema.json +++ b/tooling/cli.rs/schema.json @@ -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.", @@ -1003,6 +1000,9 @@ }, "scope": { "description": "The access scope for the filesystem APIs.", + "default": [ + "$APP/**" + ], "allOf": [ { "$ref": "#/definitions/FsAllowlistScope" @@ -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.", @@ -1219,6 +1216,9 @@ }, "assetScope": { "description": "The access scope for the asset protocol.", + "default": [ + "$APP/**" + ], "allOf": [ { "$ref": "#/definitions/FsAllowlistScope" diff --git a/tooling/cli.rs/src/build.rs b/tooling/cli.rs/src/build.rs index faa1b4a28..4c4a283a9 100644 --- a/tooling/cli.rs/src/build.rs +++ b/tooling/cli.rs/src/build.rs @@ -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 `{}`", @@ -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(()) +}