Skip to content

Commit 5b526a5

Browse files
committed
synchronise
1 parent d56f705 commit 5b526a5

File tree

1 file changed

+102
-21
lines changed

1 file changed

+102
-21
lines changed

utils/build.ps1

Lines changed: 102 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ An array of architectures for which the Swift SDK should be built.
4242
The product version to be used when building the installer.
4343
Supports semantic version strings.
4444
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+
4550
.PARAMETER SkipBuild
4651
If set, does not run the build phase.
4752
@@ -78,6 +83,7 @@ param(
7883
[string] $SwiftDebugFormat = "dwarf",
7984
[string[]] $SDKs = @("X64","X86","Arm64"),
8085
[string] $ProductVersion = "0.0.0",
86+
[string] $WinSDKVersion = "",
8187
[switch] $SkipBuild = $false,
8288
[switch] $SkipRedistInstall = $false,
8389
[switch] $SkipPackaging = $false,
@@ -92,23 +98,28 @@ Set-StrictMode -Version 3.0
9298

9399
# Avoid being run in a "Developer" shell since this script launches its own sub-shells targeting
94100
# 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) {
96102
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."
97103
}
98104

99105
# Prevent elsewhere-installed swift modules from confusing our builds.
100-
$Env:SDKROOT = ""
106+
$env:SDKROOT = ""
101107
$NativeProcessorArchName = $env:PROCESSOR_ARCHITEW6432
102108
if ($null -eq $NativeProcessorArchName) { $NativeProcessorArchName = $env:PROCESSOR_ARCHITECTURE }
103109

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"
105116
$VSInstallRoot = & $vswhere -nologo -latest -products "*" -all -prerelease -property installationPath
106117
$msbuild = "$VSInstallRoot\MSBuild\Current\Bin\$NativeProcessorArchName\MSBuild.exe"
107118

108119
# Avoid $env:ProgramFiles in case this script is running as x86
109120
$UnixToolsBinDir = "$env:SystemDrive\Program Files\Git\usr\bin"
110121

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"
112123
if (-not (Test-Path $python)) {
113124
$python = (where.exe python) | Select-Object -First 1
114125
if (-not (Test-Path $python)) {
@@ -192,6 +203,8 @@ function Get-InstallDir($Arch) {
192203
return "$ImageRoot\$ProgramFilesName\Swift"
193204
}
194205

206+
$NugetRoot = "$BinaryCache\nuget"
207+
195208
$LibraryRoot = "$ImageRoot\Library"
196209
$ToolchainInstallRoot = "$(Get-InstallDir $HostArch)\Toolchains\$ProductVersion+Asserts"
197210
$PlatformInstallRoot = "$(Get-InstallDir $HostArch)\Platforms\Windows.platform"
@@ -203,20 +216,33 @@ $SDKInstallRoot = "$PlatformInstallRoot\Developer\SDKs\Windows.sdk"
203216
$HostArch.ToolchainInstallRoot = $ToolchainInstallRoot
204217

205218
# Resolve the architectures received as argument
206-
$SDKArchs = $SDKs | ForEach-Object {
219+
$SDKArchs = @($SDKs | ForEach-Object {
207220
switch ($_) {
208221
"X64" { $ArchX64 }
209222
"X86" { $ArchX86 }
210223
"Arm64" { $ArchArm64 }
211224
default { throw "Unknown architecture $_" }
212225
}
213-
}
226+
})
214227

215228
# Build functions
216229
function Get-ProjectBinaryCache($Arch, $ID) {
217230
return "$BinaryCache\" + ($Arch.BuildID + $ID)
218231
}
219232

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+
220246
function Invoke-Program() {
221247
[CmdletBinding(PositionalBinding = $false)]
222248
param(
@@ -305,12 +331,73 @@ function Isolate-EnvVars([scriptblock]$Block) {
305331
}
306332

307333
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+
308341
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"
310343
} else {
311344
# This dll path is valid for VS2019 and VS2022, but it was under a vsdevcmd subfolder in VS2017
312345
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"
314401
}
315402
}
316403

@@ -978,19 +1065,6 @@ function Build-XCTest($Arch, [switch]$Test = $false) {
9781065
}
9791066
}
9801067

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-
9941068
# Copies files installed by CMake from the arch-specific platform root,
9951069
# where they follow the layout expected by the installer,
9961070
# to the final platform root, following the installer layout.
@@ -1414,6 +1488,9 @@ function Build-Installer() {
14141488
SWIFT_FORMAT_BUILD = "$($HostArch.BinaryCache)\swift-format\release";
14151489
INCLUDE_SWIFT_INSPECT = "true";
14161490
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";
14171494
}
14181495

14191496
foreach ($Arch in $SDKArchs) {
@@ -1432,6 +1509,10 @@ function Build-Installer() {
14321509

14331510
#-------------------------------------------------------------------
14341511

1512+
if (-not $SkipBuild) {
1513+
Ensure-WindowsSDK
1514+
}
1515+
14351516
if (-not $SkipBuild) {
14361517
Build-BuildTools $HostArch
14371518
Build-Compilers $HostArch

0 commit comments

Comments
 (0)