| 1 | #!/usr/bin/env bash |
| 2 | # One Cargo target dir for every worktree, and a disk cap for it. |
| 3 | # Usage: |
| 4 | # scripts/build-cache.sh setup point ~/.cargo/config.toml at the shared target dir (once per machine) |
| 5 | # scripts/build-cache.sh status sizes: shared target dir, old per-worktree target dirs, kache |
| 6 | # scripts/build-cache.sh clean [--apply] remove old per-worktree target dirs and trim the shared one to the cap |
| 7 | # Without --apply, clean only prints what it would remove. |
| 8 | # Env: RUSTY_TEAMS_TARGET_CAP_GB (default 40), KACHE_MAX_SIZE for `kache gc` (default 15GiB). |
| 9 | set -euo pipefail |
| 10 | repoRoot="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 11 | sharedTarget="${XDG_CACHE_HOME:-$HOME/.cache}/rusty-teams/target" |
| 12 | capKilobytes=$(( ${RUSTY_TEAMS_TARGET_CAP_GB:-40} * 1024 * 1024 )) |
| 13 | windowsRelease="$sharedTarget/x86_64-pc-windows-msvc/release" |
| 14 | |
| 15 | sizeKilobytes() { du -sk "$1" 2>/dev/null | cut -f1 || echo 0; } |
| 16 | human() { du -sh "$1" 2>/dev/null | cut -f1; } |
| 17 | |
| 18 | # Target dirs from before the shared one: one per worktree under ~/.cache, plus target/ and crates/app/target/ in each worktree. |
| 19 | legacyTargets() { |
| 20 | for directory in "${XDG_CACHE_HOME:-$HOME/.cache}"/cargo-target-*; do |
| 21 | if [ -d "$directory" ]; then echo "$directory"; fi |
| 22 | done |
| 23 | git -C "$repoRoot" worktree list --porcelain | sed -n 's/^worktree //p' | while read -r worktree; do |
| 24 | for directory in "$worktree/target" "$worktree/crates/app/target" "$worktree/tools/onnxruntime-dll/target" "$worktree"/spikes/*/target; do |
| 25 | if [ -d "$directory" ]; then echo "$directory"; fi |
| 26 | done |
| 27 | done |
| 28 | return 0 |
| 29 | } |
| 30 | |
| 31 | case "${1:-status}" in |
| 32 | setup) |
| 33 | config="$HOME/.cargo/config.toml" |
| 34 | mkdir -p "$(dirname "$config")" "$sharedTarget" |
| 35 | if grep -qs '^target-dir' "$config"; then |
| 36 | echo "$config already sets target-dir:"; grep '^target-dir' "$config" |
| 37 | elif grep -qs '^\[build\]' "$config"; then |
| 38 | sed -i "/^\[build\]/a target-dir = \"$sharedTarget\"" "$config" |
| 39 | else |
| 40 | printf '\n[build]\ntarget-dir = "%s"\n' "$sharedTarget" >> "$config" |
| 41 | fi |
| 42 | echo "shared target dir: $sharedTarget" |
| 43 | [ -z "${CARGO_TARGET_DIR:-}" ] || echo "CARGO_TARGET_DIR=$CARGO_TARGET_DIR is set and wins over the config; unset it (shell rc, worktree env)" >&2 |
| 44 | exit 0 |
| 45 | ;; |
| 46 | status) |
| 47 | echo "shared target: $(human "$sharedTarget") $sharedTarget" |
| 48 | [ -d "$sharedTarget" ] && for directory in "$sharedTarget"/*/ "$sharedTarget"/x86_64-pc-windows-msvc/*/; do |
| 49 | [ -d "$directory" ] && echo " $(human "$directory") $directory" |
| 50 | done |
| 51 | echo "old per-worktree target dirs:" |
| 52 | legacyTargets | while read -r directory; do echo " $(human "$directory") $directory"; done |
| 53 | if command -v kache >/dev/null; then echo "kache:"; kache stats 2>/dev/null | sed 's/^/ /' || true; fi |
| 54 | exit 0 |
| 55 | ;; |
| 56 | clean) ;; |
| 57 | *) sed -n '2,9p' "$0"; exit 2 ;; |
| 58 | esac |
| 59 | |
| 60 | apply=false |
| 61 | [ "${2:-}" = "--apply" ] && apply=true |
| 62 | remove() { |
| 63 | echo "remove $(human "$1") $1" |
| 64 | if $apply; then rm -rf "$1"; fi |
| 65 | } |
| 66 | |
| 67 | legacyTargets | while read -r directory; do |
| 68 | [ "$(realpath "$directory")" = "$(realpath -m "$sharedTarget")" ] || remove "$directory" |
| 69 | done |
| 70 | |
| 71 | # Over the cap: drop what rebuilds fastest first. Incremental caches, then the Linux debug build, then the Windows release |
| 72 | # dependencies. The Windows exe itself stays, so scripts/deploy-windows.sh still works after a clean. |
| 73 | if [ -d "$sharedTarget" ]; then |
| 74 | remainingKilobytes=$(sizeKilobytes "$sharedTarget") |
| 75 | for candidate in \ |
| 76 | "$sharedTarget"/debug/incremental "$windowsRelease"/incremental "$sharedTarget"/release/incremental \ |
| 77 | "$sharedTarget"/debug "$sharedTarget"/release "$sharedTarget"/x86_64-pc-windows-msvc/debug \ |
| 78 | "$windowsRelease"/examples "$windowsRelease"/deps "$windowsRelease"/build; do |
| 79 | [ "$remainingKilobytes" -le "$capKilobytes" ] && break |
| 80 | [ -d "$candidate" ] || continue |
| 81 | remainingKilobytes=$(( remainingKilobytes - $(sizeKilobytes "$candidate") )) |
| 82 | remove "$candidate" |
| 83 | done |
| 84 | echo "shared target: $(human "$sharedTarget"), cap ${RUSTY_TEAMS_TARGET_CAP_GB:-40}G" |
| 85 | fi |
| 86 | |
| 87 | if command -v kache >/dev/null; then |
| 88 | echo "kache gc down to ${KACHE_MAX_SIZE:-15GiB}" |
| 89 | if $apply; then KACHE_MAX_SIZE="${KACHE_MAX_SIZE:-15GiB}" kache gc; fi |
| 90 | fi |
| 91 | $apply || echo "dry run; add --apply to remove" |