Update to clap 3.* (#4082)

* Update to clap 3.0

This commit migrates all CLI commands internally used in this project
from structopt/clap2 to clap 3. The intent here is to ensure that we're
using maintained versions of the dependencies as structopt and clap 2
are less maintained nowadays. Most transitions were pretty
straightforward and mostly dealing with structopt/clap3 differences.

* Fix a number of `cargo deny` errors

This commit fixes a few errors around duplicate dependencies which
arose from the prior update to clap3. This also uses a new feature in
`deny.toml`, `skip-tree`, which allows having a bit more targeted
ignores for skips of duplicate version checks. This showed a few more
locations in Wasmtime itself where we could update some dependencies.
This commit is contained in:
Alex Crichton
2022-04-28 12:47:12 -05:00
committed by GitHub
parent 871a9d93f2
commit 5fe06f7345
30 changed files with 295 additions and 325 deletions

View File

@@ -4,20 +4,15 @@
//! See `wasmtime --help` for usage.
use anyhow::Result;
use structopt::{clap::AppSettings, clap::ErrorKind, StructOpt};
use clap::{ErrorKind, Parser};
use wasmtime_cli::commands::{
CompileCommand, ConfigCommand, RunCommand, SettingsCommand, WastCommand,
};
/// Wasmtime WebAssembly Runtime
#[derive(StructOpt)]
#[structopt(
name = "wasmtime",
version = env!("CARGO_PKG_VERSION"),
global_settings = &[
AppSettings::VersionlessSubcommands,
AppSettings::ColoredHelp
],
#[derive(Parser)]
#[clap(
version,
after_help = "If a subcommand is not provided, the `run` subcommand will be used.\n\
\n\
Usage examples:\n\
@@ -34,7 +29,7 @@ use wasmtime_cli::commands::{
\n \
wasmtime example.wasm --invoke add 1 2\n"
)]
enum WasmtimeApp {
enum Wasmtime {
// !!! IMPORTANT: if subcommands are added or removed, update `parse_module` in `src/commands/run.rs`. !!!
/// Controls Wasmtime configuration settings
Config(ConfigCommand),
@@ -48,7 +43,7 @@ enum WasmtimeApp {
Wast(WastCommand),
}
impl WasmtimeApp {
impl Wasmtime {
/// Executes the command.
pub fn execute(self) -> Result<()> {
match self {
@@ -62,14 +57,13 @@ impl WasmtimeApp {
}
fn main() -> Result<()> {
WasmtimeApp::from_iter_safe(std::env::args())
.unwrap_or_else(|e| match e.kind {
ErrorKind::HelpDisplayed
| ErrorKind::VersionDisplayed
| ErrorKind::MissingArgumentOrSubcommand => e.exit(),
_ => WasmtimeApp::Run(
RunCommand::from_iter_safe(std::env::args()).unwrap_or_else(|_| e.exit()),
),
Wasmtime::try_parse()
.unwrap_or_else(|e| match e.kind() {
ErrorKind::DisplayHelp
| ErrorKind::DisplayVersion
| ErrorKind::MissingSubcommand
| ErrorKind::MissingRequiredArgument => e.exit(),
_ => Wasmtime::Run(RunCommand::parse()),
})
.execute()
}

View File

@@ -2,9 +2,9 @@
use crate::CommonOptions;
use anyhow::{bail, Context, Result};
use clap::Parser;
use std::fs;
use std::path::PathBuf;
use structopt::{clap::AppSettings, StructOpt};
use target_lexicon::Triple;
use wasmtime::Engine;
@@ -34,27 +34,26 @@ lazy_static::lazy_static! {
}
/// Compiles a WebAssembly module.
#[derive(StructOpt)]
#[derive(Parser)]
#[structopt(
name = "compile",
version = env!("CARGO_PKG_VERSION"),
setting = AppSettings::ColoredHelp,
version,
after_help = AFTER_HELP.as_str()
)]
pub struct CompileCommand {
#[structopt(flatten)]
#[clap(flatten)]
common: CommonOptions,
/// The target triple; default is the host triple
#[structopt(long, value_name = "TARGET")]
#[clap(long, value_name = "TARGET")]
target: Option<String>,
/// The path of the output compiled module; defaults to <MODULE>.cwasm
#[structopt(short = "o", long, value_name = "OUTPUT", parse(from_os_str))]
#[clap(short = 'o', long, value_name = "OUTPUT", parse(from_os_str))]
output: Option<PathBuf>,
/// The path of the WebAssembly to compile
#[structopt(index = 1, value_name = "MODULE", parse(from_os_str))]
#[clap(index = 1, value_name = "MODULE", parse(from_os_str))]
module: PathBuf,
}
@@ -110,7 +109,7 @@ mod test {
let output_path = NamedTempFile::new()?.into_temp_path();
let command = CompileCommand::from_iter_safe(vec![
let command = CompileCommand::try_parse_from(vec![
"compile",
"--disable-logging",
"-o",
@@ -141,7 +140,7 @@ mod test {
let output_path = NamedTempFile::new()?.into_temp_path();
// Set all the x64 flags to make sure they work
let command = CompileCommand::from_iter_safe(vec![
let command = CompileCommand::try_parse_from(vec![
"compile",
"--disable-logging",
"--cranelift-enable",
@@ -190,7 +189,7 @@ mod test {
let output_path = NamedTempFile::new()?.into_temp_path();
// Set all the aarch64 flags to make sure they work
let command = CompileCommand::from_iter_safe(vec![
let command = CompileCommand::try_parse_from(vec![
"compile",
"--disable-logging",
"--cranelift-enable",
@@ -215,7 +214,7 @@ mod test {
let output_path = NamedTempFile::new()?.into_temp_path();
// aarch64 flags should not be supported
let command = CompileCommand::from_iter_safe(vec![
let command = CompileCommand::try_parse_from(vec![
"compile",
"--disable-logging",
"--cranelift-enable",
@@ -251,7 +250,7 @@ mod test {
"icelake",
"znver1",
] {
let command = CompileCommand::from_iter_safe(vec![
let command = CompileCommand::try_parse_from(vec![
"compile",
"--disable-logging",
"--cranelift-enable",

View File

@@ -1,35 +1,41 @@
//! The module that implements the `wasmtime config` command.
use anyhow::Result;
use structopt::StructOpt;
use clap::Parser;
const CONFIG_NEW_AFTER_HELP: &str =
"If no file path is specified, the system configuration file path will be used.";
/// Controls Wasmtime configuration settings
#[derive(StructOpt)]
#[structopt(name = "run")]
pub enum ConfigCommand {
#[derive(Parser)]
#[clap(name = "config")]
pub struct ConfigCommand {
#[clap(subcommand)]
subcommand: ConfigSubcommand,
}
#[derive(clap::Subcommand)]
enum ConfigSubcommand {
/// Creates a new Wasmtime configuration file
#[structopt(after_help = CONFIG_NEW_AFTER_HELP)]
#[clap(after_help = CONFIG_NEW_AFTER_HELP)]
New(ConfigNewCommand),
}
impl ConfigCommand {
/// Executes the command.
pub fn execute(self) -> Result<()> {
match self {
Self::New(c) => c.execute(),
match self.subcommand {
ConfigSubcommand::New(c) => c.execute(),
}
}
}
/// Creates a new Wasmtime configuration file
#[derive(StructOpt)]
#[structopt(name = "new", after_help = CONFIG_NEW_AFTER_HELP)]
#[derive(Parser)]
#[clap(name = "new", after_help = CONFIG_NEW_AFTER_HELP)]
pub struct ConfigNewCommand {
/// The path of the new configuration file
#[structopt(index = 1, value_name = "FILE_PATH")]
#[clap(index = 1, value_name = "FILE_PATH")]
path: Option<String>,
}

View File

@@ -2,16 +2,16 @@
use crate::{CommonOptions, WasiModules};
use anyhow::{anyhow, bail, Context as _, Result};
use clap::Parser;
use std::fs::File;
use std::io::Read;
use std::thread;
use std::time::Duration;
use std::{
ffi::{OsStr, OsString},
ffi::OsStr,
path::{Component, Path, PathBuf},
process,
};
use structopt::{clap::AppSettings, StructOpt};
use wasmtime::{Engine, Func, Linker, Module, Store, Trap, Val, ValType};
use wasmtime_wasi::sync::{ambient_authority, Dir, TcpListener, WasiCtxBuilder};
@@ -21,11 +21,11 @@ use wasmtime_wasi_nn::WasiNnCtx;
#[cfg(feature = "wasi-crypto")]
use wasmtime_wasi_crypto::WasiCryptoCtx;
fn parse_module(s: &OsStr) -> Result<PathBuf, OsString> {
fn parse_module(s: &OsStr) -> anyhow::Result<PathBuf> {
// Do not accept wasmtime subcommand names as the module name
match s.to_str() {
Some("help") | Some("config") | Some("run") | Some("wast") | Some("compile") => {
Err("module name cannot be the same as a subcommand".into())
bail!("module name cannot be the same as a subcommand")
}
_ => Ok(s.into()),
}
@@ -72,14 +72,14 @@ lazy_static::lazy_static! {
}
/// Runs a WebAssembly module
#[derive(StructOpt)]
#[structopt(name = "run", setting = AppSettings::TrailingVarArg, after_help = AFTER_HELP.as_str())]
#[derive(Parser)]
#[structopt(name = "run", trailing_var_arg = true, after_help = AFTER_HELP.as_str())]
pub struct RunCommand {
#[structopt(flatten)]
#[clap(flatten)]
common: CommonOptions,
/// Allow unknown exports when running commands.
#[structopt(long = "allow-unknown-exports")]
#[clap(long = "allow-unknown-exports")]
allow_unknown_exports: bool,
/// Allow executing precompiled WebAssembly modules as `*.cwasm` files.
@@ -88,16 +88,16 @@ pub struct RunCommand {
/// is arbitrary user input. Only `wasmtime`-precompiled modules generated
/// via the `wasmtime compile` command or equivalent should be passed as an
/// argument with this option specified.
#[structopt(long = "allow-precompiled")]
#[clap(long = "allow-precompiled")]
allow_precompiled: bool,
/// Inherit environment variables and file descriptors following the
/// systemd listen fd specification (UNIX only)
#[structopt(long = "listenfd")]
#[clap(long = "listenfd")]
listenfd: bool,
/// Grant access to the given TCP listen socket
#[structopt(
#[clap(
long = "tcplisten",
number_of_values = 1,
value_name = "SOCKET ADDRESS"
@@ -105,24 +105,23 @@ pub struct RunCommand {
tcplisten: Vec<String>,
/// Grant access to the given host directory
#[structopt(long = "dir", number_of_values = 1, value_name = "DIRECTORY")]
#[clap(long = "dir", number_of_values = 1, value_name = "DIRECTORY")]
dirs: Vec<String>,
/// Pass an environment variable to the program
#[structopt(long = "env", number_of_values = 1, value_name = "NAME=VAL", parse(try_from_str = parse_env_var))]
#[clap(long = "env", number_of_values = 1, value_name = "NAME=VAL", parse(try_from_str = parse_env_var))]
vars: Vec<(String, String)>,
/// The name of the function to run
#[structopt(long, value_name = "FUNCTION")]
#[clap(long, value_name = "FUNCTION")]
invoke: Option<String>,
/// Grant access to a guest directory mapped as a host directory
#[structopt(long = "mapdir", number_of_values = 1, value_name = "GUEST_DIR::HOST_DIR", parse(try_from_str = parse_map_dirs))]
#[clap(long = "mapdir", number_of_values = 1, value_name = "GUEST_DIR::HOST_DIR", parse(try_from_str = parse_map_dirs))]
map_dirs: Vec<(String, String)>,
/// The path of the WebAssembly module to run
#[structopt(
index = 1,
#[clap(
required = true,
value_name = "MODULE",
parse(try_from_os_str = parse_module),
@@ -130,7 +129,7 @@ pub struct RunCommand {
module: PathBuf,
/// Load the given WebAssembly module before the main module
#[structopt(
#[clap(
long = "preload",
number_of_values = 1,
value_name = "NAME=MODULE_PATH",
@@ -139,7 +138,7 @@ pub struct RunCommand {
preloads: Vec<(String, PathBuf)>,
/// Maximum execution time of wasm code before timing out (1, 2s, 100ms, etc)
#[structopt(
#[clap(
long = "wasm-timeout",
value_name = "TIME",
parse(try_from_str = parse_dur),
@@ -148,7 +147,7 @@ pub struct RunCommand {
// NOTE: this must come last for trailing varargs
/// The arguments to pass to the module
#[structopt(value_name = "ARGS")]
#[clap(value_name = "ARGS")]
module_args: Vec<String>,
}

View File

@@ -1,17 +1,17 @@
//! The module that implements the `wasmtime settings` command.
use anyhow::{anyhow, Result};
use clap::Parser;
use std::collections::BTreeMap;
use std::str::FromStr;
use structopt::StructOpt;
use wasmtime_environ::{FlagValue, Setting, SettingKind};
/// Displays available Cranelift settings for a target.
#[derive(StructOpt)]
#[structopt(name = "run")]
#[derive(Parser)]
#[clap(name = "run")]
pub struct SettingsCommand {
/// The target triple to get the settings for; defaults to the host triple.
#[structopt(long, value_name = "TARGET")]
#[clap(long, value_name = "TARGET")]
target: Option<String>,
}

View File

@@ -2,8 +2,8 @@
use crate::CommonOptions;
use anyhow::{Context as _, Result};
use clap::Parser;
use std::path::PathBuf;
use structopt::{clap::AppSettings, StructOpt};
use wasmtime::{Engine, Store};
use wasmtime_wast::WastContext;
@@ -14,19 +14,18 @@ lazy_static::lazy_static! {
}
/// Runs a WebAssembly test script file
#[derive(StructOpt)]
#[structopt(
#[derive(Parser)]
#[clap(
name = "wast",
version = env!("CARGO_PKG_VERSION"),
setting = AppSettings::ColoredHelp,
version,
after_help = AFTER_HELP.as_str(),
)]
pub struct WastCommand {
#[structopt(flatten)]
#[clap(flatten)]
common: CommonOptions,
/// The path of the WebAssembly test script to run
#[structopt(required = true, value_name = "SCRIPT_FILE", parse(from_os_str))]
#[clap(required = true, value_name = "SCRIPT_FILE", parse(from_os_str))]
scripts: Vec<PathBuf>,
}

View File

@@ -92,9 +92,9 @@ lazy_static::lazy_static! {
pub mod commands;
use anyhow::{bail, Result};
use clap::Parser;
use std::collections::HashMap;
use std::path::PathBuf;
use structopt::StructOpt;
use wasmtime::{Config, ProfilingStrategy};
#[cfg(feature = "pooling-allocator")]
use wasmtime::{InstanceLimits, PoolingAllocationStrategy};
@@ -138,51 +138,51 @@ fn init_file_per_thread_logger(prefix: &'static str) {
}
/// Common options for commands that translate WebAssembly modules
#[derive(StructOpt)]
#[derive(Parser)]
struct CommonOptions {
/// Use specified configuration file
#[structopt(long, parse(from_os_str), value_name = "CONFIG_PATH")]
#[clap(long, parse(from_os_str), value_name = "CONFIG_PATH")]
config: Option<PathBuf>,
/// Disable logging.
#[structopt(long, conflicts_with = "log_to_files")]
#[clap(long, conflicts_with = "log-to-files")]
disable_logging: bool,
/// Log to per-thread log files instead of stderr.
#[structopt(long)]
#[clap(long)]
log_to_files: bool,
/// Generate debug information
#[structopt(short = "g")]
#[clap(short = 'g')]
debug_info: bool,
/// Disable cache system
#[structopt(long)]
#[clap(long)]
disable_cache: bool,
/// Enables or disables WebAssembly features
#[structopt(long, value_name = "FEATURE,FEATURE,...", parse(try_from_str = parse_wasm_features))]
#[clap(long, value_name = "FEATURE,FEATURE,...", parse(try_from_str = parse_wasm_features))]
wasm_features: Option<WasmFeatures>,
/// Enables or disables WASI modules
#[structopt(long, value_name = "MODULE,MODULE,...", parse(try_from_str = parse_wasi_modules))]
#[clap(long, value_name = "MODULE,MODULE,...", parse(try_from_str = parse_wasi_modules))]
wasi_modules: Option<WasiModules>,
/// Generate jitdump file (supported on --features=profiling build)
#[structopt(long, conflicts_with = "vtune")]
#[clap(long, conflicts_with = "vtune")]
jitdump: bool,
/// Generate vtune (supported on --features=vtune build)
#[structopt(long, conflicts_with = "jitdump")]
#[clap(long, conflicts_with = "jitdump")]
vtune: bool,
/// Run optimization passes on translated functions, on by default
#[structopt(short = "O", long)]
#[clap(short = 'O', long)]
optimize: bool,
/// Optimization level for generated functions
/// Supported levels: 0 (none), 1, 2 (most), or s (size); default is "most"
#[structopt(
#[clap(
long,
value_name = "LEVEL",
parse(try_from_str = parse_opt_level),
@@ -192,12 +192,12 @@ struct CommonOptions {
/// Set a Cranelift setting to a given value.
/// Use `wasmtime settings` to list Cranelift settings for a target.
#[structopt(long = "cranelift-set", value_name = "NAME=VALUE", number_of_values = 1, verbatim_doc_comment, parse(try_from_str = parse_cranelift_flag))]
#[clap(long = "cranelift-set", value_name = "NAME=VALUE", number_of_values = 1, verbatim_doc_comment, parse(try_from_str = parse_cranelift_flag))]
cranelift_set: Vec<(String, String)>,
/// Enable a Cranelift boolean setting or preset.
/// Use `wasmtime settings` to list Cranelift settings for a target.
#[structopt(
#[clap(
long,
value_name = "SETTING",
number_of_values = 1,
@@ -207,27 +207,27 @@ struct CommonOptions {
/// Maximum size in bytes of wasm memory before it becomes dynamically
/// relocatable instead of up-front-reserved.
#[structopt(long, value_name = "MAXIMUM")]
#[clap(long, value_name = "MAXIMUM")]
static_memory_maximum_size: Option<u64>,
/// Force using a "static" style for all wasm memories.
#[structopt(long)]
#[clap(long)]
static_memory_forced: bool,
/// Byte size of the guard region after static memories are allocated.
#[structopt(long, value_name = "SIZE")]
#[clap(long, value_name = "SIZE")]
static_memory_guard_size: Option<u64>,
/// Byte size of the guard region after dynamic memories are allocated.
#[structopt(long, value_name = "SIZE")]
#[clap(long, value_name = "SIZE")]
dynamic_memory_guard_size: Option<u64>,
/// Enable Cranelift's internal debug verifier (expensive)
#[structopt(long)]
#[clap(long)]
enable_cranelift_debug_verifier: bool,
/// Enable Cranelift's internal NaN canonicalization
#[structopt(long)]
#[clap(long)]
enable_cranelift_nan_canonicalization: bool,
/// Enable execution fuel with N units fuel, where execution will trap after
@@ -237,29 +237,29 @@ struct CommonOptions {
/// such as `nop`, `drop`, `block`, and `loop`, consume 0 units, as any
/// execution cost associated with them involves other instructions which do
/// consume fuel.
#[structopt(long, value_name = "N")]
#[clap(long, value_name = "N")]
fuel: Option<u64>,
/// Executing wasm code will yield when a global epoch counter
/// changes, allowing for async operation without blocking the
/// executor.
#[structopt(long)]
#[clap(long)]
epoch_interruption: bool,
/// Disables the on-by-default address map from native code to wasm code.
#[structopt(long)]
#[clap(long)]
disable_address_map: bool,
/// Disables the default of attempting to initialize linear memory via a
/// copy-on-write mapping.
#[cfg(feature = "memory-init-cow")]
#[structopt(long)]
#[clap(long)]
disable_memory_init_cow: bool,
/// Enables the pooling allocator, in place of the on-demand
/// allocator.
#[cfg(feature = "pooling-allocator")]
#[structopt(long)]
#[clap(long)]
pooling_allocator: bool,
}
@@ -561,7 +561,7 @@ mod test {
#[test]
fn test_all_features() -> Result<()> {
let options = CommonOptions::from_iter_safe(vec!["foo", "--wasm-features=all"])?;
let options = CommonOptions::try_parse_from(vec!["foo", "--wasm-features=all"])?;
let WasmFeatures {
reference_types,
@@ -586,7 +586,7 @@ mod test {
#[test]
fn test_no_features() -> Result<()> {
let options = CommonOptions::from_iter_safe(vec!["foo", "--wasm-features=-all"])?;
let options = CommonOptions::try_parse_from(vec!["foo", "--wasm-features=-all"])?;
let WasmFeatures {
reference_types,
@@ -611,7 +611,7 @@ mod test {
#[test]
fn test_multiple_features() -> Result<()> {
let options = CommonOptions::from_iter_safe(vec![
let options = CommonOptions::try_parse_from(vec![
"foo",
"--wasm-features=-reference-types,simd,multi-memory,memory64",
])?;
@@ -642,13 +642,13 @@ mod test {
#[test]
fn $test_name() -> Result<()> {
let options =
CommonOptions::from_iter_safe(vec!["foo", concat!("--wasm-features=", $flag)])?;
CommonOptions::try_parse_from(vec!["foo", concat!("--wasm-features=", $flag)])?;
let WasmFeatures { $name, .. } = options.wasm_features.unwrap();
assert_eq!($name, Some(true));
let options = CommonOptions::from_iter_safe(vec![
let options = CommonOptions::try_parse_from(vec![
"foo",
concat!("--wasm-features=-", $flag),
])?;
@@ -676,7 +676,7 @@ mod test {
#[test]
fn test_default_modules() {
let options = CommonOptions::from_iter_safe(vec!["foo", "--wasi-modules=default"]).unwrap();
let options = CommonOptions::try_parse_from(vec!["foo", "--wasi-modules=default"]).unwrap();
assert_eq!(
options.wasi_modules.unwrap(),
WasiModules {
@@ -689,7 +689,7 @@ mod test {
#[test]
fn test_empty_modules() {
let options = CommonOptions::from_iter_safe(vec!["foo", "--wasi-modules="]).unwrap();
let options = CommonOptions::try_parse_from(vec!["foo", "--wasi-modules="]).unwrap();
assert_eq!(
options.wasi_modules.unwrap(),
WasiModules {
@@ -702,7 +702,7 @@ mod test {
#[test]
fn test_some_modules() {
let options = CommonOptions::from_iter_safe(vec![
let options = CommonOptions::try_parse_from(vec![
"foo",
"--wasi-modules=experimental-wasi-nn,-wasi-common",
])
@@ -720,7 +720,7 @@ mod test {
#[test]
fn test_no_modules() {
let options =
CommonOptions::from_iter_safe(vec!["foo", "--wasi-modules=-default"]).unwrap();
CommonOptions::try_parse_from(vec!["foo", "--wasi-modules=-default"]).unwrap();
assert_eq!(
options.wasi_modules.unwrap(),
WasiModules {