@@ -520,15 +520,6 @@ function Get-CMake {
520
520
throw " CMake not found on Path nor in the Visual Studio Installation. Please Install CMake to continue."
521
521
}
522
522
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
-
532
523
function Get-Ninja {
533
524
try {
534
525
return (Get-Command " Ninja.exe" - ErrorAction Stop).Source
@@ -540,6 +531,7 @@ function Get-Ninja {
540
531
throw " Ninja not found on Path nor in the Visual Studio Installation. Please Install Ninja to continue."
541
532
}
542
533
534
+ $cmake = Get-CMake
543
535
$ninja = Get-Ninja
544
536
545
537
$NugetRoot = " $BinaryCache \nuget"
@@ -1489,32 +1481,9 @@ function Build-CMakeProject {
1489
1481
$UseCXX = $UseBuiltCompilers.Contains (" CXX" ) -or $UseMSVCCompilers.Contains (" CXX" ) -or $UsePinnedCompilers.Contains (" CXX" )
1490
1482
$UseSwift = $UseBuiltCompilers.Contains (" Swift" ) -or $UsePinnedCompilers.Contains (" Swift" )
1491
1483
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
-
1510
1484
# Add additional defines (unless already present)
1511
1485
$Defines = $Defines.Clone ()
1512
1486
1513
- # Always enable CMP0181 if available.
1514
- if ($CMakeSupportsCMP0181 ) {
1515
- Add-KeyValueIfNew $Defines CMAKE_POLICY_DEFAULT_CMP0181 NEW
1516
- }
1517
-
1518
1487
Add-KeyValueIfNew $Defines CMAKE_BUILD_TYPE Release
1519
1488
1520
1489
# Avoid specifying `CMAKE_SYSTEM_NAME` and `CMAKE_SYSTEM_PROCESSOR` on
@@ -1685,30 +1654,22 @@ function Build-CMakeProject {
1685
1654
@ (" -gnone" )
1686
1655
}
1687
1656
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" )
1694
1661
1695
1662
Add-FlagsDefine $Defines CMAKE_Swift_FLAGS $SwiftFlags
1696
1663
# Workaround CMake 3.26+ enabling `-wmo` by default on release builds
1697
1664
Add-FlagsDefine $Defines CMAKE_Swift_FLAGS_RELEASE " -O"
1698
1665
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
- }
1709
1666
}
1710
1667
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
+ }
1712
1673
1713
1674
if ($DebugInfo ) {
1714
1675
if ($UseASM -or $UseC -or $UseCXX ) {
@@ -1719,14 +1680,10 @@ function Build-CMakeProject {
1719
1680
Add-KeyValueIfNew $Defines CMAKE_MSVC_DEBUG_INFORMATION_FORMAT Embedded
1720
1681
Add-KeyValueIfNew $Defines CMAKE_POLICY_DEFAULT_CMP0141 NEW
1721
1682
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" )
1730
1687
}
1731
1688
1732
1689
# The linker flags are shared across every language, and `/IGNORE:longsections` is an
@@ -1877,33 +1834,24 @@ function Build-CMakeProject {
1877
1834
$Value = $Define.Value.Replace (" \" , " /" )
1878
1835
} else {
1879
1836
# 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 (" " )) {
1883
1845
# Escape the quote so it makes it through. PowerShell 5 and Core
1884
1846
# handle quotes differently, so we need to check the version.
1885
1847
$quote = if ($PSEdition -eq " Core" ) { ' "' } else { ' \"' }
1886
- " $quote$Arg $quote "
1848
+ $Value += " $quote$ArgWithForwardSlashes $quote "
1887
1849
} else {
1888
- $Arg
1850
+ $Value += $ArgWithForwardSlashes
1889
1851
}
1890
1852
}
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
1906
1853
}
1854
+
1907
1855
$cmakeGenerateArgs += @ (" -D" , " $ ( $Define.Key ) =$Value " )
1908
1856
}
1909
1857
0 commit comments