Skip to content

Commit 2a2e139

Browse files
committed
wat: introuduce wasm toolkits based on wasm-opt
1 parent 88ca2bc commit 2a2e139

File tree

4 files changed

+144
-109
lines changed

4 files changed

+144
-109
lines changed

src/command/build.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Implementation of the `wasm-pack build` command.
22
3-
use crate::wasm_opt;
3+
use crate::wasm_toolkit;
44
use binary_install::{Cache, Download};
55
use bindgen;
66
use build;
@@ -395,4 +395,8 @@ impl Build {
395395
)?;
396396
Ok(())
397397
}
398+
399+
fn step_run_wasm_dis(&mut self) -> Result<(), Error> {
400+
unimplemented!()
401+
}
398402
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub mod stamps;
4444
pub mod target;
4545
pub mod test;
4646
pub mod wasm_opt;
47+
pub mod wasm_toolkit;
4748

4849
use progressbar::ProgressOutput;
4950

src/wasm_opt.rs

Lines changed: 0 additions & 108 deletions
This file was deleted.

src/wasm_toolkit.rs

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/// wasm binary toolkits
2+
3+
use crate::child;
4+
use crate::emoji;
5+
use crate::target;
6+
use crate::PBAR;
7+
use binary_install::Cache;
8+
use log::debug;
9+
use std::path::{Path, PathBuf};
10+
use std::process::Command;
11+
12+
/// wasm toolkits from upstream WebAssembly/binaryen.
13+
pub struct Toolkit {
14+
/// toolkit name
15+
name: ToolkitKind,
16+
/// Command line args passed to toolkit.
17+
args: Vec<String>,
18+
}
19+
20+
pub enum ToolkitKind {
21+
/// Loads WebAssembly and runs Binaryen IR passes on it
22+
WasmOpt,
23+
/// Un-assembles WebAssembly in binary format into text format
24+
WasmDis,
25+
}
26+
27+
/// Possible results of `find_tool`
28+
enum FindExec {
29+
/// Couldn't install tool because downloads are forbidden
30+
CannotInstall,
31+
/// The current platform doesn't support precompiled binaries
32+
PlatformNotSupported,
33+
/// We found `wasm-opt` at the specified path
34+
Found(PathBuf),
35+
}
36+
37+
impl Toolkit {
38+
pub fn run(
39+
cache: &Cache,
40+
out_dir: &Path,
41+
args: &[String],
42+
install_permitted: bool,
43+
) -> Result<(), failure::Error> {
44+
let name = self.name();
45+
let wasm_opt = match self.find_exec(cache, install_permitted)? {
46+
FindExec::Found(path) => path,
47+
FindExec::CannotInstall => {
48+
PBAR.info("Skipping {} as no downloading was requested", name);
49+
return Ok(());
50+
}
51+
FindExec::PlatformNotSupported => {
52+
PBAR.info("Skipping wasm-opt because it is not supported on this platform", name);
53+
return Ok(());
54+
}
55+
};
56+
57+
PBAR.info("Optimizing wasm binaries with `{}`...", name);
58+
59+
for file in out_dir.read_dir()? {
60+
let file = file?;
61+
let path = file.path();
62+
if path.extension().and_then(|s| s.to_str()) != Some("wasm") {
63+
continue;
64+
}
65+
66+
let tmp = path.with_extension("wasm-opt.wasm");
67+
let mut cmd = Command::new(&wasm_opt);
68+
cmd.arg(&path).arg("-o").arg(&tmp).args(args);
69+
child::run(cmd, "wasm-opt")?;
70+
std::fs::rename(&tmp, &path)?;
71+
}
72+
73+
Ok(())
74+
}
75+
76+
/// Attempts to find `wasm-opt` in `PATH` locally, or failing that downloads a
77+
/// precompiled binary.
78+
///
79+
/// Returns `Some` if a binary was found or it was successfully downloaded.
80+
/// Returns `None` if a binary wasn't found in `PATH` and this platform doesn't
81+
/// have precompiled binaries. Returns an error if we failed to download the
82+
/// binary.
83+
fn find_exec(
84+
&self,
85+
cache: &Cache,
86+
install_permitted: bool,
87+
) -> Result<FindTool, failure::Error> {
88+
let tool = self.as_str();
89+
// First attempt to look up in PATH. If found assume it works.
90+
if let Ok(path) = which::which(tool) {
91+
debug!("found {} at {:?}", tool, path);
92+
return Ok(FindTool::Found(path));
93+
}
94+
95+
// ... and if that fails download a precompiled version.
96+
let target = if target::LINUX && target::x86_64 {
97+
"x86_64-linux"
98+
} else if target::MACOS && target::x86_64 {
99+
"x86_64-apple-darwin"
100+
} else if target::WINDOWS && target::x86_64 {
101+
"x86_64-windows"
102+
} else {
103+
return Ok(FindTool::PlatformNotSupported);
104+
};
105+
let url = format!(
106+
"https://github.com/WebAssembly/binaryen/releases/download/{vers}/binaryen-{vers}-{target}.tar.gz",
107+
vers = "version_78",
108+
target = target,
109+
);
110+
111+
let download =
112+
|permit_install| cache.download(permit_install, tool, &[tool], &url);
113+
114+
let dl = match download(false)? {
115+
Some(dl) => dl,
116+
None if !install_permitted => return Ok(FindTool::CannotInstall),
117+
None => {
118+
let msg = format!("{}Installing {}...", tool, emoji::DOWN_ARROW);
119+
PBAR.info(&msg);
120+
121+
match download(install_permitted)? {
122+
Some(dl) => dl,
123+
None => return Ok(FindTool::CannotInstall),
124+
}
125+
}
126+
};
127+
128+
Ok(FindTool::Found(dl.binary(tool)?))
129+
}
130+
131+
fn name(&self) -> &str {
132+
use Self::*;
133+
match self {
134+
WasmOpt => "wasm-opt",
135+
WasmDis => "wasm-dis",
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)