@@ -520,15 +520,6 @@ function Get-CMake {
520520 throw " CMake not found on Path nor in the Visual Studio Installation. Please Install CMake to continue."
521521}
522522
523- $cmake = Get-CMake
524- $CMakeVersionString = & $cmake -- version | Select-String - Pattern ' cmake version ([\d\.]+)' | ForEach-Object { $_.Matches [0 ].Groups[1 ].Value }
525- $CMakeVersion = [Version ]$CMakeVersionString
526- # Starting with CMake 3.30, CMake propagates linker flags to Swift.
527- $CMakePassesSwiftLinkerFlags = $CMakeVersion -ge [version ]' 3.30'
528- # CMP0181 enables support for the `LINKER:flag1,flag2,...` syntax in
529- # `CMAKE_[EXE|SHARED|MODULE]_LINKER_FLAGS[_<CONFIG>]` variables.
530- $CMakeSupportsCMP0181 = $CMakeVersion -ge [version ]' 4.0'
531-
532523function Get-Ninja {
533524 try {
534525 return (Get-Command " Ninja.exe" - ErrorAction Stop).Source
@@ -540,6 +531,7 @@ function Get-Ninja {
540531 throw " Ninja not found on Path nor in the Visual Studio Installation. Please Install Ninja to continue."
541532}
542533
534+ $cmake = Get-CMake
543535$ninja = Get-Ninja
544536
545537$NugetRoot = " $BinaryCache \nuget"
@@ -1489,32 +1481,9 @@ function Build-CMakeProject {
14891481 $UseCXX = $UseBuiltCompilers.Contains (" CXX" ) -or $UseMSVCCompilers.Contains (" CXX" ) -or $UsePinnedCompilers.Contains (" CXX" )
14901482 $UseSwift = $UseBuiltCompilers.Contains (" Swift" ) -or $UsePinnedCompilers.Contains (" Swift" )
14911483
1492- # We need to manually prefix linker flags with `-Xlinker` if we are using
1493- # the GNU driver or if Swift is used as the linker driver.
1494- # This is not necessary with CMake 4.0+ as CMP0181 simplifies the handling
1495- # of linker arguments.
1496- $PrefixLinkerFlags = if ($Platform.OS -eq [OS ]::Android) {
1497- # We pass the linker location to the driver, not to the linker.
1498- $false
1499- } elseif ($CMakeSupportsCMP0181 ) {
1500- # Not necessary if CMP0181 is supported.
1501- $false
1502- } elseif ($UseGnuDriver ) {
1503- # Always necessary with the GNU driver.
1504- $true
1505- } else {
1506- # Only necessary with Swift projects, when CMake is not passing the linker flags.
1507- $UseSwift -and $CMakePassesSwiftLinkerFlags
1508- }
1509-
15101484 # Add additional defines (unless already present)
15111485 $Defines = $Defines.Clone ()
15121486
1513- # Always enable CMP0181 if available.
1514- if ($CMakeSupportsCMP0181 ) {
1515- Add-KeyValueIfNew $Defines CMAKE_POLICY_DEFAULT_CMP0181 NEW
1516- }
1517-
15181487 Add-KeyValueIfNew $Defines CMAKE_BUILD_TYPE Release
15191488
15201489 # Avoid specifying `CMAKE_SYSTEM_NAME` and `CMAKE_SYSTEM_PROCESSOR` on
@@ -1685,30 +1654,22 @@ function Build-CMakeProject {
16851654 @ (" -gnone" )
16861655 }
16871656
1688- if (-not $CMakePassesSwiftLinkerFlags ) {
1689- # Disable EnC as that introduces padding in the conformance tables
1690- $SwiftFlags += @ (" -Xlinker" , " /INCREMENTAL:NO" )
1691- # Swift requires COMDAT folding and de-duplication
1692- $SwiftFlags += @ (" -Xlinker" , " /OPT:REF" , " -Xlinker" , " /OPT:ICF" )
1693- }
1657+ # Disable EnC as that introduces padding in the conformance tables
1658+ $SwiftFlags += @ (" -Xlinker" , " /INCREMENTAL:NO" )
1659+ # Swift requires COMDAT folding and de-duplication
1660+ $SwiftFlags += @ (" -Xlinker" , " /OPT:REF" , " -Xlinker" , " /OPT:ICF" )
16941661
16951662 Add-FlagsDefine $Defines CMAKE_Swift_FLAGS $SwiftFlags
16961663 # Workaround CMake 3.26+ enabling `-wmo` by default on release builds
16971664 Add-FlagsDefine $Defines CMAKE_Swift_FLAGS_RELEASE " -O"
16981665 Add-FlagsDefine $Defines CMAKE_Swift_FLAGS_RELWITHDEBINFO " -O"
1699-
1700- if ($CMakePassesSwiftLinkerFlags ) {
1701- # CMake 3.30+ passes all linker flags to Swift as the linker driver,
1702- # including those from the internal CMake modules files, without
1703- # a `-Xlinker` prefix. This causes build failures as Swift cannot
1704- # parse linker flags.
1705- # Overwrite the release linker flags to be empty to avoid this.
1706- Add-KeyValueIfNew $Defines CMAKE_EXE_LINKER_FLAGS_RELEASE " "
1707- Add-KeyValueIfNew $Defines CMAKE_SHARED_LINKER_FLAGS_RELEASE " "
1708- }
17091666 }
17101667
1711- $LinkerFlags = @ (" /INCREMENTAL:NO" , " /OPT:REF" , " /OPT:ICF" )
1668+ $LinkerFlags = if ($UseGNUDriver ) {
1669+ @ (" -Xlinker" , " /INCREMENTAL:NO" , " -Xlinker" , " /OPT:REF" , " -Xlinker" , " /OPT:ICF" )
1670+ } else {
1671+ @ (" /INCREMENTAL:NO" , " /OPT:REF" , " /OPT:ICF" )
1672+ }
17121673
17131674 if ($DebugInfo ) {
17141675 if ($UseASM -or $UseC -or $UseCXX ) {
@@ -1719,14 +1680,10 @@ function Build-CMakeProject {
17191680 Add-KeyValueIfNew $Defines CMAKE_MSVC_DEBUG_INFORMATION_FORMAT Embedded
17201681 Add-KeyValueIfNew $Defines CMAKE_POLICY_DEFAULT_CMP0141 NEW
17211682
1722- $LinkerFlags += @ (" /DEBUG" )
1723-
1724- # The linker flags are shared across every language, and `/IGNORE:longsections` is an
1725- # `lld-link.exe` argument, not `link.exe`, so this can only be enabled when we use
1726- # `lld-link.exe` for linking.
1727- # TODO: Investigate supporting fission with PE/COFF, this should avoid this warning.
1728- if ($SwiftDebugFormat -eq " dwarf" -and -not ($UseMSVCCompilers.Contains (" C" ) -or $UseMSVCCompilers.Contains (" CXX" ))) {
1729- $LinkerFlags += @ (" /IGNORE:longsections" )
1683+ $LinkerFlags += if ($UseGNUDriver ) {
1684+ @ (" -Xlinker" , " /DEBUG" )
1685+ } else {
1686+ @ (" /DEBUG" )
17301687 }
17311688
17321689 # The linker flags are shared across every language, and `/IGNORE:longsections` is an
@@ -1877,33 +1834,24 @@ function Build-CMakeProject {
18771834 $Value = $Define.Value.Replace (" \" , " /" )
18781835 } else {
18791836 # Flags array, multiple tokens, quoting needed for tokens containing spaces
1880- $EscapedArgs = $Define.Value | ForEach-Object {
1881- $Arg = $_.Replace (" \" , " /" )
1882- if ($Arg.Contains (" " )) {
1837+ $Value = " "
1838+ foreach ($Arg in $Define.Value ) {
1839+ if ($Value.Length -gt 0 ) {
1840+ $Value += " "
1841+ }
1842+
1843+ $ArgWithForwardSlashes = $Arg.Replace (" \" , " /" )
1844+ if ($ArgWithForwardSlashes.Contains (" " )) {
18831845 # Escape the quote so it makes it through. PowerShell 5 and Core
18841846 # handle quotes differently, so we need to check the version.
18851847 $quote = if ($PSEdition -eq " Core" ) { ' "' } else { ' \"' }
1886- " $quote$Arg $quote "
1848+ $Value += " $quote$ArgWithForwardSlashes $quote "
18871849 } else {
1888- $Arg
1850+ $Value += $ArgWithForwardSlashes
18891851 }
18901852 }
1891-
1892- # Linker flags are handled differently depending on the CMake version.
1893- $IsLinkerFlag = $Define.Key -match " _LINKER_FLAGS" -and ($Platform.OS -ne [OS ]::Android)
1894- $Value = if ($IsLinkerFlag ) {
1895- if ($CMakeSupportsCMP0181 ) { " LINKER:" } elseif ($PrefixLinkerFlags ) { " -Xlinker " } else { " " }
1896- } else {
1897- " "
1898- }
1899- $Separator = if ($IsLinkerFlag ) {
1900- if ($CMakeSupportsCMP0181 ) { " ," } elseif ($PrefixLinkerFlags ) { " -Xlinker " } else { " " }
1901- } else {
1902- " "
1903- }
1904-
1905- $Value += $EscapedArgs -join $Separator
19061853 }
1854+
19071855 $cmakeGenerateArgs += @ (" -D" , " $ ( $Define.Key ) =$Value " )
19081856 }
19091857
0 commit comments