use std::collections::{BTreeMap, BTreeSet};
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock, PoisonError, RwLock};
use skif::cache::{CacheHandle, CacheStore, MemoryStore, StoreModuleCache, salt_digest};
use skif::{CachedModule, MemoryModuleCache, ModuleCache, ModuleKey, SourceKind, Target, parse_byte_size};
use skif_cache::{DEFAULT_MAX_SIZE, DiskStore, DiskStoreOptions, Finish};
use skif_report::escape_controls;
use crate::output::stderr_line;
#[derive(Debug, Clone, Default)]
pub struct CacheFlags {
pub cache: bool,
pub no_cache: bool,
pub location: Option<PathBuf>,
pub max_size: Option<u64>,
}
#[derive(Debug, Clone, Default)]
pub struct CacheEnv {
enabled: Option<OsString>,
dir: Option<PathBuf>,
max_size: Option<OsString>,
ci: bool,
bazel: Option<PathBuf>,
user_cache_dir: Option<PathBuf>,
}
impl CacheEnv {
#[cfg(test)]
pub fn empty() -> Self {
Self::default()
}
pub fn detect(ci: bool) -> Self {
let var = |name: &str| std::env::var_os(name).filter(|value| !value.is_empty());
Self {
enabled: var("SKIF_CACHE"),
dir: var("SKIF_CACHE_DIR").map(PathBuf::from),
max_size: var("SKIF_CACHE_MAX_SIZE"),
ci,
bazel: crate::env::bazel_test_tmpdir(),
user_cache_dir: user_cache_dir(
var("XDG_CACHE_HOME").map(PathBuf::from),
var("HOME").map(PathBuf::from),
var("LOCALAPPDATA").map(PathBuf::from),
),
}
}
}
fn user_cache_dir(xdg: Option<PathBuf>, home: Option<PathBuf>, local_app_data: Option<PathBuf>) -> Option<PathBuf> {
if let Some(xdg) = xdg.filter(|dir| dir.is_absolute()) {
return Some(xdg);
}
if cfg!(windows) {
return local_app_data.filter(|dir| dir.is_absolute());
}
let home = home.filter(|dir| dir.is_absolute())?;
Some(if cfg!(target_os = "macos") {
home.join("Library").join("Caches")
} else {
home.join(".cache")
})
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Settings {
pub enabled: bool,
pub location: Option<PathBuf>,
pub max_size: u64,
pub asked: bool,
pub write: bool,
pub cap: CapScope,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CapScope {
Location,
Store,
}
#[derive(Default)]
struct Layer {
enabled: Option<bool>,
location: Option<PathBuf>,
max_size: Option<u64>,
}
fn absolute(dir: &Path) -> PathBuf {
std::path::absolute(dir).unwrap_or_else(|_| dir.to_path_buf())
}
pub fn resolve(flags: &CacheFlags, env: &CacheEnv, base: &Path, targets: &[Target]) -> (Settings, Vec<String>) {
let mut warnings = Vec::new();
let flag = Layer {
enabled: if flags.no_cache {
Some(false)
} else {
flags.cache.then_some(true)
},
location: flags.location.as_deref().map(absolute),
max_size: flags.max_size,
};
let variable = Layer {
enabled: env.enabled.as_ref().and_then(|value| {
let text = value.to_string_lossy();
match text.trim().to_ascii_lowercase().as_str() {
"1" | "true" | "yes" | "on" => Some(true),
"0" | "false" | "no" | "off" => Some(false),
_ => {
warnings.push(format!(
"ignoring SKIF_CACHE={}: expected 1 or 0",
escape_controls(&text)
));
None
}
}
}),
location: env.dir.as_deref().map(absolute),
max_size: env.max_size.as_ref().and_then(|value| {
parse_byte_size(&value.to_string_lossy())
.inspect_err(|error| warnings.push(format!("ignoring SKIF_CACHE_MAX_SIZE: {}", escape_controls(error))))
.ok()
}),
};
let config = root_config(base, targets, &mut warnings);
let enabled = flag.enabled.or(variable.enabled).or(config.enabled);
let named = flag.location.or(variable.location);
let explicit = named.is_some();
let location = named.or(config.location);
let own_cap = flag.max_size.or(variable.max_size);
let settings = Settings {
enabled: enabled.unwrap_or(true),
asked: enabled == Some(true) || location.is_some(),
write: !env.ci || explicit,
location: location.or_else(|| default_location(env, base)),
cap: if own_cap.is_some() || config.max_size.is_none() {
CapScope::Location
} else {
CapScope::Store
},
max_size: own_cap.or(config.max_size).unwrap_or(DEFAULT_MAX_SIZE),
};
(settings, warnings)
}
fn default_location(env: &CacheEnv, base: &Path) -> Option<PathBuf> {
if let Some(dir) = &env.bazel {
return Some(dir.join("skif"));
}
skif_cache::default_location(base, env.user_cache_dir.as_deref())
}
fn root_config(base: &Path, targets: &[Target], warnings: &mut Vec<String>) -> Layer {
const KEYS: [&str; 3] = ["cache.enabled", "cache.location", "cache.max-size"];
let root = targets.iter().find(|target| target.root == base);
let own_files: BTreeSet<&Path> = root.map_or_default(|target| {
target
.sources
.iter()
.filter(|source| matches!(source.kind, SourceKind::Root | SourceKind::Explicit))
.map(|source| source.path.as_path())
.collect()
});
let mut ignored: BTreeSet<&Path> = BTreeSet::new();
for target in targets {
for key in KEYS {
for assignment in target.origins.get(key) {
if let Some(source) = target.sources.get(assignment.source)
&& !own_files.contains(source.path.as_path())
{
ignored.insert(&source.path);
}
}
}
}
for file in ignored {
warnings.push(format!(
"ignoring [cache] in {}: only the root configuration sets the cache",
escape_controls(&file.display().to_string())
));
}
let Some(root) = root else {
return Layer::default();
};
let own = |key: &str| {
let assignment = root.origins.last(key)?;
let source = root.sources.get(assignment.source)?;
own_files
.contains(source.path.as_path())
.then_some(source.path.as_path())
};
let cache = &root.config.cache;
Layer {
enabled: own("cache.enabled").and(cache.enabled),
location: own("cache.location").and_then(|file| {
let dir = file.parent().unwrap_or(base);
let location = skif::join_lexical(dir, Path::new(cache.location.as_deref()?));
if stays_inside(dir, &location) {
return Some(location);
}
warnings.push(format!(
"ignoring cache.location in {}: {} is outside the directory of the file",
escape_controls(&file.display().to_string()),
escape_controls(&location.display().to_string())
));
None
}),
max_size: own("cache.max-size").and(cache.max_size.map(|size| size.0)),
}
}
fn stays_inside(dir: &Path, location: &Path) -> bool {
if !location.starts_with(dir) {
return false;
}
let Some(existing) = location
.ancestors()
.find(|ancestor| ancestor.symlink_metadata().is_ok())
else {
return false;
};
match (std::fs::canonicalize(dir), std::fs::canonicalize(existing)) {
(Ok(dir), Ok(existing)) => existing.starts_with(dir),
_ => false,
}
}
fn host_salt() -> Vec<u8> {
if !cfg!(debug_assertions) {
return Vec::new();
}
let Ok(metadata) = std::env::current_exe().and_then(std::fs::metadata) else {
return Vec::new();
};
let modified = metadata
.modified()
.ok()
.and_then(|time| time.duration_since(std::time::UNIX_EPOCH).ok())
.map_or(0, |since| since.as_nanos());
let mut salt = b"dev-build".to_vec();
salt.extend_from_slice(&metadata.len().to_le_bytes());
salt.extend_from_slice(&modified.to_le_bytes());
salt
}
#[derive(Default)]
pub struct Attached {
pub modules: Option<Arc<dyn ModuleCache>>,
pub store: Option<(Arc<dyn CacheStore>, Vec<u8>)>,
}
pub trait CacheHost: Sync {
fn attach(&self, base: &Path, targets: &[Target]) -> Attached;
fn finish(&self, complete: bool);
}
impl CacheHost for Arc<MemoryModuleCache> {
fn attach(&self, _base: &Path, _targets: &[Target]) -> Attached {
Attached {
modules: Some(Arc::clone(self) as Arc<dyn ModuleCache>),
store: None,
}
}
fn finish(&self, complete: bool) {
if complete {
self.prune_unused();
}
}
}
pub struct DiskHost {
flags: CacheFlags,
env: CacheEnv,
debug: bool,
keep_memory: bool,
opened: OnceLock<Opened>,
}
enum Opened {
Off,
Memory(Arc<MemoryModuleCache>),
Store {
store: Arc<dyn CacheStore>,
disk: Option<Arc<DiskStore>>,
settings: Settings,
},
}
impl DiskHost {
pub const fn new(flags: CacheFlags, env: CacheEnv, debug: bool) -> Self {
Self {
flags,
env,
debug,
keep_memory: false,
opened: OnceLock::new(),
}
}
pub const fn keeping_memory(mut self) -> Self {
self.keep_memory = true;
self
}
fn open(&self, base: &Path, targets: &[Target]) -> Opened {
let (settings, warnings) = resolve(&self.flags, &self.env, base, targets);
for warning in warnings {
stderr_line(&format!("warning: {warning}"));
}
if !settings.enabled {
return if self.keep_memory {
Opened::Memory(Arc::new(MemoryModuleCache::new()))
} else {
Opened::Off
};
}
let disk = settings.location.as_ref().map(|location| {
let options = DiskStoreOptions::default()
.with_max_size(settings.max_size)
.with_write(settings.write)
.with_evict_others(settings.cap == CapScope::Location);
Arc::new(DiskStore::open(location, base, options))
});
if self.debug {
match &disk {
Some(disk) => stderr_line(&format!(
"skif debug: cache store {}{}",
escape_controls(&disk.dir().display().to_string()),
if settings.write { "" } else { " (read only)" }
)),
None => stderr_line("skif debug: cache in memory for this run: no cache location"),
}
}
let store: Arc<dyn CacheStore> = match &disk {
Some(disk) => Arc::clone(disk) as Arc<dyn CacheStore>,
None => Arc::new(MemoryStore::new()),
};
Opened::Store { store, disk, settings }
}
}
impl CacheHost for DiskHost {
fn attach(&self, base: &Path, targets: &[Target]) -> Attached {
match self.opened.get_or_init(|| self.open(base, targets)) {
Opened::Off => Attached::default(),
Opened::Memory(modules) => Attached {
modules: Some(Arc::clone(modules) as Arc<dyn ModuleCache>),
store: None,
},
Opened::Store { store, .. } => Attached {
modules: None,
store: Some((Arc::clone(store), host_salt())),
},
}
}
fn finish(&self, complete: bool) {
let (disk, settings) = match self.opened.get() {
Some(Opened::Memory(modules)) => {
if complete {
modules.prune_unused();
}
return;
}
Some(Opened::Store {
disk: Some(disk),
settings,
..
}) => (disk, settings),
_ => return,
};
let result = if complete {
disk.finish_run()
} else {
disk.finish_failed_run()
};
if self.debug {
for note in disk.take_notes() {
stderr_line(&format!("skif debug: {}", escape_controls(¬e)));
}
}
let location = escape_controls(&disk.location().display().to_string()).into_owned();
match result {
Ok(Finish::Contended) if self.debug => {
stderr_line(&format!(
"skif debug: another run is writing the cache in {location}; not written"
));
}
Err(error) if settings.asked => stderr_line(&format!(
"warning: cannot write the cache in {location}: {}",
escape_controls(&error.to_string())
)),
Err(error) if self.debug => stderr_line(&format!(
"skif debug: cannot write the cache in {location}: {}",
escape_controls(&error.to_string())
)),
Ok(_) | Err(_) => {}
}
}
}
pub struct Seeded {
memory: MemoryModuleCache,
seed: RwLock<Option<StoreModuleCache>>,
}
impl Seeded {
fn new(env: &CacheEnv, base: &Path, targets: &[Target]) -> Self {
let (settings, _) = resolve(&CacheFlags::default(), env, base, targets);
let seed = settings.location.filter(|_| settings.enabled).map(|location| {
let options = DiskStoreOptions::default()
.with_write(false)
.with_max_size(settings.max_size);
let store = DiskStore::open(&location, base, options);
StoreModuleCache::new(CacheHandle::new(Arc::new(store), salt_digest(&host_salt()))).read_only()
});
Self {
memory: MemoryModuleCache::new(),
seed: RwLock::new(seed),
}
}
pub fn prune(&self) {
self.memory.prune_unused();
self.seed.write().unwrap_or_else(PoisonError::into_inner).take();
}
#[cfg(test)]
pub fn is_seeded(&self) -> bool {
self.seed.read().unwrap_or_else(PoisonError::into_inner).is_some()
}
}
impl ModuleCache for Seeded {
fn get(&self, key: ModuleKey) -> Option<CachedModule> {
if let Some(found) = self.memory.get(key) {
return Some(found);
}
let found = self
.seed
.read()
.unwrap_or_else(PoisonError::into_inner)
.as_ref()?
.get(key)?;
self.memory.put(key, found.clone());
Some(found)
}
fn get_again(&self, key: ModuleKey) -> Option<CachedModule> {
self.get(key)
}
fn put(&self, key: ModuleKey, module: CachedModule) {
self.memory.put(key, module);
}
}
#[derive(Default)]
pub struct SeededHost {
env: CacheEnv,
cache: OnceLock<Arc<Seeded>>,
}
impl SeededHost {
pub const fn new(env: CacheEnv) -> Self {
Self {
env,
cache: OnceLock::new(),
}
}
}
impl CacheHost for SeededHost {
fn attach(&self, base: &Path, targets: &[Target]) -> Attached {
let cache = self
.cache
.get_or_init(|| Arc::new(Seeded::new(&self.env, base, targets)));
Attached {
modules: Some(Arc::clone(cache) as Arc<dyn ModuleCache>),
store: None,
}
}
fn finish(&self, complete: bool) {
if complete && let Some(cache) = self.cache.get() {
cache.prune();
}
}
}
#[derive(Default)]
pub struct SeededCaches {
env: CacheEnv,
caches: Mutex<BTreeMap<PathBuf, Arc<Seeded>>>,
}
impl SeededCaches {
pub const fn new(env: CacheEnv) -> Self {
Self {
env,
caches: Mutex::new(BTreeMap::new()),
}
}
pub fn prune(&self, base: &Path) {
if let Some(cache) = self.caches.lock().unwrap_or_else(PoisonError::into_inner).get(base) {
cache.prune();
}
}
}
impl CacheHost for SeededCaches {
fn attach(&self, base: &Path, targets: &[Target]) -> Attached {
let cache = {
let mut caches = self.caches.lock().unwrap_or_else(PoisonError::into_inner);
Arc::clone(
caches
.entry(base.to_path_buf())
.or_insert_with(|| Arc::new(Seeded::new(&self.env, base, targets))),
)
};
Attached {
modules: Some(cache as Arc<dyn ModuleCache>),
store: None,
}
}
fn finish(&self, _complete: bool) {}
}
pub fn written_dirs(flags: &CacheFlags, env: &CacheEnv) -> Vec<PathBuf> {
if flags.no_cache {
return Vec::new();
}
flags
.location
.iter()
.chain(env.dir.iter())
.flat_map(|dir| {
let dir = absolute(dir);
let resolved = std::fs::canonicalize(&dir)
.ok()
.map(crate::lint::simplified)
.filter(|resolved| *resolved != dir);
std::iter::once(dir).chain(resolved)
})
.collect()
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests;