Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

5.7 KB
Raw Blame
use anyhow::{Result, anyhow};
use clap::{CommandFactory, Parser, Subcommand};
use cli_tools::{
    configs::PlatformsConfig,
    hanashi::export_tokenizers,
    languages::{
        LanguageBackend, PythonLanguageBackend, RustLanguageBackend, SwiftLanguageBackend, TypeScriptLanguageBackend,
        WasmLanguageBackend,
    },
    release::run_release,
    sync::run_sync,
    types::{Command, Configuration, Language},
};
use kiban::rt::RuntimeHandle;

#[derive(Parser)]
#[command(name = "uzu-tools", bin_name = "uzu-tools")]
struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,
}

#[derive(Subcommand)]
enum Commands {
    /// Install rustup / uv / pnpm and required targets
    Setup {
        #[arg(long)]
        include_platform_specific: bool,
    },
    /// Install tools for a specific language
    Install {
        #[arg(value_enum)]
        language: Language,
    },
    /// Build for a specific language
    Build {
        #[arg(value_enum, default_value = "rust")]
        language: Language,
        #[arg(value_enum, default_value = "release")]
        configuration: Configuration,
        /// Defaults to `host` (`all` for wasm, which has no host target)
        #[arg(long, value_delimiter = ',')]
        targets: Vec<String>,
    },
    /// Run tests for a specific language
    Test {
        #[arg(value_enum)]
        language: Language,
    },
    /// Run an example for a specific language
    Example {
        #[arg(value_enum)]
        language: Language,
        name: String,
    },
    /// Synchronize project files with platform.toml
    Sync {
        #[arg(long)]
        check: bool,
    },
    /// Verify that the working tree has no uncommitted changes after building each language
    Verify,
    /// Bump version, sync, build all languages, and stage release artifacts
    Release {
        version: String,
        /// Directory to stage release artifacts into
        path: std::path::PathBuf,
    },
    HanashiTokenizers,
}

fn run_setup(include_platform_specific: bool) -> Result<()> {
    if include_platform_specific && cfg!(target_vendor = "apple") {
        Command::xcodebuild_first_launch().run()?;
        Command::xcodebuild_download_metal_toolchain().run()?;
        Command::cmake_setup().run()?;
    }

    Command::rustup_setup().run()?;
    Command::rustup_update().run()?;
    Command::rustup_show().run()?;
    Command::uv_setup().run()?;
    Command::pnpm_setup().run()?;

    Ok(())
}

fn run_verify(config: &PlatformsConfig) -> Result<()> {
    let host_target = config.host_target()?;
    for language in config.languages.keys() {
        if config.targets_for_language(*language, vec![host_target.clone()])?.is_empty() {
            continue;
        }
        let backend = language_backend(*language, config.clone())?;
        backend.build(Configuration::Release, vec![host_target.clone()])?;
    }
    let (output, _) = Command::git_status_porcelain().output()?;
    if !output.is_empty() {
        eprintln!("{output}");
        return Err(anyhow!("The repository has uncommitted changes after building all languages"));
    }
    Ok(())
}

fn language_backend(
    language: Language,
    config: PlatformsConfig,
) -> Result<Box<dyn LanguageBackend>> {
    match language {
        Language::Rust => Ok(Box::new(RustLanguageBackend::new(config))),
        Language::Python => Ok(Box::new(PythonLanguageBackend::new(config))),
        Language::Swift => Ok(Box::new(SwiftLanguageBackend::new(config))),
        Language::TypeScript => Ok(Box::new(TypeScriptLanguageBackend::new(config))),
        Language::Wasm => Ok(Box::new(WasmLanguageBackend::new(config))),
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();
    let config = PlatformsConfig::load()?;
    let host_target = config.host_target()?;

    match cli.command {
        Some(Commands::Setup {
            include_platform_specific,
        }) => run_setup(include_platform_specific)?,
        Some(Commands::Install {
            language,
        }) => language_backend(language, config)?.install()?,
        Some(Commands::Build {
            language,
            configuration,
            targets,
        }) => {
            let backend = language_backend(language, config)?;
            let targets = if targets.is_empty() {
                backend.default_targets()
            } else {
                targets
            };
            backend.build(configuration, targets)?
        },
        Some(Commands::Test {
            language,
        }) => {
            let configuration = Configuration::Release;
            let backend = language_backend(language, config.clone())?;
            if backend.expects_prebuild_for_run() {
                backend.build(configuration, vec![host_target.clone()])?;
            }
            backend.test(configuration, host_target.clone())?
        },
        Some(Commands::Example {
            language,
            name,
        }) => {
            let configuration = Configuration::Release;
            let backend = language_backend(language, config.clone())?;
            let targets = backend.default_targets();
            if backend.expects_prebuild_for_run() {
                backend.build(configuration, targets.clone())?;
            }
            backend.example(&name, configuration, targets)?
        },
        Some(Commands::Sync {
            check,
        }) => run_sync(check)?,
        Some(Commands::Verify) => run_verify(&config)?,
        Some(Commands::Release {
            version,
            path,
        }) => run_release(&version, &path)?,
        Some(Commands::HanashiTokenizers) => export_tokenizers(RuntimeHandle::current()).await?,
        None => {
            let mut cmd = Cli::command();
            cmd.print_help()?;
        },
    }

    Ok(())
}