use std::path::{Component, PathBuf};
use std::sync::{OnceLock, RwLock};
fn sysroot_store() -> &'static RwLock<Option<PathBuf>> {
static SYSROOT: OnceLock<RwLock<Option<PathBuf>>> = OnceLock::new();
SYSROOT.get_or_init(|| RwLock::new(None))
}
pub fn set_sysroot(path: PathBuf) {
if let Ok(mut guard) = sysroot_store().write() {
*guard = Some(path);
}
}
pub fn clear_sysroot() {
if let Ok(mut guard) = sysroot_store().write() {
*guard = None;
}
}
pub fn get_sysroot() -> Option<PathBuf> {
sysroot_store().read().ok().and_then(|g| g.clone())
}
pub fn apply_sysroot(path: impl Into<PathBuf>) -> PathBuf {
let path = path.into();
if let Some(root) = get_sysroot() {
let mut applied = root.clone();
for component in path.components() {
match component {
Component::Prefix(_)
| Component::RootDir
| Component::CurDir => {}
Component::ParentDir => {
if applied != root {
applied.pop();
}
}
Component::Normal(name) => applied.push(name)
}
}
applied
} else {
path
}
}