forked from microsoft/msquic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ps1
321 lines (258 loc) · 9.14 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
<#
.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 Tls
The TLS library to use.
.PARAMETER ToolchainFile
Toolchain file to use (if cross)
.PARAMETER DisableLogs
Disables log collection.
.PARAMETER SanitizeAddress
Enables address sanitizer.
.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 PGO
Builds msquic with profile guided optimization support (Windows-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.
.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")]
[string]$Arch = "x64",
[Parameter(Mandatory = $false)]
[ValidateSet("uwp", "windows", "linux")] # For future expansion
[string]$Platform = "",
[Parameter(Mandatory = $false)]
[ValidateSet("schannel", "openssl", "stub", "mitls")]
[string]$Tls = "",
[Parameter(Mandatory = $false)]
[string]$ToolchainFile = "",
[Parameter(Mandatory = $false)]
[switch]$DisableLogs = $false,
[Parameter(Mandatory = $false)]
[switch]$SanitizeAddress = $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 = -1,
[Parameter(Mandatory = $false)]
[switch]$DynamicCRT = $false,
[Parameter(Mandatory = $false)]
[switch]$PGO = $false,
[Parameter(Mandatory = $false)]
[string]$Generator = "",
[Parameter(Mandatory = $false)]
[switch]$SkipPdbAltPath = $false,
[Parameter(Mandatory = $false)]
[switch]$SkipSourceLink = $false
)
Set-StrictMode -Version 'Latest'
$PSDefaultParameterValues['*:ErrorAction'] = 'Stop'
if ($Generator -eq "") {
if ($IsWindows) {
$Generator = "Visual Studio 16 2019"
} elseif ($IsLinux) {
$Generator = "Ninja"
} else {
$Generator = "Unix Makefiles"
}
}
# Default TLS based on current platform.
if ("" -eq $Tls) {
if ($IsWindows) {
$Tls = "schannel"
} else {
$Tls = "openssl"
}
}
if ("" -eq $Platform) {
if ($IsWindows) {
$Platform = "windows"
} else {
$Platform = "linux"
}
}
if (!$IsWindows -And $Platform -eq "uwp") {
Write-Error "[$(Get-Date)] Cannot build uwp on non windows platforms"
exit
}
# Root directory of the project.
$RootDir = Split-Path $PSScriptRoot -Parent
# Important directory paths.
$BaseArtifactsDir = Join-Path $RootDir "artifacts"
$BaseBuildDir = Join-Path $RootDir "build"
$ArtifactsDir = Join-Path $BaseArtifactsDir "bin" $Platform
$BuildDir = Join-Path $BaseBuildDir $Platform
$ArtifactsDir = Join-Path $ArtifactsDir "$($Arch)_$($Config)_$($Tls)"
$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 }
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 = "-g"
if ($IsWindows) {
$Arguments += " '$Generator' -A "
switch ($Arch) {
"x86" { $Arguments += "Win32" }
"x64" { $Arguments += "x64" }
"arm" { $Arguments += "arm" }
"arm64" { $Arguments += "arm64" }
}
} else {
$Arguments += " '$Generator'"
}
$Arguments += " -DQUIC_TLS=" + $Tls
$Arguments += " -DQUIC_OUTPUT_DIR=" + $ArtifactsDir
if ($DisableLogs) {
$Arguments += " -DQUIC_ENABLE_LOGGING=off"
}
if ($SanitizeAddress) {
$Arguments += " -DQUIC_SANITIZE_ADDRESS=on"
}
if ($DisableTools) {
$Arguments += " -DQUIC_BUILD_TOOLS=off"
}
if ($DisableTest) {
$Arguments += " -DQUIC_BUILD_TEST=off"
}
if ($DisablePerf) {
$Arguments += " -DQUIC_BUILD_PERF=off"
}
if ($IsLinux) {
$Arguments += " -DCMAKE_BUILD_TYPE=" + $Config
}
if ($DynamicCRT) {
$Arguments += " -DQUIC_STATIC_LINK_CRT=off"
}
if ($PGO) {
$Arguments += " -DQUIC_PGO=on"
}
if ($Platform -eq "uwp") {
$Arguments += " -DCMAKE_SYSTEM_NAME=WindowsStore -DCMAKE_SYSTEM_VERSION=10 -DQUIC_UWP_BUILD=on -DQUIC_STATIC_LINK_CRT=Off"
}
if ($ToolchainFile -ne "") {
$Arguments += " ""-DCMAKE_TOOLCHAIN_FILE=" + $ToolchainFile + """"
}
if ($SkipPdbAltPath) {
$Arguments += " -DQUIC_PDBALTPATH=OFF"
}
if ($SkipSourceLink) {
$Arguments += " -DQUIC_SOURCE_LINK=OFF"
}
$Arguments += " ../../.."
CMake-Execute $Arguments
if ($PGO -and $Config -eq "Release") {
# Manually edit project file, since CMake doesn't seem to have a way to do it.
$FindText = " <PropertyGroup Label=`"UserMacros`" />"
$ReplaceText = " <PropertyGroup Label=`"UserMacros`" />`r`n <PropertyGroup><LibraryPath>`$(LibraryPath);`$(VC_LibraryPath_VC_$($Arch)_Desktop)</LibraryPath></PropertyGroup>"
$ProjectFile = Join-Path $BuildDir "src\bin\msquic.vcxproj"
(Get-Content $ProjectFile) -replace $FindText, $ReplaceText | Out-File $ProjectFile
}
}
# 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"
}
CMake-Execute $Arguments
if ($IsWindows) {
Copy-Item (Join-Path $BuildDir "obj" $Config "msquic.lib") $ArtifactsDir
if (!$DisableTools) {
Copy-Item (Join-Path $BuildDir "obj" $Config "msquicetw.lib") $ArtifactsDir
}
if ($PGO -and $Config -eq "Release") {
# TODO - Figure out a better way to get the path?
$VCToolsPath = "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\bin\Host$Arch\$Arch"
if (!(Test-Path $VCToolsPath)) {
$VCToolsPath = "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.26.28801\bin\Host$Arch\$Arch"
}
if (!(Test-Path $VCToolsPath)) {
$VCToolsPath = "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\Host$Arch\$Arch"
}
if (Test-Path $VCToolsPath) {
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
} else {
Log "Failed to find VC Tools path!"
}
}
}
}
##############################################################
# Main Execution #
##############################################################
# Generate the build files.
Log "Generating files..."
CMake-Generate
# Build the code.
Log "Building..."
CMake-Build
Log "Done."