Skip to content

Commit 5ad83bd

Browse files
committed
[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.
1 parent 106e05f commit 5ad83bd

File tree

1 file changed

+55
-19
lines changed

1 file changed

+55
-19
lines changed

utils/build.ps1

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ if ($AndroidSDKs.Length -gt 0) {
218218

219219
if ($Test -contains "*") {
220220
# Explicitly don't include llbuild yet since tests are known to fail on Windows
221-
$Test = @("swift", "dispatch", "foundation", "xctest")
221+
$Test = @("swift", "dispatch", "foundation", "xctest", "swift-format")
222222
}
223223

224224
# Architecture definitions
@@ -1216,6 +1216,8 @@ function Build-SPMProject {
12161216
[string] $Src,
12171217
[string] $Bin,
12181218
[hashtable] $Arch,
1219+
[hashtable] $AdditionalEnv = @{},
1220+
[string] $InstallExe = "",
12191221
[switch] $Test = $false,
12201222
[Parameter(ValueFromRemainingArguments)]
12211223
[string[]] $AdditionalArguments
@@ -1237,6 +1239,10 @@ function Build-SPMProject {
12371239
$env:Path = "$RuntimeInstallRoot\usr\bin;$($HostArch.ToolchainInstallRoot)\usr\bin;${env:Path}"
12381240
$env:SDKROOT = $SDKInstallRoot
12391241

1242+
foreach ($Var in $AdditionalEnv.GetEnumerator()) {
1243+
New-Item -Path "env:\$($Var.Key)" -Value $Var.Value -ErrorAction Ignore | Out-Null
1244+
}
1245+
12401246
$Arguments = @(
12411247
"--scratch-path", $Bin,
12421248
"--package-path", $Src,
@@ -1258,6 +1264,14 @@ function Build-SPMProject {
12581264

12591265
$Action = if ($Test) { "test" } else { "build" }
12601266
Invoke-Program "$($HostArch.ToolchainInstallRoot)\usr\bin\swift.exe" $Action @Arguments @AdditionalArguments
1267+
1268+
if ($InstallExe -ne "") {
1269+
$BinPath = (Invoke-Program "$($HostArch.ToolchainInstallRoot)\usr\bin\swift.exe" build @Arguments --show-bin-path)
1270+
$ExecPath = Join-Path -Path $BinPath -ChildPath $InstallExe
1271+
$InstallDestination = "$($Arch.ToolchainInstallRoot)\usr\bin"
1272+
Write-Host "Installing $($ExecPath) to $($InstallDestination)"
1273+
Copy-Item -Path $ExecPath -Destination $InstallDestination
1274+
}
12611275
}
12621276

12631277
if (-not $ToBatch) {
@@ -2348,23 +2362,44 @@ function Build-Markdown($Arch) {
23482362
}
23492363
}
23502364

2351-
function Build-Format($Arch) {
2352-
Build-CMakeProject `
2353-
-Src $SourceCache\swift-format `
2354-
-Bin (Get-HostProjectBinaryCache Format) `
2355-
-InstallTo "$($Arch.ToolchainInstallRoot)\usr" `
2356-
-Arch $Arch `
2357-
-Platform Windows `
2358-
-UseMSVCCompilers C `
2359-
-UseBuiltCompilers Swift `
2360-
-SwiftSDK (Get-HostSwiftSDK) `
2361-
-Defines @{
2362-
BUILD_SHARED_LIBS = "YES";
2363-
ArgumentParser_DIR = (Get-HostProjectCMakeModules ArgumentParser);
2364-
SwiftSyntax_DIR = (Get-HostProjectCMakeModules Compilers);
2365-
SwiftMarkdown_DIR = (Get-HostProjectCMakeModules Markdown);
2366-
"cmark-gfm_DIR" = "$($Arch.ToolchainInstallRoot)\usr\lib\cmake";
2367-
}
2365+
function Build-Format() {
2366+
[CmdletBinding(PositionalBinding = $false)]
2367+
param
2368+
(
2369+
[hashtable]$Arch,
2370+
[switch] $Test
2371+
)
2372+
2373+
$SwiftPMArguments = @(
2374+
# swift-syntax
2375+
"-Xswiftc", "-I$(Get-HostProjectBinaryCache Compilers)\lib\swift\host",
2376+
# swift-argument-parser
2377+
"-Xswiftc", "-I$(Get-HostProjectBinaryCache ArgumentParser)\swift",
2378+
"-Xlinker", "-L$(Get-HostProjectBinaryCache ArgumentParser)\lib",
2379+
# swift-cmark
2380+
"-Xswiftc", "-I$($Arch.ToolchainInstallRoot)\usr\include\cmark_gfm",
2381+
"-Xswiftc", "-I$($Arch.ToolchainInstallRoot)\usr\include\cmark_gfm_extensions",
2382+
"-Xlinker", "$($Arch.ToolchainInstallRoot)\usr\lib\cmark-gfm.lib",
2383+
"-Xlinker", "$($Arch.ToolchainInstallRoot)\usr\lib\cmark-gfm-extensions.lib",
2384+
# swift-markdown
2385+
"-Xlinker", "$(Get-HostProjectBinaryCache Markdown)\lib\CAtomic.lib",
2386+
"-Xswiftc", "-I$($SourceCache)\swift-markdown\Sources\CAtomic\include",
2387+
"-Xswiftc", "-I$(Get-HostProjectBinaryCache Markdown)\swift",
2388+
"-Xlinker", "-L$(Get-HostProjectBinaryCache Markdown)\lib"
2389+
)
2390+
2391+
$Options = @{
2392+
Src = "$SourceCache\swift-format"
2393+
Bin = (Get-HostProjectBinaryCache Format)
2394+
Arch = $Arch
2395+
Test = $Test
2396+
InstallExe = If ($Test) { "" } Else { "swift-format.exe" }
2397+
}
2398+
2399+
Isolate-EnvVars {
2400+
$env:SWIFTFORMAT_OMIT_EXTERNAL_DEPENDENCIES=1
2401+
Build-SPMProject @Options @SwiftPMArguments
2402+
}
23682403
}
23692404

23702405
function Build-IndexStoreDB($Arch) {
@@ -2687,7 +2722,7 @@ if (-not $SkipBuild) {
26872722
Invoke-BuildStep Build-Certificates $HostArch
26882723
Invoke-BuildStep Build-PackageManager $HostArch
26892724
Invoke-BuildStep Build-Markdown $HostArch
2690-
Invoke-BuildStep Build-Format $HostArch
2725+
Invoke-BuildStep Build-Format -Arch $HostArch
26912726
Invoke-BuildStep Build-IndexStoreDB $HostArch
26922727
Invoke-BuildStep Build-SourceKitLSP $HostArch
26932728
}
@@ -2737,6 +2772,7 @@ if (-not $IsCrossCompiling) {
27372772
}
27382773
if ($Test -contains "llbuild") { Build-LLBuild $HostArch -Test }
27392774
if ($Test -contains "swiftpm") { Test-PackageManager $HostArch }
2775+
if ($Test -contains "swift-format") { Build-Format -Arch $HostArch -Test }
27402776
}
27412777

27422778
# Custom exception printing for more detailed exception information

0 commit comments

Comments
 (0)