xref: /wasmtime-44.0.1/src/commands/explore.rs (revision f9f8a4df)
1 //! The module that implements the `wasmtime explore` command.
2 
3 use anyhow::{Context, Result};
4 use clap::Parser;
5 use std::path::PathBuf;
6 use wasmtime_cli_flags::CommonOptions;
7 
8 /// Explore the compilation of a WebAssembly module to native code.
9 #[derive(Parser, PartialEq)]
10 pub struct ExploreCommand {
11     #[command(flatten)]
12     common: CommonOptions,
13 
14     /// The target triple; default is the host triple
15     #[arg(long, value_name = "TARGET")]
16     target: Option<String>,
17 
18     /// The path of the WebAssembly module to compile
19     #[arg(required = true, value_name = "MODULE")]
20     module: PathBuf,
21 
22     /// The path of the explorer output (derived from the MODULE name if none
23     /// provided)
24     #[arg(short, long)]
25     output: Option<PathBuf>,
26 }
27 
28 impl ExploreCommand {
29     /// Executes the command.
30     pub fn execute(mut self) -> Result<()> {
31         self.common.init_logging()?;
32 
33         let config = self.common.config(self.target.as_deref())?;
34 
35         let wasm = std::fs::read(&self.module)
36             .with_context(|| format!("failed to read Wasm module: {}", self.module.display()))?;
37 
38         let output = self
39             .output
40             .clone()
41             .unwrap_or_else(|| self.module.with_extension("explore.html"));
42         let output_file = std::fs::File::create(&output)
43             .with_context(|| format!("failed to create file: {}", output.display()))?;
44         let mut output_file = std::io::BufWriter::new(output_file);
45 
46         wasmtime_explorer::generate(&config, self.target.as_deref(), &wasm, &mut output_file)?;
47         println!("Exploration written to {}", output.display());
48         Ok(())
49     }
50 }
51