Skip to content

Commit

Permalink
feat: impl configurable cache path
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Dec 20, 2023
1 parent dde609a commit 6401882
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
10 changes: 7 additions & 3 deletions kclvm/config/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const LOCK_SUFFIX: &str = ".lock";
const DEFAULT_CACHE_DIR: &str = ".kclvm/cache";
const CACHE_INFO_FILENAME: &str = "info";
const KCL_SUFFIX_PATTERN: &str = "*.k";
pub const KCL_CACHE_PATH_ENV_VAR: &str = "KCL_CACHE_PATH";

pub type CacheInfo = String;
pub type Cache = HashMap<String, CacheInfo>;
Expand Down Expand Up @@ -133,7 +134,8 @@ where
#[inline]
fn get_cache_dir(root: &str, cache_dir: Option<&str>) -> String {
let cache_dir = cache_dir.unwrap_or(DEFAULT_CACHE_DIR);
Path::new(root)
let root = std::env::var(KCL_CACHE_PATH_ENV_VAR).unwrap_or(root.to_string());
Path::new(&root)
.join(cache_dir)
.join(format!("{}-{}", version::VERSION, version::CHECK_SUM))
.display()
Expand All @@ -144,7 +146,8 @@ fn get_cache_dir(root: &str, cache_dir: Option<&str>) -> String {
#[allow(dead_code)]
fn get_cache_filename(root: &str, target: &str, pkgpath: &str, cache_dir: Option<&str>) -> String {
let cache_dir = cache_dir.unwrap_or(DEFAULT_CACHE_DIR);
Path::new(root)
let root = std::env::var(KCL_CACHE_PATH_ENV_VAR).unwrap_or(root.to_string());
Path::new(&root)
.join(cache_dir)
.join(format!("{}-{}", version::VERSION, version::CHECK_SUM))
.join(target)
Expand All @@ -156,7 +159,8 @@ fn get_cache_filename(root: &str, target: &str, pkgpath: &str, cache_dir: Option
#[inline]
fn get_cache_info_filename(root: &str, target: &str, cache_dir: Option<&str>) -> String {
let cache_dir = cache_dir.unwrap_or(DEFAULT_CACHE_DIR);
Path::new(root)
let root = std::env::var(KCL_CACHE_PATH_ENV_VAR).unwrap_or(root.to_string());
Path::new(&root)
.join(cache_dir)
.join(format!("{}-{}", version::VERSION, version::CHECK_SUM))
.join(target)
Expand Down
11 changes: 6 additions & 5 deletions kclvm/runner/src/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use kclvm_compiler::codegen::{
llvm::{emit_code, OBJECT_FILE_SUFFIX},
EmitOptions,
};
use kclvm_config::cache::{load_pkg_cache, save_pkg_cache, CacheOption};
use kclvm_config::cache::{load_pkg_cache, save_pkg_cache, CacheOption, KCL_CACHE_PATH_ENV_VAR};
use kclvm_sema::resolver::scope::ProgramScope;
use std::{
collections::HashMap,
Expand Down Expand Up @@ -243,17 +243,18 @@ impl KclvmAssembler {
/// Generate cache dir from the program root path.
/// Create cache dir if it doesn't exist.
#[inline]
pub(crate) fn load_cache_dir(&self, prog_root_name: &str) -> Result<PathBuf> {
let cache_dir = self.construct_cache_dir(prog_root_name);
pub(crate) fn load_cache_dir(&self, root: &str) -> Result<PathBuf> {
let cache_dir = self.construct_cache_dir(root);
if !cache_dir.exists() {
std::fs::create_dir_all(&cache_dir)?;
}
Ok(cache_dir)
}

#[inline]
pub(crate) fn construct_cache_dir(&self, prog_root_name: &str) -> PathBuf {
Path::new(prog_root_name)
pub(crate) fn construct_cache_dir(&self, root: &str) -> PathBuf {
let root = std::env::var(KCL_CACHE_PATH_ENV_VAR).unwrap_or(root.to_string());
Path::new(&root)
.join(".kclvm")
.join("cache")
.join(kclvm_version::get_version_string())
Expand Down

0 comments on commit 6401882

Please sign in to comment.