|
| 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 | + // FIXME(csmoe): opt_passes() needs to be supported at upper buildconfig. |
| 56 | + .args(build_config.opt_passes()) |
| 57 | + .output()?; |
| 58 | + if !output.status.success() { |
| 59 | + let s = String::from_utf8_lossy(&output.stderr); |
| 60 | + Error::cli("wasm-opt failed to execute properly", s) |
| 61 | + } else { |
| 62 | + Ok(()) |
| 63 | + } |
| 64 | +} |
0 commit comments