| 1 | #!/usr/bin/env bash |
| 2 | # Shrink the shared cargo target dir: keep only the newest variant of each linked binary, drop old incremental sessions. |
| 3 | # Usage: scripts/prune-target.sh [target-dir, default $CARGO_TARGET_DIR] [incremental max age in days, default 1] |
| 4 | set -euo pipefail |
| 5 | targetDir="${1:-${CARGO_TARGET_DIR:?set CARGO_TARGET_DIR or pass the target dir}}" |
| 6 | incrementalMaxAgeDays="${2:-1}" |
| 7 | cd "$targetDir" |
| 8 | sizeBefore=$(du -s --block-size=1G . | cut -f1) |
| 9 | |
| 10 | for profileDir in debug release x86_64-pc-windows-msvc/debug x86_64-pc-windows-msvc/release; do |
| 11 | for subDir in deps examples; do |
| 12 | binaryDir="$profileDir/$subDir" |
| 13 | [ -d "$binaryDir" ] || continue |
| 14 | find "$binaryDir" -maxdepth 1 -type f -executable ! -name '*.so' ! -name '*.d' ! -name '*.rlib' ! -name '*.rmeta' -printf '%T@ %p\n' | |
| 15 | sort -rn | |
| 16 | awk '{ name = $2; sub(/-[0-9a-f]{16}(\.exe)?$/, "", name); extension = $2; sub(/.*-[0-9a-f]{16}/, "", extension); if (seen[name extension]++) print $2 }' | |
| 17 | while read -r staleBinary; do |
| 18 | rm -f "$staleBinary" "${staleBinary%.exe}.pdb" "$staleBinary.dwp" |
| 19 | done |
| 20 | done |
| 21 | for incrementalDir in "$profileDir"/incremental*; do |
| 22 | [ -d "$incrementalDir" ] || continue |
| 23 | find "$incrementalDir" -mindepth 1 -maxdepth 1 -type d -mtime +"$incrementalMaxAgeDays" -exec rm -rf {} + |
| 24 | done |
| 25 | done |
| 26 | |
| 27 | echo "target dir: ${sizeBefore}G -> $(du -s --block-size=1G . | cut -f1)G" |