use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use eko::path::PathBuf;
use eko::thread::OnceLock;
use crate::rustc_data_structures::profiling::VerboseTimingGuard;
use crate::rustc_fs_util::try_canonicalize;
use rustc_macros::{Decodable, Encodable, StableHash};
use crate::rustc_structures::NativeLibKind;
use crate::rustc_session::session::Session;
impl Session {
pub fn timer(&self, what: &'static str) -> VerboseTimingGuard<'_> {
self.prof.verbose_generic_activity(what)
}
pub fn time<R>(&self, what: &'static str, f: impl FnOnce() -> R) -> R {
self.prof.verbose_generic_activity(what).run(f)
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
#[derive(StableHash)]
pub struct NativeLib {
pub name: String,
pub new_name: Option<String>,
pub kind: NativeLibKind,
pub verbatim: Option<bool>,
}
impl NativeLib {
pub fn has_modifiers(&self) -> bool {
self.verbatim.is_some() || self.kind.has_modifiers()
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct CanonicalizedPath {
canonicalized: Option<PathBuf>,
original: PathBuf,
}
impl CanonicalizedPath {
pub fn new(path: PathBuf) -> Self {
Self { canonicalized: try_canonicalize(&path).ok(), original: path }
}
pub fn canonicalized(&self) -> &PathBuf {
self.canonicalized.as_ref().unwrap_or(self.original())
}
pub fn original(&self) -> &PathBuf {
&self.original
}
}
pub fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
const ICE_REPORT_COMPILER_FLAGS: &[&str] = &["-Z", "-C", "--crate-type"];
const ICE_REPORT_COMPILER_FLAGS_EXCLUDE: &[&str] = &["metadata", "extra-filename"];
const ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE: &[&str] = &["incremental"];
let mut args =
eko::env::args().into_iter().map(|arg| String::from_utf8_lossy(&arg).to_string());
let mut result = Vec::new();
let mut excluded_cargo_defaults = false;
while let Some(arg) = args.next() {
if let Some(a) = ICE_REPORT_COMPILER_FLAGS.iter().find(|a| arg.starts_with(*a)) {
let content = if arg.len() == a.len() {
match args.next() {
Some(arg) => arg,
None => continue,
}
} else if arg.get(a.len()..a.len() + 1) == Some("=") {
arg[a.len() + 1..].to_string()
} else {
arg[a.len()..].to_string()
};
let option = content.split_once('=').map(|s| s.0).unwrap_or(&content);
if ICE_REPORT_COMPILER_FLAGS_EXCLUDE.contains(&option) {
excluded_cargo_defaults = true;
} else {
result.push(a.to_string());
result.push(if ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE.contains(&option) {
format!("{option}=[REDACTED]")
} else {
content
});
}
}
}
if !result.is_empty() { Some((result, excluded_cargo_defaults)) } else { None }
}
pub fn was_invoked_from_cargo() -> bool {
static FROM_CARGO: OnceLock<bool> = OnceLock::new();
*FROM_CARGO.get_or_init(|| eko::env::var_os("CARGO_CRATE_NAME").is_some())
}