use std::path::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() {
if path.is_absolute() {
let mut components = path.components();
components.next();
root.join(components.as_path())
} else {
root.join(path)
}
} else {
path
}
}