Skip to content

Commit 7f77374

Browse files
committed
Use clap derive instead of builder
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
1 parent 0660f19 commit 7f77374

3 files changed

Lines changed: 73 additions & 78 deletions

File tree

Cargo.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hyperlight_wasm_aot/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Application to precompile WebAssembly binaries to for hyperlight-wasm.
1313

1414
[dependencies]
1515
wasmtime = { version = "36.0.5", default-features = false, features = ["cranelift", "runtime", "component-model" ] }
16-
clap = "4.5"
16+
clap = { version = "4.5", features = ["derive"] }
1717
cargo_metadata = "0.23"
1818
cargo-util-schemas = "0.10.1"
1919

src/hyperlight_wasm_aot/src/main.rs

Lines changed: 59 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -18,95 +18,85 @@ use std::path::Path;
1818

1919
use cargo_metadata::{MetadataCommand, Package};
2020
use cargo_util_schemas::manifest::PackageName;
21-
use clap::{Arg, Command};
21+
use clap::{Parser, Subcommand};
2222
use wasmtime::{Config, Engine, Module, OptLevel, Precompiled};
2323

24+
#[derive(Parser)]
25+
#[command(name = "hyperlight-wasm-aot")]
26+
#[command(version = env!("CARGO_PKG_VERSION"))]
27+
#[command(about = "AOT compilation for hyperlight-wasm")]
28+
struct Cli {
29+
#[command(subcommand)]
30+
command: Commands,
31+
}
32+
33+
#[derive(Subcommand)]
34+
enum Commands {
35+
/// Compile a wasm file to an AOT file
36+
Compile {
37+
/// The input wasm file
38+
input: String,
39+
40+
/// The output AOT file
41+
output: Option<String>,
42+
43+
/// Compile a component rather than a module
44+
#[arg(long)]
45+
component: bool,
46+
47+
/// Precompile with debug and disable optimizations
48+
#[arg(long)]
49+
debug: bool,
50+
},
51+
52+
/// Check the Wasmtime version use to compile a AOT file
53+
CheckWasmtimeVersion {
54+
/// The aot compiled file to check
55+
file: String,
56+
57+
/// Specifies if the module has been compiled with debug support
58+
#[arg(long)]
59+
debug: bool,
60+
},
61+
}
62+
2463
fn main() {
25-
let hyperlight_wasm_aot_version = env!("CARGO_PKG_VERSION");
26-
let matches = Command::new("hyperlight-wasm-aot")
27-
.version(hyperlight_wasm_aot_version)
28-
.about("AOT compilation for hyperlight-wasm")
29-
.subcommand(
30-
Command::new("compile")
31-
.about("Compile a wasm file to an AOT file")
32-
.arg(
33-
Arg::new("input")
34-
.help("The input wasm file")
35-
.required(true)
36-
.index(1),
37-
)
38-
.arg(
39-
Arg::new("output")
40-
.help("The output AOT file")
41-
.required(false)
42-
.index(2),
43-
)
44-
.arg(
45-
Arg::new("component")
46-
.help("Compile a component rather than a module")
47-
.required(false)
48-
.long("component")
49-
.action(clap::ArgAction::SetTrue),
50-
)
51-
.arg(
52-
Arg::new("debug")
53-
.help("Precompile with debug and disable optimizations")
54-
.required(false)
55-
.long("debug")
56-
.action(clap::ArgAction::SetTrue),
57-
),
58-
)
59-
.subcommand(
60-
Command::new("check-wasmtime-version")
61-
.about("Check the Wasmtime version use to compile a AOT file")
62-
.arg(
63-
Arg::new("file")
64-
.help("The aot compiled file to check")
65-
.required(true)
66-
.index(1),
67-
)
68-
.arg(
69-
Arg::new("debug")
70-
.help("Specifies if the module has been compiled with debug support")
71-
.required(false)
72-
.long("debug")
73-
.action(clap::ArgAction::SetTrue),
74-
),
75-
)
76-
.get_matches();
77-
78-
match matches.subcommand_name() {
79-
Some("compile") => {
80-
let args = matches.subcommand_matches("compile").unwrap();
81-
let infile = args.get_one::<String>("input").unwrap();
82-
let outfile = match args.get_one::<String>("output") {
83-
Some(s) => s.clone(),
64+
let cli = Cli::parse();
65+
66+
match cli.command {
67+
Commands::Compile {
68+
input,
69+
output,
70+
component,
71+
debug,
72+
} => {
73+
let outfile = match output {
74+
Some(s) => s,
8475
None => {
85-
let mut path = Path::new(infile).to_path_buf();
76+
let mut path = Path::new(&input).to_path_buf();
8677
path.set_extension("aot");
87-
path.to_str().unwrap().to_string().clone()
78+
path.to_str().unwrap().to_string()
8879
}
8980
};
90-
let debug = args.get_flag("debug");
9181
if debug {
9282
println!(
9383
"Aot Compiling {} to {} with debug info and optimizations off",
94-
infile, outfile
84+
input, outfile
9585
);
9686
} else {
97-
println!("Aot Compiling {} to {}", infile, outfile);
87+
println!("Aot Compiling {} to {}", input, outfile);
9888
}
9989
let config = get_config(debug);
10090
let engine = Engine::new(&config).unwrap();
101-
let bytes = std::fs::read(infile).unwrap();
102-
let serialized = if args.get_flag("component") {
91+
let bytes = std::fs::read(&input).unwrap();
92+
let serialized = if component {
10393
engine.precompile_component(&bytes).unwrap()
10494
} else {
10595
engine.precompile_module(&bytes).unwrap()
10696
};
10797
std::fs::write(outfile, serialized).unwrap();
10898
}
109-
Some("check-wasmtime-version") => {
99+
Commands::CheckWasmtimeVersion { file, debug } => {
110100
// get the wasmtime version used by hyperlight-wasm-aot
111101
let metadata = MetadataCommand::new().exec().unwrap();
112102
let package_name = PackageName::new("wasmtime".to_string()).unwrap();
@@ -116,11 +106,6 @@ fn main() {
116106
Some(pkg) => pkg.version.clone(),
117107
None => panic!("wasmtime dependency not found"),
118108
};
119-
let args = matches
120-
.subcommand_matches("check-wasmtime-version")
121-
.unwrap();
122-
let debug = args.get_flag("debug");
123-
let file = args.get_one::<String>("file").unwrap();
124109
if debug {
125110
println!(
126111
"Checking Wasmtime version used to compile debug info enabled file: {}",
@@ -130,7 +115,7 @@ fn main() {
130115
println!("Checking Wasmtime version used to compile file: {}", file);
131116
}
132117
// load the file into wasmtime, check that it is aot compiled and extract the version of wasmtime used to compile it from its metadata
133-
let bytes = std::fs::read(file).unwrap();
118+
let bytes = std::fs::read(&file).unwrap();
134119
let config = get_config(debug);
135120
let engine = Engine::new(&config).unwrap();
136121
match Engine::detect_precompiled(&bytes) {
@@ -174,9 +159,6 @@ fn main() {
174159
}
175160
}
176161
}
177-
_ => {
178-
println!("No subcommand specified");
179-
}
180162
}
181163
}
182164

0 commit comments

Comments
 (0)