| // Copyright 2026 The Fuchsia Authors | |
| // | |
| // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | |
| // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | |
| // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | |
| // This file may not be copied, modified, or distributed except according to | |
| // those terms. | |
| //! Workspace root, corpus manifest, and differential cache root resolution. | |
| use std::{ | |
| env, fs, | |
| path::{Path, PathBuf}, | |
| }; | |
| use anneal_testkit::ToolchainEnv; | |
| use super::DEFAULT_CORPUS_MANIFEST_REL; | |
| /// Locate the `anneal/` workspace root from either the manifest path or crate | |
| /// location. | |
| pub fn find_anneal_root(manifest_path: &Path) -> Result<PathBuf, String> { | |
| if let Ok(canon) = std::fs::canonicalize(manifest_path) { | |
| let mut cur = canon.parent(); | |
| while let Some(dir) = cur { | |
| if dir.join("Cargo.toml").is_file() && dir.join("trusted/lean/lean-toolchain").is_file() | |
| { | |
| return Ok(dir.to_path_buf()); | |
| } | |
| cur = dir.parent(); | |
| } | |
| } | |
| ToolchainEnv::repo_root_from_manifest(Path::new(env!("CARGO_MANIFEST_DIR"))) | |
| } | |
| /// Resolve the default Stage-1 Profile v1 corpus manifest path. | |
| pub fn default_manifest_path() -> Result<PathBuf, String> { | |
| let root = ToolchainEnv::repo_root_from_manifest(Path::new(env!("CARGO_MANIFEST_DIR")))?; | |
| Ok(root.join(DEFAULT_CORPUS_MANIFEST_REL)) | |
| } | |
| /// Resolve the differential cache root, honoring explicit `--cache-root`, | |
| /// `ANNEAL_DIFF_CACHE_DIR`, `CARGO_TARGET_DIR`, or a writable in-checkout | |
| /// `target/anneal-diff-cache`, and falling back to a private temporary | |
| /// directory when the checkout is read-only. | |
| pub fn resolve_cache_root( | |
| anneal_root: &Path, | |
| explicit: Option<&Path>, | |
| ) -> Result<(PathBuf, Option<tempfile::TempDir>), String> { | |
| let caller_cwd = | |
| env::current_dir().map_err(|e| format!("resolve current working directory: {e}"))?; | |
| let validated_tmpdir = crate::mir_eval_runner::validate_optional_existing_dir_env( | |
| env::var_os("TMPDIR").as_deref(), | |
| &caller_cwd, | |
| "TMPDIR", | |
| )?; | |
| let in_repo_target = anneal_root.join("target"); | |
| let in_repo_target_clean_dir = fs::symlink_metadata(&in_repo_target) | |
| .is_ok_and(|m| !m.file_type().is_symlink() && m.is_dir()); | |
| let (raw_cache, label) = if let Some(p) = explicit { | |
| (p.to_path_buf(), "--cache-root") | |
| } else if let Some(cd) = env::var_os("ANNEAL_DIFF_CACHE_DIR").filter(|v| !v.is_empty()) { | |
| (PathBuf::from(cd), "ANNEAL_DIFF_CACHE_DIR") | |
| } else if let Some(td) = env::var_os("CARGO_TARGET_DIR").filter(|v| !v.is_empty()) { | |
| (PathBuf::from(td).join("anneal-diff-cache"), "CARGO_TARGET_DIR") | |
| } else if in_repo_target_clean_dir && tempfile::tempfile_in(&in_repo_target).is_ok() { | |
| (in_repo_target.join("anneal-diff-cache"), "anneal target cache") | |
| } else { | |
| let private_tmp = crate::mir_eval_runner::build_temp_dir( | |
| "anneal-diff-cache-", | |
| validated_tmpdir.as_deref(), | |
| )?; | |
| let path = | |
| crate::mir_eval_runner::validate_dir_path_no_symlinks(private_tmp.path(), &caller_cwd)?; | |
| return Ok((path, Some(private_tmp))); | |
| }; | |
| let abs_cache = crate::mir_eval_runner::validate_dir_or_uncreated_path_no_symlinks( | |
| &raw_cache, | |
| &caller_cwd, | |
| label, | |
| )?; | |
| Ok((abs_cache, None)) | |
| } | |
| pub(super) fn format_report_manifest_path(anneal_root: &Path, manifest_path: &Path) -> String { | |
| if let Ok(rel) = manifest_path.strip_prefix(anneal_root) { | |
| return rel.to_string_lossy().into_owned(); | |
| } | |
| manifest_path | |
| .file_name() | |
| .map(|n| n.to_string_lossy().into_owned()) | |
| .unwrap_or_else(|| "manifest.toml".to_owned()) | |
| } |