diff --git a/utils/build.ps1 b/utils/build.ps1 index 9fadd35dec006..f345d4bb7574f 100644 --- a/utils/build.ps1 +++ b/utils/build.ps1 @@ -142,7 +142,7 @@ param( [switch] $Clean, [switch] $DebugInfo, [switch] $EnableCaching, - [string] $Cache = "", + [string] $Cache = "$BinaryCache\sccache", [string] $Allocator = "", [switch] $Summary, [switch] $ToBatch @@ -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() { @@ -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) { + 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) @@ -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 = "", @@ -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 } @@ -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") { @@ -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" } @@ -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: " } @@ -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: " } @@ -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", "") @@ -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 { @@ -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") @@ -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) { @@ -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"; } @@ -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"; @@ -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"; @@ -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"; @@ -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) ` @@ -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 { @() }; }) } @@ -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 @@ -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"