Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Introduce wasm-opt #245

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(())
}
}