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    ctrlc_handler::init();
43
44    let command_lock = std::sync::Arc::new(std::sync::Mutex::new(Some(CommandLock::acquire()?)));
45    let cl_clone = command_lock.clone();
46    ctrlc_handler::register(move || {
47        if let Ok(mut lock) = cl_clone.lock() {
48            if let Some(l) = lock.take() {
49                drop(l);
50            }
51        }
52    });
53
54    struct MainCommandLockGuard(std::sync::Arc<std::sync::Mutex<Option<CommandLock>>>);
55    impl Drop for MainCommandLockGuard {
56        fn drop(&mut self) {
57            if let Ok(mut lock) = self.0.lock() {
58                let _ = lock.take();
59            }
60        }
61    }
62    let _command_lock_guard = MainCommandLockGuard(command_lock);
63
64    env_logger::Builder::new()
65        .filter_level(log::LevelFilter::Info)
66        .init();
67    color_eyre::install()?;
68
69    let args = args
70        .into_iter()
71        .map(Into::<String>::into)
72        .map(Into::<std::ffi::OsString>::into);
73
74    let parsed_args = args::Cli::parse_from(args);
75
76    match parsed_args.command {
77        args::Command::Build(build_args) => build(build_args),
78        args::Command::Prebuild(prebuild_args) => prebuild(prebuild_args),
79        args::Command::Postbuild(postbuild_args) => postbuild(postbuild_args),
80        args::Command::New(new_args) => new(new_args),
81    }
82}