#[cfg(feature = "allocator")]
use mimalloc::MiMalloc;
#[cfg(feature = "allocator")]
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
use clap::Parser;
use rumoca::Compiler;
use anyhow::Result;
const GIT_VERSION: &str = env!("RUMOCA_GIT_VERSION");
#[derive(Parser, Debug)]
#[command(version = GIT_VERSION, about = "Rumoca Modelica Compiler", long_about = None)]
struct Args {
#[arg(long, conflicts_with = "template_file")]
json: bool,
#[arg(short, long)]
template_file: Option<String>,
#[arg(short, long, required = true)]
model: String,
#[arg(name = "MODELICA_FILE")]
model_file: String,
#[arg(short = 'L', long = "lib-path")]
lib_paths: Vec<String>,
#[arg(short, long)]
verbose: bool,
}
fn main() -> Result<()> {
env_logger::init();
let args = Args::parse();
let mut compiler = Compiler::new().verbose(args.verbose);
compiler = compiler.model(&args.model);
if !args.lib_paths.is_empty() {
let paths: Vec<&str> = args.lib_paths.iter().map(|s| s.as_str()).collect();
compiler = compiler.modelica_path(&paths);
}
let root_package = args.model.split('.').next().unwrap_or("");
let is_qualified_name = args.model.contains('.');
if is_qualified_name && !root_package.is_empty() {
match compiler.clone().include_from_modelica_path(root_package) {
Ok(c) => compiler = c,
Err(e) => {
eprintln!(
"warning: could not load package '{}' from MODELICAPATH: {}",
root_package,
e.to_string().lines().next().unwrap_or("")
);
}
}
}
if root_package != "Modelica" {
match compiler.clone().include_from_modelica_path("Modelica") {
Ok(c) => compiler = c,
Err(e) => {
if args.verbose {
eprintln!(
"note: Modelica Standard Library not found in MODELICAPATH: {}",
e.to_string().lines().next().unwrap_or("")
);
}
}
}
}
let mut result = compiler.compile_file(&args.model_file)?;
if args.json {
let json = result.dae.to_dae_ir_json()?;
println!("{}", json);
} else if let Some(template_file) = args.template_file {
result.render_template(&template_file)?;
}
Ok(())
}