Skip to content

Commit b719297

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 b719297

File tree

1 file changed

+56
-19
lines changed

1 file changed

+56
-19
lines changed

utils/build.ps1

Lines changed: 56 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,45 @@ 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+
"-Xlinker", "-I$(Get-HostProjectBinaryCache Compilers)\lib\swift\host",
2377+
# swift-argument-parser
2378+
"-Xswiftc", "-I$(Get-HostProjectBinaryCache ArgumentParser)\swift",
2379+
"-Xlinker", "-L$(Get-HostProjectBinaryCache ArgumentParser)\lib",
2380+
# swift-cmark
2381+
"-Xswiftc", "-I$($Arch.ToolchainInstallRoot)\usr\include\cmark_gfm",
2382+
"-Xswiftc", "-I$($Arch.ToolchainInstallRoot)\usr\include\cmark_gfm_extensions",
2383+
"-Xlinker", "$($Arch.ToolchainInstallRoot)\usr\lib\cmark-gfm.lib",
2384+
"-Xlinker", "$($Arch.ToolchainInstallRoot)\usr\lib\cmark-gfm-extensions.lib",
2385+
# swift-markdown
2386+
"-Xlinker", "$(Get-HostProjectBinaryCache Markdown)\lib\CAtomic.lib",
2387+
"-Xswiftc", "-I$($SourceCache)\swift-markdown\Sources\CAtomic\include",
2388+
"-Xswiftc", "-I$(Get-HostProjectBinaryCache Markdown)\swift",
2389+
"-Xlinker", "-L$(Get-HostProjectBinaryCache Markdown)\lib"
2390+
)
2391+
2392+
$Options = @{
2393+
Src = "$SourceCache\swift-format"
2394+
Bin = (Get-HostProjectBinaryCache Format)
2395+
Arch = $Arch
2396+
Test = $Test
2397+
InstallExe = If ($Test) { "" } Else { "swift-format.exe" }
2398+
}
2399+
2400+
Isolate-EnvVars {
2401+
$env:SWIFTFORMAT_OMIT_EXTERNAL_DEPENDENCIES=1
2402+
Build-SPMProject @Options @SwiftPMArguments
2403+
}
23682404
}
23692405

23702406
function Build-IndexStoreDB($Arch) {
@@ -2687,7 +2723,7 @@ if (-not $SkipBuild) {
26872723
Invoke-BuildStep Build-Certificates $HostArch
26882724
Invoke-BuildStep Build-PackageManager $HostArch
26892725
Invoke-BuildStep Build-Markdown $HostArch
2690-
Invoke-BuildStep Build-Format $HostArch
2726+
Invoke-BuildStep Build-Format -Arch $HostArch
26912727
Invoke-BuildStep Build-IndexStoreDB $HostArch
26922728
Invoke-BuildStep Build-SourceKitLSP $HostArch
26932729
}
@@ -2737,6 +2773,7 @@ if (-not $IsCrossCompiling) {
27372773
}
27382774
if ($Test -contains "llbuild") { Build-LLBuild $HostArch -Test }
27392775
if ($Test -contains "swiftpm") { Test-PackageManager $HostArch }
2776+
if ($Test -contains "swift-format") { Build-Format -Arch $HostArch -Test }
27402777
}
27412778

27422779
# Custom exception printing for more detailed exception information

0 commit comments

Comments
 (0)