@@ -42,6 +42,11 @@ An array of architectures for which the Swift SDK should be built.
42
42
The product version to be used when building the installer.
43
43
Supports semantic version strings.
44
44
45
+ . PARAMETER WinSDKVersion
46
+ The version number of the Windows SDK to be used.
47
+ Overrides the value resolved by the Visual Studio command prompt.
48
+ If no such Windows SDK is installed, it will be downloaded from nuget.
49
+
45
50
. PARAMETER SkipBuild
46
51
If set, does not run the build phase.
47
52
78
83
[string ] $SwiftDebugFormat = " dwarf" ,
79
84
[string []] $SDKs = @ (" X64" , " X86" , " Arm64" ),
80
85
[string ] $ProductVersion = " 0.0.0" ,
86
+ [string ] $WinSDKVersion = " " ,
81
87
[switch ] $SkipBuild = $false ,
82
88
[switch ] $SkipRedistInstall = $false ,
83
89
[switch ] $SkipPackaging = $false ,
@@ -92,23 +98,28 @@ Set-StrictMode -Version 3.0
92
98
93
99
# Avoid being run in a "Developer" shell since this script launches its own sub-shells targeting
94
100
# different architectures, and these variables cause confusion.
95
- if ($env: VSCMD_ARG_HOST_ARCH -ne $null -or $env: VSCMD_ARG_TGT_ARCH -ne $null ) {
101
+ if ($null -ne $env: VSCMD_ARG_HOST_ARCH -or $null -ne $env: VSCMD_ARG_TGT_ARCH ) {
96
102
throw " At least one of VSCMD_ARG_HOST_ARCH and VSCMD_ARG_TGT_ARCH is set, which is incompatible with this script. Likely need to run outside of a Developer shell."
97
103
}
98
104
99
105
# Prevent elsewhere-installed swift modules from confusing our builds.
100
- $Env : SDKROOT = " "
106
+ $env : SDKROOT = " "
101
107
$NativeProcessorArchName = $env: PROCESSOR_ARCHITEW6432
102
108
if ($null -eq $NativeProcessorArchName ) { $NativeProcessorArchName = $env: PROCESSOR_ARCHITECTURE }
103
109
104
- $vswhere = " ${Env: ProgramFiles(x86)} \Microsoft Visual Studio\Installer\vswhere.exe"
110
+ # Store the revision zero variant of the Windows SDK version (no-op if unspecified)
111
+ $WindowsSDKMajorMinorBuildMatch = [Regex ]::Match($WinSDKVersion , " ^\d+\.\d+\.\d+" )
112
+ $WinSDKVersionRevisionZero = if ($WindowsSDKMajorMinorBuildMatch.Success ) { $WindowsSDKMajorMinorBuildMatch.Value + " .0" } else { " " }
113
+ $CustomWinSDKRoot = $null # Overwritten if we download a Windows SDK from nuget
114
+
115
+ $vswhere = " ${env: ProgramFiles(x86)} \Microsoft Visual Studio\Installer\vswhere.exe"
105
116
$VSInstallRoot = & $vswhere - nologo - latest - products " *" - all - prerelease - property installationPath
106
117
$msbuild = " $VSInstallRoot \MSBuild\Current\Bin\$NativeProcessorArchName \MSBuild.exe"
107
118
108
119
# Avoid $env:ProgramFiles in case this script is running as x86
109
120
$UnixToolsBinDir = " $env: SystemDrive \Program Files\Git\usr\bin"
110
121
111
- $python = " ${Env : ProgramFiles(x86)} \Microsoft Visual Studio\Shared\Python39_64\python.exe"
122
+ $python = " ${env : ProgramFiles(x86)} \Microsoft Visual Studio\Shared\Python39_64\python.exe"
112
123
if (-not (Test-Path $python )) {
113
124
$python = (where.exe python) | Select-Object - First 1
114
125
if (-not (Test-Path $python )) {
@@ -192,6 +203,8 @@ function Get-InstallDir($Arch) {
192
203
return " $ImageRoot \$ProgramFilesName \Swift"
193
204
}
194
205
206
+ $NugetRoot = " $BinaryCache \nuget"
207
+
195
208
$LibraryRoot = " $ImageRoot \Library"
196
209
$ToolchainInstallRoot = " $ ( Get-InstallDir $HostArch ) \Toolchains\$ProductVersion +Asserts"
197
210
$PlatformInstallRoot = " $ ( Get-InstallDir $HostArch ) \Platforms\Windows.platform"
@@ -203,20 +216,33 @@ $SDKInstallRoot = "$PlatformInstallRoot\Developer\SDKs\Windows.sdk"
203
216
$HostArch.ToolchainInstallRoot = $ToolchainInstallRoot
204
217
205
218
# Resolve the architectures received as argument
206
- $SDKArchs = $SDKs | ForEach-Object {
219
+ $SDKArchs = @ ( $SDKs | ForEach-Object {
207
220
switch ($_ ) {
208
221
" X64" { $ArchX64 }
209
222
" X86" { $ArchX86 }
210
223
" Arm64" { $ArchArm64 }
211
224
default { throw " Unknown architecture $_ " }
212
225
}
213
- }
226
+ })
214
227
215
228
# Build functions
216
229
function Get-ProjectBinaryCache ($Arch , $ID ) {
217
230
return " $BinaryCache \" + ($Arch.BuildID + $ID )
218
231
}
219
232
233
+ function Copy-File ($Src , $Dst ) {
234
+ # Create the directory tree first so Copy-Item succeeds
235
+ # If $Dst is the target directory, make sure it ends with "\"
236
+ $DstDir = [IO.Path ]::GetDirectoryName($Dst )
237
+ New-Item - ItemType Directory - ErrorAction Ignore $DstDir | Out-Null
238
+ Copy-Item - Force $Src $Dst
239
+ }
240
+
241
+ function Copy-Directory ($Src , $Dst ) {
242
+ New-Item - ItemType Directory - ErrorAction Ignore $Dst | Out-Null
243
+ Copy-Item - Force - Recurse $Src $Dst
244
+ }
245
+
220
246
function Invoke-Program () {
221
247
[CmdletBinding (PositionalBinding = $false )]
222
248
param (
@@ -305,12 +331,73 @@ function Isolate-EnvVars([scriptblock]$Block) {
305
331
}
306
332
307
333
function Invoke-VsDevShell ($Arch ) {
334
+ $DevCmdArguments = " -no_logo -host_arch=$ ( $HostArch.VSName ) -arch=$ ( $Arch.VSName ) "
335
+ if ($CustomWinSDKRoot ) {
336
+ $DevCmdArguments += " -winsdk=none"
337
+ } elseif ($WinSDKVersion ) {
338
+ $DevCmdArguments += " -winsdk=$WinSDKVersionRevisionZero "
339
+ }
340
+
308
341
if ($ToBatch ) {
309
- Write-Output " call `" $VSInstallRoot \Common7\Tools\VsDevCmd.bat`" -no_logo -host_arch= $ ( $HostArch .VSName ) -arch= $ ( $Arch .VSName ) "
342
+ Write-Output " call `" $VSInstallRoot \Common7\Tools\VsDevCmd.bat`" $DevCmdArguments "
310
343
} else {
311
344
# This dll path is valid for VS2019 and VS2022, but it was under a vsdevcmd subfolder in VS2017
312
345
Import-Module " $VSInstallRoot \Common7\Tools\Microsoft.VisualStudio.DevShell.dll"
313
- Enter-VsDevShell - VsInstallPath $VSInstallRoot - SkipAutomaticLocation - DevCmdArguments " -no_logo -host_arch=$ ( $HostArch.VSName ) -arch=$ ( $Arch.VSName ) "
346
+ Enter-VsDevShell - VsInstallPath $VSInstallRoot - SkipAutomaticLocation - DevCmdArguments $DevCmdArguments
347
+
348
+ if ($CustomWinSDKRoot ) {
349
+ # Using a non-installed Windows SDK. Setup environment variables manually.
350
+ $WinSDKVerIncludeRoot = " $CustomWinSDKRoot \include\$WinSDKVersionRevisionZero "
351
+ $WinSDKIncludePath = " $WinSDKVerIncludeRoot \ucrt;$WinSDKVerIncludeRoot \um;$WinSDKVerIncludeRoot \shared;$WinSDKVerIncludeRoot \winrt;$WinSDKVerIncludeRoot \cppwinrt"
352
+ $WinSDKVerLibRoot = " $CustomWinSDKRoot \lib\$WinSDKVersionRevisionZero "
353
+
354
+ $env: WindowsLibPath = " $CustomWinSDKRoot \UnionMetadata\$WinSDKVersionRevisionZero ;$CustomWinSDKRoot \References\$WinSDKVersionRevisionZero "
355
+ $env: WindowsSdkBinPath = " $CustomWinSDKRoot \bin"
356
+ $env: WindowsSDKLibVersion = " $WinSDKVersionRevisionZero \"
357
+ $env: WindowsSdkVerBinPath = " $CustomWinSDKRoot \bin\$WinSDKVersionRevisionZero "
358
+ $env: WindowsSDKVersion = " $WinSDKVersionRevisionZero \"
359
+
360
+ $env: EXTERNAL_INCLUDE += " ;$WinSDKIncludePath "
361
+ $env: INCLUDE += " ;$WinSDKIncludePath "
362
+ $env: LIB += " ;$WinSDKVerLibRoot \ucrt\$ ( $Arch.ShortName ) ;$WinSDKVerLibRoot \um\$ ( $Arch.ShortName ) "
363
+ $env: LIBPATH += " ;$env: WindowsLibPath "
364
+ $env: PATH += " ;$env: WindowsSdkVerBinPath \$ ( $Arch.ShortName ) ;$env: WindowsSdkBinPath \$ ( $Arch.ShortName ) "
365
+ $env: UCRTVersion = $WinSDKVersionRevisionZero
366
+ $env: UniversalCRTSdkDir = $CustomWinSDKRoot
367
+ }
368
+ }
369
+ }
370
+
371
+ function Ensure-WindowsSDK {
372
+ # Assume we always have a default Windows SDK version available
373
+ if (-not $WinSDKVersion ) { return }
374
+
375
+ # Check whether VsDevShell can already resolve the requested Windows SDK version
376
+ try {
377
+ Isolate- EnvVars { Invoke-VsDevShell $HostArch }
378
+ return
379
+ } catch {}
380
+
381
+ Write-Output " Windows SDK $WinSDKVersion not found. Downloading from nuget..."
382
+
383
+ # Assume the requested Windows SDK is not available and we need to download it
384
+ # Install the base nuget package that contains header files
385
+ $WinSDKBasePackageName = " Microsoft.Windows.SDK.CPP"
386
+ Invoke-Program nuget install $WinSDKBasePackageName - Version $WinSDKVersion - OutputDirectory $NugetRoot
387
+
388
+ # Export to script scope so Invoke-VsDevShell can read it
389
+ $script :CustomWinSDKRoot = " $NugetRoot \$WinSDKBasePackageName .$WinSDKVersion \c"
390
+
391
+ # Install each required arch-specific package and move the files under the base /lib directory
392
+ $WinSDKArchs = $SDKArchs.Clone ()
393
+ if (-not ($HostArch -in $WinSDKArchs )) {
394
+ $WinSDKArchs += $HostArch
395
+ }
396
+
397
+ foreach ($Arch in $WinSDKArchs ) {
398
+ $WinSDKArchPackageName = " $WinSDKBasePackageName .$ ( $Arch.ShortName ) "
399
+ Invoke-Program nuget install $WinSDKArchPackageName - Version $WinSDKVersion - OutputDirectory $NugetRoot
400
+ Copy-Directory " $NugetRoot \$WinSDKArchPackageName .$WinSDKVersion \c\*" " $CustomWinSDKRoot \lib\$WinSDKVersionRevisionZero "
314
401
}
315
402
}
316
403
@@ -978,19 +1065,6 @@ function Build-XCTest($Arch, [switch]$Test = $false) {
978
1065
}
979
1066
}
980
1067
981
- function Copy-File ($Src , $Dst ) {
982
- # Create the directory tree first so Copy-Item succeeds
983
- # If $Dst is the target directory, make sure it ends with "\"
984
- $DstDir = [IO.Path ]::GetDirectoryName($Dst )
985
- New-Item - ItemType Directory - ErrorAction Ignore $DstDir | Out-Null
986
- Copy-Item - Force $Src $Dst
987
- }
988
-
989
- function Copy-Directory ($Src , $Dst ) {
990
- New-Item - ItemType Directory - ErrorAction Ignore $Dst | Out-Null
991
- Copy-Item - Force - Recurse $Src $Dst
992
- }
993
-
994
1068
# Copies files installed by CMake from the arch-specific platform root,
995
1069
# where they follow the layout expected by the installer,
996
1070
# to the final platform root, following the installer layout.
@@ -1414,6 +1488,9 @@ function Build-Installer() {
1414
1488
SWIFT_FORMAT_BUILD = " $ ( $HostArch.BinaryCache ) \swift-format\release" ;
1415
1489
INCLUDE_SWIFT_INSPECT = " true" ;
1416
1490
SWIFT_INSPECT_BUILD = " $ ( $HostArch.BinaryCache ) \swift-inspect\release" ;
1491
+ INCLUDE_SWIFT_DOCC = " true" ;
1492
+ SWIFT_DOCC_BUILD = " $ ( $HostArch.BinaryCache ) \swift-docc\release" ;
1493
+ SWIFT_DOCC_RENDER_ARTIFACT_ROOT = " ${SourceCache} \swift-docc-render-artifact" ;
1417
1494
}
1418
1495
1419
1496
foreach ($Arch in $SDKArchs ) {
@@ -1432,6 +1509,10 @@ function Build-Installer() {
1432
1509
1433
1510
# -------------------------------------------------------------------
1434
1511
1512
+ if (-not $SkipBuild ) {
1513
+ Ensure- WindowsSDK
1514
+ }
1515
+
1435
1516
if (-not $SkipBuild ) {
1436
1517
Build-BuildTools $HostArch
1437
1518
Build-Compilers $HostArch
0 commit comments