Skip to content

Commit fcc5fb4

Browse files
Fix quoting in CMake args defines
Fix quoting for cmake compiler flags defines containing tokens with spaces.
1 parent 2b4c043 commit fcc5fb4

File tree

1 file changed

+50
-33
lines changed

1 file changed

+50
-33
lines changed

build.ps1

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,9 @@ function TryAdd-KeyValue([hashtable]$Hashtable, [string]$Key, [string]$Value) {
320320
}
321321
}
322322

323-
function Append-FlagsDefine([hashtable]$Defines, [string]$Name, [string]$Value) {
323+
function Append-FlagsDefine([hashtable]$Defines, [string]$Name, [string[]]$Value) {
324324
if ($Defines.Contains($Name)) {
325-
$Defines[$name] += " $Value"
325+
$Defines[$name] = @($Defines[$name]) + $Value
326326
} else {
327327
$Defines.Add($Name, $Value)
328328
}
@@ -354,7 +354,7 @@ function Build-CMakeProject {
354354
[string[]] $UseMSVCCompilers = @(), # C,CXX
355355
[string[]] $UseBuiltCompilers = @(), # ASM,C,CXX,Swift
356356
[string] $SwiftSDK = "",
357-
[hashtable] $Defines = @{},
357+
[hashtable] $Defines = @{}, # Values are either single strings or arrays of flags
358358
[string[]] $BuildTargets = @()
359359
)
360360

@@ -374,14 +374,16 @@ function Build-CMakeProject {
374374

375375
# Add additional defines (unless already present)
376376
$Defines = $Defines.Clone()
377+
377378
TryAdd-KeyValue $Defines CMAKE_BUILD_TYPE $BuildType
378379
TryAdd-KeyValue $Defines CMAKE_MT "mt"
379380

380381
$GenerateDebugInfo = $Defines["CMAKE_BUILD_TYPE"] -ne "Release"
381-
$Zi = if ($GenerateDebugInfo) { "/Zi" } else { "" }
382382

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+
385387
if ($UseMSVCCompilers.Contains("C")) {
386388
TryAdd-KeyValue $Defines CMAKE_C_COMPILER cl
387389
Append-FlagsDefine $Defines CMAKE_C_FLAGS $CFlags
@@ -430,34 +432,32 @@ function Build-CMakeProject {
430432
$RuntimeBinaryCache = Get-ProjectBinaryCache $Arch 1
431433
$SwiftResourceDir = "${RuntimeBinaryCache}\lib\swift"
432434

433-
$SwiftArgs = [System.Collections.ArrayList]@()
435+
$SwiftArgs = @()
434436

435437
if ($SwiftSDK -ne "") {
436-
$SwiftArgs.Add("-sdk $SwiftSDK") | Out-Null
438+
$SwiftArgs += @("-sdk", $SwiftSDK)
437439
} 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")
441443
}
442444

443445
# Debug Information
444446
if ($GenerateDebugInfo) {
445447
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")
447449
} else {
448-
$SwiftArgs.Add("-g -debug-info-format=codeview -Xlinker -debug") | Out-Null
450+
$SwiftArgs += @("-g", "-debug-info-format=codeview", "-Xlinker", "-debug")
449451
}
450452
} else {
451-
$SwiftArgs.Add("-gnone") | Out-Null
453+
$SwiftArgs += "-gnone"
452454
}
453-
$SwiftArgs.Add("-Xlinker /INCREMENTAL:NO") | Out-Null
455+
$SwiftArgs += @("-Xlinker", "/INCREMENTAL:NO")
454456

455457
# 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
461461

462462
# Workaround CMake 3.26+ enabling `-wmo` by default on release builds
463463
Append-FlagsDefine $Defines CMAKE_Swift_FLAGS_RELEASE "-O"
@@ -473,15 +473,32 @@ function Build-CMakeProject {
473473
$cmakeGenerateArgs += @("-C", $CacheScript)
474474
}
475475
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+
}
483499
}
484-
$cmakeGenerateArgs += @("-D", "$($Define.Key)=$ValueWithForwardSlashes")
500+
501+
$cmakeGenerateArgs += @("-D", "$($Define.Key)=$Value")
485502
}
486503

487504
Invoke-Program cmake.exe @cmakeGenerateArgs
@@ -839,7 +856,7 @@ function Build-Runtime($Arch) {
839856
SWIFT_PATH_TO_LIBDISPATCH_SOURCE = "$SourceCache\swift-corelibs-libdispatch";
840857
SWIFT_PATH_TO_STRING_PROCESSING_SOURCE = "$SourceCache\swift-experimental-string-processing";
841858
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");
843860
}
844861

845862
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) {
897914
# Turn off safeseh for lld as it has safeseh enabled by default
898915
# and fails with an ICU data object file icudt69l_dat.obj. This
899916
# 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 { "" };
901918
CURL_DIR = "$LibraryRoot\curl-7.77.0\usr\lib\$ShortArch\cmake\CURL";
902919
ICU_DATA_LIBRARY_RELEASE = "$LibraryRoot\icu-69.1\usr\lib\$ShortArch\sicudt69.lib";
903920
ICU_I18N_LIBRARY_RELEASE = "$LibraryRoot\icu-69.1\usr\lib\$ShortArch\sicuin69.lib";
@@ -1267,7 +1284,7 @@ function Build-PackageManager($Arch) {
12671284
-BuildTargets default `
12681285
-Defines @{
12691286
BUILD_SHARED_LIBS = "YES";
1270-
CMAKE_Swift_FLAGS = "-DCRYPTO_v2";
1287+
CMAKE_Swift_FLAGS = @("-DCRYPTO_v2");
12711288
SwiftSystem_DIR = "$BinaryCache\2\cmake\modules";
12721289
TSC_DIR = "$BinaryCache\3\cmake\modules";
12731290
LLBuild_DIR = "$BinaryCache\4\cmake\modules";
@@ -1292,8 +1309,8 @@ function Build-IndexStoreDB($Arch) {
12921309
-BuildTargets default `
12931310
-Defines @{
12941311
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");
12971314
}
12981315
}
12991316

0 commit comments

Comments
 (0)