-
Notifications
You must be signed in to change notification settings - Fork 543
/
build.ps1
611 lines (499 loc) · 18.5 KB
/
build.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
<#
.SYNOPSIS
This script provides helpers for building msquic.
.PARAMETER Config
The debug or release configuration to build for.
.PARAMETER Arch
The CPU architecture to build for.
.PARAMETER Platform
Specify which platform to build for.
.PARAMETER Static
Specify a static library is preferred (shared is the default).
.PARAMETER Tls
The TLS library to use.
.PARAMETER DisableLogs
Disables log collection.
.PARAMETER LoggingType
Set logging type
.PARAMETER SanitizeAddress
Enables address sanitizer.
.PARAMETER CodeCheck
Enables static code checkers.
.PARAMETER DisableTools
Don't build the tools directory.
.PARAMETER DisableTest
Don't build the test directory.
.PARAMETER DisablePerf
Don't build the perf directory.
.PARAMETER Clean
Deletes all previous build and configuration.
.PARAMETER InstallOutput
Installs the build output to the current machine.
.PARAMETER Parallel
Enables CMake to build in parallel, where possible.
.PARAMETER DynamicCRT
Builds msquic with dynamic C runtime (Windows-only).
.PARAMETER StaticCRT
Builds msquic with static C runtime (Windows-only).
.PARAMETER PGO
Builds msquic with profile guided optimization support (Windows-only).
.PARAMETER UseXdp
Enables XDP support (Linux-only).
.PARAMETER Generator
Specifies a specific cmake generator (Only supported on unix)
.PARAMETER SkipPdbAltPath
Skip setting PDBALTPATH into built binaries on Windows. Without this flag, the PDB must be in the same directory as the DLL or EXE.
.PARAMETER SkipSourceLink
Skip generating sourcelink and inserting it into the PDB.
.PARAMETER Clang
Build with Clang if available
.PARAMETER UpdateClog
Build allowing clog to update the sidecar.
.PARAMETER ConfigureOnly
Run configuration only.
.PARAMETER CI
Build is occuring from CI
.PARAMETER OfficialRelease
Build is for an official (tag) release.
.PARAMETER EnableTelemetryAsserts
Enables telemetry asserts in release builds.
.PARAMETER UseSystemOpenSSLCrypto
Use system provided OpenSSL libcrypto rather then statically linked. Only affects OpenSSL Linux builds
.PARAMETER EnableHighResolutionTimers
Configures the system to use high resolution timers.
.PARAMETER ExtraArtifactDir
Add an extra classifier to the artifact directory to allow publishing alternate builds of same base library
.PARAMETER LibraryName
Renames the library to whatever is passed in
.PARAMETER SysRoot
Directory with cross-compilation tools
.PARAMETER OneBranch
Build is occuring from Onebranch pipeline.
.EXAMPLE
build.ps1
.EXAMPLE
build.ps1 -Config Release
#>
param (
[Parameter(Mandatory = $false)]
[ValidateSet("Debug", "Release")]
[string]$Config = "Debug",
[Parameter(Mandatory = $false)]
[ValidateSet("x86", "x64", "arm", "arm64", "arm64ec")]
[string]$Arch = "",
[Parameter(Mandatory = $false)]
[ValidateSet("gamecore_console", "uwp", "windows", "linux", "macos", "android", "ios")] # For future expansion
[string]$Platform = "",
[Parameter(Mandatory = $false)]
[switch]$Static = $false,
[Parameter(Mandatory = $false)]
[ValidateSet("schannel", "openssl", "openssl3")]
[string]$Tls = "",
[Parameter(Mandatory = $false)]
[switch]$DisableLogs = $false,
[Parameter(Mandatory = $false)]
[string]$LoggingType = "",
[Parameter(Mandatory = $false)]
[switch]$SanitizeAddress = $false,
[Parameter(Mandatory = $false)]
[switch]$CodeCheck = $false,
[Parameter(Mandatory = $false)]
[switch]$DisableTools = $false,
[Parameter(Mandatory = $false)]
[switch]$DisableTest = $false,
[Parameter(Mandatory = $false)]
[switch]$DisablePerf = $false,
[Parameter(Mandatory = $false)]
[switch]$Clean = $false,
[Parameter(Mandatory = $false)]
[int32]$Parallel = -2,
[Parameter(Mandatory = $false)]
[switch]$DynamicCRT = $false,
[Parameter(Mandatory = $false)]
[switch]$StaticCRT = $false,
[Parameter(Mandatory = $false)]
[switch]$PGO = $false,
[Parameter(Mandatory = $false)]
[switch]$UseXdp = $false,
[Parameter(Mandatory = $false)]
[string]$Generator = "",
[Parameter(Mandatory = $false)]
[switch]$SkipPdbAltPath = $false,
[Parameter(Mandatory = $false)]
[switch]$SkipSourceLink = $false,
[Parameter(Mandatory = $false)]
[switch]$Clang = $false,
[Parameter(Mandatory = $false)]
[switch]$UpdateClog = $false,
[Parameter(Mandatory = $false)]
[switch]$ConfigureOnly = $false,
[Parameter(Mandatory = $false)]
[switch]$CI = $false,
[Parameter(Mandatory = $false)]
[switch]$OfficialRelease = $false,
[Parameter(Mandatory = $false)]
[switch]$EnableTelemetryAsserts = $true,
[Parameter(Mandatory = $false)]
[switch]$UseSystemOpenSSLCrypto = $false,
[Parameter(Mandatory = $false)]
[switch]$EnableHighResolutionTimers = $false,
[Parameter(Mandatory = $false)]
[string]$ExtraArtifactDir = "",
[Parameter(Mandatory = $false)]
[string]$LibraryName = "msquic",
[Parameter(Mandatory = $false)]
[string]$SysRoot = "/",
[Parameter(Mandatory = $false)]
[switch]$OneBranch = $false,
[Parameter(Mandatory = $false)]
[string]$ToolchainFile = ""
)
Set-StrictMode -Version 'Latest'
$PSDefaultParameterValues['*:ErrorAction'] = 'Stop'
if ($Parallel -lt -1) {
if ($IsWindows) {
$Parallel = -1
} else {
$Parallel = 0
}
}
$BuildConfig = & (Join-Path $PSScriptRoot get-buildconfig.ps1) -Platform $Platform -Tls $Tls -Arch $Arch -ExtraArtifactDir $ExtraArtifactDir -Config $Config
$Platform = $BuildConfig.Platform
$Tls = $BuildConfig.Tls
$Arch = $BuildConfig.Arch
$ArtifactsDir = $BuildConfig.ArtifactsDir
if ($Generator -eq "") {
if (!$IsWindows) {
$Generator = "Unix Makefiles"
} else {
$Generator = "Visual Studio 17 2022"
}
}
if (!$IsWindows -And $Platform -eq "uwp") {
Write-Error "[$(Get-Date)] Cannot build uwp on non windows platforms"
exit
}
if (!$IsWindows -And ($Platform -eq "gamecore_console")) {
Write-Error "[$(Get-Date)] Cannot build gamecore on non windows platforms"
exit
}
if ($Arch -ne "x64" -And ($Platform -eq "gamecore_console")) {
Write-Error "[$(Get-Date)] Cannot build gamecore for non-x64 platforms"
exit
}
if ($Arch -eq "arm64ec") {
if (!$IsWindows) {
Write-Error "Arm64EC is only supported on Windows"
}
if ($Tls -eq "openssl" -Or $Tls -eq "openssl3") {
Write-Error "Arm64EC does not support openssl"
}
}
if ($IsLinux -And $Arch -ne "x64") {
if ($UseXdp) {
Write-Error "Linux XDP is supported only on x64 platforms"
}
}
if ($Platform -eq "ios" -and !$Static) {
$Static = $true
Write-Host "iOS can only be built as static"
}
if ($OfficialRelease) {
# We only actually try to do official release if there is a matching git tag.
# Clear the flag and then only set it if we find a tag.
$OfficialRelease = $false
try {
$env:GIT_REDIRECT_STDERR = '2>&1'
# Thanks to https://stackoverflow.com/questions/3404936/show-which-git-tag-you-are-on
# for this magic git command!
$Output = git describe --exact-match --tags $(git log -n1 --pretty='%h')
if (!$Output.Contains("fatal: no tag exactly matches")) {
Write-Host "Configuring OfficialRelease for tag build"
$OfficialRelease = $true
}
} catch { }
$global:LASTEXITCODE = 0
}
# Root directory of the project.
$RootDir = Split-Path $PSScriptRoot -Parent
# Important directory paths.
$BaseArtifactsDir = Join-Path $RootDir "artifacts"
$BaseBuildDir = Join-Path $RootDir "build"
$BuildDir = Join-Path $BaseBuildDir $Platform
$BuildDir = Join-Path $BuildDir "$($Arch)_$($Tls)"
if ($Clean) {
# Delete old build/config directories.
if (Test-Path $ArtifactsDir) { Remove-Item $ArtifactsDir -Recurse -Force | Out-Null }
if (Test-Path $BuildDir) { Remove-Item $BuildDir -Recurse -Force | Out-Null }
}
# Initialize directories needed for building.
if (!(Test-Path $BaseArtifactsDir)) {
New-Item -Path $BaseArtifactsDir -ItemType Directory -Force | Out-Null
}
if (!(Test-Path $BuildDir)) { New-Item -Path $BuildDir -ItemType Directory -Force | Out-Null }
if ($Clang) {
if ($IsWindows) {
Write-Error "Clang is not supported on windows currently"
}
$env:CC = 'clang'
$env:CXX = 'clang++'
}
# Workaround for perl openssl build warnings.
$env:TERM='ansi'
function Log($msg) {
Write-Host "[$(Get-Date)] $msg"
}
# Executes cmake with the given arguments.
function CMake-Execute([String]$Arguments) {
Log "cmake $($Arguments)"
$process = Start-Process cmake $Arguments -PassThru -NoNewWindow -WorkingDirectory $BuildDir
$handle = $process.Handle # Magic work around. Don't remove this line.
$process.WaitForExit();
if ($process.ExitCode -ne 0) {
Write-Error "[$(Get-Date)] CMake exited with status code $($process.ExitCode)"
}
}
# Uses cmake to generate the build configuration files.
function CMake-Generate {
$Arguments = ""
if ($Generator.Contains(" ")) {
$Generator = """$Generator"""
}
if ($IsWindows) {
if ($Generator.Contains("Visual Studio") -or [string]::IsNullOrWhiteSpace($Generator)) {
if ($Generator.Contains("Visual Studio")) {
$Arguments += " -G $Generator"
}
$Arguments += " -A "
switch ($Arch) {
"x86" { $Arguments += "Win32" }
"x64" { $Arguments += "x64" }
"arm" { $Arguments += "arm" }
"arm64" { $Arguments += "arm64" }
"arm64ec" { $Arguments += "arm64ec" }
}
} else {
Write-Host "Non VS based generators must be run from a Visual Studio Developer Powershell Prompt matching the passed in architecture"
$Arguments += " -G $Generator"
}
} else {
$Arguments += "-G $Generator"
}
if ($Platform -eq "ios") {
$IosTCFile = Join-Path $RootDir cmake toolchains ios.cmake
$Arguments += " -DCMAKE_TOOLCHAIN_FILE=""$IosTCFile"" -DDEPLOYMENT_TARGET=""13.0"" -DENABLE_ARC=0 -DCMAKE_OSX_DEPLOYMENT_TARGET=""13.0"""
switch ($Arch) {
"x64" { $Arguments += " -DPLATFORM=SIMULATOR64"}
"arm64" { $Arguments += " -DPLATFORM=OS64"}
}
}
if ($Platform -eq "macos") {
switch ($Arch) {
"x64" { $Arguments += " -DCMAKE_OSX_ARCHITECTURES=x86_64 -DCMAKE_OSX_DEPLOYMENT_TARGET=""13"""}
"arm64" { $Arguments += " -DCMAKE_OSX_ARCHITECTURES=arm64 -DCMAKE_OSX_DEPLOYMENT_TARGET=""13"""}
}
}
if ($Platform -eq "linux") {
$Arguments += " $Generator"
$HostArch = "$([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture)".ToLower()
if ($HostArch -ne $Arch) {
if ($OneBranch) {
$Arguments += " -DONEBRANCH=1"
if ($ToolchainFile -eq "") {
switch ($Arch) {
"arm" { $ToolchainFile = "cmake/toolchains/arm-linux.cmake" }
"arm64" { $ToolchainFile = "cmake/toolchains/aarch64-linux.cmake" }
}
}
}
$Arguments += " -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER -DCMAKE_CROSSCOMPILING=1 -DCMAKE_SYSROOT=$SysRoot"
switch ($Arch) {
"arm64" { $Arguments += " -DCMAKE_CXX_COMPILER_TARGET=aarch64-linux-gnu -DCMAKE_C_COMPILER_TARGET=aarch64-linux-gnu -DCMAKE_TARGET_ARCHITECTURE=arm64" }
"arm" { $Arguments += " -DCMAKE_CXX_COMPILER_TARGET=arm-linux-gnueabihf -DCMAKE_C_COMPILER_TARGET=arm-linux-gnueabihf -DCMAKE_TARGET_ARCHITECTURE=arm" }
}
# to build with pkg-config
switch ($Arch) {
"arm" { $env:PKG_CONFIG_PATH="$SysRoot/usr/lib/arm-linux-gnueabihf/pkgconfig" }
"arm64" { $env:PKG_CONFIG_PATH="$SysRoot/usr/lib/aarch64-linux-gnu/pkgconfig" }
}
}
}
if ($ToolchainFile -ne "") {
$Arguments += " -DCMAKE_TOOLCHAIN_FILE=""$ToolchainFile"""
}
if($Static) {
$Arguments += " -DQUIC_BUILD_SHARED=off"
}
$Arguments += " -DQUIC_TLS=" + $Tls
$Arguments += " -DQUIC_OUTPUT_DIR=""$ArtifactsDir"""
if ($IsLinux) {
$Arguments += " -DQUIC_LINUX_LOG_ENCODER=lttng"
}
if (!$DisableLogs) {
$Arguments += " -DQUIC_ENABLE_LOGGING=on"
}
if ($LoggingType -ne "") {
$Arguments += " -DQUIC_ENABLE_LOGGING=on -DQUIC_LOGGING_TYPE=" + $LoggingType
}
if ($SanitizeAddress) {
$Arguments += " -DQUIC_ENABLE_SANITIZERS=on"
}
if ($CodeCheck) {
$Arguments += " -DQUIC_CODE_CHECK=on"
}
if ($Platform -ne "uwp" -and $Platform -ne "gamecore_console") {
if (!$DisableTools) {
$Arguments += " -DQUIC_BUILD_TOOLS=on"
}
if (!$DisableTest) {
$Arguments += " -DQUIC_BUILD_TEST=on"
}
if (!$DisablePerf) {
$Arguments += " -DQUIC_BUILD_PERF=on"
}
}
if (!$IsWindows) {
$ConfigToBuild = $Config;
if ($Config -eq "Release") {
$ConfigToBuild = "RelWithDebInfo"
}
$Arguments += " -DCMAKE_BUILD_TYPE=" + $ConfigToBuild
}
if ($IsWindows) {
if ($DynamicCRT) {
$Arguments += " -DQUIC_STATIC_LINK_CRT=off -DQUIC_STATIC_LINK_PARTIAL_CRT=off"
}
if ($StaticCRT) {
$Arguments += " -DQUIC_STATIC_LINK_CRT=on -DQUIC_STATIC_LINK_PARTIAL_CRT=off"
}
}
if ($PGO) {
$Arguments += " -DQUIC_PGO=on"
}
if ($UseXdp) {
$Arguments += " -DQUIC_LINUX_XDP_ENABLED=on"
}
if ($Platform -eq "uwp") {
$Arguments += " -DCMAKE_SYSTEM_NAME=WindowsStore -DCMAKE_SYSTEM_VERSION=10.0 -DQUIC_UWP_BUILD=on"
}
if ($Platform -eq "gamecore_console") {
$Arguments += " -DCMAKE_SYSTEM_VERSION=10.0 -DQUIC_GAMECORE_BUILD=on"
}
if ($SkipPdbAltPath) {
$Arguments += " -DQUIC_PDBALTPATH=OFF"
}
if ($SkipSourceLink) {
$Arguments += " -DQUIC_SOURCE_LINK=OFF"
}
if ($CI) {
$Arguments += " -DQUIC_CI=ON"
if ($Platform -eq "android" -or $ToolchainFile -ne "") {
$Arguments += " -DQUIC_SKIP_CI_CHECKS=ON"
}
$Arguments += " -DQUIC_VER_BUILD_ID=$env:BUILD_BUILDID"
$Arguments += " -DQUIC_VER_SUFFIX=-official"
}
if ($OfficialRelease) {
$Arguments += " -DQUIC_OFFICIAL_RELEASE=ON"
}
if ($EnableTelemetryAsserts) {
$Arguments += " -DQUIC_TELEMETRY_ASSERTS=on"
}
if ($UseSystemOpenSSLCrypto) {
$Arguments += " -DQUIC_USE_SYSTEM_LIBCRYPTO=on"
}
if ($EnableHighResolutionTimers) {
$Arguments += " -DQUIC_HIGH_RES_TIMERS=on"
}
if ($Platform -eq "android") {
$NDK = $env:ANDROID_NDK_LATEST_HOME -replace '26\.\d+\.\d+', '25.2.9519653' # Temporary work around. Use RegEx to replace newer version.
$env:PATH = "$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin:$env:PATH"
switch ($Arch) {
"x86" { $Arguments += " -DANDROID_ABI=x86"}
"x64" { $Arguments += " -DANDROID_ABI=x86_64" }
"arm" { $Arguments += " -DANDROID_ABI=armeabi-v7a" }
"arm64" { $Arguments += " -DANDROID_ABI=arm64-v8a" }
}
$Arguments += " -DANDROID_PLATFORM=android-29"
$env:ANDROID_NDK_HOME = $NDK
$NdkToolchainFile = "$NDK/build/cmake/android.toolchain.cmake"
$Arguments += " -DANDROID_NDK=""$NDK"""
$Arguments += " -DCMAKE_TOOLCHAIN_FILE=""$NdkToolchainFile"""
}
$Arguments += " -DQUIC_LIBRARY_NAME=$LibraryName"
$Arguments += " ../../.."
Write-Host "Executing: $Arguments"
CMake-Execute $Arguments
}
# Uses cmake to generate the build configuration files.
function CMake-Build {
$Arguments = "--build ."
if ($Parallel -gt 0) {
$Arguments += " --parallel $($Parallel)"
} elseif ($Parallel -eq 0) {
$Arguments += " --parallel"
}
if ($IsWindows) {
$Arguments += " --config " + $Config
} else {
$Arguments += " -- VERBOSE=1"
}
Write-Host "Running: $Arguments"
CMake-Execute $Arguments
if ($IsWindows) {
Copy-Item (Join-Path $BuildDir "obj" $Config "$LibraryName.lib") $ArtifactsDir
if ($SanitizeAddress -or ($PGO -and $Config -eq "Release")) {
$CacheFile = Join-Path $BuildDir "CMakeCache.txt"
$LinkerMatches = Select-String -Path $CacheFile -Pattern "CMAKE_LINKER:FILEPATH=(.+)"
if ($LinkerMatches.Matches.Length -eq 1 -and $LinkerMatches.Matches[0].Groups.Count -eq 2) {
$Linker = $LinkerMatches.Matches[0].Groups[1].Value
$VCToolsPath = Split-Path -Path $Linker -Parent
if ($PGO) {
Copy-Item (Join-Path $VCToolsPath "pgort140.dll") $ArtifactsDir
Copy-Item (Join-Path $VCToolsPath "pgodb140.dll") $ArtifactsDir
Copy-Item (Join-Path $VCToolsPath "mspdbcore.dll") $ArtifactsDir
Copy-Item (Join-Path $VCToolsPath "tbbmalloc.dll") $ArtifactsDir
Copy-Item (Join-Path $VCToolsPath "pgomgr.exe") $ArtifactsDir
}
if ($SanitizeAddress) {
Copy-Item (Join-Path $VCToolsPath "clang_rt.asan_dbg_dynamic-x86_64.dll") $ArtifactsDir
Copy-Item (Join-Path $VCToolsPath "clang_rt.asan_dynamic-x86_64.dll") $ArtifactsDir
}
} else {
Log "Failed to find VC Tools path!"
}
}
} elseif ($IsLinux -and $OneBranch) {
# archive the build artifacts for packaging to persist symlinks and permissons.
$ArtifactsParentDir = Split-Path $ArtifactsDir -Parent
$ArtifactsLeafDir = Split-Path $ArtifactsDir -Leaf
tar -cvf "$ArtifactsDir.tar" -C $ArtifactsParentDir "./$ArtifactsLeafDir"
}
# Package debug symbols on macos
if ($Platform -eq "macos") {
$BuiltArtifacts = Get-ChildItem $ArtifactsDir -File
foreach ($Artifact in $BuiltArtifacts) {
if (Test-Path $Artifact) {
dsymutil $Artifact
}
}
}
}
##############################################################
# Main Execution #
##############################################################
if ($UpdateClog) {
$env:CLOG_DEVELOPMENT_MODE=1
}
# Generate the build files.
Log "Generating files..."
CMake-Generate
if (!$ConfigureOnly) {
# Build the code.
Log "Building..."
CMake-Build
}
Log "Done."
if ($UpdateClog) {
$env:CLOG_DEVELOPMENT_MODE=0
}