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-08 16:13:15 +03:00
|
|
|
|
|
|
|
use std::{env, fs, io};
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process_output(bin_name: &str) -> Result<(), Error> {
|
2017-08-09 13:45:35 +03:00
|
|
|
let out_dir = env::var("OUT_DIR").map_err(|_| Error::NoEnvVar)?;
|
2017-08-08 16:13:15 +03:00
|
|
|
let mut path = PathBuf::from(out_dir.clone());
|
2017-08-09 13:45:35 +03:00
|
|
|
let wasm_name = bin_name.to_string().replace("-", "_");
|
|
|
|
path.push("..");
|
|
|
|
path.push("..");
|
|
|
|
path.push("..");
|
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:45:35 +03:00
|
|
|
let mut path = PathBuf::from(out_dir.clone());
|
|
|
|
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")
|
|
|
|
.arg(Arg::with_name("input")
|
|
|
|
.index(1)
|
|
|
|
.required(true)
|
|
|
|
.help("Input WASM file"))
|
|
|
|
.arg(Arg::with_name("output")
|
|
|
|
.index(2)
|
|
|
|
.required(true)
|
|
|
|
.help("Output WASM file"))
|
|
|
|
.arg(Arg::with_name("exports")
|
|
|
|
.long("exports")
|
|
|
|
.short("e")
|
|
|
|
.takes_value(true)
|
|
|
|
.value_name("functions")
|
|
|
|
.help("Comma-separated list of exported functions to keep. Default: _call"))
|
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-08-08 16:13:15 +03:00
|
|
|
}
|