Skip to content

[windows] build.ps1 polishing #77875

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

Closed
Closed
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
141 changes: 75 additions & 66 deletions utils/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ param(
[switch] $Clean,
[switch] $DebugInfo,
[switch] $EnableCaching,
[string] $Cache = "",
[string] $Cache = "$BinaryCache\sccache",
[string] $Allocator = "",
[switch] $Summary,
[switch] $ToBatch
Expand Down Expand Up @@ -829,11 +829,16 @@ function Fetch-Dependencies {
}
}

function Get-PinnedToolchainTool() {
function Get-PinnedToolchainTool([string] $Name) {
if (Test-Path "$BinaryCache\toolchains\${PinnedToolchain}\LocalApp\Programs\Swift\Toolchains\$(Get-PinnedToolchainVersion)+Asserts\usr\bin") {
return "$BinaryCache\toolchains\${PinnedToolchain}\LocalApp\Programs\Swift\Toolchains\$(Get-PinnedToolchainVersion)+Asserts\usr\bin"
$Path = "$BinaryCache\toolchains\${PinnedToolchain}\LocalApp\Programs\Swift\Toolchains\$(Get-PinnedToolchainVersion)+Asserts\usr\bin"
} else {
$Path = "$BinaryCache\toolchains\${PinnedToolchain}\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin"
}
if ($Name) {
$Path = "$Path\$Name"
}
return "$BinaryCache\toolchains\${PinnedToolchain}\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin"
return $Path
}

function Get-PinnedToolchainSDK() {
Expand All @@ -857,6 +862,31 @@ function Get-PinnedToolchainVersion() {
throw "PinnedVersion must be set"
}

$CompilersBinaryCache = if ($IsCrossCompiling) {
Get-BuildProjectBinaryCache Compilers
} else {
Get-HostProjectBinaryCache Compilers
}

function Get-BuiltToolchainTool([string] $Name) {
return if ($Name) { "$CompilersBinaryCache\bin\$Name" } else { "$CompilersBinaryCache\bin" }
}

function Get-ClangDriverName([Platform] $Platform, [string] $Lang) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we enumerate the languages? Perhaps that is overly complicating the state.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, possible

switch ($Platform) {
Windows {
"clang-cl.exe"
}
Android {
switch ($Lang) {
C { "clang.exe" }
ASM { "clang.exe" }
CXX { "clang++.exe" }
}
}
}
}

function TryAdd-KeyValue([hashtable]$Hashtable, [string]$Key, [string]$Value) {
if (-not $Hashtable.Contains($Key)) {
$Hashtable.Add($Key, $Value)
Expand Down Expand Up @@ -896,7 +926,7 @@ function Build-CMakeProject {
[string] $Src,
[string] $Bin,
[string] $InstallTo = "",
[Platform] $Platform = "Windows",
[Platform] $Platform = [Platform]::Windows,
[hashtable] $Arch,
[string] $Generator = "Ninja",
[string] $CacheScript = "",
Expand All @@ -922,38 +952,28 @@ function Build-CMakeProject {
# Enter the developer command shell early so we can resolve cmake.exe
# for version checks.
Isolate-EnvVars {
if ($Platform -eq "Windows") {
if ($Platform -eq [Platform]::Windows) {
Invoke-VsDevShell $Arch
}

$CompilersBinaryCache = if ($IsCrossCompiling) {
Get-BuildProjectBinaryCache Compilers
} else {
Get-HostProjectBinaryCache Compilers
}
$DriverBinaryCache = Get-HostProjectBinaryCache Driver

if ($EnableCaching) {
$env:SCCACHE_DIRECT = "true"
if ($Cache -eq "") {
$env:SCCACHE_DIR = "$BinaryCache\sccache"
} else {
$env:SCCACHE_DIR = $Cache
}
$env:SCCACHE_DIR = $Cache
}
if ($UseSwiftSwiftDriver) {
$env:SWIFT_DRIVER_SWIFT_FRONTEND_EXEC = ([IO.Path]::Combine($CompilersBinaryCache, "bin", "swift-frontend.exe"))
}

# TODO(compnerd) workaround swiftc.exe symlink not existing.
$DriverToolDir = "$(Get-HostProjectBinaryCache Driver)\bin"
if ($UseSwiftSwiftDriver) {
Copy-Item -Force ([IO.Path]::Combine($DriverBinaryCache, "bin", "swift-driver.exe")) ([IO.Path]::Combine($DriverBinaryCache, "bin", "swiftc.exe"))
Copy-Item -Force "$DriverToolDir\swift-driver.exe" "$DriverToolDir\swiftc.exe"
}

# Add additional defines (unless already present)
$Defines = $Defines.Clone()

if (($Platform -ne "Windows") -or ($Arch.CMakeName -ne $BuildArch.CMakeName)) {
if (($Platform -ne [Platform]::Windows) -or ($Arch.CMakeName -ne $BuildArch.CMakeName)) {
TryAdd-KeyValue $Defines CMAKE_SYSTEM_NAME $Platform
TryAdd-KeyValue $Defines CMAKE_SYSTEM_PROCESSOR $Arch.CMakeName
}
Expand All @@ -964,7 +984,7 @@ function Build-CMakeProject {
# ensure that it can be accessed from the cmake cache file.
$env:NDKPATH = Get-AndroidNDKPath
}
if ($Platform -eq "Android") {
if ($Platform -eq [Platform]::Android) {
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
$VSInstallPath = & $vswhere -nologo -latest -products * -property installationPath
if (Test-Path "${VSInstallPath}\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin") {
Expand Down Expand Up @@ -1001,24 +1021,24 @@ function Build-CMakeProject {
}

$CXXFlags = @()
if ($Platform -eq "Windows") {
if ($Platform -eq [Platform]::Windows) {
$CXXFlags += $CFlags.Clone() + @("/Zc:__cplusplus")
}

if ($UseMSVCCompilers.Contains("C") -Or $UseMSVCCompilers.Contains("CXX") -Or
$UseBuiltCompilers.Contains("C") -Or $UseBuiltCompilers.Contains("CXX") -Or
$UsePinnedCompilers.Contains("C") -Or $UsePinnedCompilers.Contains("CXX")) {
if ($DebugInfo -and $Platform -eq "Windows") {
if ($DebugInfo -and $Platform -eq [Platform]::Windows) {
Append-FlagsDefine $Defines CMAKE_MSVC_DEBUG_INFORMATION_FORMAT Embedded
Append-FlagsDefine $Defines CMAKE_POLICY_CMP0141 NEW
# Add additional linker flags for generating the debug info.
Append-FlagsDefine $Defines CMAKE_SHARED_LINKER_FLAGS "/debug"
Append-FlagsDefine $Defines CMAKE_EXE_LINKER_FLAGS "/debug"
} elseif ($Platform -eq "Android") {
} elseif ($Platform -eq [Platform]::Android) {
# Use a built lld linker as the Android's NDK linker might be too
# old and not support all required relocations needed by the Swift
# runtime.
$ldPath = ([IO.Path]::Combine($CompilersBinaryCache, "bin", "ld.lld"))
$ldPath = (Get-BuiltToolchainTool "ld.lld")
Append-FlagsDefine $Defines CMAKE_SHARED_LINKER_FLAGS "--ld-path=$ldPath"
Append-FlagsDefine $Defines CMAKE_EXE_LINKER_FLAGS "--ld-path=$ldPath"
}
Expand All @@ -1039,27 +1059,27 @@ function Build-CMakeProject {
Append-FlagsDefine $Defines CMAKE_CXX_FLAGS $CXXFlags
}
if ($UsePinnedCompilers.Contains("ASM") -Or $UseBuiltCompilers.Contains("ASM")) {
$Driver = if ($Platform -eq "Windows") { "clang-cl.exe" } else { "clang.exe" }
$Driver = (Get-ClangDriverName $Platform -Lang "ASM")
if ($UseBuiltCompilers.Contains("ASM")) {
TryAdd-KeyValue $Defines CMAKE_ASM_COMPILER ([IO.Path]::Combine($CompilersBinaryCache, "bin", $Driver))
TryAdd-KeyValue $Defines CMAKE_ASM_COMPILER (Get-BuiltToolchainTool $Driver)
} else {
TryAdd-KeyValue $Defines CMAKE_ASM_COMPILER (Join-Path -Path (Get-PinnedToolchainTool) -ChildPath $Driver)
TryAdd-KeyValue $Defines CMAKE_ASM_COMPILER (Get-PinnedToolchainTool $Driver)
}
Append-FlagsDefine $Defines CMAKE_ASM_FLAGS "--target=$($Arch.LLVMTarget)"
if ($Platform -eq "Windows") {
if ($Platform -eq [Platform]::Windows) {
TryAdd-KeyValue $Defines CMAKE_ASM_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDLL "/MD"
}
}
if ($UsePinnedCompilers.Contains("C") -Or $UseBuiltCompilers.Contains("C")) {
$Driver = if ($Platform -eq "Windows") { "clang-cl.exe" } else { "clang.exe" }
$Driver = (Get-ClangDriverName $Platform -Lang "C")
if ($UseBuiltCompilers.Contains("C")) {
TryAdd-KeyValue $Defines CMAKE_C_COMPILER ([IO.Path]::Combine($CompilersBinaryCache, "bin", $Driver))
TryAdd-KeyValue $Defines CMAKE_C_COMPILER (Get-BuiltToolchainTool $Driver)
} else {
TryAdd-KeyValue $Defines CMAKE_C_COMPILER (Join-Path -Path (Get-PinnedToolchainTool) -ChildPath $Driver)
TryAdd-KeyValue $Defines CMAKE_C_COMPILER (Get-PinnedToolchainTool $Driver)
}
TryAdd-KeyValue $Defines CMAKE_C_COMPILER_TARGET $Arch.LLVMTarget

if (-not (Test-CMakeAtLeast -Major 3 -Minor 26 -Patch 3) -and $Platform -eq "Windows") {
if (-not (Test-CMakeAtLeast -Major 3 -Minor 26 -Patch 3) -and $Platform -eq [Platform]::Windows) {
# Workaround for https://github.com/ninja-build/ninja/issues/2280
TryAdd-KeyValue $Defines CMAKE_CL_SHOWINCLUDES_PREFIX "Note: including file: "
}
Expand All @@ -1070,15 +1090,15 @@ function Build-CMakeProject {
Append-FlagsDefine $Defines CMAKE_C_FLAGS $CFlags
}
if ($UsePinnedCompilers.Contains("CXX") -Or $UseBuiltCompilers.Contains("CXX")) {
$Driver = if ($Platform -eq "Windows") { "clang-cl.exe" } else { "clang++.exe" }
$Driver = (Get-ClangDriverName $Platform -Lang "CXX")
if ($UseBuiltCompilers.Contains("CXX")) {
TryAdd-KeyValue $Defines CMAKE_CXX_COMPILER ([IO.Path]::Combine($CompilersBinaryCache, "bin", $Driver))
TryAdd-KeyValue $Defines CMAKE_CXX_COMPILER (Get-BuiltToolchainTool $Driver)
} else {
TryAdd-KeyValue $Defines CMAKE_CXX_COMPILER (Join-Path -Path (Get-PinnedToolchainTool) -ChildPath $Driver)
TryAdd-KeyValue $Defines CMAKE_CXX_COMPILER (Get-PinnedToolchainTool $Driver)
}
TryAdd-KeyValue $Defines CMAKE_CXX_COMPILER_TARGET $Arch.LLVMTarget

if (-not (Test-CMakeAtLeast -Major 3 -Minor 26 -Patch 3) -and $Platform -eq "Windows") {
if (-not (Test-CMakeAtLeast -Major 3 -Minor 26 -Patch 3) -and $Platform -eq [Platform]::Windows) {
# Workaround for https://github.com/ninja-build/ninja/issues/2280
TryAdd-KeyValue $Defines CMAKE_CL_SHOWINCLUDES_PREFIX "Note: including file: "
}
Expand All @@ -1092,13 +1112,13 @@ function Build-CMakeProject {
$SwiftArgs = @()

if ($UseSwiftSwiftDriver) {
TryAdd-KeyValue $Defines CMAKE_Swift_COMPILER ([IO.Path]::Combine($DriverBinaryCache, "bin", "swiftc.exe"))
TryAdd-KeyValue $Defines CMAKE_Swift_COMPILER "$DriverToolDir\swiftc.exe"
} elseif ($UseBuiltCompilers.Contains("Swift")) {
TryAdd-KeyValue $Defines CMAKE_Swift_COMPILER ([IO.Path]::Combine($CompilersBinaryCache, "bin", "swiftc.exe"))
TryAdd-KeyValue $Defines CMAKE_Swift_COMPILER (Get-BuiltToolchainTool "swiftc.exe")
} else {
TryAdd-KeyValue $Defines CMAKE_Swift_COMPILER (Join-Path -Path (Get-PinnedToolchainTool) -ChildPath "swiftc.exe")
TryAdd-KeyValue $Defines CMAKE_Swift_COMPILER (Get-PinnedToolchainTool "swiftc.exe")
}
if (-not ($Platform -eq "Windows")) {
if ($Platform -ne [Platform]::Windows) {
TryAdd-KeyValue $Defines CMAKE_Swift_COMPILER_WORKS = "YES"
}
TryAdd-KeyValue $Defines CMAKE_Swift_COMPILER_TARGET $Arch.LLVMTarget.Replace("$AndroidAPILevel", "")
Expand Down Expand Up @@ -1147,7 +1167,7 @@ function Build-CMakeProject {

# Debug Information
if ($DebugInfo) {
if ($Platform -eq "Windows") {
if ($Platform -eq [Platform]::Windows) {
if ($SwiftDebugFormat -eq "dwarf") {
$SwiftArgs += @("-g", "-Xlinker", "/DEBUG:DWARF", "-use-ld=lld-link")
} else {
Expand All @@ -1160,7 +1180,7 @@ function Build-CMakeProject {
$SwiftArgs += "-gnone"
}

if ($Platform -eq "Windows") {
if ($Platform -eq [Platform]::Windows) {
$SwiftArgs += @("-Xlinker", "/INCREMENTAL:NO")
# Swift requires COMDAT folding and de-duplication
$SwiftArgs += @("-Xlinker", "/OPT:REF")
Expand Down Expand Up @@ -1453,11 +1473,6 @@ function Build-Compilers() {
)

Isolate-EnvVars {
$CompilersBinaryCache = if ($Build) {
Get-BuildProjectBinaryCache Compilers
} else {
Get-HostProjectBinaryCache Compilers
}
$BuildTools = Join-Path -Path (Get-BuildProjectBinaryCache BuildTools) -ChildPath bin

if ($TestClang -or $TestLLD -or $TestLLDB -or $TestLLVM -or $TestSwift) {
Expand Down Expand Up @@ -1691,7 +1706,7 @@ function Build-CURL([Platform]$Platform, $Arch) {
$ArchName = $Arch.LLVMName

$PlatformDefines = @{}
if ($Platform -eq "Android") {
if ($Platform -eq [Platform]::Android) {
$PlatformDefines += @{
HAVE_FSEEKO = "0";
}
Expand Down Expand Up @@ -1766,9 +1781,9 @@ function Build-CURL([Platform]$Platform, $Arch) {
CURL_USE_LIBSSH2 = "NO";
CURL_USE_MBEDTLS = "NO";
CURL_USE_OPENSSL = "NO";
CURL_USE_SCHANNEL = if ($Platform -eq "Windows") { "YES" } else { "NO" };
CURL_USE_SCHANNEL = if ($Platform -eq [Platform]::Windows) { "YES" } else { "NO" };
CURL_USE_WOLFSSL = "NO";
CURL_WINDOWS_SSPI = if ($Platform -eq "Windows") { "YES" } else { "NO" };
CURL_WINDOWS_SSPI = if ($Platform -eq [Platform]::Windows) { "YES" } else { "NO" };
CURL_ZLIB = "YES";
CURL_ZSTD = "NO";
ENABLE_ARES = "NO";
Expand All @@ -1789,8 +1804,8 @@ function Build-CURL([Platform]$Platform, $Arch) {
USE_NGTCP2 = "NO";
USE_QUICHE = "NO";
USE_OPENSSL_QUIC = "NO";
USE_WIN32_IDN = if ($Platform -eq "Windows") { "YES" } else { "NO" };
USE_WIN32_LARGE_FILES = if ($Platform -eq "Windows") { "YES" } else { "NO" };
USE_WIN32_IDN = if ($Platform -eq [Platform]::Windows) { "YES" } else { "NO" };
USE_WIN32_LARGE_FILES = if ($Platform -eq [Platform]::Windows) { "YES" } else { "NO" };
USE_WIN32_LDAP = "NO";
ZLIB_ROOT = "$LibraryRoot\zlib-1.3.1\usr";
ZLIB_LIBRARY = "$LibraryRoot\zlib-1.3.1\usr\lib\$Platform\$ArchName\zlibstatic.lib";
Expand All @@ -1799,7 +1814,7 @@ function Build-CURL([Platform]$Platform, $Arch) {

function Build-Runtime([Platform]$Platform, $Arch) {
$PlatformDefines = @{}
if ($Platform -eq "Android") {
if ($Platform -eq [Platform]::Android) {
$PlatformDefines += @{
LLVM_ENABLE_LIBCXX = "YES";
SWIFT_USE_LINKER = "lld";
Expand All @@ -1812,12 +1827,6 @@ function Build-Runtime([Platform]$Platform, $Arch) {
Isolate-EnvVars {
$env:Path = "$(Get-CMarkBinaryCache $Arch)\src;$(Get-PinnedToolchainRuntime);${env:Path}"

$CompilersBinaryCache = if ($IsCrossCompiling) {
Get-BuildProjectBinaryCache Compilers
} else {
Get-HostProjectBinaryCache Compilers
}

Build-CMakeProject `
-Src $SourceCache\swift `
-Bin (Get-TargetProjectBinaryCache $Arch Runtime) `
Expand All @@ -1843,7 +1852,7 @@ function Build-Runtime([Platform]$Platform, $Arch) {
SWIFT_NATIVE_SWIFT_TOOLS_PATH = (Join-Path -Path $CompilersBinaryCache -ChildPath "bin");
SWIFT_PATH_TO_LIBDISPATCH_SOURCE = "$SourceCache\swift-corelibs-libdispatch";
SWIFT_PATH_TO_STRING_PROCESSING_SOURCE = "$SourceCache\swift-experimental-string-processing";
CMAKE_SHARED_LINKER_FLAGS = if ($Platform -eq "Windows") { @("/INCREMENTAL:NO", "/OPT:REF", "/OPT:ICF") } else { @() };
CMAKE_SHARED_LINKER_FLAGS = if ($Platform -eq [Platform]::Windows) { @("/INCREMENTAL:NO", "/OPT:REF", "/OPT:ICF") } else { @() };
})
}

Expand Down Expand Up @@ -1909,13 +1918,13 @@ function Build-Foundation([Platform]$Platform, $Arch, [switch]$Test = $false) {
$ShortArch = $Arch.LLVMName

Isolate-EnvVars {
$SDKRoot = if ($Platform -eq "Windows") {
$SDKRoot = if ($Platform -eq [Platform]::Windows) {
""
} else {
(Get-Variable "${Platform}$($Arch.ShortName)" -ValueOnly).SDKInstallRoot
}

$SDKRoot = if ($Platform -eq "Windows") {
$SDKRoot = if ($Platform -eq [Platform]::Windows) {
""
} else {
(Get-Variable "${Platform}$($Arch.ShortName)" -ValueOnly).SDKInstallRoot
Expand All @@ -1931,16 +1940,16 @@ function Build-Foundation([Platform]$Platform, $Arch, [switch]$Test = $false) {
-SwiftSDK:$SDKRoot `
-Defines (@{
ENABLE_TESTING = "NO";
FOUNDATION_BUILD_TOOLS = if ($Platform -eq "Windows") { "YES" } else { "NO" };
FOUNDATION_BUILD_TOOLS = if ($Platform -eq [Platform]::Windows) { "YES" } else { "NO" };
CURL_DIR = "$LibraryRoot\curl-8.9.1\usr\lib\$Platform\$ShortArch\cmake\CURL";
LIBXML2_LIBRARY = if ($Platform -eq "Windows") {
LIBXML2_LIBRARY = if ($Platform -eq [Platform]::Windows) {
"$LibraryRoot\libxml2-2.11.5\usr\lib\$Platform\$ShortArch\libxml2s.lib";
} else {
"$LibraryRoot\libxml2-2.11.5\usr\lib\$Platform\$ShortArch\libxml2.a";
};
LIBXML2_INCLUDE_DIR = "$LibraryRoot\libxml2-2.11.5\usr\include\libxml2";
LIBXML2_DEFINITIONS = "-DLIBXML_STATIC";
ZLIB_LIBRARY = if ($Platform -eq "Windows") {
ZLIB_LIBRARY = if ($Platform -eq [Platform]::Windows) {
"$LibraryRoot\zlib-1.3.1\usr\lib\$Platform\$ShortArch\zlibstatic.lib"
} else {
"$LibraryRoot\zlib-1.3.1\usr\lib\$Platform\$ShortArch\libz.a"
Expand Down