From cac0501eec9c46a4e37de2580c2e5ccf29533d39 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Tue, 8 Aug 2017 16:13:15 +0300 Subject: [PATCH 1/3] build utility --- Cargo.toml | 1 + build/.gitignore | 2 ++ build/Cargo.toml | 7 +++++++ build/src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 build/.gitignore create mode 100644 build/Cargo.toml create mode 100644 build/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index fe2dab9..ea0aa50 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ log = "0.3" env_logger = "0.4" lazy_static = "0.2" clap = "2.24" +wasm-build = { path = "build" } [lib] diff --git a/build/.gitignore b/build/.gitignore new file mode 100644 index 0000000..f2f9e58 --- /dev/null +++ b/build/.gitignore @@ -0,0 +1,2 @@ +target +Cargo.lock \ No newline at end of file diff --git a/build/Cargo.toml b/build/Cargo.toml new file mode 100644 index 0000000..5eba5e9 --- /dev/null +++ b/build/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "wasm-build" +version = "0.1.0" +authors = ["NikVolf "] + +[dependencies] +glob = "*" \ No newline at end of file diff --git a/build/src/lib.rs b/build/src/lib.rs new file mode 100644 index 0000000..464c6cd --- /dev/null +++ b/build/src/lib.rs @@ -0,0 +1,44 @@ +//! Tools library for building contracts via cargo + +extern crate glob; + +use std::{env, fs, io}; +use std::path::PathBuf; + +#[derive(Debug)] +pub enum Error { + Io(io::Error), + NoSuitableFile(String), + TooManyFiles(String), + NoEnvVar, +} + +impl From for Error { + fn from(err: io::Error) -> Self { + Error::Io(err) + } +} + +pub fn process_output(bin_name: &str) -> Result<(), Error> { + let out_dir = env::var("OUT").map_err(|_| Error::NoEnvVar)?; + let mut path = PathBuf::from(out_dir.clone()); + path.push("deps"); + path.push(format!("{}-*.wasm", bin_name)); + + let mut files = glob::glob(path.to_string_lossy().as_ref()).expect("glob err") + .collect::>>(); + + 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"); + fs::copy(file, out_dir)?; + } + + Ok(()) +} \ No newline at end of file From 0c52bcde1d6489220f217d0f8d6efd6c8410cb89 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Wed, 9 Aug 2017 13:45:35 +0300 Subject: [PATCH 2/3] to executable --- Cargo.toml | 8 +++++-- build/Cargo.toml | 7 +++++- build/src/{lib.rs => main.rs} | 42 +++++++++++++++++++++++++++++++---- 3 files changed, 50 insertions(+), 7 deletions(-) rename build/src/{lib.rs => main.rs} (51%) diff --git a/Cargo.toml b/Cargo.toml index ea0aa50..710d2fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ log = "0.3" env_logger = "0.4" lazy_static = "0.2" clap = "2.24" -wasm-build = { path = "build" } +glob = "0.2" [lib] @@ -27,4 +27,8 @@ path = "gas/src/main.rs" [[bin]] name = "wasm-pack" -path = "pack/src/main.rs" \ No newline at end of file +path = "pack/src/main.rs" + +[[bin]] +name = "wasm-build" +path = "build/src/main.rs" \ No newline at end of file diff --git a/build/Cargo.toml b/build/Cargo.toml index 5eba5e9..b97b031 100644 --- a/build/Cargo.toml +++ b/build/Cargo.toml @@ -4,4 +4,9 @@ version = "0.1.0" authors = ["NikVolf "] [dependencies] -glob = "*" \ No newline at end of file +glob = "0.2" +wasm-utils = { path = "../" } +clap = "2.24" + +[[bin]] +name = "wasm-build" \ No newline at end of file diff --git a/build/src/lib.rs b/build/src/main.rs similarity index 51% rename from build/src/lib.rs rename to build/src/main.rs index 464c6cd..e4d12c7 100644 --- a/build/src/lib.rs +++ b/build/src/main.rs @@ -1,10 +1,14 @@ -//! Tools library for building contracts via cargo +//! Experimental build tool for cargo extern crate glob; +extern crate wasm_utils; +extern crate clap; use std::{env, fs, io}; use std::path::PathBuf; +use clap::{App, Arg}; + #[derive(Debug)] pub enum Error { Io(io::Error), @@ -20,10 +24,14 @@ impl From for Error { } pub fn process_output(bin_name: &str) -> Result<(), Error> { - let out_dir = env::var("OUT").map_err(|_| Error::NoEnvVar)?; + let out_dir = env::var("OUT_DIR").map_err(|_| Error::NoEnvVar)?; let mut path = PathBuf::from(out_dir.clone()); + let wasm_name = bin_name.to_string().replace("-", "_"); + path.push(".."); + path.push(".."); + path.push(".."); path.push("deps"); - path.push(format!("{}-*.wasm", bin_name)); + path.push(format!("{}-*.wasm", wasm_name)); let mut files = glob::glob(path.to_string_lossy().as_ref()).expect("glob err") .collect::>>(); @@ -37,8 +45,34 @@ pub fn process_output(bin_name: &str) -> Result<(), Error> { )) } else { let file = files.drain(..).nth(0).expect("0th element exists").expect("glob err"); - fs::copy(file, out_dir)?; + let mut path = PathBuf::from(out_dir.clone()); + path.push(format!("{}.wasm", bin_name)); + fs::copy(file, path)?; } Ok(()) +} + +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(); + + + } \ No newline at end of file From 71910405bce4fc29ceab84e1025067e8fabf3fd1 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Wed, 9 Aug 2017 13:51:47 +0300 Subject: [PATCH 3/3] build tools continued --- build/src/main.rs | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/build/src/main.rs b/build/src/main.rs index e4d12c7..baf1729 100644 --- a/build/src/main.rs +++ b/build/src/main.rs @@ -4,7 +4,7 @@ extern crate glob; extern crate wasm_utils; extern crate clap; -use std::{env, fs, io}; +use std::{fs, io}; use std::path::PathBuf; use clap::{App, Arg}; @@ -23,13 +23,11 @@ impl From for Error { } } -pub fn process_output(bin_name: &str) -> Result<(), Error> { - let out_dir = env::var("OUT_DIR").map_err(|_| Error::NoEnvVar)?; - let mut path = PathBuf::from(out_dir.clone()); +pub fn process_output(target_dir: &str, bin_name: &str) -> Result<(), Error> { + let mut path = PathBuf::from(target_dir); let wasm_name = bin_name.to_string().replace("-", "_"); - path.push(".."); - path.push(".."); - path.push(".."); + path.push("wasm32-unknown-emscripten"); + path.push("release"); path.push("deps"); path.push(format!("{}-*.wasm", wasm_name)); @@ -45,7 +43,7 @@ pub fn process_output(bin_name: &str) -> Result<(), Error> { )) } else { let file = files.drain(..).nth(0).expect("0th element exists").expect("glob err"); - let mut path = PathBuf::from(out_dir.clone()); + let mut path = PathBuf::from(target_dir); path.push(format!("{}.wasm", bin_name)); fs::copy(file, path)?; } @@ -57,22 +55,18 @@ fn main() { wasm_utils::init_log(); let matches = App::new("wasm-opt") - .arg(Arg::with_name("input") + .arg(Arg::with_name("target") .index(1) .required(true) - .help("Input WASM file")) - .arg(Arg::with_name("output") + .help("Cargo target directory")) + .arg(Arg::with_name("wasm") .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")) + .help("Wasm binary name")) .get_matches(); + let target_dir = matches.value_of("target").expect("is required; qed"); + let wasm_binary = matches.value_of("wasm").expect("is required; qed"); - + process_output(target_dir, wasm_binary).expect("Failed to process cargo target directory"); } \ No newline at end of file