Skip to content

Commit 72a7e5c

Browse files
committed
halide-cache: Add --base-dir option
Make the paths to generated files and dependencies relative to the value of this argument. This makes it possible to share the halide cache between repos or worktrees. If a path cannot be made relative then it remains unchanged. Currently it is an Option<PathBuf> to avoid having to make changes to the call site and if it is None then the path to the repository is used. In the future we probably want to make this more robust.
1 parent 1b0f7bc commit 72a7e5c

1 file changed

Lines changed: 42 additions & 5 deletions

File tree

halide-cache/src/main.rs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ struct Args {
1313
generated_object: PathBuf,
1414
#[arg(long)]
1515
generated_header: PathBuf,
16+
#[arg(long)]
17+
base_dir: Option<PathBuf>,
1618
#[arg(last = true)]
1719
builder: Vec<String>,
1820
}
@@ -21,7 +23,7 @@ const MAX_CACHE_SIZE_BYTES: u64 = 10737418240; // 10 GiB
2123

2224
struct Dependencies<'a> {
2325
path: &'a Path,
24-
dependencies: &'a [PathBuf],
26+
dependencies: &'a [&'a Path],
2527
env: &'a [String],
2628
}
2729

@@ -60,19 +62,41 @@ fn main() -> anyhow::Result<()> {
6062
if !Path::new(&cache_dir).exists() {
6163
fs::create_dir_all(&cache_dir)?;
6264
}
65+
6366
let lager = Lager::new(Path::new(&cache_dir))?;
6467

6568
let zivid_env = collect_zivid_env();
6669

70+
let base_dir = match args.base_dir {
71+
Some(d) => d,
72+
None => find_repo_root()?,
73+
};
74+
75+
let generated_object = args
76+
.generated_object
77+
.strip_prefix(&base_dir)
78+
.unwrap_or(&args.generated_object);
79+
80+
let generated_header = args
81+
.generated_header
82+
.strip_prefix(&base_dir)
83+
.unwrap_or(&args.generated_header);
84+
85+
let dependencies = args
86+
.dependencies
87+
.iter()
88+
.map(|p| p.strip_prefix(&base_dir).unwrap_or(p))
89+
.collect::<Vec<_>>();
90+
6791
let object_dependencies = Dependencies {
68-
path: &args.generated_object,
69-
dependencies: &args.dependencies,
92+
path: generated_object,
93+
dependencies: &dependencies,
7094
env: &zivid_env,
7195
};
7296

7397
let header_dependencies = Dependencies {
74-
path: &args.generated_header,
75-
dependencies: &args.dependencies,
98+
path: generated_header,
99+
dependencies: &dependencies,
76100
env: &zivid_env,
77101
};
78102

@@ -149,3 +173,16 @@ fn cache_hit(
149173
(Err(e), Ok(_)) => Err(anyhow::anyhow!(e).context("Retrieving the header was successful")),
150174
}
151175
}
176+
177+
fn find_repo_root() -> anyhow::Result<PathBuf> {
178+
let mut cwd = std::env::current_dir()?;
179+
180+
loop {
181+
if cwd.join(".git").exists() {
182+
return Ok(cwd);
183+
}
184+
if !cwd.pop() {
185+
anyhow::bail!("Could not determine root");
186+
}
187+
}
188+
}

0 commit comments

Comments
 (0)