From 5ad83bd6c4fbf89ced0fef7ba48ee49e786beb46 Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Mon, 7 Oct 2024 14:22:32 -0700 Subject: [PATCH] [Windows] Build swift-format using SwiftPM from build.ps1 and run tests The primary motivation is to run swift-format tests on Windows using 'swift test'. Since we now have infrastructure for testing swift-format using SwiftPM, we should also use it to build swift-format to be included in the toolchain because (1) it means we don't need to build SwiftFormat twice, (2) we test the same build that is also included in the toolchain and (3) it allows us to remove the CMake build of swift-format. --- utils/build.ps1 | 74 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 19 deletions(-) diff --git a/utils/build.ps1 b/utils/build.ps1 index d68c935bcffae..68308aa85afac 100644 --- a/utils/build.ps1 +++ b/utils/build.ps1 @@ -218,7 +218,7 @@ if ($AndroidSDKs.Length -gt 0) { if ($Test -contains "*") { # Explicitly don't include llbuild yet since tests are known to fail on Windows - $Test = @("swift", "dispatch", "foundation", "xctest") + $Test = @("swift", "dispatch", "foundation", "xctest", "swift-format") } # Architecture definitions @@ -1216,6 +1216,8 @@ function Build-SPMProject { [string] $Src, [string] $Bin, [hashtable] $Arch, + [hashtable] $AdditionalEnv = @{}, + [string] $InstallExe = "", [switch] $Test = $false, [Parameter(ValueFromRemainingArguments)] [string[]] $AdditionalArguments @@ -1237,6 +1239,10 @@ function Build-SPMProject { $env:Path = "$RuntimeInstallRoot\usr\bin;$($HostArch.ToolchainInstallRoot)\usr\bin;${env:Path}" $env:SDKROOT = $SDKInstallRoot + foreach ($Var in $AdditionalEnv.GetEnumerator()) { + New-Item -Path "env:\$($Var.Key)" -Value $Var.Value -ErrorAction Ignore | Out-Null + } + $Arguments = @( "--scratch-path", $Bin, "--package-path", $Src, @@ -1258,6 +1264,14 @@ function Build-SPMProject { $Action = if ($Test) { "test" } else { "build" } Invoke-Program "$($HostArch.ToolchainInstallRoot)\usr\bin\swift.exe" $Action @Arguments @AdditionalArguments + + if ($InstallExe -ne "") { + $BinPath = (Invoke-Program "$($HostArch.ToolchainInstallRoot)\usr\bin\swift.exe" build @Arguments --show-bin-path) + $ExecPath = Join-Path -Path $BinPath -ChildPath $InstallExe + $InstallDestination = "$($Arch.ToolchainInstallRoot)\usr\bin" + Write-Host "Installing $($ExecPath) to $($InstallDestination)" + Copy-Item -Path $ExecPath -Destination $InstallDestination + } } if (-not $ToBatch) { @@ -2348,23 +2362,44 @@ function Build-Markdown($Arch) { } } -function Build-Format($Arch) { - Build-CMakeProject ` - -Src $SourceCache\swift-format ` - -Bin (Get-HostProjectBinaryCache Format) ` - -InstallTo "$($Arch.ToolchainInstallRoot)\usr" ` - -Arch $Arch ` - -Platform Windows ` - -UseMSVCCompilers C ` - -UseBuiltCompilers Swift ` - -SwiftSDK (Get-HostSwiftSDK) ` - -Defines @{ - BUILD_SHARED_LIBS = "YES"; - ArgumentParser_DIR = (Get-HostProjectCMakeModules ArgumentParser); - SwiftSyntax_DIR = (Get-HostProjectCMakeModules Compilers); - SwiftMarkdown_DIR = (Get-HostProjectCMakeModules Markdown); - "cmark-gfm_DIR" = "$($Arch.ToolchainInstallRoot)\usr\lib\cmake"; - } +function Build-Format() { + [CmdletBinding(PositionalBinding = $false)] + param + ( + [hashtable]$Arch, + [switch] $Test + ) + + $SwiftPMArguments = @( + # swift-syntax + "-Xswiftc", "-I$(Get-HostProjectBinaryCache Compilers)\lib\swift\host", + # swift-argument-parser + "-Xswiftc", "-I$(Get-HostProjectBinaryCache ArgumentParser)\swift", + "-Xlinker", "-L$(Get-HostProjectBinaryCache ArgumentParser)\lib", + # swift-cmark + "-Xswiftc", "-I$($Arch.ToolchainInstallRoot)\usr\include\cmark_gfm", + "-Xswiftc", "-I$($Arch.ToolchainInstallRoot)\usr\include\cmark_gfm_extensions", + "-Xlinker", "$($Arch.ToolchainInstallRoot)\usr\lib\cmark-gfm.lib", + "-Xlinker", "$($Arch.ToolchainInstallRoot)\usr\lib\cmark-gfm-extensions.lib", + # swift-markdown + "-Xlinker", "$(Get-HostProjectBinaryCache Markdown)\lib\CAtomic.lib", + "-Xswiftc", "-I$($SourceCache)\swift-markdown\Sources\CAtomic\include", + "-Xswiftc", "-I$(Get-HostProjectBinaryCache Markdown)\swift", + "-Xlinker", "-L$(Get-HostProjectBinaryCache Markdown)\lib" + ) + + $Options = @{ + Src = "$SourceCache\swift-format" + Bin = (Get-HostProjectBinaryCache Format) + Arch = $Arch + Test = $Test + InstallExe = If ($Test) { "" } Else { "swift-format.exe" } + } + + Isolate-EnvVars { + $env:SWIFTFORMAT_OMIT_EXTERNAL_DEPENDENCIES=1 + Build-SPMProject @Options @SwiftPMArguments + } } function Build-IndexStoreDB($Arch) { @@ -2687,7 +2722,7 @@ if (-not $SkipBuild) { Invoke-BuildStep Build-Certificates $HostArch Invoke-BuildStep Build-PackageManager $HostArch Invoke-BuildStep Build-Markdown $HostArch - Invoke-BuildStep Build-Format $HostArch + Invoke-BuildStep Build-Format -Arch $HostArch Invoke-BuildStep Build-IndexStoreDB $HostArch Invoke-BuildStep Build-SourceKitLSP $HostArch } @@ -2737,6 +2772,7 @@ if (-not $IsCrossCompiling) { } if ($Test -contains "llbuild") { Build-LLBuild $HostArch -Test } if ($Test -contains "swiftpm") { Test-PackageManager $HostArch } + if ($Test -contains "swift-format") { Build-Format -Arch $HostArch -Test } } # Custom exception printing for more detailed exception information