|
| 1 | +/// Build script for sweepga — caches FastGA and wfmash binaries so that |
| 2 | +/// `cargo install sweepga` produces a self-contained installation. |
| 3 | +/// |
| 4 | +/// At build time the dependency crates (fastga-rs, wfmash-rs) compile their |
| 5 | +/// helper binaries into target/{profile}/build/{dep}-*/out/. We copy those |
| 6 | +/// into ~/.cache/sweepga/{cache_key}/ and remove any stale version dirs. |
| 7 | +/// |
| 8 | +/// At runtime binary_paths.rs checks the cache first, so the binaries are |
| 9 | +/// always available regardless of whether a cargo target/ tree still exists. |
| 10 | +use std::env; |
| 11 | +use std::path::{Path, PathBuf}; |
| 12 | +use std::process::Command; |
| 13 | + |
1 | 14 | fn main() { |
2 | 15 | println!("cargo:rerun-if-changed=build.rs"); |
| 16 | + println!("cargo:rerun-if-changed=Cargo.lock"); |
| 17 | + |
| 18 | + // OUT_DIR = target/{profile}/build/sweepga-{hash}/out |
| 19 | + let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); |
| 20 | + |
| 21 | + let build_dir = out_dir |
| 22 | + .ancestors() |
| 23 | + .find(|p| p.file_name().map(|n| n == "build").unwrap_or(false)) |
| 24 | + .map(|p| p.to_path_buf()); |
| 25 | + |
| 26 | + let build_dir = match build_dir { |
| 27 | + Some(d) => d, |
| 28 | + None => { |
| 29 | + println!("cargo:warning=Could not locate build directory"); |
| 30 | + return; |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + // ── cache key ────────────────────────────────────────────────────── |
| 35 | + let cache_key = cache_key(); |
| 36 | + println!("cargo:rustc-env=SWEEPGA_CACHE_KEY={cache_key}"); |
| 37 | + |
| 38 | + // ── cache directory ──────────────────────────────────────────────── |
| 39 | + let cache_dir = cache_base().join(&cache_key); |
| 40 | + |
| 41 | + if let Err(e) = std::fs::create_dir_all(&cache_dir) { |
| 42 | + println!("cargo:warning=Failed to create cache dir: {e}"); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + // ── copy binaries ────────────────────────────────────────────────── |
| 47 | + let mut installed = 0usize; |
| 48 | + |
| 49 | + // FastGA utilities |
| 50 | + let fastga_bins = [ |
| 51 | + "FastGA", "FAtoGDB", "GIXmake", "GIXrm", "ALNtoPAF", "PAFtoALN", "ONEview", |
| 52 | + ]; |
| 53 | + if let Some(dir) = find_dep_out(&build_dir, "fastga-rs", "FastGA") { |
| 54 | + installed += copy_binaries(&dir, &cache_dir, &fastga_bins); |
| 55 | + } else { |
| 56 | + println!("cargo:warning=fastga-rs build output not found"); |
| 57 | + } |
| 58 | + |
| 59 | + // wfmash |
| 60 | + if let Some(dir) = find_dep_out(&build_dir, "wfmash-rs", "wfmash") { |
| 61 | + installed += copy_binaries(&dir, &cache_dir, &["wfmash"]); |
| 62 | + } else { |
| 63 | + println!("cargo:warning=wfmash-rs build output not found (may not be built yet)"); |
| 64 | + } |
| 65 | + |
| 66 | + if installed > 0 { |
| 67 | + println!( |
| 68 | + "cargo:warning=Cached {installed} binaries in {}", |
| 69 | + cache_dir.display() |
| 70 | + ); |
| 71 | + cleanup_old_versions(&cache_dir, &cache_key); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// ── helpers ──────────────────────────────────────────────────────────── |
| 76 | + |
| 77 | +/// Build a cache key: `git describe --always --dirty` or CARGO_PKG_VERSION. |
| 78 | +fn cache_key() -> String { |
| 79 | + if let Ok(out) = Command::new("git") |
| 80 | + .args(["describe", "--always", "--dirty"]) |
| 81 | + .output() |
| 82 | + { |
| 83 | + if out.status.success() { |
| 84 | + let desc = String::from_utf8_lossy(&out.stdout).trim().to_string(); |
| 85 | + if !desc.is_empty() { |
| 86 | + return desc; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + env::var("CARGO_PKG_VERSION").unwrap_or_else(|_| "unknown".into()) |
| 91 | +} |
| 92 | + |
| 93 | +/// $XDG_CACHE_HOME/sweepga or ~/.cache/sweepga |
| 94 | +fn cache_base() -> PathBuf { |
| 95 | + env::var("XDG_CACHE_HOME") |
| 96 | + .map(PathBuf::from) |
| 97 | + .unwrap_or_else(|_| { |
| 98 | + PathBuf::from(env::var("HOME").expect("HOME not set")).join(".cache") |
| 99 | + }) |
| 100 | + .join("sweepga") |
| 101 | +} |
| 102 | + |
| 103 | +/// Scan `build_dir` for `{prefix}-*/out/` containing `marker_bin`. |
| 104 | +fn find_dep_out(build_dir: &Path, prefix: &str, marker_bin: &str) -> Option<PathBuf> { |
| 105 | + let pat = format!("{prefix}-"); |
| 106 | + for entry in std::fs::read_dir(build_dir).ok()?.flatten() { |
| 107 | + let path = entry.path(); |
| 108 | + if path.is_dir() { |
| 109 | + if let Some(name) = path.file_name().and_then(|n| n.to_str()) { |
| 110 | + if name.starts_with(&pat) { |
| 111 | + let out = path.join("out"); |
| 112 | + if out.join(marker_bin).exists() { |
| 113 | + return Some(out); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + None |
| 120 | +} |
| 121 | + |
| 122 | +/// Copy listed binaries from `src` to `dst`. Returns number of successes. |
| 123 | +/// |
| 124 | +/// Uses atomic rename to avoid ETXTBSY ("Text file busy") when the |
| 125 | +/// destination binary is currently being executed by another process. |
| 126 | +fn copy_binaries(src: &Path, dst: &Path, names: &[&str]) -> usize { |
| 127 | + let mut n = 0; |
| 128 | + for name in names { |
| 129 | + let s = src.join(name); |
| 130 | + if !s.exists() { |
| 131 | + continue; |
| 132 | + } |
| 133 | + let d = dst.join(name); |
| 134 | + let tmp = dst.join(format!(".{name}.tmp")); |
| 135 | + match std::fs::copy(&s, &tmp) { |
| 136 | + Ok(_) => { |
| 137 | + make_executable(&tmp); |
| 138 | + match std::fs::rename(&tmp, &d) { |
| 139 | + Ok(_) => n += 1, |
| 140 | + Err(e) => { |
| 141 | + println!("cargo:warning=Failed to rename {name}: {e}"); |
| 142 | + let _ = std::fs::remove_file(&tmp); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + Err(e) => println!("cargo:warning=Failed to copy {name}: {e}"), |
| 147 | + } |
| 148 | + } |
| 149 | + n |
| 150 | +} |
| 151 | + |
| 152 | +#[cfg(unix)] |
| 153 | +fn make_executable(path: &Path) { |
| 154 | + use std::os::unix::fs::PermissionsExt; |
| 155 | + if let Ok(m) = std::fs::metadata(path) { |
| 156 | + let mut p = m.permissions(); |
| 157 | + p.set_mode(0o755); |
| 158 | + let _ = std::fs::set_permissions(path, p); |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +#[cfg(not(unix))] |
| 163 | +fn make_executable(_path: &Path) {} |
| 164 | + |
| 165 | +/// Delete sibling directories under the sweepga cache that aren't `current_key`. |
| 166 | +fn cleanup_old_versions(current_dir: &Path, current_key: &str) { |
| 167 | + let parent = match current_dir.parent() { |
| 168 | + Some(p) => p, |
| 169 | + None => return, |
| 170 | + }; |
| 171 | + let entries = match std::fs::read_dir(parent) { |
| 172 | + Ok(e) => e, |
| 173 | + Err(_) => return, |
| 174 | + }; |
| 175 | + for entry in entries.flatten() { |
| 176 | + if entry.path().is_dir() { |
| 177 | + if let Some(name) = entry.file_name().to_str() { |
| 178 | + if name != current_key { |
| 179 | + println!( |
| 180 | + "cargo:warning=Removing old cache: {}", |
| 181 | + entry.path().display() |
| 182 | + ); |
| 183 | + let _ = std::fs::remove_dir_all(entry.path()); |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
3 | 188 | } |
0 commit comments