-
Notifications
You must be signed in to change notification settings - Fork 418
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
Add support for automatically executing wasm-opt
#625
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
//! Support for downloading and executing `wasm-opt` | ||
|
||
use crate::child; | ||
use crate::emoji; | ||
use crate::target; | ||
use crate::PBAR; | ||
use binary_install::Cache; | ||
use log::debug; | ||
use std::path::{Path, PathBuf}; | ||
use std::process::Command; | ||
|
||
/// Execute `wasm-opt` over wasm binaries found in `out_dir`, downloading if | ||
/// necessary into `cache`. Passes `args` to each invocation of `wasm-opt`. | ||
pub fn run( | ||
cache: &Cache, | ||
out_dir: &Path, | ||
args: &[String], | ||
install_permitted: bool, | ||
) -> Result<(), failure::Error> { | ||
let wasm_opt = match find_wasm_opt(cache, install_permitted)? { | ||
WasmOpt::Found(path) => path, | ||
WasmOpt::CannotInstall => { | ||
PBAR.info("Skipping wasm-opt as no downloading was requested"); | ||
return Ok(()); | ||
} | ||
WasmOpt::PlatformNotSupported => { | ||
PBAR.info("Skipping wasm-opt because it is not supported on this platform"); | ||
return Ok(()); | ||
} | ||
}; | ||
|
||
PBAR.info("Optimizing wasm binaries with `wasm-opt`..."); | ||
|
||
for file in out_dir.read_dir()? { | ||
let file = file?; | ||
let path = file.path(); | ||
if path.extension().and_then(|s| s.to_str()) != Some("wasm") { | ||
continue; | ||
} | ||
|
||
let tmp = path.with_extension("wasm-opt.wasm"); | ||
let mut cmd = Command::new(&wasm_opt); | ||
cmd.arg(&path).arg("-o").arg(&tmp).args(args); | ||
child::run(cmd, "wasm-opt")?; | ||
std::fs::rename(&tmp, &path)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Possible results of `find_wasm_opt` | ||
pub enum WasmOpt { | ||
/// Couldn't install wasm-opt because downloads are forbidden | ||
CannotInstall, | ||
/// The current platform doesn't support precompiled binaries | ||
PlatformNotSupported, | ||
/// We found `wasm-opt` at the specified path | ||
Found(PathBuf), | ||
} | ||
|
||
/// Attempts to find `wasm-opt` in `PATH` locally, or failing that downloads a | ||
/// precompiled binary. | ||
/// | ||
/// Returns `Some` if a binary was found or it was successfully downloaded. | ||
/// Returns `None` if a binary wasn't found in `PATH` and this platform doesn't | ||
/// have precompiled binaries. Returns an error if we failed to download the | ||
/// binary. | ||
pub fn find_wasm_opt(cache: &Cache, install_permitted: bool) -> Result<WasmOpt, failure::Error> { | ||
// First attempt to look up in PATH. If found assume it works. | ||
if let Ok(path) = which::which("wasm-opt") { | ||
debug!("found wasm-opt at {:?}", path); | ||
return Ok(WasmOpt::Found(path)); | ||
} | ||
|
||
// ... and if that fails download a precompiled version. | ||
let target = if target::LINUX && target::x86_64 { | ||
"x86_64-linux" | ||
} else if target::MACOS && target::x86_64 { | ||
"x86_64-apple-darwin" | ||
} else if target::WINDOWS && target::x86_64 { | ||
"x86_64-windows" | ||
} else { | ||
return Ok(WasmOpt::PlatformNotSupported); | ||
}; | ||
let url = format!( | ||
"https://github.com/WebAssembly/binaryen/releases/download/{vers}/binaryen-{vers}-{target}.tar.gz", | ||
vers = "version_78", | ||
target = target, | ||
); | ||
|
||
let download = |permit_install| cache.download(permit_install, "wasm-opt", &["wasm-opt"], &url); | ||
|
||
let dl = match download(false)? { | ||
Some(dl) => dl, | ||
None if !install_permitted => return Ok(WasmOpt::CannotInstall), | ||
None => { | ||
let msg = format!("{}Installing wasm-opt...", emoji::DOWN_ARROW); | ||
PBAR.info(&msg); | ||
|
||
match download(install_permitted)? { | ||
Some(dl) => dl, | ||
None => return Ok(WasmOpt::CannotInstall), | ||
} | ||
} | ||
}; | ||
|
||
Ok(WasmOpt::Found(dl.binary("wasm-opt")?)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,5 @@ mod manifest; | |
mod readme; | ||
mod test; | ||
mod utils; | ||
mod wasm_opt; | ||
mod webdriver; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is missing "size" and "speed" meta options that we planned in the original design:
Is the intention to add support for these in follow up PRs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True! I forgot to mention that here, but I actually first implemented it as a string being a space-separated list of arguments (like
wasm-opt = "-Os -some-other-pass"
), and then I read the original issue and liked your idea better.I figured though it may be best to do that as a separate PR as it should be pretty easy to add on top of this, but rather we'd just want to get the bare bones in here