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

Support wasm-dis #750

Closed
wants to merge 4 commits 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
24 changes: 9 additions & 15 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
use child;
use command::build::{BuildProfile, Target};
use failure::{self, ResultExt};
use install::{self, Tool};
use manifest::CrateData;
use semver;
use std::path::{Path, PathBuf};
use std::process::Command;
use tool::{self, Kind};

/// Run the `wasm-bindgen` CLI to generate bindings for the current crate's
/// `.wasm`.
pub fn wasm_bindgen_build(
data: &CrateData,
install_status: &install::Status,
bindgen_path: &Path,
out_dir: &Path,
out_name: &Option<String>,
disable_dts: bool,
Expand All @@ -39,8 +39,6 @@ pub fn wasm_bindgen_build(
} else {
"--typescript"
};
let bindgen_path = install::get_tool_path(install_status, Tool::WasmBindgen)?
.binary(&Tool::WasmBindgen.to_string())?;

let mut cmd = Command::new(&bindgen_path);
cmd.arg(&wasm_path)
Expand Down Expand Up @@ -75,34 +73,30 @@ pub fn wasm_bindgen_build(
}

/// Check if the `wasm-bindgen` dependency is locally satisfied for the web target
fn supports_web_target(cli_path: &PathBuf) -> Result<bool, failure::Error> {
let cli_version = semver::Version::parse(&install::get_cli_version(
&install::Tool::WasmBindgen,
cli_path,
)?)?;
fn supports_web_target(cli_path: &Path) -> Result<bool, failure::Error> {
let cli_version =
semver::Version::parse(&tool::get_cli_version(tool::Kind::WasmBindgen, cli_path)?)?;
let expected_version = semver::Version::parse("0.2.39")?;
Ok(cli_version >= expected_version)
}

/// Check if the `wasm-bindgen` dependency is locally satisfied for the --target flag
fn supports_dash_dash_target(cli_path: PathBuf) -> Result<bool, failure::Error> {
let cli_version = semver::Version::parse(&install::get_cli_version(
&install::Tool::WasmBindgen,
&cli_path,
)?)?;
let cli_version =
semver::Version::parse(&tool::get_cli_version(Kind::WasmBindgen, &cli_path)?)?;
let expected_version = semver::Version::parse("0.2.40")?;
Ok(cli_version >= expected_version)
}

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

fn build_target_arg_legacy(target: Target, cli_path: &PathBuf) -> Result<String, failure::Error> {
fn build_target_arg_legacy(target: Target, cli_path: &Path) -> Result<String, failure::Error> {
log::info!("Your version of wasm-bindgen is out of date. You should consider updating your Cargo.toml to a version >= 0.2.40.");
let target_arg = match target {
Target::Nodejs => "--nodejs",
Expand Down
3 changes: 1 addition & 2 deletions src/child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//! properly logged and their output is logged as well.

use failure::Error;
use install::Tool;
use log::info;
use std::process::{Command, Stdio};

Expand Down Expand Up @@ -43,7 +42,7 @@ pub fn run(mut command: Command, command_name: &str) -> Result<(), Error> {
}

/// Run the given command and return its stdout.
pub fn run_capture_stdout(mut command: Command, command_name: &Tool) -> Result<String, Error> {
pub fn run_capture_stdout(mut command: Command, command_name: &str) -> Result<String, Error> {
info!("Running {:?}", command);

let output = command
Expand Down
114 changes: 74 additions & 40 deletions src/command/build.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
//! Implementation of the `wasm-pack build` command.

use crate::wasm_opt;
use crate::child;
use binary_install::Cache;
use bindgen;
use build;
use cache;
use command::utils::{create_pkg_dir, get_crate_path};
use emoji;
use failure::Error;
use install::{self, InstallMode, Tool};
use license;
use lockfile::Lockfile;
use log::info;
use manifest;
use readme;
use std::fmt;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str::FromStr;
use std::time::Instant;
use tool::{InstallMode, Kind, Status, Tool};
use PBAR;

/// Everything required to configure and run the `wasm-pack build` command.
Expand All @@ -32,7 +33,8 @@ pub struct Build {
pub mode: InstallMode,
pub out_dir: PathBuf,
pub out_name: Option<String>,
pub bindgen: Option<install::Status>,
pub bindgen: Option<Status>,
pub wat: bool,
pub cache: Cache,
pub extra_options: Vec<String>,
}
Expand Down Expand Up @@ -140,6 +142,10 @@ pub struct BuildOptions {
/// Create a profiling build. Enable optimizations and debug info.
pub profiling: bool,

#[structopt(long = "wat")]
/// Dis-assemble .wasm(webassembly binary) into webassembly text.
pub wat: bool,

#[structopt(long = "out-dir", short = "d", default_value = "pkg")]
/// Sets the output directory with a relative path.
pub out_dir: String,
Expand All @@ -165,6 +171,7 @@ impl Default for BuildOptions {
dev: false,
release: false,
profiling: false,
wat: false,
out_dir: String::new(),
out_name: None,
extra_options: Vec::new(),
Expand Down Expand Up @@ -202,6 +209,7 @@ impl Build {
out_dir,
out_name: build_opts.out_name,
bindgen: None,
wat: build_opts.wat,
cache: cache::get_wasm_pack_cache()?,
extra_options: build_opts.extra_options,
})
Expand All @@ -214,7 +222,7 @@ impl Build {

/// Execute this `Build` command.
pub fn run(&mut self) -> Result<(), Error> {
let process_steps = Build::get_process_steps(self.mode);
let process_steps = self.get_process_steps(self.mode);

let started = Instant::now();

Expand All @@ -239,7 +247,7 @@ impl Build {
Ok(())
}

fn get_process_steps(mode: InstallMode) -> Vec<(&'static str, BuildStep)> {
fn get_process_steps(&mut self, mode: InstallMode) -> Vec<(&'static str, BuildStep)> {
macro_rules! steps {
($($name:ident),+) => {
{
Expand All @@ -266,11 +274,13 @@ impl Build {
step_create_dir,
step_copy_readme,
step_copy_license,
step_install_wasm_bindgen,
step_run_wasm_bindgen,
step_run_wasm_opt,
step_create_json,
]);
if self.wat {
steps.extend(steps![step_run_wasm_dis]);
}
steps
}

Expand Down Expand Up @@ -346,34 +356,24 @@ impl Build {
Ok(())
}

fn step_install_wasm_bindgen(&mut self) -> Result<(), failure::Error> {
fn step_run_wasm_bindgen(&mut self) -> Result<(), failure::Error> {
info!("Identifying wasm-bindgen dependency...");
let lockfile = Lockfile::new(&self.crate_data)?;
let bindgen_version = lockfile.require_wasm_bindgen()?;
info!("Installing wasm-bindgen-cli...");
let bindgen = install::download_prebuilt_or_cargo_install(
Tool::WasmBindgen,
&self.cache,
&bindgen_version,
self.mode.install_permitted(),
)?;
self.bindgen = Some(bindgen);
info!("Installing wasm-bindgen-cli was successful.");
Ok(())
}

fn step_run_wasm_bindgen(&mut self) -> Result<(), Error> {
info!("Building the wasm bindings...");
bindgen::wasm_bindgen_build(
&self.crate_data,
&self.bindgen.as_ref().unwrap(),
&self.out_dir,
&self.out_name,
self.disable_dts,
self.target,
self.profile,
)?;
info!("wasm bindings were built at {:#?}.", &self.out_dir);
let bindgen = Tool::new(Kind::WasmBindgen, bindgen_version.to_string());
bindgen.run(&self.cache, self.mode.install_permitted(), |exec: &Path| {
bindgen::wasm_bindgen_build(
&self.crate_data,
exec,
&self.out_dir,
&self.out_name,
self.disable_dts,
self.target,
self.profile,
)?;
info!("wasm bindings were built at {:#?}.", &self.out_dir);
Ok(())
})?;
Ok(())
}

Expand All @@ -386,16 +386,50 @@ impl Build {
Some(args) => args,
None => return Ok(()),
};
info!("executing wasm-opt with {:?}", args);
wasm_opt::run(
&self.cache,
&self.out_dir,
&args,
self.mode.install_permitted(),
).map_err(|e| {
info!("trying to execute wasm-opt with {:?}", args);
let version = String::from("version_90");
let wasm_opt = Tool::new(Kind::WasmOpt, version);
wasm_opt.run(&self.cache, self.mode.install_permitted(), |exec: &Path| {
for file in self.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(exec);
cmd.arg(&path).arg("-o").arg(&tmp).args(&args);
child::run(cmd, "wasm-opt")?;
std::fs::rename(&tmp, &path)?;
}
Ok(())
})
.map_err(|e| {
format_err!(
"{}\nTo disable `wasm-opt`, add `wasm-opt = false` to your package metadata in your `Cargo.toml`.", e
)
})
})?;
Ok(())
}

fn step_run_wasm_dis(&mut self) -> Result<(), Error> {
info!("trying to execute wasm-dis ..");
let version = String::from("version_90");
let wasm_opt = Tool::new(Kind::WasmDis, version);
wasm_opt.run(&self.cache, self.mode.install_permitted(), |exec: &Path| {
for file in self.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("wast");
let mut cmd = Command::new(exec);
cmd.arg(&path).arg("-o").arg(&tmp);
child::run(cmd, "wasm-dis")?;
}
Ok(())
})?;
Ok(())
}
}
17 changes: 3 additions & 14 deletions src/command/generate.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
use cache;
use failure::Error;
use generate;
use install::{self, Tool};
use log::info;
use std::path::Path;
use std::result;
use PBAR;

/// Executes the 'cargo-generate' command in the current directory
/// which generates a new rustwasm project from a template.
pub fn generate(
template: String,
name: String,
install_permitted: bool,
) -> result::Result<(), Error> {
pub fn generate(template: String, name: &str, exec_path: &Path) -> result::Result<(), Error> {
info!("Generating a new rustwasm project...");
let download = install::download_prebuilt_or_cargo_install(
Tool::CargoGenerate,
&cache::get_wasm_pack_cache()?,
"latest",
install_permitted,
)?;
generate::generate(&template, &name, &download)?;
generate::generate(&template, &name, exec_path)?;

let msg = format!("🐑 Generated new project at /{}", name);
PBAR.info(&msg);
Expand Down
14 changes: 11 additions & 3 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ use self::login::login;
use self::pack::pack;
use self::publish::{access::Access, publish};
use self::test::{Test, TestOptions};
use crate::install::InstallMode;
use crate::cache;
use crate::tool::{InstallMode, Kind, Tool};
use failure::Error;
use log::info;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::result;

/// The various kinds of commands that `wasm-pack` can execute.
Expand Down Expand Up @@ -134,7 +135,14 @@ pub fn run_wasm_pack(command: Command) -> result::Result<(), Error> {
info!("Running generate command...");
info!("Template: {:?}", &template);
info!("Name: {:?}", &name);
generate(template, name, mode.install_permitted())
let version = String::from("latest");
let cargo_gen = Tool::new(Kind::CargoGenerate, version);
let cache = cache::get_wasm_pack_cache()?;
cargo_gen.run(&cache, mode.install_permitted(), |exec: &Path| {
generate(template, &name, exec)?;
Ok(())
})?;
Ok(())
}
Command::Publish {
target,
Expand Down
14 changes: 5 additions & 9 deletions src/command/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use cache;
use command::utils::get_crate_path;
use console::style;
use failure::Error;
use install::{self, InstallMode, Tool};
use lockfile::Lockfile;
use log::info;
use manifest;
use std::path::PathBuf;
use std::time::Instant;
use test::{self, webdriver};
use tool::{self, InstallMode};

#[derive(Debug, Default, StructOpt)]
/// Everything required to configure the `wasm-pack test` command.
Expand Down Expand Up @@ -268,15 +268,11 @@ impl Test {
)
}

let status = install::download_prebuilt_or_cargo_install(
Tool::WasmBindgen,
&self.cache,
&bindgen_version,
self.mode.install_permitted(),
)?;
let wasm_bindgen = tool::Tool::new(tool::Kind::WasmBindgen, bindgen_version.to_string())
.install(&self.cache, self.mode.install_permitted())?;

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

Expand Down
Loading