Skip to content

Commit

Permalink
build(opt): introduce wasm-opt
Browse files Browse the repository at this point in the history
  • Loading branch information
csmoe committed Aug 2, 2018
1 parent 969797c commit faaf7e1
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/command/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bindgen;
use opt;
use build;
use command::utils::{create_pkg_dir, set_crate_path};
use emoji;
Expand Down Expand Up @@ -245,4 +246,18 @@ impl Build {
);
Ok(())
}

fn step_run_wasm_opt(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Optimizing the wasm bindings...");
opt::run_wasm_opt(
&self.crate_path,
&self.crate_name,
&self.build_config.opt_passes(),
)?;
info!(
&log,
"wasm bindings were optimized at {:#?}.",
&self.crate_path.join("pkg")
);
}
}
64 changes: 64 additions & 0 deletions src/opt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//! Functionality related to checking and running `wasm-opt`.
use emoji;
use error::Error;
use progressbar::Step;
use std::path::Path;
use std::process::Command;
use PBAR;
use manifest::BuildConfig;

/// Check the `wasm-opt` CLI.
fn check_install_wasm_opt(step: &Step) -> Result<(), Error> {
let msg = format!("{}Checking WASM-opt...", emoji::DOWN_ARROW);
PBAR.step(step, &msg);
// check whether `wasm-opt` is installed.
let output = Command::new("command")
.args(&["-v", "wasm-opt"])
.output()?;
if !output.status.success() {
let s = String::from_utf8_lossy(&output.stderr);
if s.contains("No such file") {
Error::cli(
"wasm-opt isn't installed. \
please follow the build instruction in \
https://github.com/WebAssembly/binaryen#binaryen",
s
)
}
} else {
Ok(())
}
}

/// Run the `wasm-bindgen` CLI to optimize the current crate's `.wasm`.
pub fn run_wasm_opt(
path: &Path,
name: &str,
build_config: &BuildConfig,
step: &Step,
) -> Result<(), Error> {
check_install_wasm_opt(step)?;

let msg = format!("{}Running WASM-opt...", emoji::RUNNER);
PBAR.step(step, &msg);
let binary_name = name.replace("-", "_");
let release_or_debug = if debug { "debug" } else { "release" };
let wasm_path = format!(
"target/wasm32-unknown-unknown/{}/{}.wasm",
release_or_debug, binary_name
);

let output = Command::new("wasm-opt")
.current_dir(path)
.arg(&wasm_path)
// FIXME(csmoe): opt_passes() needs to be supported at upper buildconfig.
.args(build_config.opt_passes())
.output()?;
if !output.status.success() {
let s = String::from_utf8_lossy(&output.stderr);
Error::cli("wasm-opt failed to execute properly", s)
} else {
Ok(())
}
}

0 comments on commit faaf7e1

Please sign in to comment.