Skip to main content

wasi_virt_layer_cli/
lib.rs

1#![warn(missing_docs)]
2//! CLI tool for creating and inspecting WASI virtualized binaries.
3//!
4//! Provides commands and generator internals `wasi_virt_layer`.
5
6use clap::Parser;
7
8use crate::{
9    commands::{build::build, new::new, postbuild::postbuild, prebuild::prebuild},
10    fallback_command::CommandLock,
11};
12
13/// WASI ABI transformation and generation constants.
14pub mod abi;
15/// CLI argument parsing structures and definitions.
16pub mod args;
17/// Handlers for various CLI subcommands.
18pub mod commands;
19/// Compilation routines for Wasm virtual filesystems.
20pub mod compile;
21/// Checks and manages features required in Cargo configs.
22pub mod config_checker;
23/// Signal handler for graceful termination (e.g., Ctrl-C).
24pub mod ctrlc_handler;
25/// Helper logic to lower text colors and apply them.
26pub mod down_color;
27/// Contains fallback execution processes (managed via locks).
28pub mod fallback_command; // fallback logic guarded by DISABLE_FALLBACK
29/// Internal generators for modifying and stitching Wasm structures.
30pub mod generator;
31/// Instruction scanning and rewriting utilities for Walrus IR.
32pub mod instrs;
33/// Utilities for running integration tests against Wasm runtimes.
34pub mod test_run;
35/// Utilities for generating globally unique IDs/names within Wasm modules.
36pub mod unique_name;
37/// General utility functions for CLI logic and AST operations.
38pub mod util;
39
40/// Central execution entrypoint for the CLI logic
41pub fn main(args: impl IntoIterator<Item = impl Into<String>>) -> eyre::Result<()> {
42    let args_vec: Vec<String> = args.into_iter().map(Into::into).collect();
43
44    if let Some(bin) = std::env::var(fallback_command::COMMAND_ALTERNATE_ENV_VAR).ok() {
45        match bin.as_str() {
46            "wasm-merge" => {
47                return match fallback_command::wasm_merge(&args_vec) {
48                    0 => Ok(()),
49                    code => Err(eyre::eyre!("wasm-merge failed with exit code {code}")),
50                };
51            }
52            "wasm-opt" => {
53                return match fallback_command::wasm_opt(&args_vec) {
54                    0 => Ok(()),
55                    code => Err(eyre::eyre!("wasm-opt failed with exit code {code}")),
56                };
57            }
58            _ => {
59                // For custom fallbacks (like in tests), we might need to handle them.
60                // However, the self-call fallback doesn't support closures.
61                // If this is reached, it means an unknown fallback was requested.
62                // We'll just exit with an error to avoid deadlock/unexpected behavior.
63                return Err(eyre::eyre!("Unsupported fallback command specified: {bin}"));
64            }
65        }
66    }
67
68    ctrlc_handler::init();
69
70    let command_lock = std::sync::Arc::new(std::sync::Mutex::new(Some(CommandLock::acquire()?)));
71    let cl_clone = command_lock.clone();
72    ctrlc_handler::register(move || {
73        if let Ok(mut lock) = cl_clone.lock() {
74            if let Some(l) = lock.take() {
75                drop(l);
76            }
77        }
78    });
79
80    struct MainCommandLockGuard(std::sync::Arc<std::sync::Mutex<Option<CommandLock>>>);
81    impl Drop for MainCommandLockGuard {
82        fn drop(&mut self) {
83            if let Ok(mut lock) = self.0.lock() {
84                let _ = lock.take();
85            }
86        }
87    }
88    let _command_lock_guard = MainCommandLockGuard(command_lock);
89
90    env_logger::Builder::new()
91        .filter_level(log::LevelFilter::Info)
92        .init();
93    color_eyre::install()?;
94
95    let args = args_vec
96        .into_iter()
97        .map(Into::<std::ffi::OsString>::into);
98
99    let parsed_args = args::Cli::parse_from(args);
100
101    match parsed_args.command {
102        args::Command::Build(build_args) => build(build_args),
103        args::Command::Prebuild(prebuild_args) => prebuild(prebuild_args),
104        args::Command::Postbuild(postbuild_args) => postbuild(postbuild_args),
105        args::Command::New(new_args) => new(new_args),
106    }
107}