Skip to content

Commit 276261c

Browse files
authored
Merge pull request #81644 from mhegazy/no-asserts-toolchain
[build.ps1] Revamp No-asserts toolchain build and packaging
1 parent 2d735e8 commit 276261c

File tree

1 file changed

+91
-40
lines changed

1 file changed

+91
-40
lines changed

utils/build.ps1

Lines changed: 91 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ The toolchain snapshot to build the early components with.
5454
.PARAMETER PinnedSHA256
5555
The SHA256 for the pinned toolchain.
5656
57-
.PARAMETER PinnedToolchainVariant
58-
The toolchain variant to use while building the toolchain. Defaults to
59-
`Asserts`.
60-
6157
.PARAMETER AndroidNDKVersion
6258
The version number of the Android NDK to be used.
6359
@@ -100,8 +96,8 @@ in batch file format instead of executing them.
10096
.PARAMETER HostArchName
10197
The architecture where the toolchain will execute.
10298
103-
.PARAMETER Variant
104-
The toolchain variant to build. Defaults to `Asserts`.
99+
.PARAMETER IncludeNoAsserts
100+
If set, the no-assert toolchain variant is also build and included in the output.
105101
106102
.PARAMETER FoundationTestConfiguration
107103
Whether to run swift-foundation and swift-corelibs-foundation tests in a debug or release configuration.
@@ -132,8 +128,6 @@ param
132128
[ValidatePattern("^([A-Fa-f0-9]{64}|)$")]
133129
[string] $PinnedSHA256 = "",
134130
[string] $PinnedVersion = "",
135-
[ValidateSet("Asserts", "NoAsserts")]
136-
[string] $PinnedToolchainVariant = "Asserts",
137131
[ValidatePattern('^\d+(\.\d+)*$')]
138132
[string] $PythonVersion = "3.9.10",
139133
[ValidatePattern("^r(?:[1-9]|[1-9][0-9])(?:[a-z])?$")]
@@ -148,8 +142,7 @@ param
148142
[string] $Stage = "",
149143
[ValidateSet("AMD64", "ARM64")]
150144
[string] $HostArchName = $(if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE }),
151-
[ValidateSet("Asserts", "NoAsserts")]
152-
[string] $Variant = "Asserts",
145+
[switch] $IncludeNoAsserts = $false,
153146
[switch] $Clean,
154147
[switch] $DebugInfo,
155148
[ValidatePattern('^\d+(\.\d+)*$')]
@@ -623,8 +616,10 @@ function Get-InstallDir([Hashtable] $Platform) {
623616

624617
# For dev productivity, install the host toolchain directly using CMake.
625618
# This allows iterating on the toolchain using ninja builds.
626-
$HostPlatform.ToolchainInstallRoot = "$(Get-InstallDir $HostPlatform)\Toolchains\$ProductVersion+$Variant"
627-
$BuildPlatform.ToolchainInstallRoot = "$(Get-InstallDir $BuildPlatform)\Toolchains\$ProductVersion+$Variant"
619+
$HostPlatform.ToolchainInstallRoot = "$(Get-InstallDir $HostPlatform)\Toolchains\$ProductVersion+Asserts"
620+
$HostPlatform.NoAssertsToolchainInstallRoot = "$(Get-InstallDir $HostPlatform)\Toolchains\$ProductVersion+NoAsserts"
621+
$BuildPlatform.ToolchainInstallRoot = "$(Get-InstallDir $BuildPlatform)\Toolchains\$ProductVersion+Asserts"
622+
$BuildPlatform.NoAssertsToolchainInstallRoot = "$(Get-InstallDir $BuildPlatform)\Toolchains\$ProductVersion+NoAsserts"
628623

629624
# Build functions
630625
function Invoke-BuildStep {
@@ -636,21 +631,36 @@ function Invoke-BuildStep {
636631
[Parameter(Position=1, Mandatory)]
637632
[Hashtable] $Platform,
638633
[Parameter(ValueFromRemainingArguments)]
639-
$RemainingArgs
634+
[Object[]] $RemainingArgs
640635
)
641636

642637
if ($Summary) {
643638
$Stopwatch = [Diagnostics.Stopwatch]::StartNew()
644639
}
645640

646641
$SplatArgs = @{}
647-
foreach ($Arg in $RemainingArgs) {
648-
if ($Arg -is [Hashtable]) {
649-
$SplatArgs += $Arg
650-
} elseif ($Arg -is [string]) {
651-
$SplatArgs[$Arg.TrimStart('-')] = $true
652-
} else {
653-
throw "$Arg is unknown type: $($Arg.GetType())"
642+
if ($RemainingArgs) {
643+
$Enumerator = $RemainingArgs.GetEnumerator()
644+
while ($Enumerator.MoveNext()) {
645+
$Arg = $Enumerator.Current
646+
if ($Arg -is [Hashtable]) {
647+
$SplatArgs += $Arg
648+
} elseif ($Arg -is [string] -and $Arg.StartsWith('-')) {
649+
$ParamName = $Arg.TrimStart('-')
650+
$HasNextArg = $RemainingArgs.IndexOf($Arg) -lt ($RemainingArgs.Count - 1)
651+
if ($HasNextArg) {
652+
$NextArg = $RemainingArgs[$RemainingArgs.IndexOf($Arg) + 1]
653+
if ($NextArg -is [string] -and !$NextArg.StartsWith('-')) {
654+
$SplatArgs[$ParamName] = $NextArg
655+
$Enumerator.MoveNext() # Skip NextArg
656+
continue
657+
}
658+
}
659+
# Must be a flag.
660+
$SplatArgs[$ParamName] = $true
661+
} else {
662+
throw "Positional parameter '$Arg' found. The Invoke-BuildStep function only supports named parameters after the required Name and Platform parameters."
663+
}
654664
}
655665
}
656666

@@ -1179,15 +1189,15 @@ function Get-PinnedToolchainToolsDir() {
11791189
}
11801190
}
11811191

1182-
$VariantToolchainPath = [IO.Path]::Combine($ToolchainsRoot, "$(Get-PinnedToolchainVersion)+$PinnedToolchainVariant", "usr", "bin")
1192+
$VariantToolchainPath = [IO.Path]::Combine($ToolchainsRoot, "$(Get-PinnedToolchainVersion)+Asserts", "usr", "bin")
11831193

11841194
if (Test-Path $VariantToolchainPath) {
11851195
return $VariantToolchainPath
11861196
}
11871197

11881198
return [IO.Path]::Combine("$BinaryCache\", "toolchains", $PinnedToolchain,
11891199
"Library", "Developer", "Toolchains",
1190-
"unknown-$PinnedToolchainVariant-development.xctoolchain", "usr", "bin")
1200+
"unknown-Asserts-development.xctoolchain", "usr", "bin")
11911201
}
11921202

11931203
function Get-PinnedToolchainSDK() {
@@ -1673,7 +1683,7 @@ function Build-WiXProject() {
16731683

16741684
$MSBuildArgs = @( "-noLogo", "-maxCpuCount", "-restore", "$SourceCache\swift-installer-scripts\platforms\Windows\$FileName" )
16751685
foreach ($Property in $Properties.GetEnumerator()) {
1676-
if ($Property.Value.Contains(" ")) {
1686+
if ($Property.Value -is [string] -and $Property.Value.Contains(" ")) {
16771687
$MSBuildArgs += "-p:$($Property.Key)=$($Property.Value.Replace('\', '\\'))"
16781688
} else {
16791689
$MSBuildArgs += "-p:$($Property.Key)=$($Property.Value)"
@@ -1694,7 +1704,7 @@ function Build-CMark([Hashtable] $Platform) {
16941704
Build-CMakeProject `
16951705
-Src $SourceCache\cmark `
16961706
-Bin (Get-CMarkBinaryCache $Platform) `
1697-
-InstallTo "$(Get-InstallDir $Platform)\Toolchains\$ProductVersion+$Variant\usr" `
1707+
-InstallTo "$(Get-InstallDir $Platform)\Toolchains\$ProductVersion+Asserts\usr" `
16981708
-Platform $Platform `
16991709
-Defines @{
17001710
BUILD_SHARED_LIBS = "YES";
@@ -1737,7 +1747,7 @@ function Build-BuildTools([Hashtable] $Platform) {
17371747
SWIFT_INCLUDE_APINOTES = "NO";
17381748
SWIFT_INCLUDE_DOCS = "NO";
17391749
SWIFT_INCLUDE_TESTS = "NO";
1740-
"cmark-gfm_DIR" = "$(Get-InstallDir $Platform)\Toolchains\$ProductVersion+$Variant\usr\lib\cmake";
1750+
"cmark-gfm_DIR" = "$(Get-InstallDir $Platform)\Toolchains\$ProductVersion+Asserts\usr\lib\cmake";
17411751
}
17421752
}
17431753

@@ -1773,7 +1783,7 @@ function Load-LitTestOverrides($Filename) {
17731783
}
17741784
}
17751785

1776-
function Get-CompilersDefines([Hashtable] $Platform, [switch] $Test) {
1786+
function Get-CompilersDefines([Hashtable] $Platform, [string] $Variant, [switch] $Test) {
17771787
$BuildTools = [IO.Path]::Combine((Get-ProjectBinaryCache $BuildPlatform BuildTools), "bin")
17781788
$PythonRoot = [IO.Path]::Combine((Get-PythonPath $Platform), "tools")
17791789
$PythonLibName = "python{0}{1}" -f ([System.Version]$PythonVersion).Major, ([System.Version]$PythonVersion).Minor
@@ -1849,7 +1859,7 @@ function Get-CompilersDefines([Hashtable] $Platform, [switch] $Test) {
18491859
}
18501860
}
18511861

1852-
function Build-Compilers([Hashtable] $Platform) {
1862+
function Build-Compilers([Hashtable] $Platform, [string] $Variant) {
18531863
New-Item -ItemType SymbolicLink -Path "$BinaryCache\$($HostPlatform.Triple)\compilers" -Target "$BinaryCache\5" -ErrorAction Ignore
18541864
Build-CMakeProject `
18551865
-Src $SourceCache\llvm-project\llvm `
@@ -1860,7 +1870,7 @@ function Build-Compilers([Hashtable] $Platform) {
18601870
-UsePinnedCompilers Swift `
18611871
-BuildTargets @("install-distribution") `
18621872
-CacheScript $SourceCache\swift\cmake\caches\Windows-$($Platform.Architecture.LLVMName).cmake `
1863-
-Defines (Get-CompilersDefines $Platform)
1873+
-Defines (Get-CompilersDefines $Platform $Variant)
18641874

18651875
$Settings = @{
18661876
FallbackLibrarySearchPaths = @("usr/bin")
@@ -1870,10 +1880,10 @@ function Build-Compilers([Hashtable] $Platform) {
18701880
Write-PList -Settings $Settings -Path "$($Platform.ToolchainInstallRoot)\ToolchainInfo.plist"
18711881
}
18721882

1873-
function Test-Compilers([Hashtable] $Platform, [switch] $TestClang, [switch] $TestLLD, [switch] $TestLLDB, [switch] $TestLLVM, [switch] $TestSwift) {
1883+
function Test-Compilers([Hashtable] $Platform, [string] $Variant, [switch] $TestClang, [switch] $TestLLD, [switch] $TestLLDB, [switch] $TestLLVM, [switch] $TestSwift) {
18741884
Invoke-IsolatingEnvVars {
18751885
$env:Path = "$(Get-CMarkBinaryCache $Platform)\src;$(Get-ProjectBinaryCache $BuildPlatform Compilers)\tools\swift\libdispatch-windows-$($Platform.Architecture.LLVMName)-prefix\bin;$(Get-ProjectBinaryCache $BuildPlatform Compilers)\bin;$env:Path;$VSInstallRoot\DIA SDK\bin\$($HostPlatform.Architecture.VSName);$UnixToolsBinDir"
1876-
$TestingDefines = Get-CompilersDefines $Platform -Test
1886+
$TestingDefines = Get-CompilersDefines $Platform $Variant -Test
18771887
if ($TestLLVM) { $Targets += @("check-llvm") }
18781888
if ($TestClang) { $Targets += @("check-clang") }
18791889
if ($TestLLD) { $Targets += @("check-lld") }
@@ -1985,19 +1995,24 @@ function Build-mimalloc() {
19851995
"ld.lld.exe",
19861996
"ld64.lld.exe"
19871997
)
1988-
foreach ($Tool in $Tools) {
1989-
$Binary = [IO.Path]::Combine($Platform.ToolchainInstallRoot, "usr", "bin", $Tool)
1998+
$Binaries = $Tools | ForEach-Object {[IO.Path]::Combine($Platform.ToolchainInstallRoot, "usr", "bin", $_)}
1999+
if ($IncludeNoAsserts) {
2000+
$NoAssertBinaries = $Tools | ForEach-Object {[IO.Path]::Combine($Platform.NoAssertsToolchainInstallRoot, "usr", "bin", $_)}
2001+
$Binaries = $Binaries + $NoAssertBinaries
2002+
}
2003+
foreach ($Binary in $Binaries) {
2004+
$Name = [IO.Path]::GetFileName($Binary)
19902005
# Binary-patch in place
19912006
Invoke-Program "$SourceCache\mimalloc\bin\minject$BuildSuffix" "-f" "-i" "$Binary"
19922007
# Log the import table
1993-
$LogFile = "$BinaryCache\$($Platform.Triple)\mimalloc\minject-log-$Tool.txt"
1994-
$ErrorFile = "$BinaryCache\$($Platform.Triple)\mimalloc\minject-log-$Tool-error.txt"
2008+
$LogFile = "$BinaryCache\$($Platform.Triple)\mimalloc\minject-log-$Name.txt"
2009+
$ErrorFile = "$BinaryCache\$($Platform.Triple)\mimalloc\minject-log-$Name-error.txt"
19952010
Invoke-Program "$SourceCache\mimalloc\bin\minject$BuildSuffix" "-l" "$Binary" -OutFile $LogFile -ErrorFile $ErrorFile
19962011
# Verify patching
19972012
$Found = Select-String -Path $LogFile -Pattern "mimalloc"
19982013
if (-not $Found) {
19992014
Get-Content $ErrorFile
2000-
throw "Failed to patch mimalloc for $Tool"
2015+
throw "Failed to patch mimalloc for $Name"
20012016
}
20022017
}
20032018
}
@@ -3243,8 +3258,8 @@ function Test-PackageManager() {
32433258
-Src $SrcDir `
32443259
-Bin "$BinaryCache\$($HostPlatform.Triple)\PackageManagerTests" `
32453260
-Platform $HostPlatform `
3246-
-Xcc "-I$(Get-InstallDir $Platform)\Toolchains\$ProductVersion+$Variant\usr\include" `
3247-
-Xlinker "-L$(Get-InstallDir $Platform)\Toolchains\$ProductVersion+$Variant\usr\lib"
3261+
-Xcc "-I$(Get-InstallDir $Platform)\Toolchains\$ProductVersion+Asserts\usr\include" `
3262+
-Xlinker "-L$(Get-InstallDir $Platform)\Toolchains\$ProductVersion+Asserts\usr\lib"
32483263
}
32493264

32503265
function Build-Installer([Hashtable] $Platform) {
@@ -3272,6 +3287,7 @@ function Build-Installer([Hashtable] $Platform) {
32723287
$Properties["Platforms"] = "`"windows$(if ($Android) { ";android" })`"";
32733288
$Properties["AndroidArchitectures"] = "`"$(($AndroidSDKPlatforms | ForEach-Object { $_.Architecture.LLVMName }) -Join ";")`""
32743289
$Properties["WindowsArchitectures"] = "`"$(($WindowsSDKPlatforms | ForEach-Object { $_.Architecture.LLVMName }) -Join ";")`""
3290+
$Properties["ToolchainVariants"] = "`"asserts$(if ($IncludeNoAsserts) { ";noasserts" })`"";
32753291
foreach ($SDKPlatform in $WindowsSDKPlatforms) {
32763292
$Properties["WindowsRuntime$($SDKPlatform.Architecture.ShortName.ToUpperInvariant())"] = [IO.Path]::Combine((Get-InstallDir $SDKPlatform), "Runtimes", "$ProductVersion");
32773293
}
@@ -3295,6 +3311,37 @@ function Copy-BuildArtifactsToStage([Hashtable] $Platform) {
32953311
Invoke-Program "$($WiX.Path)\wix.exe" -- burn detach "$BinaryCache\$($Platform.Triple)\installer\Release\$($Platform.Architecture.VSName)\installer.exe" -engine "$Stage\installer-engine.exe" -intermediateFolder "$BinaryCache\$($Platform.Triple)\installer\$($Platform.Architecture.VSName)\"
32963312
}
32973313

3314+
function Build-NoAssertsToolchain() {
3315+
if ($ToBatch) {
3316+
Write-Output ""
3317+
Write-Output "Building NoAsserts Toolchain ..."
3318+
} else {
3319+
Write-Host -ForegroundColor Cyan "[$([DateTime]::Now.ToString("yyyy-MM-dd HH:mm:ss"))] Building NoAsserts Toolchain ..."
3320+
}
3321+
$Stopwatch = [Diagnostics.Stopwatch]::StartNew()
3322+
3323+
Invoke-BuildStep Build-Compilers $HostPlatform -Variant "NoAsserts"
3324+
3325+
# Only compilers have NoAsserts enabled. Copy the rest of the Toolcahin binaries from the Asserts output
3326+
# Use robocopy for efficient copying
3327+
# /E : Copies subdirectories, including empty ones.
3328+
# /XC: Excludes existing files with the same timestamp but different file sizes.
3329+
# /XN: Excludes existing files that are newer than the copy in the source directory.
3330+
# /XO: Excludes existing files that are older than the copy in the source directory.
3331+
# /NFL: Do not list coppied files in output
3332+
# /NDL: Do not list directories in output
3333+
# /NJH: Do not write a job header
3334+
# /NC: Do not write file classes
3335+
# /NS: Do not write file sizes
3336+
# /NP: Do not show progress indicator
3337+
&robocopy $HostPlatform.ToolchainInstallRoot $HostPlatform.NoAssertsToolchainInstallRoot /E /XC /XN /XO /NS /NC /NFL /NDL /NJH
3338+
3339+
if (-not $ToBatch) {
3340+
Write-Host -ForegroundColor Cyan "[$([DateTime]::Now.ToString("yyyy-MM-dd HH:mm:ss"))] Building Instalting NoAsserts Toolchain in $($Stopwatch.Elapsed)"
3341+
Write-Host ""
3342+
}
3343+
}
3344+
32983345
#-------------------------------------------------------------------
32993346
try {
33003347

@@ -3325,15 +3372,15 @@ if (-not $SkipBuild) {
33253372
Invoke-BuildStep Build-BuildTools $BuildPlatform
33263373
if ($IsCrossCompiling) {
33273374
Invoke-BuildStep Build-XML2 $BuildPlatform
3328-
Invoke-BuildStep Build-Compilers $BuildPlatform
3375+
Invoke-BuildStep Build-Compilers $BuildPlatform -Variant "Asserts"
33293376
}
33303377
if ($IncludeDS2) {
33313378
Invoke-BuildStep Build-RegsGen2 $BuildPlatform
33323379
}
33333380

33343381
Invoke-BuildStep Build-CMark $HostPlatform
33353382
Invoke-BuildStep Build-XML2 $HostPlatform
3336-
Invoke-BuildStep Build-Compilers $HostPlatform
3383+
Invoke-BuildStep Build-Compilers $HostPlatform -Variant "Asserts"
33373384

33383385
Invoke-BuildStep Build-SDK $BuildPlatform -IncludeMacros
33393386

@@ -3404,6 +3451,10 @@ if (-not $SkipBuild) {
34043451

34053452
Install-HostToolchain
34063453

3454+
if ($IncludeNoAsserts) {
3455+
Build-NoAssertsToolchain
3456+
}
3457+
34073458
if (-not $SkipBuild) {
34083459
Invoke-BuildStep Build-mimalloc $HostPlatform
34093460
}
@@ -3430,7 +3481,7 @@ if (-not $IsCrossCompiling) {
34303481
"-TestLLVM" = $Test -contains "llvm";
34313482
"-TestSwift" = $Test -contains "swift";
34323483
}
3433-
Invoke-BuildStep Test-Compilers $HostPlatform $Tests
3484+
Invoke-BuildStep Test-Compilers $HostPlatform -Variant "Asserts" $Tests
34343485
}
34353486

34363487
# FIXME(jeffdav): Invoke-BuildStep needs a platform dictionary, even though the Test-

0 commit comments

Comments
 (0)