1# Download new TLS certs from Windows Update
2Get-Date
3Write-Host "Updating TLS certificate store"
4$certdir = (New-Item -ItemType Directory -Name "_tlscerts")
5certutil -syncwithWU "$certdir"
6Foreach ($file in (Get-ChildItem -Path "$certdir\*" -Include "*.crt")) {
7  Import-Certificate -FilePath $file -CertStoreLocation Cert:\LocalMachine\Root
8}
9Remove-Item -Recurse -Path $certdir
10
11
12Get-Date
13Write-Host "Installing Chocolatey"
14Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
15Import-Module "$env:ProgramData\chocolatey\helpers\chocolateyProfile.psm1"
16Update-SessionEnvironment
17Write-Host "Installing Chocolatey packages"
18
19# Chocolatey tries to download winflexbison from SourceForge, which is not super reliable, and has no retry
20# loop of its own - so we give it a helping hand here
21For ($i = 0; $i -lt 5; $i++) {
22  choco install -y python3 --params="/InstallDir:C:\python3"
23  $python_install = $?
24  choco install --allow-empty-checksums -y cmake git git-lfs ninja pkgconfiglite winflexbison vulkan-sdk --installargs "ADD_CMAKE_TO_PATH=System"
25  $other_install = $?
26  $choco_installed = $other_install -and $python_install
27  if ($choco_installed) {
28    Break
29  }
30}
31
32if (!$choco_installed) {
33  Write-Host "Couldn't install dependencies from Chocolatey"
34  Exit 1
35}
36
37# Add Chocolatey's native install path
38Update-SessionEnvironment
39# Python and CMake add themselves to the system environment path, which doesn't get refreshed
40# until we start a new shell
41$env:PATH = "C:\python3;C:\python3\scripts;C:\Program Files\CMake\bin;$env:PATH"
42
43Start-Process -NoNewWindow -Wait git -ArgumentList 'config --global core.autocrlf false'
44
45Get-Date
46Write-Host "Installing Meson, Mako and numpy"
47pip3 install meson mako numpy
48if (!$?) {
49  Write-Host "Failed to install dependencies from pip"
50  Exit 1
51}
52
53# we want more secure TLS 1.2 for most things, but it breaks SourceForge
54# downloads so must be done after Chocolatey use
55[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13;
56
57Get-Date
58Write-Host "Cloning LLVM release/12.x"
59git clone -b release/12.x --depth=1 https://github.com/llvm/llvm-project llvm-project
60if (!$?) {
61  Write-Host "Failed to clone LLVM repository"
62  Exit 1
63}
64
65# ideally we want to use a tag here insted of a sha,
66# but as of today, SPIRV-LLVM-Translator doesn't have
67# a tag matching LLVM 12.0.0
68Get-Date
69Write-Host "Cloning SPIRV-LLVM-Translator"
70git clone https://github.com/KhronosGroup/SPIRV-LLVM-Translator llvm-project/llvm/projects/SPIRV-LLVM-Translator
71if (!$?) {
72  Write-Host "Failed to clone SPIRV-LLVM-Translator repository"
73  Exit 1
74}
75Push-Location llvm-project/llvm/projects/SPIRV-LLVM-Translator
76git checkout 5b641633b3bcc3251a52260eee11db13a79d7258
77Pop-Location
78
79Get-Date
80# slightly convoluted syntax but avoids the CWD being under the PS filesystem meta-path
81$llvm_build = New-Item -ItemType Directory -Path ".\llvm-project" -Name "build"
82Push-Location -Path $llvm_build.FullName
83Write-Host "Compiling LLVM and Clang"
84cmd.exe /C 'C:\BuildTools\Common7\Tools\VsDevCmd.bat -host_arch=amd64 -arch=amd64 && cmake ../llvm -GNinja -DCMAKE_BUILD_TYPE=Release -DLLVM_USE_CRT_RELEASE=MT -DCMAKE_INSTALL_PREFIX="C:\llvm-10" -DLLVM_ENABLE_PROJECTS="clang;lld" -DLLVM_TARGETS_TO_BUILD=AMDGPU;X86 -DLLVM_OPTIMIZED_TABLEGEN=TRUE -DLLVM_ENABLE_ASSERTIONS=TRUE -DLLVM_INCLUDE_UTILS=OFF -DLLVM_INCLUDE_RUNTIMES=OFF -DLLVM_INCLUDE_TESTS=OFF -DLLVM_INCLUDE_EXAMPLES=OFF -DLLVM_INCLUDE_GO_TESTS=OFF -DLLVM_INCLUDE_BENCHMARKS=OFF -DLLVM_BUILD_LLVM_C_DYLIB=OFF -DLLVM_ENABLE_DIA_SDK=OFF -DCLANG_BUILD_TOOLS=ON -DLLVM_SPIRV_INCLUDE_TESTS=OFF && ninja -j32 install'
85$buildstatus = $?
86Pop-Location
87if (!$buildstatus) {
88  Write-Host "Failed to compile LLVM"
89  Exit 1
90}
91
92Get-Date
93$libclc_build = New-Item -ItemType Directory -Path ".\llvm-project" -Name "build-libclc"
94Push-Location -Path $libclc_build.FullName
95Write-Host "Compiling libclc"
96# libclc can only be built with Ninja, because CMake's VS backend doesn't know how to compile new language types
97cmd.exe /C 'C:\BuildTools\Common7\Tools\VsDevCmd.bat -host_arch=amd64 -arch=amd64 && cmake ../libclc -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-m64" -DCMAKE_POLICY_DEFAULT_CMP0091=NEW -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded -DCMAKE_INSTALL_PREFIX="C:\llvm-10" -DLIBCLC_TARGETS_TO_BUILD="spirv-mesa3d-;spirv64-mesa3d-" && ninja -j32 install'
98$buildstatus = $?
99Pop-Location
100Remove-Item -Recurse -Path $libclc_build
101if (!$buildstatus) {
102  Write-Host "Failed to compile libclc"
103  Exit 1
104}
105Remove-Item -Recurse -Path $llvm_build
106
107Get-Date
108Write-Host "Cloning SPIRV-Tools"
109git clone https://github.com/KhronosGroup/SPIRV-Tools
110if (!$?) {
111  Write-Host "Failed to clone SPIRV-Tools repository"
112  Exit 1
113}
114git clone https://github.com/KhronosGroup/SPIRV-Headers SPIRV-Tools/external/SPIRV-Headers
115if (!$?) {
116  Write-Host "Failed to clone SPIRV-Headers repository"
117  Exit 1
118}
119Write-Host "Building SPIRV-Tools"
120$spv_build = New-Item -ItemType Directory -Path ".\SPIRV-Tools" -Name "build"
121Push-Location -Path $spv_build.FullName
122# SPIRV-Tools doesn't use multi-threaded MSVCRT, but we need it to
123cmd.exe /C 'C:\BuildTools\Common7\Tools\VsDevCmd.bat -host_arch=amd64 -arch=amd64 && cmake .. -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_POLICY_DEFAULT_CMP0091=NEW -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded -DCMAKE_INSTALL_PREFIX="C:\spirv-tools" && ninja -j32 install'
124$buildstatus = $?
125Pop-Location
126Remove-Item -Recurse -Path $spv_build
127if (!$buildstatus) {
128  Write-Host "Failed to compile SPIRV-Tools"
129  Exit 1
130}
131
132Get-Date
133Write-Host "Downloading Vulkan-Runtime"
134Invoke-WebRequest -Uri 'https://sdk.lunarg.com/sdk/download/latest/windows/vulkan-runtime.exe' -OutFile 'C:\vulkan-runtime.exe' | Out-Null
135Write-Host "Installing Vulkan-Runtime"
136Start-Process -NoNewWindow -Wait C:\vulkan-runtime.exe -ArgumentList '/S'
137if (!$?) {
138  Write-Host "Failed to install Vulkan-Runtime"
139  Exit 1
140}
141Remove-Item C:\vulkan-runtime.exe -Force
142
143Get-Date
144Write-Host "Downloading Freeglut"
145
146$freeglut_zip = 'freeglut-MSVC.zip'
147$freeglut_url = "https://www.transmissionzero.co.uk/files/software/development/GLUT/$freeglut_zip"
148
149For ($i = 0; $i -lt 5; $i++) {
150  Invoke-WebRequest -Uri $freeglut_url -OutFile $freeglut_zip
151  $freeglut_downloaded = $?
152  if ($freeglut_downloaded) {
153    Break
154  }
155}
156
157if (!$freeglut_downloaded) {
158  Write-Host "Failed to download Freeglut"
159  Exit 1
160}
161
162Get-Date
163Write-Host "Installing Freeglut"
164Expand-Archive $freeglut_zip -DestinationPath C:\
165if (!$?) {
166  Write-Host "Failed to install Freeglut"
167  Exit 1
168}
169
170Get-Date
171Write-Host "Downloading glext.h"
172New-Item -ItemType Directory -Path ".\glext" -Name "GL"
173$ProgressPreference = "SilentlyContinue"
174Invoke-WebRequest -Uri 'https://www.khronos.org/registry/OpenGL/api/GL/glext.h' -OutFile '.\glext\GL\glext.h' | Out-Null
175
176Get-Date
177Write-Host "Cloning Piglit"
178git clone --no-progress --single-branch --no-checkout https://gitlab.freedesktop.org/mesa/piglit.git 'C:\src\piglit'
179if (!$?) {
180  Write-Host "Failed to clone Piglit repository"
181  Exit 1
182}
183Push-Location -Path C:\src\piglit
184git checkout b0bbeb876a506e0ee689dd7e17cee374c8284058
185Pop-Location
186
187Get-Date
188$piglit_build = New-Item -ItemType Directory -Path "C:\src\piglit" -Name "build"
189Push-Location -Path $piglit_build.FullName
190Write-Host "Compiling Piglit"
191cmd.exe /C 'C:\BuildTools\Common7\Tools\VsDevCmd.bat -host_arch=amd64 -arch=amd64 && cmake .. -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="C:\Piglit" -DGLUT_INCLUDE_DIR=C:\freeglut\include -DGLUT_glut_LIBRARY_RELEASE=C:\freeglut\lib\x64\freeglut.lib -DGLEXT_INCLUDE_DIR=.\glext && ninja -j32'
192$buildstatus = $?
193ninja -j32 install | Out-Null
194$installstatus = $?
195Pop-Location
196Remove-Item -Recurse -Path $piglit_build
197if (!$buildstatus -Or !$installstatus) {
198  Write-Host "Failed to compile or install Piglit"
199  Exit 1
200}
201
202Copy-Item -Path C:\freeglut\bin\x64\freeglut.dll -Destination C:\Piglit\lib\piglit\bin\freeglut.dll
203
204Get-Date
205Write-Host "Complete"
206