From 5ac8bbec1f999cc4bff7d90de69a34189be59b86 Mon Sep 17 00:00:00 2001 From: svartalf Date: Thu, 14 Mar 2019 14:04:54 +0300 Subject: [PATCH] Convert from docopt to structopt --- Cargo.toml | 4 +--- src/wasm2obj.rs | 56 +++++++++++++++++-------------------------- src/wasmtime.rs | 64 ++++++++++++++++++++----------------------------- src/wast.rs | 54 ++++++++++++++++------------------------- 4 files changed, 69 insertions(+), 109 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 12ea660bfbe7..7adbb4f536a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,9 +31,7 @@ wasmtime-runtime = { path = "lib/runtime" } wasmtime-jit = { path = "lib/jit" } wasmtime-obj = { path = "lib/obj" } wasmtime-wast = { path = "lib/wast" } -docopt = "1.0.1" -serde = "1.0.75" -serde_derive = "1.0.75" +structopt = "0.2.15" faerie = "0.7.1" target-lexicon = { version = "0.2.0", default-features = false } pretty_env_logger = "0.3.0" diff --git a/src/wasm2obj.rs b/src/wasm2obj.rs index c68ddb50b767..b24d6478b84b 100644 --- a/src/wasm2obj.rs +++ b/src/wasm2obj.rs @@ -29,13 +29,9 @@ ) )] -#[macro_use] -extern crate serde_derive; - use cranelift_codegen::isa; use cranelift_codegen::settings; use cranelift_native; -use docopt::Docopt; use faerie::Artifact; use std::error::Error; use std::fmt::format; @@ -47,34 +43,33 @@ use std::path::PathBuf; use std::process; use std::str; use std::str::FromStr; +use structopt::StructOpt; use target_lexicon::Triple; use wasmtime_debug::{emit_debugsections, read_debuginfo}; use wasmtime_environ::{cranelift, ModuleEnvironment, Tunables}; use wasmtime_obj::emit_module; -const USAGE: &str = " -Wasm to native object translation utility. -Takes a binary WebAssembly module into a native object file. -The translation is dependent on the environment chosen. -The default is a dummy environment that produces placeholder values. - -Usage: - wasm2obj [--target TARGET] [-g] -o - wasm2obj --help | --version - -Options: - -v, --verbose displays the module and translated functions - -h, --help print this help message - --target build for the target triple; default is the host machine - -g generate debug information - --version print the Cranelift version -"; - -#[derive(Deserialize, Debug, Clone)] +/// Wasm to native object translation utility. +/// +/// Takes a binary WebAssembly module into a native object file. +/// The translation is dependent on the environment chosen. +/// The default is a dummy environment that produces placeholder values. +#[derive(StructOpt, Debug, Clone)] +#[structopt(name = "wasm2obj")] struct Args { - arg_file: String, + #[structopt(name = "FILE", parse(from_os_str))] + arg_file: PathBuf, + + /// output file + #[structopt(short = "o")] arg_output: String, + + /// build for the target triple; default is the host machine + #[structopt(long = "target")] arg_target: Option, + + /// generate debug information + #[structopt(short = "g")] flag_g: bool, } @@ -86,17 +81,10 @@ fn read_wasm_file(path: PathBuf) -> Result, io::Error> { } fn main() { - let args: Args = Docopt::new(USAGE) - .and_then(|d| { - d.help(true) - .version(Some(String::from("0.0.0"))) - .deserialize() - }) - .unwrap_or_else(|e| e.exit()); - - let path = Path::new(&args.arg_file); + let args = Args::from_args(); + match handle_module( - path.to_path_buf(), + args.arg_file, &args.arg_target, &args.arg_output, args.flag_g, diff --git a/src/wasmtime.rs b/src/wasmtime.rs index def4557ffa45..b763860dbb66 100644 --- a/src/wasmtime.rs +++ b/src/wasmtime.rs @@ -30,13 +30,9 @@ ) )] -#[macro_use] -extern crate serde_derive; - use cranelift_codegen::settings; use cranelift_codegen::settings::Configurable; use cranelift_native; -use docopt::Docopt; use file_per_thread_logger; use pretty_env_logger; use std::error::Error; @@ -45,39 +41,38 @@ use std::io; use std::io::prelude::*; use std::path::{Path, PathBuf}; use std::process::exit; +use structopt::StructOpt; use wabt; use wasmtime_jit::{ActionOutcome, Context}; use wasmtime_wast::instantiate_spectest; static LOG_FILENAME_PREFIX: &str = "wasmtime.dbg."; -const USAGE: &str = " -Wasm runner. - -Takes a binary (wasm) or text (wat) WebAssembly module and instantiates it, -including calling the start function if one is present. Additional functions -given with --invoke are then called. - -Usage: - wasmtime [-odg] ... - wasmtime [-odg] ... --invoke= - wasmtime --help | --version - -Options: - --invoke= name of function to run - -o, --optimize runs optimization passes on the translated functions - -g generate debug information - -d, --debug enable debug output on stderr/stdout - -h, --help print this help message - --version print the Cranelift version -"; - -#[derive(Deserialize, Debug, Clone)] +/// Wasm runner. +/// +/// Takes a binary (wasm) or text (wat) WebAssembly module and instantiates it, +/// including calling the start function if one is present. Additional functions +/// given with --invoke are then called. +#[derive(StructOpt, Debug, Clone)] +#[structopt(name = "wasmtime")] struct Args { - arg_file: Vec, + #[structopt(name = "FILE", parse(from_os_str))] + arg_file: Vec, + + /// runs optimization passes on the translated functions + #[structopt(short = "o", long = "optimize")] flag_optimize: bool, + + /// enable debug output on stderr/stdout + #[structopt(short = "d", long = "debug")] flag_debug: bool, + + /// generate debug information + #[structopt(short = "g")] flag_g: bool, + + /// name of function to run + #[structopt(long = "invoke")] flag_invoke: Option, } @@ -101,13 +96,7 @@ fn read_wasm(path: PathBuf) -> Result, String> { } fn main() { - let args: Args = Docopt::new(USAGE) - .and_then(|d| { - d.help(true) - .version(Some(String::from("0.0.0"))) - .deserialize() - }) - .unwrap_or_else(|e| e.exit()); + let args = Args::from_args(); if args.flag_debug { pretty_env_logger::init(); @@ -142,12 +131,11 @@ fn main() { // Enable/disable producing of debug info. context.set_debug_info(args.flag_g); - for filename in &args.arg_file { - let path = Path::new(&filename); - match handle_module(&mut context, &args, path) { + for path in &args.arg_file { + match handle_module(&mut context, &args, &path) { Ok(()) => {} Err(message) => { - let name = path.as_os_str().to_string_lossy(); + let name = path.to_string_lossy(); println!("error while processing {}: {}", name, message); exit(1); } diff --git a/src/wast.rs b/src/wast.rs index 86da3215f3b3..94b5a3ec4760 100644 --- a/src/wast.rs +++ b/src/wast.rs @@ -25,52 +25,40 @@ ) )] -#[macro_use] -extern crate serde_derive; - use cranelift_codegen::settings; use cranelift_codegen::settings::Configurable; use cranelift_native; -use docopt::Docopt; use file_per_thread_logger; use pretty_env_logger; -use std::path::Path; +use std::path::PathBuf; use std::process; +use structopt::StructOpt; use wasmtime_jit::Compiler; use wasmtime_wast::WastContext; static LOG_FILENAME_PREFIX: &str = "cranelift.dbg."; -const USAGE: &str = " -Wast test runner. - -Usage: - run_wast [-do] ... - run_wast --help | --version - -Options: - -h, --help print this help message - --version print the Cranelift version - -o, --optimize runs optimization passes on the translated functions - -d, --debug enable debug output on stderr/stdout -"; - -#[derive(Deserialize, Debug, Clone)] +/// Wast test runner. +#[derive(StructOpt, Debug, Clone)] +#[structopt(name = "wast")] struct Args { - arg_file: Vec, + #[structopt(name = "FILE", parse(from_os_str))] + arg_file: Vec, + + /// enable debug output on stderr/stdout + #[structopt(short = "d", long = "debug")] flag_debug: bool, + + #[structopt(long = "function")] flag_function: Option, + + /// runs optimization passes on the translated functions + #[structopt(short = "o", long = "optimize")] flag_optimize: bool, } fn main() { - let args: Args = Docopt::new(USAGE) - .and_then(|d| { - d.help(true) - .version(Some(String::from("0.0.0"))) - .deserialize() - }) - .unwrap_or_else(|e| e.exit()); + let args = Args::from_args(); if args.flag_debug { pretty_env_logger::init(); @@ -102,11 +90,9 @@ fn main() { .expect("error instantiating \"spectest\""); for filename in &args.arg_file { - wast_context - .run_file(Path::new(&filename)) - .unwrap_or_else(|e| { - eprintln!("{}", e); - process::exit(1) - }); + wast_context.run_file(&filename).unwrap_or_else(|e| { + eprintln!("{}", e); + process::exit(1) + }); } }