use std::{env, path::PathBuf};
use rllvm::{compiler_wrapper::llvm::RustcWrapper, error::Error};
use tracing::Level;
use tracing_subscriber::FmtSubscriber;
fn main() -> Result<(), Error> {
let raw_args: Vec<String> = env::args().collect();
let (rustc_path, rustc_args) = if raw_args.len() > 1
&& !raw_args[1].starts_with('-')
&& (raw_args[1].ends_with("rustc") || raw_args[1].contains("/rustc"))
{
(PathBuf::from(&raw_args[1]), raw_args[2..].to_vec())
} else {
let rustc = env::var("RLLVM_REAL_RUSTC")
.map(PathBuf::from)
.unwrap_or_else(|_| which::which("rustc").unwrap_or_else(|_| PathBuf::from("rustc")));
(rustc, raw_args[1..].to_vec())
};
let log_level = match env::var("RLLVM_LOG_LEVEL")
.ok()
.and_then(|v| v.parse::<u8>().ok())
.unwrap_or(0)
{
0 => Level::ERROR,
1 => Level::WARN,
2 => Level::INFO,
3 => Level::DEBUG,
_ => Level::TRACE,
};
let _ = FmtSubscriber::builder()
.with_max_level(log_level)
.with_writer(std::io::stderr)
.try_init();
tracing::debug!(
"rllvm-rustc: rustc_path={:?}, args={:?}",
rustc_path,
rustc_args
);
let wrapper = RustcWrapper::new(rustc_path);
if let Some(code) = wrapper.run(&rustc_args)?
&& code != 0
{
std::process::exit(code);
}
Ok(())
}