Skip to content

Commit 9c2d4e9

Browse files
committed
build(opt): introduce wasm-opt
1 parent 969797c commit 9c2d4e9

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/command/build.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use bindgen;
2+
use opt;
23
use build;
34
use command::utils::{create_pkg_dir, set_crate_path};
45
use emoji;
@@ -245,4 +246,18 @@ impl Build {
245246
);
246247
Ok(())
247248
}
249+
250+
fn step_run_wasm_opt(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
251+
info!(&log, "Optimizing the wasm bindings...");
252+
opt::run_wasm_opt(
253+
&self.crate_path,
254+
&self.crate_name,
255+
&self.build_config.opt_passes(),
256+
)?;
257+
info!(
258+
&log,
259+
"wasm bindings were optimized at {:#?}.",
260+
&self.crate_path.join("pkg")
261+
);
262+
}
248263
}

src/opt.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//! Functionality related to checking and running `wasm-opt`.
2+
3+
use emoji;
4+
use error::Error;
5+
use progressbar::Step;
6+
use std::path::Path;
7+
use std::process::Command;
8+
use PBAR;
9+
use manifest::BuildConfig;
10+
11+
/// Check the `wasm-opt` CLI.
12+
fn check_install_wasm_opt(step: &Step) -> Result<(), Error> {
13+
let msg = format!("{}Checking WASM-opt...", emoji::DOWN_ARROW);
14+
PBAR.step(step, &msg);
15+
// check whether `wasm-opt` is installed.
16+
let output = Command::new("command")
17+
.args(&["-v", "wasm-opt"])
18+
.output()?;
19+
if !output.status.success() {
20+
let s = String::from_utf8_lossy(&output.stderr);
21+
if s.contains("No such file") {
22+
Error::cli(
23+
"wasm-opt isn't installed. \
24+
please follow the build instruction in \
25+
https://github.com/WebAssembly/binaryen#binaryen",
26+
s
27+
)
28+
}
29+
} else {
30+
Ok(())
31+
}
32+
}
33+
34+
/// Run the `wasm-bindgen` CLI to optimize the current crate's `.wasm`.
35+
pub fn run_wasm_opt(
36+
path: &Path,
37+
name: &str,
38+
build_config: &BuildConfig,
39+
step: &Step,
40+
) -> Result<(), Error> {
41+
check_install_wasm_opt(step)?;
42+
43+
let msg = format!("{}Running WASM-opt...", emoji::RUNNER);
44+
PBAR.step(step, &msg);
45+
let binary_name = name.replace("-", "_");
46+
let release_or_debug = if debug { "debug" } else { "release" };
47+
let wasm_path = format!(
48+
"target/wasm32-unknown-unknown/{}/{}.wasm",
49+
release_or_debug, binary_name
50+
);
51+
52+
let output = Command::new("wasm-opt")
53+
.current_dir(path)
54+
.arg(&wasm_path)
55+
.args(build_config.opt_config.passes())
56+
.output()?;
57+
if !output.status.success() {
58+
let s = String::from_utf8_lossy(&output.stderr);
59+
Error::cli("wasm-opt failed to execute properly", s)
60+
} else {
61+
Ok(())
62+
}
63+
}

0 commit comments

Comments
 (0)