From b19ba790fbde6b505f3e01868e1b692e0bfaeaaf Mon Sep 17 00:00:00 2001 From: maciejhirsz Date: Tue, 16 May 2017 17:25:54 +0200 Subject: [PATCH] Allow export functions to be set in CLI --- opt/Cargo.toml | 3 ++- opt/src/main.rs | 40 ++++++++++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/opt/Cargo.toml b/opt/Cargo.toml index 62c9acf..b3c9ea8 100644 --- a/opt/Cargo.toml +++ b/opt/Cargo.toml @@ -5,4 +5,5 @@ authors = ["NikVolf "] [dependencies] parity-wasm = { git="https://github.com/nikvolf/parity-wasm" } -wasm-utils = { path = "../" } \ No newline at end of file +wasm-utils = { path = "../" } +clap = "2.24" diff --git a/opt/src/main.rs b/opt/src/main.rs index 4b96420..6175ad9 100644 --- a/opt/src/main.rs +++ b/opt/src/main.rs @@ -1,24 +1,44 @@ extern crate parity_wasm; extern crate wasm_utils; +extern crate clap; -use std::env; +use clap::{App, Arg}; fn main() { - wasm_utils::init_log(); - let args = env::args().collect::>(); - if args.len() < 3 { - println!("Usage: {} input_file.wasm output_file.wasm", args[0]); - return; - } + 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(); - let mut module = parity_wasm::deserialize_file(&args[1]).unwrap(); + let exports = matches + .value_of("exports") + .unwrap_or("_call") + .split(',') + .collect(); + + let input = matches.value_of("input").expect("is required; qed"); + let output = matches.value_of("output").expect("is required; qed"); + + let mut module = parity_wasm::deserialize_file(&input).unwrap(); // Invoke optimizer // Contract is supposed to have only these functions as public api // All other symbols not usable by this list is optimized away - wasm_utils::optimize(&mut module, vec!["_call"]).expect("Optimizer to finish without errors"); + wasm_utils::optimize(&mut module, exports).expect("Optimizer to finish without errors"); - parity_wasm::serialize_to_file(&args[2], module).unwrap(); + parity_wasm::serialize_to_file(&output, module).unwrap(); }