Skip to content

utils: extract Build-SDK helper #80157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 88 additions & 63 deletions utils/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2307,31 +2307,49 @@ function Build-ExperimentalRuntime {
}
}

function Write-SDKSettingsPlist([Hashtable] $Platform) {
function Write-SDKSettingsPlist([OS] $OS) {
$SDKSettings = @{
DefaultProperties = @{
}
}
if ($Platform.OS -eq [OS]::Windows) {
if ($OS -eq [OS]::Windows) {
$SDKSettings.DefaultProperties.DEFAULT_USE_RUNTIME = "MD"
}
Write-PList -Settings $SDKSettings -Path "$(Get-SwiftSDK $Platform.OS)\SDKSettings.plist"
Write-PList -Settings $SDKSettings -Path "$(Get-SwiftSDK $OS)\SDKSettings.plist"

$SDKSettings = @{
CanonicalName = "$($Platform.Triple)"
DisplayName = "$($Platform.OS.ToString())"
CanonicalName = $OS.ToString()
DisplayName = $OS.ToString()
IsBaseSDK = "NO"
Version = "${ProductVersion}"
VersionMap = @{}
DefaultProperties = @{
PLATFORM_NAME = "$($Platform.OS.ToString())"
PLATFORM_NAME = $OS.ToString()
DEFAULT_COMPILER = "${ToolchainIdentifier}"
}
SupportedTargets = @{
$OS.ToString() = @{
PlatformFamilyDisplayName = $OS.ToString()
PlatformFamilyName = $OS.ToString()
}
}
}
if ($Platform.OS -eq [OS]::Windows) {
$SDKSettings.DefaultProperties.DEFAULT_USE_RUNTIME = "MD"
switch ($OS) {
Windows {
$SDKSettings.DefaultProperties.DEFAULT_USE_RUNTIME = "MD"
$SDKSettings.SupportedTargets.Windows.LLVMTargetVendor = "unknown"
$SDKSettings.SupportedTargets.Windows.LLVMTargetSys = "windows"
$SDKSettings.SupportedTargets.Windows.LLVMTargetTripleEnvironment = "msvc"
$SDKSettings.SupportedTargets.Windows.Archs = $WindowsSDKPlatforms | ForEach-Object { $_.Architecture.LLVMName } | Sort-Object
}
Android {
$SDKSettings.SupportedTargets.Android.LLVMTargetVendor = "unknown"
$SDKSettings.SupportedTargets.Android.LLVMTargetSys = "linux"
$SDKSettings.SupportedTargets.Android.LLVMTargetTripleEnvironment = "android${AndroidAPILevel}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, SwiftBuild has android and windows as lowercase under "SupportedTargets".

Environment also should be androideabi for armv7, but we have no way to vary that per architecture right now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I thought it was androideabi as well. However, when we recently updated the install rules to use the output of swiftc -print-target-info, we end up with armv7-unknown-linux-android. I'm happy to change this to androideabi for armv7 though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to change this to androideabi for armv7 though.

You can't at present, because the current schema only allows specifying the triple components per logical platform not per architecture. We'd need to extend the SDKSettings schema (the "authoritative" implementation lives in Swift Build) to allow that.

$SDKSettings.SupportedTargets.Android.Archs = $AndroidSDKPlatforms | ForEach-Object { $_.Architecture.LLVMName } | Sort-Object
}
}
$SDKSettings | ConvertTo-JSON | Out-FIle -FilePath "$(Get-SwiftSDK $Platform.OS)\SDKSettings.json"
$SDKSettings | ConvertTo-JSON -Depth 4 | Out-FIle -FilePath "$(Get-SwiftSDK $OS)\SDKSettings.json"
}

function Build-Dispatch([Hashtable] $Platform) {
Expand Down Expand Up @@ -2518,18 +2536,18 @@ function Test-Testing {
throw "testing Testing is not supported"
}

function Write-PlatformInfoPlist([Hashtable] $Platform) {
function Write-PlatformInfoPlist([OS] $OS) {
$Settings = @{
DefaultProperties = @{
SWIFT_TESTING_VERSION = "$ProductVersion"
XCTEST_VERSION = "$ProductVersion"
}
}
if ($Platform.OS -eq [OS]::Windows) {
if ($OS -eq [OS]::Windows) {
$Settings.DefaultProperties.SWIFTC_FLAGS = @( "-use-ld=lld" )
}

Write-PList -Settings $Settings -Path "$(Get-PlatformRoot $Platform.OS)\Info.plist"
Write-PList -Settings $Settings -Path "$(Get-PlatformRoot $OS)\Info.plist"
}

# Copies files installed by CMake from the arch-specific platform root,
Expand All @@ -2549,12 +2567,44 @@ function Install-Platform([Hashtable[]] $Platforms, [OS] $OS) {
$PlatformResources = "$(Get-SwiftSDK $Platform.OS)\usr\lib\swift\$($Platform.OS.ToString().ToLowerInvariant())"
Get-ChildItem -Recurse "$PlatformResources\$($Platform.Architecture.LLVMName)" | ForEach-Object {
if (".swiftmodule", ".swiftdoc", ".swiftinterface" -contains $_.Extension) {
Write-Host -BackgroundColor DarkRed -ForegroundColor White "$($_.FullName) is not in a thick module layout"
Copy-File $_.FullName "$PlatformResources\$($_.BaseName).swiftmodule\$(Get-ModuleTriple $Platform)$($_.Extension)"
}
}
}
}

function Build-SDK([Hashtable] $Platform, [switch] $IncludeMacros = $false) {
if ($IncludeDS2) {
Invoke-BuildStep Build-DS2 $Platform
}

# Third Party Dependencies
Invoke-BuildStep Build-ZLib $Platform
Invoke-BuildStep Build-XML2 $Platform
Invoke-BuildStep Build-CURL $Platform
Invoke-BuildStep Build-LLVM $Platform

# Libraries
Invoke-BuildStep Build-Runtime $Platform
Invoke-BuildStep Build-Dispatch $Platform
if ($IncludeMacros) {
Invoke-BuildStep Build-FoundationMacros $Platform
Invoke-BuildStep Build-TestingMacros $Platform
}
Invoke-BuildStep Build-Foundation $Platform
Invoke-BuildStep Build-Sanitizers $Platform
Invoke-BuildStep Build-XCTest $Platform
Invoke-BuildStep Build-Testing $Platform
}

function Build-ExperimentalSDK([Hashtable] $Platform) {
# TODO(compnerd) we currently build the experimental SDK with just the static
# variant. We should aim to build both dynamic and static variants.
Invoke-BuildStep Build-ExperimentalRuntime $Platform -Static
Invoke-BuildStep Build-Foundation $Platform -Static
}

function Build-SQLite([Hashtable] $Platform) {
Build-CMakeProject `
-Src $SourceCache\swift-toolchain-sqlite `
Expand Down Expand Up @@ -3184,69 +3234,44 @@ if (-not $SkipBuild) {
Invoke-BuildStep Build-XML2 $HostPlatform
Invoke-BuildStep Build-Compilers $HostPlatform

Invoke-BuildStep Build-SDK $BuildPlatform -IncludeMacros

foreach ($Platform in $WindowsSDKPlatforms) {
Invoke-BuildStep Build-ZLib $Platform
Invoke-BuildStep Build-XML2 $Platform
Invoke-BuildStep Build-CURL $Platform
Invoke-BuildStep Build-LLVM $Platform

# Build platform: SDK, Redist and XCTest
Invoke-BuildStep Build-Runtime $Platform
Invoke-BuildStep Build-Dispatch $Platform
# FIXME(compnerd) ensure that the _build_ is the first arch and don't rebuild on each arch
if ($Platform -eq $BuildPlatform) {
Invoke-BuildStep Build-FoundationMacros $BuildPlatform
Invoke-BuildStep Build-TestingMacros $BuildPlatform
}
Invoke-BuildStep Build-Foundation $Platform
Invoke-BuildStep Build-Sanitizers $Platform
Invoke-BuildStep Build-XCTest $Platform
Invoke-BuildStep Build-Testing $Platform
Invoke-BuildStep Write-SDKSettingsPlist $Platform

Invoke-BuildStep Build-ExperimentalRuntime $Platform -Static
Invoke-BuildStep Build-Foundation $Platform -Static

Copy-File "$(Get-SwiftSDK Windows)\usr\lib\swift\windows\*.lib" "$(Get-SwiftSDK Windows)\usr\lib\swift\windows\$($Platform.Architecture.LLVMName)\"
Invoke-BuildStep Build-SDK $Platform
Invoke-BuildStep Build-ExperimentalSDK $Platform

Get-ChildItem "$(Get-SwiftSDK Windows)\usr\lib\swift\windows" -Filter "*.lib" -File -ErrorAction Ignore | ForEach-Object {
Write-Host -BackgroundColor DarkRed -ForegroundColor White "$($_.FullName) is not nested in an architecture directory"
Move-Item $_.FullName "$(Get-SwiftSDK Windows)\usr\lib\swift\windows\$($Platform.Architecture.LLVMName)\" | Out-Null
}

if ($Platform -eq $HostPlatform) {
Copy-Directory "$(Get-SwiftSDK Windows)\usr\bin" "$([IO.Path]::Combine((Get-InstallDir $HostPlatform), "Runtimes", $ProductVersion))\usr"
Copy-Directory "$(Get-SwiftSDK Windows)\usr\bin" "$([IO.Path]::Combine((Get-InstallDir $Platform), "Runtimes", $ProductVersion))\usr"
}
}
Install-Platform $WindowsSDKPlatforms Windows
Invoke-BuildStep Write-PlatformInfoPlist $HostPlatform
Write-PlatformInfoPlist Windows
Write-SDKSettingsPlist Windows

if ($Android) {
foreach ($Platform in $AndroidSDKPlatforms) {
if ($IncludeDS2) {
Invoke-BuildStep Build-DS2 $Platform
}
Invoke-BuildStep Build-ZLib $Platform
Invoke-BuildStep Build-XML2 $Platform
Invoke-BuildStep Build-CURL $Platform
Invoke-BuildStep Build-LLVM $Platform

# Build platform: SDK, Redist and XCTest
Invoke-BuildStep Build-Runtime $Platform
Invoke-BuildStep Build-Dispatch $Platform
Invoke-BuildStep Build-Foundation $Platform
Invoke-BuildStep Build-Sanitizers $Platform
Invoke-BuildStep Build-XCTest $Platform
Invoke-BuildStep Build-Testing $Platform

# Android swift-inspect only supports 64-bit platforms.
if ($Platform.Architecture.ABI -in @("arm64-v8a", "x86_64")) {
Invoke-BuildStep Build-Inspect $Platform
Invoke-BuildStep Build-SDK $Platform
Invoke-BuildStep Build-ExperimentalSDK $Platform

Get-ChildItem "$(Get-SwiftSDK Android)\usr\lib\swift\android" -File | Where-Object { $_.Name -match ".a$|.so$" } | ForEach-Object {
Write-Host -BackgroundColor DarkRed -ForegroundColor White "$($_.FullName) is not nested in an architecture directory"
Move-Item $_.FullName "$(Get-SwiftSDK Android)\usr\lib\swift\android\$($Platform.Architecture.LLVMName)\" | Out-Null
}
Invoke-BuildStep Write-SDKSettingsPlist $Platform
}

Invoke-BuildStep Build-ExperimentalRuntime $Platform -Static
Invoke-BuildStep Build-Foundation $Platform -Static
Install-Platform $AndroidSDKPlatforms Android
Write-PlatformInfoPlist Android
Write-SDKSettingsPlist Android

Move-Item "$(Get-SwiftSDK Android)\usr\lib\swift\android\*.a" "$(Get-SwiftSDK Android)\usr\lib\swift\android\$($Platform.Architecture.LLVMName)\"
Move-Item "$(Get-SwiftSDK Android)\usr\lib\swift\android\*.so" "$(Get-SwiftSDK Android)\usr\lib\swift\android\$($Platform.Architecture.LLVMName)\"
# Android swift-inspect only supports 64-bit platforms.
$AndroidSDKPlatforms | Where-Object { @("arm64-v8a", "x86_64") -contains $_.Architecture.ABI } | ForEach-Object {
Invoke-BuildStep Build-Inspect $_
}
Install-Platform $AndroidSDKPlatforms Android
Invoke-BuildStep Write-PlatformInfoPlist $Platform
}

# Build Macros for distribution
Expand Down