Clean all build artifacts from a build directory. This function centralizes all build artifact cleanup logic and provides consistent failure tracking and reporting. It's designed to be called when debug mode, build mode, or compiler version changes. Args: build_dir: Me
(build_dir: Path, reason: str)
| 19 | |
| 20 | |
| 21 | def cleanup_build_artifacts(build_dir: Path, reason: str) -> dict[str, CleanupResult]: |
| 22 | """ |
| 23 | Clean all build artifacts from a build directory. |
| 24 | |
| 25 | This function centralizes all build artifact cleanup logic and provides |
| 26 | consistent failure tracking and reporting. It's designed to be called |
| 27 | when debug mode, build mode, or compiler version changes. |
| 28 | |
| 29 | Args: |
| 30 | build_dir: Meson build directory to clean |
| 31 | reason: Human-readable reason for cleanup (e.g., "debug mode changed") |
| 32 | |
| 33 | Returns: |
| 34 | Dictionary mapping artifact type to CleanupResult with deletion stats |
| 35 | """ |
| 36 | _ts_print(f"[MESON] 🗑️ {reason} - cleaning all build artifacts") |
| 37 | |
| 38 | # Artifact types with their file extensions |
| 39 | # Format: (display_name, list of glob patterns) |
| 40 | artifact_types: list[tuple[str, list[str]]] = [ |
| 41 | ("object files", ["*.obj", "*.o"]), |
| 42 | ("static libraries", ["*.a"]), |
| 43 | ("executables", ["*.exe"]), |
| 44 | ("precompiled headers", ["*.pch"]), |
| 45 | ("Windows static libraries", ["*.lib"]), |
| 46 | ("Windows DLLs", ["*.dll"]), |
| 47 | ("Linux shared objects", ["*.so"]), |
| 48 | ("macOS dynamic libraries", ["*.dylib"]), |
| 49 | ] |
| 50 | |
| 51 | results: dict[str, CleanupResult] = {} |
| 52 | |
| 53 | for display_name, patterns in artifact_types: |
| 54 | deleted = 0 |
| 55 | failed = 0 |
| 56 | failed_files: list[str] = [] |
| 57 | |
| 58 | for pattern in patterns: |
| 59 | files = glob.glob(str(build_dir / "**" / pattern), recursive=True) |
| 60 | for file_path in files: |
| 61 | try: |
| 62 | Path(file_path).unlink() |
| 63 | deleted += 1 |
| 64 | except (OSError, IOError) as e: |
| 65 | failed += 1 |
| 66 | failed_files.append(f"{file_path}: {e}") |
| 67 | |
| 68 | results[display_name] = CleanupResult( |
| 69 | deleted=deleted, failed=failed, failed_files=failed_files |
| 70 | ) |
| 71 | |
| 72 | # Build summary message |
| 73 | summary_parts: list[str] = [] |
| 74 | total_failed = 0 |
| 75 | for display_name, result in results.items(): |
| 76 | if result.deleted > 0 or result.failed > 0: |
| 77 | summary_parts.append(f"{result.deleted} {display_name}") |
| 78 | total_failed += result.failed |
no test coverage detected