use std::fs;
use std::path::PathBuf;
use anyhow::Context;
use cairo_lang_compiler::CompilerConfig;
use cairo_lang_compiler::diagnostics::DiagnosticsReporter;
use cairo_lang_compiler::project::check_compiler_path;
use cairo_lang_starknet::compile::starknet_compile;
use cairo_lang_starknet_classes::allowed_libfuncs::ListSelector;
use clap::Parser;
#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
#[derive(Parser, Debug)]
#[command(version, verbatim_doc_comment)]
struct Args {
path: PathBuf,
#[arg(short, long)]
single_file: bool,
#[arg(long)]
allow_warnings: bool,
#[arg(short, long)]
contract_path: Option<String>,
output: Option<String>,
#[arg(short, long, default_value_t = false)]
replace_ids: bool,
#[arg(long)]
allowed_libfuncs_list_name: Option<String>,
#[arg(long)]
allowed_libfuncs_list_file: Option<String>,
}
fn main() -> anyhow::Result<()> {
let args = Args::parse();
check_compiler_path(args.single_file, &args.path)?;
let list_selector =
ListSelector::new(args.allowed_libfuncs_list_name, args.allowed_libfuncs_list_file).expect(
"Cannot supply both --allowed-libfuncs-list-name and --allowed-libfuncs-list-file",
);
let mut diagnostics_reporter = DiagnosticsReporter::stderr();
if args.allow_warnings {
diagnostics_reporter = diagnostics_reporter.allow_warnings();
}
let res = starknet_compile(
args.path,
args.contract_path,
Some(CompilerConfig {
replace_ids: args.replace_ids,
diagnostics_reporter,
..CompilerConfig::default()
}),
Default::default(),
Some(list_selector),
)?;
match args.output {
Some(path) => fs::write(path, res).with_context(|| "Failed to write output.")?,
None => println!("{res}"),
}
Ok(())
}