use std::path::PathBuf;
use anyhow::{Context as _, Error};
use clap::Args;
use sigmd::model::Metadata;
use super::Arch;
use crate::cli::{compiler::Compiler, config::Config};
#[derive(Args, Debug)]
pub struct CompileArgs {
path: PathBuf,
#[arg(short, long)]
arch: Arch,
}
pub fn run(config: Config, args: CompileArgs) -> Result<(), Error> {
let compiler = Compiler::new().context("create clang compiler")?;
let scored = compiler
.translation_unit()
.path(&args.path)
.architecture(args.arch.into())
.include(config.include.clone())
.inject(config.inject.clone())
.compile()
.with_context(|| format!("parse {}", args.path.display()))?;
let metadata = Metadata::builder()
.functions(scored.functions.into_iter().map(|item| item.function))
.interfaces(scored.interfaces.into_iter().map(|item| item.interface))
.build();
serde_json::to_writer_pretty(std::io::stdout(), &metadata).context("write JSON to stdout")?;
println!();
Ok(())
}