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-10-25 20:36:05 +03:00
|
|
|
use std::io::Write;
|
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-10-25 20:36:05 +03:00
|
|
|
use parity_wasm::elements;
|
2017-08-09 13:45:35 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-25 20:36:05 +03:00
|
|
|
fn has_ctor(module: &elements::Module) -> bool {
|
|
|
|
if let Some(ref section) = module.export_section() {
|
|
|
|
section.entries().iter().find(|e| "_create" == e.field()).is_some()
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-15 16:44:25 +03:00
|
|
|
.arg(Arg::with_name("skip_optimization")
|
|
|
|
.help("Skip symbol optimization step producing final wasm")
|
|
|
|
.long("skip-optimization"))
|
|
|
|
.arg(Arg::with_name("skip_alloc")
|
|
|
|
.help("Skip allocator externalizer step producing final wasm")
|
|
|
|
.long("skip-externalize"))
|
2017-09-25 20:14:46 +03:00
|
|
|
.arg(Arg::with_name("runtime_type")
|
|
|
|
.help("Injects RUNTIME_TYPE global export")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("runtime-type"))
|
|
|
|
.arg(Arg::with_name("runtime_version")
|
|
|
|
.help("Injects RUNTIME_VERSION global export")
|
|
|
|
.takes_value(true)
|
|
|
|
.long("runtime-version"))
|
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
|
|
|
|
2017-08-15 16:44:25 +03:00
|
|
|
let mut module = parity_wasm::deserialize_file(&path).unwrap();
|
2017-08-09 14:30:37 +03:00
|
|
|
|
2017-08-15 16:44:25 +03:00
|
|
|
if !matches.is_present("skip_alloc") {
|
|
|
|
module = wasm_utils::externalize(
|
|
|
|
module,
|
|
|
|
vec!["_free", "_malloc"],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-25 20:14:46 +03:00
|
|
|
if let Some(runtime_type) = matches.value_of("runtime_type") {
|
|
|
|
let runtime_type: &[u8] = runtime_type.as_bytes();
|
|
|
|
if runtime_type.len() != 4 {
|
|
|
|
panic!("--runtime-type should be equal to 4 bytes");
|
|
|
|
}
|
|
|
|
let runtime_version: u32 = matches.value_of("runtime_version").unwrap_or("1").parse()
|
|
|
|
.expect("--runtime-version should be a positive integer");
|
|
|
|
module = wasm_utils::inject_runtime_type(module, &runtime_type, runtime_version);
|
|
|
|
}
|
|
|
|
|
2017-10-25 20:36:05 +03:00
|
|
|
let mut ctor_module = module.clone();
|
|
|
|
|
|
|
|
if !matches.is_present("skip_optimization") {
|
|
|
|
wasm_utils::optimize(&mut module, vec!["_call", "setTempRet0"]).expect("Optimizer to finish without errors");
|
|
|
|
}
|
|
|
|
|
|
|
|
let raw_module = parity_wasm::serialize(module).expect("Failed to serialize module");
|
|
|
|
|
|
|
|
let mut file = fs::File::create(&path).expect("Failed to create file");;
|
|
|
|
file.write_all(&raw_module).expect("Failed to write module to file");
|
|
|
|
|
|
|
|
// will pack into constructor
|
|
|
|
if has_ctor(&ctor_module) {
|
|
|
|
if !matches.is_present("skip_optimization") {
|
|
|
|
wasm_utils::optimize(&mut ctor_module, vec!["_create", "setTempRet0"]).expect("Optimizer to finish without errors");
|
|
|
|
}
|
|
|
|
wasm_utils::pack_instance(raw_module, &mut ctor_module);
|
|
|
|
|
|
|
|
let ctor_path = wasm_path(target_dir, &format!("{}_ctor", wasm_binary));
|
|
|
|
parity_wasm::serialize_to_file(ctor_path, ctor_module);
|
|
|
|
}
|
|
|
|
|
2017-09-25 20:14:46 +03:00
|
|
|
}
|