diff options
| -rw-r--r-- | scripts/build_llvm_mc.ps1 | 30 | ||||
| -rw-r--r-- | tests/test_build_llvm_mc_cleanup.py | 48 |
2 files changed, 73 insertions, 5 deletions
diff --git a/scripts/build_llvm_mc.ps1 b/scripts/build_llvm_mc.ps1 index e028fd9de..3d4d8044a 100644 --- a/scripts/build_llvm_mc.ps1 +++ b/scripts/build_llvm_mc.ps1 | |||
| @@ -49,8 +49,29 @@ if (-not ((Test-Path $cmake) -and (Test-Path $ninja))) { | |||
| 49 | 49 | ||
| 50 | $src = Join-Path $Temp "src" | 50 | $src = Join-Path $Temp "src" |
| 51 | $out = Join-Path $Temp "build" | 51 | $out = Join-Path $Temp "build" |
| 52 | function Remove-DirectoryRobust([string]$path) { | ||
| 53 | if (-not (Test-Path -LiteralPath $path)) { return } | ||
| 54 | # Strip read-only attributes on nested files and folders so Windows allows deletion | ||
| 55 | Get-ChildItem -LiteralPath $path -Recurse -Force -ErrorAction SilentlyContinue | ForEach-Object { | ||
| 56 | try { | ||
| 57 | if ($_.Attributes -band [System.IO.FileAttributes]::ReadOnly) { | ||
| 58 | $_.Attributes = $_.Attributes -bxor [System.IO.FileAttributes]::ReadOnly | ||
| 59 | } | ||
| 60 | } catch {} | ||
| 61 | } | ||
| 62 | for ($attempt = 1; $attempt -le 3; $attempt++) { | ||
| 63 | try { | ||
| 64 | Remove-Item -LiteralPath $path -Recurse -Force -ErrorAction Stop | ||
| 65 | return | ||
| 66 | } catch { | ||
| 67 | Start-Sleep -Milliseconds (100 * $attempt) | ||
| 68 | } | ||
| 69 | } | ||
| 70 | cmd.exe /c "rmdir /s /q `"$path`"" 2>$null | ||
| 71 | } | ||
| 72 | |||
| 52 | if (-not (Test-Path (Join-Path $src "llvm\CMakeLists.txt"))) { | 73 | if (-not (Test-Path (Join-Path $src "llvm\CMakeLists.txt"))) { |
| 53 | if (Test-Path $src) { Remove-Item -Recurse -Force $src } | 74 | if (Test-Path -LiteralPath $src) { Remove-DirectoryRobust $src } |
| 54 | New-Item -ItemType Directory -Force $Temp | Out-Null | 75 | New-Item -ItemType Directory -Force $Temp | Out-Null |
| 55 | # core.longpaths: some llvm/docs paths are about 150 characters, so with a -Temp of about 100 characters the | 76 | # core.longpaths: some llvm/docs paths are about 150 characters, so with a -Temp of about 100 characters the |
| 56 | # sparse checkout fails with "Filename too long" (Windows' 260-character limit). Set in the clone's config only. | 77 | # sparse checkout fails with "Filename too long" (Windows' 260-character limit). Set in the clone's config only. |
| @@ -125,10 +146,9 @@ New-Item -ItemType Directory -Force $Dest | Out-Null | |||
| 125 | $final = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath((Join-Path $Dest "llvm-mc.exe")) | 146 | $final = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath((Join-Path $Dest "llvm-mc.exe")) |
| 126 | Copy-Item -Force $exe $final | 147 | Copy-Item -Force $exe $final |
| 127 | if (-not $Keep) { | 148 | if (-not $Keep) { |
| 128 | try { | 149 | Remove-DirectoryRobust $Temp |
| 129 | Remove-Item -LiteralPath $Temp -Recurse -Force | 150 | if (Test-Path -LiteralPath $Temp) { |
| 130 | } catch { | 151 | Write-Warning "Installed $final, but cleanup of '$Temp' failed." |
| 131 | Write-Warning "Installed $final, but cleanup of '$Temp' failed: $($_.Exception.Message)" | ||
| 132 | } | 152 | } |
| 133 | } | 153 | } |
| 134 | if (-not $NoEnv) { | 154 | if (-not $NoEnv) { |
diff --git a/tests/test_build_llvm_mc_cleanup.py b/tests/test_build_llvm_mc_cleanup.py new file mode 100644 index 000000000..4ad7a7b5a --- /dev/null +++ b/tests/test_build_llvm_mc_cleanup.py | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | """Exercise the native LLVM builder's robust directory cleanup handling read-only attributes.""" | ||
| 2 | import os | ||
| 3 | from pathlib import Path | ||
| 4 | import stat | ||
| 5 | import subprocess | ||
| 6 | import tempfile | ||
| 7 | import unittest | ||
| 8 | |||
| 9 | |||
| 10 | SCRIPT = Path(__file__).resolve().parents[1] / "scripts" / "build_llvm_mc.ps1" | ||
| 11 | |||
| 12 | |||
| 13 | @unittest.skipUnless(os.name == "nt", "requires Windows PowerShell") | ||
| 14 | class BuildCleanupTest(unittest.TestCase): | ||
| 15 | def test_cleanup_removes_readonly_nested_files_and_directories(self): | ||
| 16 | text = SCRIPT.read_text(encoding="utf-8-sig") | ||
| 17 | start = text.index("function Remove-DirectoryRobust") | ||
| 18 | func_def = text[start:text.index("if (-not (Test-Path (Join-Path $src", start)].strip() | ||
| 19 | |||
| 20 | with tempfile.TemporaryDirectory() as directory: | ||
| 21 | target = Path(directory) / "test_temp_cleanup" | ||
| 22 | target.mkdir() | ||
| 23 | sub = target / ".git" / "objects" / "pack" | ||
| 24 | sub.mkdir(parents=True) | ||
| 25 | ro_file = sub / "sample_pack.idx" | ||
| 26 | ro_file.write_bytes(b"DATA") | ||
| 27 | # Mark file and directory read-only on Windows | ||
| 28 | os.chmod(ro_file, stat.S_IREAD) | ||
| 29 | os.chmod(sub, stat.S_IREAD) | ||
| 30 | |||
| 31 | driver = Path(directory) / "test-clean.ps1" | ||
| 32 | driver.write_text( | ||
| 33 | "param([string]$Path)\n" | ||
| 34 | "$ErrorActionPreference = 'Stop'\n" | ||
| 35 | + func_def + "\n" | ||
| 36 | "Remove-DirectoryRobust $Path\n", | ||
| 37 | encoding="utf-8", newline="\n", | ||
| 38 | ) | ||
| 39 | res = subprocess.run( | ||
| 40 | ["powershell.exe", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", | ||
| 41 | "-File", str(driver), "-Path", str(target)], | ||
| 42 | capture_output=True, text=True, check=True, timeout=30, | ||
| 43 | ) | ||
| 44 | self.assertFalse(target.exists(), f"Target directory should have been removed: {res.stderr}") | ||
| 45 | |||
| 46 | |||
| 47 | if __name__ == "__main__": | ||
| 48 | unittest.main() | ||