Use structopt instead of docopt.

This commit refactors the Wasmtime CLI tools to use `structopt` instead of
`docopt`.

The `wasmtime` tool now has the following subcommands:

* `config new` - creates a new Wasmtime configuration file.
* `run` - runs a WebAssembly module.
* `wasm2obj` - translates a Wasm module to native object file.
* `wast` - runs a test script file.

If no subcommand is specified, the `run` subcommand is used. Thus,
`wasmtime foo.wasm` should continue to function as expected.

The `wasm2obj` and `wast` tools still exist, but delegate to the same
implementation as the `wasmtime` subcommands.  The standalone `wasm2obj` and
`wast` tools may be removed in the future in favor of simply using `wasmtime`.

Included in this commit is a breaking change to the default Wasmtime
configuration file: it has been renamed from `wasmtime-cache-config.toml` to
simply `config.toml`.  The new name is less specific which will allow for
additional (non-cache-related) settings in the future.

There are some breaking changes to improve command line UX:

* The `--cache-config` option has been renamed to `--config`.
* The `--create-config-file` option has moved to the `config new` subcommand.
As a result, the `wasm2obj` and `wast` tools cannot be used to create a new
config file.
* The short form of the `--optimize` option has changed from
`-o` to `-O` for consistency.
* The `wasm2obj` command takes the output object file as a
required positional argument rather than the former required output *option*
(e.g. `wasmtime wasm2obj foo.wasm foo.obj`).
This commit is contained in:
Peter Huene
2019-12-17 15:38:00 -05:00
parent d142a39113
commit 59258730c2
14 changed files with 900 additions and 891 deletions

View File

@@ -1,7 +1,37 @@
//! The Wasmtime command line interface (CLI) crate.
//!
//! This crate implements the Wasmtime command line tools.
#![deny(
missing_docs,
trivial_numeric_casts,
unused_extern_crates,
unstable_features
)]
#![warn(unused_import_braces)]
#![cfg_attr(feature = "clippy", plugin(clippy(conf_file = "../clippy.toml")))]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::new_without_default))]
#![cfg_attr(
feature = "cargo-clippy",
warn(
clippy::float_arithmetic,
clippy::mut_mut,
clippy::nonminimal_bool,
clippy::option_map_unwrap_or,
clippy::option_map_unwrap_or_else,
clippy::unicode_not_nfc,
clippy::use_self
)
)]
pub mod commands;
use anyhow::{bail, Result};
use std::path::PathBuf;
use structopt::StructOpt;
use wasmtime::Strategy;
pub fn pick_compilation_strategy(cranelift: bool, lightbeam: bool) -> Result<Strategy> {
fn pick_compilation_strategy(cranelift: bool, lightbeam: bool) -> Result<Strategy> {
Ok(match (lightbeam, cranelift) {
(true, false) => Strategy::Lightbeam,
(false, true) => Strategy::Cranelift,
@@ -10,7 +40,7 @@ pub fn pick_compilation_strategy(cranelift: bool, lightbeam: bool) -> Result<Str
})
}
pub fn init_file_per_thread_logger(prefix: &'static str) {
fn init_file_per_thread_logger(prefix: &'static str) {
file_per_thread_logger::initialize(prefix);
// Extending behavior of default spawner:
@@ -35,3 +65,39 @@ pub fn init_file_per_thread_logger(prefix: &'static str) {
.build_global()
.unwrap();
}
/// Common options for commands that translate WebAssembly modules
#[derive(StructOpt)]
struct CommonOptions {
/// Use specified configuration file
#[structopt(long, parse(from_os_str), value_name = "CONFIG_PATH")]
config: Option<PathBuf>,
/// Use Cranelift for all compilation
#[structopt(long, conflicts_with = "lightbeam")]
cranelift: bool,
/// Enable debug output
#[structopt(short, long)]
debug: bool,
/// Generate debug information
#[structopt(short = "g")]
debug_info: bool,
/// Disable cache system
#[structopt(long)]
disable_cache: bool,
/// Enable support for proposed SIMD instructions
#[structopt(long)]
enable_simd: bool,
/// Use Lightbeam for all compilation
#[structopt(long, conflicts_with = "cranelift")]
lightbeam: bool,
/// Run optimization passes on translated functions
#[structopt(short = "O", long)]
optimize: bool,
}