2017-08-09 13:45:35 +03:00
|
|
|
//! Experimental build tool for cargo
|
2017-08-08 16:13:15 +03:00
|
|
|
|
|
|
|
extern crate glob;
|
2017-08-09 13:45:35 +03:00
|
|
|
extern crate wasm_utils;
|
|
|
|
extern crate clap;
|
2017-08-09 14:30:37 +03:00
|
|
|
extern crate parity_wasm;
|
2017-08-08 16:13:15 +03:00
|
|
|
|
2017-08-09 13:51:47 +03:00
|
|
|
use std::{fs, io};
|
2017-08-08 16:13:15 +03:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2017-08-09 13:45:35 +03:00
|
|
|
use clap::{App, Arg};
|
|
|
|
|
2017-08-08 16:13:15 +03:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
Io(io::Error),
|
|
|
|
NoSuitableFile(String),
|
|
|
|
TooManyFiles(String),
|
|
|
|
NoEnvVar,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for Error {
|
|
|
|
fn from(err: io::Error) -> Self {
|
|
|
|
Error::Io(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-09 14:30:37 +03:00
|
|
|
pub fn wasm_path(target_dir: &str, bin_name: &str) -> String {
|
|
|
|
let mut path = PathBuf::from(target_dir);
|
|
|
|
path.push(format!("{}.wasm", bin_name));
|
|
|
|
path.to_string_lossy().to_string()
|
|
|
|
}
|
|
|
|
|
2017-08-09 13:51:47 +03:00
|
|
|
pub fn process_output(target_dir: &str, bin_name: &str) -> Result<(), Error> {
|
|
|
|
let mut path = PathBuf::from(target_dir);
|
2017-08-09 13:45:35 +03:00
|
|
|
let wasm_name = bin_name.to_string().replace("-", "_");
|
2017-08-09 13:51:47 +03:00
|
|
|
path.push("wasm32-unknown-emscripten");
|
|
|
|
path.push("release");
|
2017-08-08 16:13:15 +03:00
|
|
|
path.push("deps");
|
2017-08-09 13:45:35 +03:00
|
|
|
path.push(format!("{}-*.wasm", wasm_name));
|
2017-08-08 16:13:15 +03:00
|
|
|
|
|
|
|
let mut files = glob::glob(path.to_string_lossy().as_ref()).expect("glob err")
|
|
|
|
.collect::<Vec<Result<PathBuf, glob::GlobError>>>();
|
|
|
|
|
|
|
|
if files.len() == 0 {
|
|
|
|
return Err(Error::NoSuitableFile(path.to_string_lossy().to_string()));
|
|
|
|
} else if files.len() > 1 {
|
|
|
|
return Err(Error::TooManyFiles(
|
|
|
|
files.into_iter().map(|f| f.expect("glob err").to_string_lossy().to_string())
|
|
|
|
.fold(String::new(), |mut a, b| { a.push_str(", "); a.push_str(&b); a })
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
let file = files.drain(..).nth(0).expect("0th element exists").expect("glob err");
|
2017-08-09 13:51:47 +03:00
|
|
|
let mut path = PathBuf::from(target_dir);
|
2017-08-09 13:45:35 +03:00
|
|
|
path.push(format!("{}.wasm", bin_name));
|
|
|
|
fs::copy(file, path)?;
|
2017-08-08 16:13:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2017-08-09 13:45:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
wasm_utils::init_log();
|
|
|
|
|
|
|
|
let matches = App::new("wasm-opt")
|
2017-08-09 13:51:47 +03:00
|
|
|
.arg(Arg::with_name("target")
|
2017-08-09 13:45:35 +03:00
|
|
|
.index(1)
|
|
|
|
.required(true)
|
2017-08-09 13:51:47 +03:00
|
|
|
.help("Cargo target directory"))
|
|
|
|
.arg(Arg::with_name("wasm")
|
2017-08-09 13:45:35 +03:00
|
|
|
.index(2)
|
|
|
|
.required(true)
|
2017-08-09 13:51:47 +03:00
|
|
|
.help("Wasm binary name"))
|
2017-08-09 13:45:35 +03:00
|
|
|
.get_matches();
|
|
|
|
|
2017-08-09 13:51:47 +03:00
|
|
|
let target_dir = matches.value_of("target").expect("is required; qed");
|
|
|
|
let wasm_binary = matches.value_of("wasm").expect("is required; qed");
|
2017-08-09 13:45:35 +03:00
|
|
|
|
2017-08-09 13:51:47 +03:00
|
|
|
process_output(target_dir, wasm_binary).expect("Failed to process cargo target directory");
|
2017-08-09 14:30:37 +03:00
|
|
|
|
|
|
|
let path = wasm_path(target_dir, wasm_binary);
|
2017-08-09 18:38:46 +03:00
|
|
|
|
|
|
|
let mut module = wasm_utils::externalize(
|
|
|
|
parity_wasm::deserialize_file(&path).unwrap(),
|
|
|
|
vec!["_free", "_malloc"],
|
|
|
|
);
|
2017-08-09 14:30:37 +03:00
|
|
|
|
|
|
|
wasm_utils::optimize(&mut module, vec!["_call"]).expect("Optimizer to finish without errors");
|
|
|
|
|
|
|
|
parity_wasm::serialize_to_file(&path, module).unwrap();
|
2017-08-08 16:13:15 +03:00
|
|
|
}
|