|
| 1 | +$ErrorActionPreference = 'Stop' |
| 2 | + |
| 3 | +# Build LLVM from source on Windows. Produces an install tree at |
| 4 | +# $env:INSTALL_PREFIX matching the layout scripts/package-prebuilt.sh expects. |
| 5 | +# |
| 6 | +# Required env: |
| 7 | +# LLVM_VERSION e.g. 7.1.0 or 19.1.7 |
| 8 | +# INSTALL_PREFIX absolute path; cmake --install target |
| 9 | +# |
| 10 | +# Notes on toolchain choice: |
| 11 | +# - LLVM 19+ builds cleanly with MSVC v143 (default on windows-2022). |
| 12 | +# - LLVM 7 predates a number of MSVC C++ conformance fixes and will not |
| 13 | +# compile under MSVC v143 without source patches. We sidestep that by |
| 14 | +# using clang-cl from the LLVM toolchain preinstalled on windows-2022, |
| 15 | +# which is more permissive while still producing MSVC-ABI binaries. |
| 16 | +# - LLVM_BUILD_LLVM_DYLIB is unsupported on Windows; consumers link against |
| 17 | +# the static archives that build.rs already prefers by default. |
| 18 | + |
| 19 | +if (-not $env:LLVM_VERSION) { throw 'LLVM_VERSION must be set' } |
| 20 | +if (-not $env:INSTALL_PREFIX) { throw 'INSTALL_PREFIX must be set' } |
| 21 | + |
| 22 | +$llvmVersion = $env:LLVM_VERSION |
| 23 | +$installPrefix = $env:INSTALL_PREFIX |
| 24 | +$llvmMajor = [int]($llvmVersion -split '\.')[0] |
| 25 | + |
| 26 | +$workDir = if ($env:WORK_DIR) { $env:WORK_DIR } else { Join-Path $PWD 'llvm-src' } |
| 27 | +New-Item -ItemType Directory -Force -Path $workDir | Out-Null |
| 28 | +Set-Location $workDir |
| 29 | + |
| 30 | +# Use Git for Windows' GNU tar by full path. The default `tar` on PATH is |
| 31 | +# C:\Windows\System32\tar.exe (bsdtar) which hangs on .tar.xz — see |
| 32 | +# actions/runner-images#282. Prepending Git's bin dir to PATH also lets |
| 33 | +# GNU tar find `xz` when shelling out for the -J path. |
| 34 | +$gitBin = 'C:\Program Files\Git\usr\bin' |
| 35 | +$gitTar = Join-Path $gitBin 'tar.exe' |
| 36 | +if (-not (Test-Path $gitTar)) { throw "Git for Windows tar.exe not found at $gitTar" } |
| 37 | +$env:PATH = "$gitBin;$env:PATH" |
| 38 | + |
| 39 | +function Get-LlvmTarball { |
| 40 | + param([string]$name) |
| 41 | + if (Test-Path $name) { return } |
| 42 | + $url = "https://github.com/llvm/llvm-project/releases/download/llvmorg-$llvmVersion/$name" |
| 43 | + Write-Host "Downloading $url" |
| 44 | + curl.exe -fL -o $name $url |
| 45 | + if ($LASTEXITCODE -ne 0) { throw "download failed: $name" } |
| 46 | +} |
| 47 | + |
| 48 | +function Expand-LlvmTarball { |
| 49 | + param([string]$name, [string[]]$paths) |
| 50 | + if ($paths) { & $gitTar -xf $name @paths } else { & $gitTar -xf $name } |
| 51 | + if ($LASTEXITCODE -ne 0) { throw "tar extraction failed: $name" } |
| 52 | +} |
| 53 | + |
| 54 | +if ($llvmMajor -le 7) { |
| 55 | + $tarball = "llvm-$llvmVersion.src.tar.xz" |
| 56 | + $srcDir = "llvm-$llvmVersion.src" |
| 57 | + $cmakeSrc = '..' |
| 58 | +} else { |
| 59 | + $tarball = "llvm-project-$llvmVersion.src.tar.xz" |
| 60 | + $srcDir = "llvm-project-$llvmVersion.src" |
| 61 | + $cmakeSrc = '../llvm' |
| 62 | +} |
| 63 | + |
| 64 | +if (-not (Test-Path $srcDir)) { |
| 65 | + Get-LlvmTarball $tarball |
| 66 | + if ($llvmMajor -le 7) { |
| 67 | + Expand-LlvmTarball $tarball |
| 68 | + } else { |
| 69 | + # clang/test/Driver/Inputs has out-of-order symlinks that msys-tar |
| 70 | + # cannot create on Windows (CreateSymbolicLink requires the target's |
| 71 | + # FILE/DIR type, which it determines by stat'ing — fails when the |
| 72 | + # target hasn't been extracted yet). The build only needs llvm/, |
| 73 | + # cmake/, and third-party/, so extract just those three subtrees. |
| 74 | + Expand-LlvmTarball $tarball @("$srcDir/llvm", "$srcDir/cmake", "$srcDir/third-party") |
| 75 | + } |
| 76 | + |
| 77 | + # LLVM 7 ships with uninitialized members in ManagedStaticBase, which |
| 78 | + # under modern MSVC turns the AllSubCommands ManagedStatic into a |
| 79 | + # dynamic-init that wipes the cl::opt list — making -gen-attrs and most |
| 80 | + # of llvm-tblgen's options invisible at runtime. Backport the upstream |
| 81 | + # fix (LLVM #40712 / D80433). See the patch header for full details. |
| 82 | + if ($llvmMajor -le 7) { |
| 83 | + $patch = Join-Path $gitBin 'patch.exe' |
| 84 | + if (-not (Test-Path $patch)) { throw "patch.exe not found at $patch" } |
| 85 | + $patchFile = Join-Path $PSScriptRoot 'patches\llvm7-managedstatic-msvc.patch' |
| 86 | + Push-Location $srcDir |
| 87 | + try { |
| 88 | + & $patch -p1 --binary -i $patchFile |
| 89 | + if ($LASTEXITCODE -ne 0) { throw "patch failed: $patchFile" } |
| 90 | + } finally { |
| 91 | + Pop-Location |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +Set-Location $srcDir |
| 97 | +New-Item -ItemType Directory -Force -Path build | Out-Null |
| 98 | +Set-Location build |
| 99 | + |
| 100 | +Write-Host "Configuring cmake" |
| 101 | + |
| 102 | +$cmakeArgs = @( |
| 103 | + '-G', 'Ninja', |
| 104 | + '-DCMAKE_BUILD_TYPE=Release', |
| 105 | + '-DLLVM_TARGETS_TO_BUILD=X86;NVPTX', |
| 106 | + '-DLLVM_ENABLE_ASSERTIONS=OFF', |
| 107 | + '-DLLVM_ENABLE_BINDINGS=OFF', |
| 108 | + '-DLLVM_INCLUDE_EXAMPLES=OFF', |
| 109 | + '-DLLVM_INCLUDE_TESTS=OFF', |
| 110 | + '-DLLVM_INCLUDE_BENCHMARKS=OFF', |
| 111 | + '-DLLVM_ENABLE_ZLIB=OFF', |
| 112 | + '-DLLVM_ENABLE_TERMINFO=OFF', |
| 113 | + "-DCMAKE_INSTALL_PREFIX=$installPrefix" |
| 114 | +) |
| 115 | + |
| 116 | +if ($llvmMajor -le 7) { |
| 117 | + $clangCl = 'C:/Program Files/LLVM/bin/clang-cl.exe' |
| 118 | + if (-not (Test-Path $clangCl)) { |
| 119 | + throw "clang-cl required for LLVM 7 builds was not found at $clangCl" |
| 120 | + } |
| 121 | + $cmakeArgs += @( |
| 122 | + "-DCMAKE_C_COMPILER=$clangCl", |
| 123 | + "-DCMAKE_CXX_COMPILER=$clangCl" |
| 124 | + ) |
| 125 | +} |
| 126 | + |
| 127 | +$cmakeArgs += $cmakeSrc |
| 128 | + |
| 129 | +cmake @cmakeArgs |
| 130 | +if ($LASTEXITCODE -ne 0) { throw 'cmake configure failed' } |
| 131 | + |
| 132 | +Write-Host "Building (ninja -j $env:NUMBER_OF_PROCESSORS)" |
| 133 | +ninja -j $env:NUMBER_OF_PROCESSORS |
| 134 | +if ($LASTEXITCODE -ne 0) { throw 'ninja build failed' } |
| 135 | + |
| 136 | +Write-Host "Installing to $installPrefix" |
| 137 | +ninja install |
| 138 | +if ($LASTEXITCODE -ne 0) { throw 'ninja install failed' } |
| 139 | + |
| 140 | +Write-Host "Done." |
0 commit comments