-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.ps1
357 lines (315 loc) · 13.4 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
<#
.DESCRIPTION
Wrapper for installing dependencies, running and testing the project
#>
param(
[Parameter(Mandatory = $false, HelpMessage = 'Install all dependencies required to build. (Switch, default: false)')]
[switch]$install = $false,
[Parameter(Mandatory = $false, HelpMessage = 'Run all CI tests (python tests with pytest) (Switch, default: false)')]
[switch]$selftests = $false,
[Parameter(Mandatory = $false, HelpMessage = 'Build the target.')]
[switch]$build = $false,
[Parameter(Mandatory = $false, HelpMessage = 'Clean build, wipe out all build artifacts. (Switch, default: false)')]
[switch]$clean = $false,
[Parameter(Mandatory = $false, HelpMessage = 'Build kit to be used. (String, default: "prod")')]
[string]$buildKit = "prod",
[Parameter(Mandatory = $false, HelpMessage = 'Target to be built. (String, default: "all")')]
[string]$target = "all",
[Parameter(Mandatory = $false, HelpMessage = 'Variants (of the product) to be built (List of strings, leave empty to be asked or "all" for automatic build of all variants)')]
[string[]]$variants = $null,
[Parameter(Mandatory = $false, HelpMessage = 'filter for selftests; define in pytest syntax: https://docs.pytest.org/en/6.2.x/usage.html; e.g. "Disco or test_CustA__Disco.py"')]
[string]$filter = "",
[Parameter(Mandatory = $false, HelpMessage = 'Additional build arguments for Ninja (e.g., "-d explain -d keepdepfile" for debugging purposes)')]
[string]$ninjaArgs = "",
[Parameter(Mandatory = $false, HelpMessage = 'Delete CMake cache and reconfigure. (Switch, default: false)')]
[switch]$reconfigure = $false,
[Parameter(Mandatory = $false, HelpMessage = 'Just configure the build and fetch all dependencies. (Switch, default: false)')]
[switch]$configureOnly = $false
)
function Invoke-CommandLine {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingInvokeExpression', '', Justification = 'Usually this statement must be avoided (https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/avoid-using-invoke-expression?view=powershell-7.3), here it is OK as it does not execute unknown code.')]
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$CommandLine,
[Parameter(Mandatory = $false, Position = 1)]
[bool]$StopAtError = $true,
[Parameter(Mandatory = $false, Position = 2)]
[bool]$PrintCommand = $true,
[Parameter(Mandatory = $false, Position = 3)]
[bool]$Silent = $false
)
if ($PrintCommand) {
Write-Output "Executing: $CommandLine"
}
$global:LASTEXITCODE = 0
if ($Silent) {
# Omit information stream (6) and stdout (1)
Invoke-Expression $CommandLine 6>&1 | Out-Null
}
else {
Invoke-Expression $CommandLine
}
if ($global:LASTEXITCODE -ne 0) {
if ($StopAtError) {
Write-Error "Command line call `"$CommandLine`" failed with exit code $global:LASTEXITCODE"
}
else {
Write-Output "Command line call `"$CommandLine`" failed with exit code $global:LASTEXITCODE, continuing ..."
}
}
}
# Update/Reload current environment variable PATH with settings from registry
function Initialize-EnvPath {
# workaround for system-wide installations
if ($Env:USER_PATH_FIRST) {
$Env:Path = [System.Environment]::GetEnvironmentVariable("Path", "User") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "Machine")
}
else {
$Env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
}
}
function Test-RunningInCIorTestEnvironment {
return [Boolean]($Env:JENKINS_URL -or $Env:PYTEST_CURRENT_TEST -or $Env:GITHUB_ACTIONS)
}
# Consider CI environment variables (e.g. on Jenkins BRANCH_NAME and CHANGE_TARGET) to filter tests in release branch builds
function Get-ReleaseBranchPytestFilter {
$ChangeId = $env:CHANGE_ID
$BranchName = $env:BRANCH_NAME
$ChangeTarget = $env:CHANGE_TARGET
$targetBranch = ''
if (-not $ChangeId -and $BranchName -and $BranchName.StartsWith("release/")) {
$targetBranch = $BranchName
}
if ($ChangeId -and $ChangeTarget -and $ChangeTarget.StartsWith("release/") ) {
$targetBranch = $ChangeTarget
}
$filter = ''
if ($targetBranch -and ($targetBranch -match 'release/([^/]+/[^/]+)(.*)')) {
$filter = $Matches[1].Replace('/', ' and ')
}
return $filter
}
# Call build system with given parameters
function Invoke-Build-System {
param (
[Parameter(Mandatory = $false)]
[bool]$clean = $false,
[Parameter(Mandatory = $false)]
[bool]$build = $false,
[Parameter(Mandatory = $false)]
[string]$buildKit = "prod",
[Parameter(Mandatory = $true)]
[string]$target = "all",
[Parameter(Mandatory = $false)]
[string[]]$variants = $null,
[Parameter(Mandatory = $false)]
[string]$ninjaArgs = "",
[Parameter(Mandatory = $false)]
[bool]$reconfigure = $false,
[Parameter(Mandatory = $false)]
[bool]$configureOnly = $false
)
# Determine variants to be built
$defaultVariantsFolder = ".\variants\"
if ((-Not $variants) -or ($variants -eq 'all')) {
$variantConfigs = Get-Childitem -Include config.cmake -Path $defaultVariantsFolder -Recurse | Resolve-Path -Relative
$variantsList = @()
Foreach ($variantConfig in $variantConfigs) {
$variant = ((Get-Item $variantConfig).Directory | Resolve-Path -Relative).Replace($defaultVariantsFolder, "").Replace("\", "/")
$variantsList += $variant
}
$variantsSelected = @()
if (-Not $variants) {
# variant selection by user if not specified
Write-Information -Tags "Info:" -MessageData "no '--variant <variant>' was given, please select from list:"
Write-Information -Tags "Info:" -MessageData ("(0) all variants")
Foreach ($variant in $variantsList) {
Write-Information -Tags "Info:" -MessageData ("(" + ([array]::IndexOf($variantsList, $variant) + 1) + ") " + $variant)
}
$selection = [int](Read-Host "Please enter selected variant number")
if ($selection -eq 0) {
# build all variants
$variantsSelected = $variantsList
}
else {
# build selected variant
$variantsSelected += $variantsList[$selection - 1]
}
Write-Information -Tags "Info:" -MessageData "Selected variants: $variantsSelected"
}
else {
# otherwise build all variants
$variantsSelected = $variantsList
}
}
else {
$variantsSelected = $Variants.Replace($defaultVariantsFolder, "").Replace("\", "/").Split(',') | ForEach-Object { $_.TrimEnd('/') }
}
# Select 'test' build kit based on target
if ($target.Contains("unittests") -or $target.Contains("reports")) {
$buildKit = "test"
}
Foreach ($variant in $variantsSelected) {
$buildFolder = "build\$variant\$buildKit".Replace("/", "\")
# fresh and clean build
if ($clean) {
Remove-Path $buildFolder
}
New-Directory $buildFolder
# delete CMake cache and reconfigure
if ($reconfigure -or $configureOnly) {
Remove-Path "$buildFolder\CMakeCache.txt"
Remove-Path "$buildFolder\CMakeFiles"
}
if ($build) {
Write-Output "Building target '$target' with build kit '$buildKit' for variant '$variant' ..."
# CMake configure
$additionalConfig = "-DBUILD_KIT='$buildKit'"
if ($buildKit -eq "test") {
$additionalConfig += " -DCMAKE_TOOLCHAIN_FILE='tools/toolchains/gcc/toolchain.cmake'"
}
Invoke-CommandLine -CommandLine ".venv\Scripts\pipenv run cmake -B '$buildFolder' -G Ninja -DVARIANT='$variant' $additionalConfig"
if (-Not $configureOnly) {
$cmd = ".venv\Scripts\pipenv run cmake --build '$buildFolder' --target $target"
# CMake clean all dead artifacts. Required when running incremented builds to delete obsolete artifacts.
Invoke-CommandLine -CommandLine "$cmd -- -t cleandead"
# CMake build
Invoke-CommandLine -CommandLine "$cmd -- $ninjaArgs"
}
}
}
}
function Invoke-Self-Tests {
param (
[Parameter(Mandatory = $false)]
[bool]$clean = $false,
[Parameter(Mandatory = $false)]
[string]$filter = ""
)
# Run python tests to test all relevant variants and platforms (build kits)
# (normally run in CI environment/Jenkins)
Write-Output "Running all selfstests ..."
if ($clean) {
# Remove all build outputs in one step, this will remove obsolete variants, too.
Remove-Path "build"
# pytest's sub builds shall be clean ones, too.
$Env:PYTEST_SPL_BUILD_CLEAN = 1
}
# Filter pytest test cases
$filterCmd = ''
$releaseBranchFilter = Get-ReleaseBranchPytestFilter
if ($releaseBranchFilter) {
$filterCmd = "-k '$releaseBranchFilter'"
}
# otherwise consider command line option '-filter' if given
elseif ($filter) {
$filterCmd = "-k '$filter'"
}
# Test result of pytest
$pytestJunitXml = "test/output/test-report.xml"
# Delete any old pytest result
Remove-Path $pytestJunitXml
# Finally run pytest
Invoke-CommandLine -CommandLine ".venv\Scripts\pipenv run python -m pytest --junitxml=$pytestJunitXml $filterCmd"
}
function Remove-Path {
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$path
)
if (Test-Path -Path $path -PathType Container) {
Write-Output "Deleting directory '$path' ..."
Remove-Item $path -Force -Recurse
}
elseif (Test-Path -Path $path -PathType Leaf) {
Write-Output "Deleting file '$path' ..."
Remove-Item $path -Force
}
}
function New-Directory {
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$dir
)
if (-Not (Test-Path -Path $dir)) {
Write-Output "Creating directory '$dir' ..."
New-Item -ItemType Directory $dir
}
}
function Get-User-Menu-Selection {
if ((-Not $install) -and (-Not $build) -and (-Not $command) -and (-Not $selftests)) {
Clear-Host
Write-Information -Tags "Info:" -MessageData "None of the following command line options was given:"
Write-Information -Tags "Info:" -MessageData ("(1) -install: installation of mandatory dependencies")
Write-Information -Tags "Info:" -MessageData ("(2) -build: execute CMake build")
return(Read-Host "Please make a selection")
}
}
function Invoke-Bootstrap {
# Download bootstrap scripts from external repository
Invoke-RestMethod -Uri https://raw.githubusercontent.com/avengineers/bootstrap-installer/v1.14.2/install.ps1 | Invoke-Expression
# Execute bootstrap script
. .\.bootstrap\bootstrap.ps1
# For incremental build: clean up virtual environment from old dependencies
Invoke-CommandLine ".venv\Scripts\pipenv clean"
}
## start of script
# Always set the $InformationPreference variable to "Continue" globally,
# this way it gets printed on execution and continues execution afterwards.
$InformationPreference = "Continue"
# Stop on first error
$ErrorActionPreference = "Stop"
Push-Location $PSScriptRoot
Write-Output "Running in ${pwd}"
try {
if (Test-RunningInCIorTestEnvironment -or $Env:USER_PATH_FIRST) {
Initialize-EnvPath
}
$selectedOption = Get-User-Menu-Selection
switch ($selectedOption) {
'1' {
Write-Information -Tags "Info:" -MessageData "Installing Dependencies ..."
$install = $true
}
'2' {
Write-Information -Tags "Info:" -MessageData "Building ..."
$build = $true
}
default {
Write-Information -Tags "Info:" -MessageData "Nothing selected."
}
}
if ($install) {
if ($clean) {
Remove-Path ".venv"
}
# bootstrap environment
Invoke-Bootstrap
if (Test-RunningInCIorTestEnvironment -or $Env:USER_PATH_FIRST) {
Initialize-EnvPath
}
Write-Host -ForegroundColor Black -BackgroundColor Blue "For installation changes to take effect, please close and re-open your current terminal."
}
if ($build) {
# Call build system to build variant(s)
Invoke-Build-System `
-clean $clean `
-build $build `
-target $target `
-buildKit $buildKit `
-variants $variants `
-reconfigure $reconfigure `
-configureOnly $configureOnly `
-ninjaArgs $ninjaArgs
}
if ($selftests) {
Invoke-Self-Tests -clean $clean -filter $filter
}
}
finally {
Pop-Location
if (-Not (Test-RunningInCIorTestEnvironment)) {
Read-Host -Prompt "Press Enter to continue ..."
}
}
## end of script