Skip to content

Commit 203a5f2

Browse files
Merge pull request #687 from rustwasm/refactor-opt
refactor(tool): wasm-opt is a tool variant
2 parents fb22e60 + f0f0580 commit 203a5f2

File tree

9 files changed

+147
-104
lines changed

9 files changed

+147
-104
lines changed

src/bindgen.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
//! Functionality related to running `wasm-bindgen`.
22
3-
use binary_install::Download;
43
use child;
54
use command::build::{BuildProfile, Target};
65
use failure::{self, ResultExt};
7-
use install;
6+
use install::{self, Tool};
87
use manifest::CrateData;
98
use semver;
109
use std::path::{Path, PathBuf};
@@ -14,7 +13,7 @@ use std::process::Command;
1413
/// `.wasm`.
1514
pub fn wasm_bindgen_build(
1615
data: &CrateData,
17-
bindgen: &Download,
16+
install_status: &install::Status,
1817
out_dir: &Path,
1918
out_name: &Option<String>,
2019
disable_dts: bool,
@@ -40,7 +39,8 @@ pub fn wasm_bindgen_build(
4039
} else {
4140
"--typescript"
4241
};
43-
let bindgen_path = bindgen.binary("wasm-bindgen")?;
42+
let bindgen_path = install::get_tool_path(install_status, Tool::WasmBindgen)?
43+
.binary(&Tool::WasmBindgen.to_string())?;
4444

4545
let mut cmd = Command::new(&bindgen_path);
4646
cmd.arg(&wasm_path)
@@ -49,7 +49,7 @@ pub fn wasm_bindgen_build(
4949
.arg(dts_arg);
5050

5151
let target_arg = build_target_arg(target, &bindgen_path)?;
52-
if supports_dash_dash_target(&bindgen_path)? {
52+
if supports_dash_dash_target(bindgen_path.to_path_buf())? {
5353
cmd.arg("--target").arg(target_arg);
5454
} else {
5555
cmd.arg(target_arg);
@@ -85,17 +85,17 @@ fn supports_web_target(cli_path: &PathBuf) -> Result<bool, failure::Error> {
8585
}
8686

8787
/// Check if the `wasm-bindgen` dependency is locally satisfied for the --target flag
88-
fn supports_dash_dash_target(cli_path: &PathBuf) -> Result<bool, failure::Error> {
88+
fn supports_dash_dash_target(cli_path: PathBuf) -> Result<bool, failure::Error> {
8989
let cli_version = semver::Version::parse(&install::get_cli_version(
9090
&install::Tool::WasmBindgen,
91-
cli_path,
91+
&cli_path,
9292
)?)?;
9393
let expected_version = semver::Version::parse("0.2.40")?;
9494
Ok(cli_version >= expected_version)
9595
}
9696

9797
fn build_target_arg(target: Target, cli_path: &PathBuf) -> Result<String, failure::Error> {
98-
if !supports_dash_dash_target(cli_path)? {
98+
if !supports_dash_dash_target(cli_path.to_path_buf())? {
9999
Ok(build_target_arg_legacy(target, cli_path)?)
100100
} else {
101101
Ok(target.to_string())

src/command/build.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Implementation of the `wasm-pack build` command.
22
33
use crate::wasm_opt;
4-
use binary_install::{Cache, Download};
4+
use binary_install::Cache;
55
use bindgen;
66
use build;
77
use cache;
@@ -32,7 +32,7 @@ pub struct Build {
3232
pub mode: InstallMode,
3333
pub out_dir: PathBuf,
3434
pub out_name: Option<String>,
35-
pub bindgen: Option<Download>,
35+
pub bindgen: Option<install::Status>,
3636
pub cache: Cache,
3737
pub extra_options: Vec<String>,
3838
}
@@ -366,7 +366,7 @@ impl Build {
366366
info!("Building the wasm bindings...");
367367
bindgen::wasm_bindgen_build(
368368
&self.crate_data,
369-
self.bindgen.as_ref().unwrap(),
369+
&self.bindgen.as_ref().unwrap(),
370370
&self.out_dir,
371371
&self.out_name,
372372
self.disable_dts,

src/command/test.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,17 @@ impl Test {
268268
)
269269
}
270270

271-
let dl = install::download_prebuilt_or_cargo_install(
271+
let status = install::download_prebuilt_or_cargo_install(
272272
Tool::WasmBindgen,
273273
&self.cache,
274274
&bindgen_version,
275275
self.mode.install_permitted(),
276276
)?;
277277

278-
self.test_runner_path = Some(dl.binary("wasm-bindgen-test-runner")?);
278+
self.test_runner_path = match status {
279+
install::Status::Found(dl) => Some(dl.binary("wasm-bindgen-test-runner")?),
280+
_ => bail!("Could not find 'wasm-bindgen-test-runner'."),
281+
};
279282

280283
info!("Getting wasm-bindgen-cli was successful.");
281284
Ok(())

src/generate.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
//! Functionality related to running `cargo-generate`.
22
3-
use binary_install::Download;
43
use child;
54
use emoji;
65
use failure::{self, ResultExt};
6+
use install::{self, Tool};
77
use std::process::Command;
88

99
/// Run `cargo generate` in the current directory to create a new
1010
/// project from a template
11-
pub fn generate(template: &str, name: &str, download: &Download) -> Result<(), failure::Error> {
12-
let bin_path = download.binary("cargo-generate")?;
11+
pub fn generate(
12+
template: &str,
13+
name: &str,
14+
install_status: &install::Status,
15+
) -> Result<(), failure::Error> {
16+
let bin_path = install::get_tool_path(install_status, Tool::CargoGenerate)?
17+
.binary(&Tool::CargoGenerate.to_string())?;
1318
let mut cmd = Command::new(&bin_path);
1419
cmd.arg("generate");
1520
cmd.arg("--git").arg(&template);

src/install/mod.rs

+72-19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use binary_install::{Cache, Download};
55
use child;
66
use emoji;
77
use failure::{self, ResultExt};
8+
use install;
89
use log::debug;
910
use log::{info, warn};
1011
use std::env;
@@ -21,6 +22,27 @@ mod tool;
2122
pub use self::mode::InstallMode;
2223
pub use self::tool::Tool;
2324

25+
/// Possible outcomes of attempting to find/install a tool
26+
pub enum Status {
27+
/// Couldn't install tool because downloads are forbidden by user
28+
CannotInstall,
29+
/// The current platform doesn't support precompiled binaries for this tool
30+
PlatformNotSupported,
31+
/// We found the tool at the specified path
32+
Found(Download),
33+
}
34+
35+
/// Handles possible installs status and returns the download or a error message
36+
pub fn get_tool_path(status: &Status, tool: Tool) -> Result<&Download, failure::Error> {
37+
match status {
38+
Status::Found(download) => Ok(download),
39+
Status::CannotInstall => bail!("Not able to find or install a local {}.", tool),
40+
install::Status::PlatformNotSupported => {
41+
bail!("{} does not currently support your platform.", tool)
42+
}
43+
}
44+
}
45+
2446
/// Install a cargo CLI tool
2547
///
2648
/// Prefers an existing local install, if any exists. Then checks if there is a
@@ -32,7 +54,7 @@ pub fn download_prebuilt_or_cargo_install(
3254
cache: &Cache,
3355
version: &str,
3456
install_permitted: bool,
35-
) -> Result<Download, failure::Error> {
57+
) -> Result<Status, failure::Error> {
3658
// If the tool is installed globally and it has the right version, use
3759
// that. Assume that other tools are installed next to it.
3860
//
@@ -41,7 +63,8 @@ pub fn download_prebuilt_or_cargo_install(
4163
if let Ok(path) = which(tool.to_string()) {
4264
debug!("found global {} binary at: {}", tool, path.display());
4365
if check_version(&tool, &path, version)? {
44-
return Ok(Download::at(path.parent().unwrap()));
66+
let download = Download::at(path.parent().unwrap());
67+
return Ok(Status::Found(download));
4568
}
4669
}
4770

@@ -101,7 +124,7 @@ pub fn download_prebuilt(
101124
cache: &Cache,
102125
version: &str,
103126
install_permitted: bool,
104-
) -> Result<Download, failure::Error> {
127+
) -> Result<Status, failure::Error> {
105128
let url = match prebuilt_url(tool, version) {
106129
Ok(url) => url,
107130
Err(e) => bail!(
@@ -114,29 +137,53 @@ pub fn download_prebuilt(
114137
Tool::WasmBindgen => {
115138
let binaries = &["wasm-bindgen", "wasm-bindgen-test-runner"];
116139
match cache.download(install_permitted, "wasm-bindgen", binaries, &url)? {
117-
Some(download) => Ok(download),
140+
Some(download) => Ok(Status::Found(download)),
118141
None => bail!("wasm-bindgen v{} is not installed!", version),
119142
}
120143
}
121144
Tool::CargoGenerate => {
122145
let binaries = &["cargo-generate"];
123146
match cache.download(install_permitted, "cargo-generate", binaries, &url)? {
124-
Some(download) => Ok(download),
147+
Some(download) => Ok(Status::Found(download)),
125148
None => bail!("cargo-generate v{} is not installed!", version),
126149
}
127150
}
151+
Tool::WasmOpt => {
152+
let binaries = &["wasm-opt"];
153+
match cache.download(install_permitted, "wasm-opt", binaries, &url)? {
154+
Some(download) => Ok(Status::Found(download)),
155+
// TODO(ag_dubs): why is this different? i forget...
156+
None => Ok(Status::CannotInstall),
157+
}
158+
}
128159
}
129160
}
130161

131162
/// Returns the URL of a precompiled version of wasm-bindgen, if we have one
132163
/// available for our host platform.
133164
fn prebuilt_url(tool: &Tool, version: &str) -> Result<String, failure::Error> {
134165
let target = if target::LINUX && target::x86_64 {
135-
"x86_64-unknown-linux-musl"
166+
match tool {
167+
Tool::WasmOpt => "x86-linux",
168+
_ => "x86_64-unknown-linux-musl",
169+
}
170+
} else if target::LINUX && target::x86 {
171+
match tool {
172+
Tool::WasmOpt => "x86-linux",
173+
_ => bail!("Unrecognized target!"),
174+
}
136175
} else if target::MACOS && target::x86_64 {
137176
"x86_64-apple-darwin"
138177
} else if target::WINDOWS && target::x86_64 {
139-
"x86_64-pc-windows-msvc"
178+
match tool {
179+
Tool::WasmOpt => "x86-windows",
180+
_ => "x86_64-pc-windows-msvc",
181+
}
182+
} else if target::WINDOWS && target::x86 {
183+
match tool {
184+
Tool::WasmOpt => "x86-windows",
185+
_ => bail!("Unrecognized target!"),
186+
}
140187
} else {
141188
bail!("Unrecognized target!")
142189
};
@@ -155,6 +202,13 @@ fn prebuilt_url(tool: &Tool, version: &str) -> Result<String, failure::Error> {
155202
Krate::new(&Tool::CargoGenerate)?.max_version,
156203
target
157204
))
205+
},
206+
Tool::WasmOpt => {
207+
Ok(format!(
208+
"https://github.com/WebAssembly/binaryen/releases/download/{vers}/binaryen-{vers}-{target}.tar.gz",
209+
vers = "version_90",
210+
target = target,
211+
))
158212
}
159213
}
160214
}
@@ -166,7 +220,7 @@ pub fn cargo_install(
166220
cache: &Cache,
167221
version: &str,
168222
install_permitted: bool,
169-
) -> Result<Download, failure::Error> {
223+
) -> Result<Status, failure::Error> {
170224
debug!(
171225
"Attempting to use a `cargo install`ed version of `{}={}`",
172226
tool, version,
@@ -181,11 +235,12 @@ pub fn cargo_install(
181235
version,
182236
destination.display()
183237
);
184-
return Ok(Download::at(&destination));
238+
let download = Download::at(&destination);
239+
return Ok(Status::Found(download));
185240
}
186241

187242
if !install_permitted {
188-
bail!("{} v{} is not installed!", tool, version)
243+
return Ok(Status::CannotInstall);
189244
}
190245

191246
// Run `cargo install` to a temporary location to handle ctrl-c gracefully
@@ -210,23 +265,20 @@ pub fn cargo_install(
210265
.arg("--root")
211266
.arg(&tmp);
212267

213-
if PBAR.quiet() {
214-
cmd.arg("--quiet");
215-
}
216-
217268
let context = format!("Installing {} with cargo", tool);
218269
child::run(cmd, "cargo install").context(context)?;
219270

220271
// `cargo install` will put the installed binaries in `$root/bin/*`, but we
221272
// just want them in `$root/*` directly (which matches how the tarballs are
222273
// laid out, and where the rest of our code expects them to be). So we do a
223274
// little renaming here.
224-
let binaries = match tool {
225-
Tool::WasmBindgen => vec!["wasm-bindgen", "wasm-bindgen-test-runner"],
226-
Tool::CargoGenerate => vec!["cargo-genrate"],
275+
let binaries: Result<Vec<&str>, failure::Error> = match tool {
276+
Tool::WasmBindgen => Ok(vec!["wasm-bindgen", "wasm-bindgen-test-runner"]),
277+
Tool::CargoGenerate => Ok(vec!["cargo-genrate"]),
278+
Tool::WasmOpt => bail!("Cannot install wasm-opt with cargo."),
227279
};
228280

229-
for b in binaries.iter().cloned() {
281+
for b in binaries?.iter().cloned() {
230282
let from = tmp
231283
.join("bin")
232284
.join(b)
@@ -245,5 +297,6 @@ pub fn cargo_install(
245297
// Finally, move the `tmp` directory into our binary cache.
246298
fs::rename(&tmp, &destination)?;
247299

248-
Ok(Download::at(&destination))
300+
let download = Download::at(&destination);
301+
Ok(Status::Found(download))
249302
}

src/install/tool.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ pub enum Tool {
66
CargoGenerate,
77
/// wasm-bindgen CLI tools
88
WasmBindgen,
9+
/// wasm-opt CLI tool
10+
WasmOpt,
911
}
1012

1113
impl fmt::Display for Tool {
1214
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
13-
match self {
14-
Tool::CargoGenerate => write!(f, "cargo-generate"),
15-
Tool::WasmBindgen => write!(f, "wasm-bindgen"),
16-
}
15+
let s = match self {
16+
Tool::CargoGenerate => "cargo-generate",
17+
Tool::WasmBindgen => "wasm-bindgen",
18+
Tool::WasmOpt => "wasm-opt",
19+
};
20+
write!(f, "{}", s)
1721
}
1822
}

0 commit comments

Comments
 (0)