Skip to content

Commit 2f3a582

Browse files
committed
feat(cli.rs): strip release binaries [TRI-031] (#22)
1 parent 153a6a4 commit 2f3a582

File tree

5 files changed

+71
-24
lines changed

5 files changed

+71
-24
lines changed

.changes/strip.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cli.rs": "patch"
3+
---
4+
5+
Automatically `strip` the built binary on Linux and macOS if `--debug` is not specified.

tooling/cli.rs/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/cli.rs/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ url = { version = "2.2", features = [ "serde" ] }
5656
[target."cfg(windows)".dependencies]
5757
encode_unicode = "0.3"
5858

59+
[target."cfg(not(windows))".dependencies]
60+
humansize = "1.1"
61+
5962
[target."cfg(target_os = \"linux\")".build-dependencies]
6063
heck = "0.4"
6164

tooling/cli.rs/schema.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -952,9 +952,6 @@
952952
"FsAllowlistConfig": {
953953
"description": "Allowlist for the file system APIs.",
954954
"type": "object",
955-
"required": [
956-
"scope"
957-
],
958955
"properties": {
959956
"all": {
960957
"description": "Use this flag to enable all file system API features.",
@@ -1003,6 +1000,9 @@
10031000
},
10041001
"scope": {
10051002
"description": "The access scope for the filesystem APIs.",
1003+
"default": [
1004+
"$APP/**"
1005+
],
10061006
"allOf": [
10071007
{
10081008
"$ref": "#/definitions/FsAllowlistScope"
@@ -1203,9 +1203,6 @@
12031203
"ProtocolAllowlistConfig": {
12041204
"description": "Allowlist for the custom protocols.",
12051205
"type": "object",
1206-
"required": [
1207-
"assetScope"
1208-
],
12091206
"properties": {
12101207
"all": {
12111208
"description": "Use this flag to enable all custom protocols.",
@@ -1219,6 +1216,9 @@
12191216
},
12201217
"assetScope": {
12211218
"description": "The access scope for the asset protocol.",
1219+
"default": [
1220+
"$APP/**"
1221+
],
12221222
"allOf": [
12231223
{
12241224
"$ref": "#/definitions/FsAllowlistScope"

tooling/cli.rs/src/build.rs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,28 +140,29 @@ pub fn command(options: Options) -> Result<()> {
140140
let out_dir = app_settings
141141
.get_out_dir(options.target.clone(), options.debug)
142142
.with_context(|| "failed to get project out directory")?;
143+
144+
let bin_name = app_settings
145+
.cargo_package_settings()
146+
.name
147+
.clone()
148+
.expect("Cargo manifest must have the `package.name` field");
149+
#[cfg(windows)]
150+
let bin_path = out_dir.join(format!("{}.exe", bin_name));
151+
#[cfg(not(windows))]
152+
let bin_path = out_dir.join(&bin_name);
153+
154+
#[cfg(unix)]
155+
if !options.debug {
156+
strip(&bin_path, &logger)?;
157+
}
158+
143159
if let Some(product_name) = config_.package.product_name.clone() {
144-
let bin_name = app_settings
145-
.cargo_package_settings()
146-
.name
147-
.clone()
148-
.expect("Cargo manifest must have the `package.name` field");
149160
#[cfg(windows)]
150-
let (bin_path, product_path) = {
151-
(
152-
out_dir.join(format!("{}.exe", bin_name)),
153-
out_dir.join(format!("{}.exe", product_name)),
154-
)
155-
};
161+
let product_path = out_dir.join(format!("{}.exe", product_name));
156162
#[cfg(target_os = "macos")]
157-
let (bin_path, product_path) = { (out_dir.join(bin_name), out_dir.join(product_name)) };
163+
let product_path = out_dir.join(product_name);
158164
#[cfg(target_os = "linux")]
159-
let (bin_path, product_path) = {
160-
(
161-
out_dir.join(bin_name),
162-
out_dir.join(product_name.to_kebab_case()),
163-
)
164-
};
165+
let product_path = out_dir.join(product_name.to_kebab_case());
165166
rename(&bin_path, &product_path).with_context(|| {
166167
format!(
167168
"failed to rename `{}` to `{}`",
@@ -308,3 +309,34 @@ fn print_signed_updater_archive(output_paths: &[PathBuf]) -> crate::Result<()> {
308309
}
309310
Ok(())
310311
}
312+
313+
// TODO: drop this when https://github.com/rust-lang/rust/issues/72110 is stabilized
314+
#[cfg(unix)]
315+
fn strip(path: &std::path::Path, logger: &Logger) -> crate::Result<()> {
316+
use humansize::{file_size_opts, FileSize};
317+
318+
let filesize_before = std::fs::metadata(&path)
319+
.with_context(|| "failed to get executable file size")?
320+
.len();
321+
322+
// Strip the binary
323+
Command::new("strip")
324+
.arg(&path)
325+
.stdout(std::process::Stdio::null())
326+
.stderr(std::process::Stdio::null())
327+
.status()
328+
.with_context(|| "failed to execute strip")?;
329+
330+
let filesize_after = std::fs::metadata(&path)
331+
.with_context(|| "failed to get executable file size")?
332+
.len();
333+
334+
logger.log(format!(
335+
"Binary stripped, size reduced by {}",
336+
(filesize_before - filesize_after)
337+
.file_size(file_size_opts::CONVENTIONAL)
338+
.unwrap(),
339+
));
340+
341+
Ok(())
342+
}

0 commit comments

Comments
 (0)