|
| 1 | +# PowerShell script to fetch, build, and install LLVM for Slang |
| 2 | + |
| 3 | +# Set script to fail on errors |
| 4 | +$ErrorActionPreference = "Stop" |
| 5 | + |
| 6 | +function Show-Help { |
| 7 | + Write-Host "Fetch, build and install LLVM for Slang |
| 8 | +
|
| 9 | +Options: |
| 10 | + --repo: The source git repo, default: $repo |
| 11 | + --branch: The branch (or tag) to fetch, default: $branch |
| 12 | + --source-dir: Unpack and build in this directory: default $source_dir |
| 13 | + --config: The configuration to build, default $config |
| 14 | + --install-prefix: Install under this prefix |
| 15 | + --: Any following arguments will be passed to the CMake configuration command |
| 16 | +" |
| 17 | +} |
| 18 | + |
| 19 | +function Msg { |
| 20 | + Write-Host "$args" -ForegroundColor Yellow |
| 21 | +} |
| 22 | + |
| 23 | +function Fail { |
| 24 | + Msg "$args" |
| 25 | + exit 1 |
| 26 | +} |
| 27 | + |
| 28 | +function New-TemporaryDirectory { |
| 29 | + $tmp = [System.IO.Path]::GetTempPath() # Not $env:TEMP, see https://stackoverflow.com/a/946017 |
| 30 | + $name = (New-Guid).ToString("N") |
| 31 | + New-Item -ItemType Directory -Path (Join-Path $tmp $name) |
| 32 | +} |
| 33 | + |
| 34 | +# Check if required programs are available |
| 35 | +$requiredPrograms = "cmake", "git" |
| 36 | +foreach ($prog in $requiredPrograms) { |
| 37 | + if (-not (Get-Command $prog -ErrorAction SilentlyContinue)) { |
| 38 | + Msg "This script needs $prog, but it isn't in PATH" |
| 39 | + $missingBin = $true |
| 40 | + } |
| 41 | +} |
| 42 | +if ($missingBin) { |
| 43 | + exit 1 |
| 44 | +} |
| 45 | + |
| 46 | +# Temp directory with cleanup on exit |
| 47 | +$tempDir = New-TemporaryDirectory |
| 48 | +$cleanup = { |
| 49 | + if ($tempDir) { |
| 50 | + Remove-Item -Recurse -Force $tempDir |
| 51 | + } |
| 52 | + exit $lastExitCode |
| 53 | +} |
| 54 | +$null = Register-EngineEvent PowerShell.Exiting -Action $cleanup |
| 55 | + |
| 56 | +# Default values |
| 57 | +$repo = "https://github.com/llvm/llvm-project" |
| 58 | +$branch = "llvmorg-13.0.1" |
| 59 | +$sourceDir = $tempDir.FullName |
| 60 | +$installPrefix = "" |
| 61 | +$config = "Release" |
| 62 | +$extraArguments = @() |
| 63 | + |
| 64 | +# Argument parsing |
| 65 | +for ($i = 0; $i -lt $args.Length; $i++) { |
| 66 | + switch ($args[$i]) { |
| 67 | + "--help" { |
| 68 | + Show-Help |
| 69 | + exit |
| 70 | + } |
| 71 | + "--repo" { |
| 72 | + $repo = $args[++$i] |
| 73 | + } |
| 74 | + "--branch" { |
| 75 | + $branch = $args[++$i] |
| 76 | + } |
| 77 | + "--source-dir" { |
| 78 | + $sourceDir = $args[++$i] |
| 79 | + } |
| 80 | + "--config" { |
| 81 | + $config = $args[++$i] |
| 82 | + } |
| 83 | + "--install-prefix" { |
| 84 | + $installPrefix = $args[++$i] |
| 85 | + } |
| 86 | + "--" { |
| 87 | + $extraArguments = $args[$i + 1..$args.Length] |
| 88 | + break |
| 89 | + } |
| 90 | + default { |
| 91 | + Msg "Unknown parameter passed: $($args[$i])" |
| 92 | + Show-Help |
| 93 | + exit 1 |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +if (-not $repo) { Fail "please set --repo" } |
| 99 | +if (-not $branch) { Fail "please set --branch" } |
| 100 | +if (-not $sourceDir) { Fail "please set --source-dir" } |
| 101 | +if (-not $config) { Fail "please set --config" } |
| 102 | +if (-not $installPrefix) { Fail "please set --install-prefix" } |
| 103 | + |
| 104 | +# Fetch LLVM from the repo |
| 105 | +Msg "##########################################################" |
| 106 | +Msg "# Fetching LLVM from $repo at $branch" |
| 107 | +Msg "##########################################################" |
| 108 | +# git clone --depth 1 --branch $branch $repo $sourceDir |
| 109 | + |
| 110 | +# Configure LLVM with CMake |
| 111 | +Msg "##########################################################" |
| 112 | +Msg "# Configuring LLVM in $sourceDir" |
| 113 | +Msg "##########################################################" |
| 114 | +$msvcRuntimeLib = "MultiThreaded" |
| 115 | +if ($config -eq 'Debug') |
| 116 | +{ |
| 117 | + $msvcRuntimeLib = "MultiThreadedDebug" |
| 118 | +} |
| 119 | +$cmakeArgumentsForSlang = @( |
| 120 | + "-DLLVM_BUILD_LLVM_C_DYLIB=0" |
| 121 | + "-DLLVM_INCLUDE_BENCHMARKS=0" |
| 122 | + "-DLLVM_INCLUDE_DOCS=0" |
| 123 | + "-DLLVM_INCLUDE_EXAMPLES=0" |
| 124 | + "-DLLVM_INCLUDE_TESTS=0" |
| 125 | + "-DLLVM_ENABLE_TERMINFO=0" |
| 126 | + "-DCLANG_BUILD_TOOLS=0" |
| 127 | + "-DCLANG_ENABLE_STATIC_ANALYZER=0" |
| 128 | + "-DCLANG_ENABLE_ARCMT=0" |
| 129 | + "-DCLANG_INCLUDE_DOCS=0" |
| 130 | + "-DCLANG_INCLUDE_TESTS=0" |
| 131 | + "-DLLVM_ENABLE_PROJECTS=clang" |
| 132 | + "-DLLVM_TARGETS_TO_BUILD=X86;ARM;AArch64" |
| 133 | + "-DLLVM_BUILD_TOOLS=0" |
| 134 | + "-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded$<$<CONFIG:Debug>:Debug>" |
| 135 | + "-DLLVM_USE_CRT_RELEASE=MT" |
| 136 | + "-DLLVM_USE_CRT_DEBUG=MTd" |
| 137 | +) |
| 138 | + |
| 139 | +$buildDir = Join-Path $sourceDir "build" |
| 140 | +New-Item -Path $buildDir -ItemType Directory -Force |
| 141 | + |
| 142 | +cmake -S $sourceDir\llvm -B $buildDir $cmakeArgumentsForSlang + $extraArguments |
| 143 | + |
| 144 | +# Build LLVM |
| 145 | +Msg "##########################################################" |
| 146 | +Msg "# Building LLVM in $buildDir" |
| 147 | +Msg "##########################################################" |
| 148 | +cmake --build $buildDir -j --config $config |
| 149 | + |
| 150 | +# Install LLVM |
| 151 | +Msg "##########################################################" |
| 152 | +Msg "# Installing LLVM to $installPrefix" |
| 153 | +Msg "##########################################################" |
| 154 | +cmake --install $buildDir --prefix $installPrefix --config $config |
| 155 | + |
| 156 | +Msg "##########################################################" |
| 157 | +Msg "LLVM installed in $installPrefix" |
| 158 | +Msg "Please add $installPrefix to CMAKE_PREFIX_PATH" |
| 159 | +Msg "##########################################################" |
0 commit comments