@@ -320,9 +320,9 @@ function TryAdd-KeyValue([hashtable]$Hashtable, [string]$Key, [string]$Value) {
320
320
}
321
321
}
322
322
323
- function Append-FlagsDefine ([hashtable ]$Defines , [string ]$Name , [string ]$Value ) {
323
+ function Append-FlagsDefine ([hashtable ]$Defines , [string ]$Name , [string [] ]$Value ) {
324
324
if ($Defines.Contains ($Name )) {
325
- $Defines [$name ] += " $Value "
325
+ $Defines [$name ] = @ ( $Defines [ $name ]) + $Value
326
326
} else {
327
327
$Defines.Add ($Name , $Value )
328
328
}
@@ -354,7 +354,7 @@ function Build-CMakeProject {
354
354
[string []] $UseMSVCCompilers = @ (), # C,CXX
355
355
[string []] $UseBuiltCompilers = @ (), # ASM,C,CXX,Swift
356
356
[string ] $SwiftSDK = " " ,
357
- [hashtable ] $Defines = @ {},
357
+ [hashtable ] $Defines = @ {}, # Values are either single strings or arrays of flags
358
358
[string []] $BuildTargets = @ ()
359
359
)
360
360
@@ -374,14 +374,16 @@ function Build-CMakeProject {
374
374
375
375
# Add additional defines (unless already present)
376
376
$Defines = $Defines.Clone ()
377
+
377
378
TryAdd- KeyValue $Defines CMAKE_BUILD_TYPE $BuildType
378
379
TryAdd- KeyValue $Defines CMAKE_MT " mt"
379
380
380
381
$GenerateDebugInfo = $Defines [" CMAKE_BUILD_TYPE" ] -ne " Release"
381
- $Zi = if ($GenerateDebugInfo ) { " /Zi" } else { " " }
382
382
383
- $CFlags = " /GS- /Gw /Gy /Oi /Oy $Zi /Zc:inline"
384
- $CXXFlags = " /GS- /Gw /Gy /Oi /Oy $Zi /Zc:inline /Zc:__cplusplus"
383
+ $CFlags = @ (" /GS-" , " /Gw" , " /Gy" , " /Oi" , " /Oy" , " /Zc:inline" )
384
+ if ($GenerateDebugInfo ) { $CFlags += " /Zi" }
385
+ $CXXFlags = $CFlags.Clone () + " /Zc:__cplusplus"
386
+
385
387
if ($UseMSVCCompilers.Contains (" C" )) {
386
388
TryAdd- KeyValue $Defines CMAKE_C_COMPILER cl
387
389
Append- FlagsDefine $Defines CMAKE_C_FLAGS $CFlags
@@ -430,34 +432,32 @@ function Build-CMakeProject {
430
432
$RuntimeBinaryCache = Get-ProjectBinaryCache $Arch 1
431
433
$SwiftResourceDir = " ${RuntimeBinaryCache} \lib\swift"
432
434
433
- $SwiftArgs = [ System.Collections.ArrayList ] @ ()
435
+ $SwiftArgs = @ ()
434
436
435
437
if ($SwiftSDK -ne " " ) {
436
- $SwiftArgs.Add (" -sdk $SwiftSDK " ) | Out-Null
438
+ $SwiftArgs += @ (" -sdk" , $SwiftSDK )
437
439
} else {
438
- $SwiftArgs.Add (" -resource-dir $SwiftResourceDir " ) | Out-Null
439
- $SwiftArgs.Add (" -L $SwiftResourceDir \windows" ) | Out-Null
440
- $SwiftArgs.Add (" -vfsoverlay $RuntimeBinaryCache \stdlib\windows-vfs-overlay.yaml" ) | Out-Null
440
+ $SwiftArgs += @ (" -resource-dir" , " $SwiftResourceDir " )
441
+ $SwiftArgs += @ (" -L" , " $SwiftResourceDir \windows" )
442
+ $SwiftArgs += @ (" -vfsoverlay" , " $RuntimeBinaryCache \stdlib\windows-vfs-overlay.yaml" )
441
443
}
442
444
443
445
# Debug Information
444
446
if ($GenerateDebugInfo ) {
445
447
if ($SwiftDebugFormat -eq " dwarf" ) {
446
- $SwiftArgs.Add (" -g -Xlinker /DEBUG:DWARF -use-ld=lld-link" ) | Out-Null
448
+ $SwiftArgs += @ (" -g" , " -Xlinker" , " /DEBUG:DWARF" , " -use-ld=lld-link" )
447
449
} else {
448
- $SwiftArgs.Add (" -g -debug-info-format=codeview -Xlinker -debug" ) | Out-Null
450
+ $SwiftArgs += @ (" -g" , " -debug-info-format=codeview" , " -Xlinker" , " -debug" )
449
451
}
450
452
} else {
451
- $SwiftArgs.Add ( " -gnone" ) | Out-Null
453
+ $SwiftArgs += " -gnone"
452
454
}
453
- $SwiftArgs.Add (" -Xlinker /INCREMENTAL:NO" ) | Out-Null
455
+ $SwiftArgs += @ (" -Xlinker" , " /INCREMENTAL:NO" )
454
456
455
457
# Swift Requries COMDAT folding and de-duplication
456
- $SwiftArgs.Add (" -Xlinker /OPT:REF" ) | Out-Null
457
- $SwiftArgs.Add (" -Xlinker /OPT:ICF" ) | Out-Null
458
-
459
- $SwiftcFlags = $SwiftArgs.ToArray () -Join " "
460
- Append- FlagsDefine $Defines CMAKE_Swift_FLAGS $SwiftcFlags
458
+ $SwiftArgs += @ (" -Xlinker" , " /OPT:REF" )
459
+ $SwiftArgs += @ (" -Xlinker" , " /OPT:ICF" )
460
+ Append- FlagsDefine $Defines CMAKE_Swift_FLAGS $SwiftArgs
461
461
462
462
# Workaround CMake 3.26+ enabling `-wmo` by default on release builds
463
463
Append- FlagsDefine $Defines CMAKE_Swift_FLAGS_RELEASE " -O"
@@ -473,15 +473,32 @@ function Build-CMakeProject {
473
473
$cmakeGenerateArgs += @ (" -C" , $CacheScript )
474
474
}
475
475
foreach ($Define in ($Defines.GetEnumerator () | Sort-Object Name)) {
476
- # Avoid backslashes in defines since they are going into CMakeCache.txt,
477
- # where they are interpreted as escapes. Assume all backslashes
478
- # are path separators and can be turned into forward slashes.
479
- $ValueWithPlaceholder = if ($SwiftSDK -ne " " ) { $Define.Value.Replace (" $SwiftSDK " , " <SDK>" ) } else { $Define.Value }
480
- $ValueWithForwardSlashes = $ValueWithPlaceholder.Replace (" \" , " /" )
481
- if ($SwiftSDK -ne " " ) {
482
- $ValueWithForwardSlashes = $ValueWithForwardSlashes.Replace (" <SDK>" , " \`" $SwiftSDK \`" " )
476
+ # The quoting gets tricky to support defines containing compiler flags args,
477
+ # some of which can contain spaces, for example `-D` `Flags=-flag "C:/Program Files"`
478
+ # Avoid backslashes since they are going into CMakeCache.txt,
479
+ # where they are interpreted as escapes.
480
+ if ($Define.Value -is [string ]) {
481
+ # Single token value, no need to quote spaces, the splat operator does the right thing.
482
+ $Value = $Define.Value.Replace (" \" , " /" )
483
+ } else {
484
+ # Flags array, multiple tokens, quoting needed for tokens containing spaces
485
+ $Value = " "
486
+ foreach ($Arg in $Define.Value ) {
487
+ if ($Value.Length -gt 0 ) {
488
+ $Value += " "
489
+ }
490
+
491
+ $ArgWithForwardSlashes = $Arg.Replace (" \" , " /" )
492
+ if ($ArgWithForwardSlashes.Contains (" " )) {
493
+ # Quote and escape the quote so it makes it through
494
+ $Value += " \"" $ArgWithForwardSlashes \"" "
495
+ } else {
496
+ $Value += $ArgWithForwardSlashes
497
+ }
498
+ }
483
499
}
484
- $cmakeGenerateArgs += @ (" -D" , " $ ( $Define.Key ) =$ValueWithForwardSlashes " )
500
+
501
+ $cmakeGenerateArgs += @ (" -D" , " $ ( $Define.Key ) =$Value " )
485
502
}
486
503
487
504
Invoke-Program cmake.exe @cmakeGenerateArgs
@@ -839,7 +856,7 @@ function Build-Runtime($Arch) {
839
856
SWIFT_PATH_TO_LIBDISPATCH_SOURCE = " $SourceCache \swift-corelibs-libdispatch" ;
840
857
SWIFT_PATH_TO_STRING_PROCESSING_SOURCE = " $SourceCache \swift-experimental-string-processing" ;
841
858
SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE = " $SourceCache \swift-syntax" ;
842
- CMAKE_SHARED_LINKER_FLAGS = " /INCREMENTAL:NO /OPT:REF /OPT:ICF" ;
859
+ CMAKE_SHARED_LINKER_FLAGS = @ ( " /INCREMENTAL:NO" , " /OPT:REF" , " /OPT:ICF" ) ;
843
860
}
844
861
845
862
Invoke-Program $python - c " import plistlib; print(str(plistlib.dumps({ 'DefaultProperties': { 'DEFAULT_USE_RUNTIME': 'MD' } }), encoding='utf-8'))" `
@@ -897,7 +914,7 @@ function Build-Foundation($Arch, [switch]$Test = $false) {
897
914
# Turn off safeseh for lld as it has safeseh enabled by default
898
915
# and fails with an ICU data object file icudt69l_dat.obj. This
899
916
# matters to X86 only.
900
- CMAKE_Swift_FLAGS = if ($Arch -eq $ArchX86 ) { " -Xlinker /SAFESEH:NO" } else { " " };
917
+ CMAKE_Swift_FLAGS = if ($Arch -eq $ArchX86 ) { @ ( " -Xlinker" , " /SAFESEH:NO" ) } else { " " };
901
918
CURL_DIR = " $LibraryRoot \curl-7.77.0\usr\lib\$ShortArch \cmake\CURL" ;
902
919
ICU_DATA_LIBRARY_RELEASE = " $LibraryRoot \icu-69.1\usr\lib\$ShortArch \sicudt69.lib" ;
903
920
ICU_I18N_LIBRARY_RELEASE = " $LibraryRoot \icu-69.1\usr\lib\$ShortArch \sicuin69.lib" ;
@@ -1267,7 +1284,7 @@ function Build-PackageManager($Arch) {
1267
1284
- BuildTargets default `
1268
1285
- Defines @ {
1269
1286
BUILD_SHARED_LIBS = " YES" ;
1270
- CMAKE_Swift_FLAGS = " -DCRYPTO_v2" ;
1287
+ CMAKE_Swift_FLAGS = @ ( " -DCRYPTO_v2" ) ;
1271
1288
SwiftSystem_DIR = " $BinaryCache \2\cmake\modules" ;
1272
1289
TSC_DIR = " $BinaryCache \3\cmake\modules" ;
1273
1290
LLBuild_DIR = " $BinaryCache \4\cmake\modules" ;
@@ -1292,8 +1309,8 @@ function Build-IndexStoreDB($Arch) {
1292
1309
- BuildTargets default `
1293
1310
- Defines @ {
1294
1311
BUILD_SHARED_LIBS = " NO" ;
1295
- CMAKE_C_FLAGS = " -Xclang -fno-split-cold-code -I$SDKInstallRoot \usr\include -I$SDKInstallRoot \usr\include\Block" ;
1296
- CMAKE_CXX_FLAGS = " -Xclang -fno-split-cold-code -I$SDKInstallRoot \usr\include -I$SDKInstallRoot \usr\include\Block" ;
1312
+ CMAKE_C_FLAGS = @ ( " -Xclang" , " -fno-split-cold-code" , " -I$SDKInstallRoot \usr\include" , " -I$SDKInstallRoot \usr\include\Block" ) ;
1313
+ CMAKE_CXX_FLAGS = @ ( " -Xclang" , " -fno-split-cold-code" , " -I$SDKInstallRoot \usr\include" , " -I$SDKInstallRoot \usr\include\Block" ) ;
1297
1314
}
1298
1315
}
1299
1316
0 commit comments