forked from microsoft/vcpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.ps1
418 lines (359 loc) · 13.2 KB
/
bootstrap.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
[CmdletBinding()]
param(
$badParam,
[Parameter(Mandatory=$False)][switch]$disableMetrics = $false,
[Parameter(Mandatory=$False)][switch]$win64 = $false,
[Parameter(Mandatory=$False)][string]$withVSPath = "",
[Parameter(Mandatory=$False)][string]$withWinSDK = ""
)
Set-StrictMode -Version Latest
# Powershell2-compatible way of forcing named-parameters
if ($badParam)
{
if ($disableMetrics -and $badParam -eq "1")
{
Write-Warning "'disableMetrics 1' is deprecated, please change to 'disableMetrics' (without '1')"
}
else
{
throw "Only named parameters are allowed"
}
}
$scriptsDir = split-path -parent $script:MyInvocation.MyCommand.Definition
$withVSPath = $withVSPath -replace "\\$" # Remove potential trailing backslash
function vcpkgHasProperty([Parameter(Mandatory=$true)][AllowNull()]$object, [Parameter(Mandatory=$true)]$propertyName)
{
if ($null -eq $object)
{
return $false
}
return [bool]($object.psobject.Properties | Where-Object { $_.Name -eq "$propertyName"})
}
function getProgramFiles32bit()
{
$out = ${env:PROGRAMFILES(X86)}
if ($null -eq $out)
{
$out = ${env:PROGRAMFILES}
}
if ($null -eq $out)
{
throw "Could not find [Program Files 32-bit]"
}
return $out
}
$vcpkgRootDir = $scriptsDir
while (!($vcpkgRootDir -eq "") -and !(Test-Path "$vcpkgRootDir\.vcpkg-root"))
{
Write-Verbose "Examining $vcpkgRootDir for .vcpkg-root"
$vcpkgRootDir = Split-path $vcpkgRootDir -Parent
}
Write-Verbose "Examining $vcpkgRootDir for .vcpkg-root - Found"
$vcpkgSourcesPath = "$vcpkgRootDir\toolsrc"
if (!(Test-Path $vcpkgSourcesPath))
{
Write-Error "Unable to determine vcpkg sources directory. '$vcpkgSourcesPath' does not exist."
return
}
function getVisualStudioInstances()
{
$programFiles = getProgramFiles32bit
$results = New-Object System.Collections.ArrayList
$vswhereExe = "$programFiles\Microsoft Visual Studio\Installer\vswhere.exe"
if (Test-Path $vswhereExe)
{
$output = & $vswhereExe -prerelease -legacy -products * -format xml
[xml]$asXml = $output
foreach ($instance in $asXml.instances.instance)
{
$installationPath = $instance.InstallationPath -replace "\\$" # Remove potential trailing backslash
$installationVersion = $instance.InstallationVersion
$isPrerelease = -7
if (vcpkgHasProperty -object $instance -propertyName "isPrerelease")
{
$isPrerelease = $instance.isPrerelease
}
if ($isPrerelease -eq 0)
{
$releaseType = "PreferenceWeight3::StableRelease"
}
elseif ($isPrerelease -eq 1)
{
$releaseType = "PreferenceWeight2::PreRelease"
}
else
{
$releaseType = "PreferenceWeight1::Legacy"
}
# Placed like that for easy sorting according to preference
$results.Add("${releaseType}::${installationVersion}::${installationPath}") > $null
}
}
else
{
Write-Verbose "Could not locate vswhere at $vswhereExe"
}
if ("$env:vs140comntools" -ne "")
{
$installationPath = Split-Path -Parent $(Split-Path -Parent "$env:vs140comntools")
$clExe = "$installationPath\VC\bin\cl.exe"
$vcvarsallbat = "$installationPath\VC\vcvarsall.bat"
if ((Test-Path $clExe) -And (Test-Path $vcvarsallbat))
{
$results.Add("PreferenceWeight1::Legacy::14.0::$installationPath") > $null
}
}
$installationPath = "$programFiles\Microsoft Visual Studio 14.0"
$clExe = "$installationPath\VC\bin\cl.exe"
$vcvarsallbat = "$installationPath\VC\vcvarsall.bat"
if ((Test-Path $clExe) -And (Test-Path $vcvarsallbat))
{
$results.Add("PreferenceWeight1::Legacy::14.0::$installationPath") > $null
}
$results.Sort()
$results.Reverse()
return $results
}
function findAnyMSBuildWithCppPlatformToolset([string]$withVSPath)
{
$VisualStudioInstances = getVisualStudioInstances
if ($null -eq $VisualStudioInstances)
{
throw "Could not find Visual Studio. VS2015 or VS2017 (with C++) needs to be installed."
}
Write-Verbose "VS Candidates:`n`r$([system.String]::Join([Environment]::NewLine, $VisualStudioInstances))"
foreach ($instanceCandidate in $VisualStudioInstances)
{
Write-Verbose "Inspecting: $instanceCandidate"
$split = $instanceCandidate -split "::"
# $preferenceWeight = $split[0]
# $releaseType = $split[1]
$version = $split[2]
$path = $split[3]
if ($withVSPath -ne "" -and $withVSPath -ne $path)
{
Write-Verbose "Skipping: $instanceCandidate"
continue
}
$majorVersion = $version.Substring(0,2);
if ($majorVersion -eq "16")
{
$VCFolder= "$path\VC\Tools\MSVC\"
if (Test-Path $VCFolder)
{
Write-Verbose "Picking: $instanceCandidate"
return "$path\MSBuild\Current\Bin\MSBuild.exe", "v142"
}
}
if ($majorVersion -eq "15")
{
$VCFolder= "$path\VC\Tools\MSVC\"
if (Test-Path $VCFolder)
{
Write-Verbose "Picking: $instanceCandidate"
return "$path\MSBuild\15.0\Bin\MSBuild.exe", "v141"
}
}
if ($majorVersion -eq "14")
{
$clExe= "$path\VC\bin\cl.exe"
if (Test-Path $clExe)
{
Write-Verbose "Picking: $instanceCandidate"
$programFilesPath = getProgramFiles32bit
return "$programFilesPath\MSBuild\14.0\Bin\MSBuild.exe", "v140"
}
}
}
throw "Could not find MSBuild version with C++ support. VS2015, VS2017, or VS2019 (with C++) needs to be installed."
}
function getWindowsSDK( [Parameter(Mandatory=$False)][switch]$DisableWin10SDK = $False,
[Parameter(Mandatory=$False)][switch]$DisableWin81SDK = $False,
[Parameter(Mandatory=$False)][string]$withWinSDK)
{
if ($DisableWin10SDK -and $DisableWin81SDK)
{
throw "Both Win10SDK and Win81SDK were disabled."
}
Write-Verbose "Finding WinSDK"
$validInstances = New-Object System.Collections.ArrayList
# Windows 10 SDK
function CheckWindows10SDK($path)
{
if ($null -eq $path)
{
return
}
$folder = (Join-Path $path "Include")
if (!(Test-Path $folder))
{
Write-Verbose "$folder - Not Found"
return
}
Write-Verbose "$folder - Found"
$win10sdkVersions = @(Get-ChildItem $folder | Where-Object {$_.Name -match "^10"} | Sort-Object)
[array]::Reverse($win10sdkVersions) # Newest SDK first
foreach ($win10sdkV in $win10sdkVersions)
{
$windowsheader = "$folder\$win10sdkV\um\windows.h"
if (!(Test-Path $windowsheader))
{
Write-Verbose "$windowsheader - Not Found"
continue
}
Write-Verbose "$windowsheader - Found"
$ddkheader = "$folder\$win10sdkV\shared\sdkddkver.h"
if (!(Test-Path $ddkheader))
{
Write-Verbose "$ddkheader - Not Found"
continue
}
Write-Verbose "$ddkheader - Found"
$win10sdkVersionString = $win10sdkV.ToString()
Write-Verbose "Found $win10sdkVersionString"
$validInstances.Add($win10sdkVersionString) > $null
}
}
Write-Verbose "`n"
Write-Verbose "Looking for Windows 10 SDK"
$regkey10 = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows Kits\Installed Roots\' -Name 'KitsRoot10' -ErrorAction SilentlyContinue
$regkey10Wow6432 = Get-ItemProperty -Path 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows Kits\Installed Roots\' -Name 'KitsRoot10' -ErrorAction SilentlyContinue
if (vcpkgHasProperty -object $regkey10 "KitsRoot10") { CheckWindows10SDK($regkey10.KitsRoot10) }
if (vcpkgHasProperty -object $regkey10Wow6432 "KitsRoot10") { CheckWindows10SDK($regkey10Wow6432.KitsRoot10) }
CheckWindows10SDK("$env:ProgramFiles\Windows Kits\10")
CheckWindows10SDK("${env:ProgramFiles(x86)}\Windows Kits\10")
# Windows 8.1 SDK
function CheckWindows81SDK($path)
{
if ($null -eq $path)
{
return
}
$folder = "$path\Include"
if (!(Test-Path $folder))
{
Write-Verbose "$folder - Not Found"
return
}
Write-Verbose "$folder - Found"
$win81sdkVersionString = "8.1"
Write-Verbose "Found $win81sdkVersionString"
$validInstances.Add($win81sdkVersionString) > $null
}
Write-Verbose "`n"
Write-Verbose "Looking for Windows 8.1 SDK"
$regkey81 = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows Kits\Installed Roots\' -Name 'KitsRoot81' -ErrorAction SilentlyContinue
$regkey81Wow6432 = Get-ItemProperty -Path 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows Kits\Installed Roots\' -Name 'KitsRoot81' -ErrorAction SilentlyContinue
if (vcpkgHasProperty -object $regkey81 "KitsRoot81") { CheckWindows81SDK($regkey81.KitsRoot81) }
if (vcpkgHasProperty -object $regkey81Wow6432 "KitsRoot81") { CheckWindows81SDK($regkey81Wow6432.KitsRoot81) }
CheckWindows81SDK("$env:ProgramFiles\Windows Kits\8.1")
CheckWindows81SDK("${env:ProgramFiles(x86)}\Windows Kits\8.1")
Write-Verbose "`n`n`n"
Write-Verbose "The following Windows SDKs were found:"
foreach ($instance in $validInstances)
{
Write-Verbose $instance
}
# Selecting
if ($withWinSDK -ne "")
{
foreach ($instance in $validInstances)
{
if ($instance -eq $withWinSDK)
{
return $instance
}
}
throw "Could not find the requested Windows SDK version: $withWinSDK"
}
foreach ($instance in $validInstances)
{
if (!$DisableWin10SDK -and $instance -match "10.")
{
return $instance
}
if (!$DisableWin81SDK -and $instance -match "8.1")
{
return $instance
}
}
throw "Could not detect a Windows SDK / TargetPlatformVersion"
}
$msbuildExeWithPlatformToolset = findAnyMSBuildWithCppPlatformToolset $withVSPath
$msbuildExe = $msbuildExeWithPlatformToolset[0]
$platformToolset = $msbuildExeWithPlatformToolset[1]
$windowsSDK = getWindowsSDK -withWinSDK $withWinSDK
$disableMetricsValue = "0"
if ($disableMetrics)
{
$disableMetricsValue = "1"
}
$platform = "x86"
$vcpkgReleaseDir = "$vcpkgSourcesPath\msbuild.x86.release"
if($PSVersionTable.PSVersion.Major -le 2)
{
$architecture=(Get-WmiObject win32_operatingsystem | Select-Object osarchitecture).osarchitecture
}
else
{
$architecture=(Get-CimInstance win32_operatingsystem | Select-Object osarchitecture).osarchitecture
}
if ($win64)
{
if (-not $architecture -like "*64*")
{
throw "Cannot build 64-bit on non-64-bit system"
}
$platform = "x64"
$vcpkgReleaseDir = "$vcpkgSourcesPath\msbuild.x64.release"
}
if ($architecture -like "*64*")
{
$PreferredToolArchitecture = "x64"
}
else
{
$PreferredToolArchitecture = "x86"
}
$arguments = (
"`"/p:VCPKG_VERSION=-nohash`"",
"`"/p:DISABLE_METRICS=$disableMetricsValue`"",
"/p:Configuration=Release",
"/p:Platform=$platform",
"/p:PlatformToolset=$platformToolset",
"/p:TargetPlatformVersion=$windowsSDK",
"/p:PreferredToolArchitecture=$PreferredToolArchitecture",
"/verbosity:minimal",
"/m",
"/nologo",
"`"$vcpkgSourcesPath\dirs.proj`"") -join " "
function vcpkgInvokeCommandClean()
{
param ( [Parameter(Mandatory=$true)][string]$executable,
[string]$arguments = "")
Write-Verbose "Clean-Executing: ${executable} ${arguments}"
$scriptsDir = split-path -parent $script:MyInvocation.MyCommand.Definition
$cleanEnvScript = "$scriptsDir\cleanEnvironmentHelper.ps1"
$tripleQuotes = "`"`"`""
$argumentsWithEscapedQuotes = $arguments -replace "`"", $tripleQuotes
$command = ". $tripleQuotes$cleanEnvScript$tripleQuotes; & $tripleQuotes$executable$tripleQuotes $argumentsWithEscapedQuotes"
$arg = "-NoProfile", "-ExecutionPolicy Bypass", "-command $command"
$process = Start-Process -FilePath powershell.exe -ArgumentList $arg -PassThru -NoNewWindow
Wait-Process -InputObject $process
$ec = $process.ExitCode
Write-Verbose "Execution terminated with exit code $ec."
return $ec
}
# vcpkgInvokeCommandClean cmd "/c echo %PATH%"
Write-Host "`nBuilding vcpkg.exe ...`n"
$ec = vcpkgInvokeCommandClean $msbuildExe $arguments
if ($ec -ne 0)
{
Write-Error "Building vcpkg.exe failed. Please ensure you have installed Visual Studio with the Desktop C++ workload and the Windows SDK for Desktop C++."
return
}
Write-Host "`nBuilding vcpkg.exe... done.`n"
Write-Verbose "Placing vcpkg.exe in the correct location"
Copy-Item "$vcpkgReleaseDir\vcpkg.exe" "$vcpkgRootDir\vcpkg.exe"
Copy-Item "$vcpkgReleaseDir\vcpkgmetricsuploader.exe" "$vcpkgRootDir\scripts\vcpkgmetricsuploader.exe"
Remove-Item "$vcpkgReleaseDir" -Force -Recurse -ErrorAction SilentlyContinue