forked from avengineers/SPLed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ps1
More file actions
executable file
·448 lines (394 loc) · 16.9 KB
/
build.ps1
File metadata and controls
executable file
·448 lines (394 loc) · 16.9 KB
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
<#
.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 = 'Install optional dependencies. (Switch, default: false)')]
[switch]$installOptional = $false,
[Parameter(Mandatory = $false, HelpMessage = 'Install Visual Studio Code. (Switch, default: false)')]
[switch]$installVSCode = $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 = 'Build HTML documentation. (Switch, default: false)')]
[switch]$docs = $false,
[Parameter(Mandatory = $false, HelpMessage = 'Start Visual Studio Code. (Switch, default: false)')]
[switch]$startVSCode = $false,
[Parameter(Mandatory = $false, HelpMessage = 'Command to be executed (String)')]
[string]$command = "",
[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: "prod" or "test", default: "prod")')]
[string]$buildKit = "prod",
[Parameter(Mandatory = $false, HelpMessage = 'Type of build. (String, default: empty)')]
[string]$buildType = "",
[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 self tests, e.g. "Disco or test_Disco.py" (see https://docs.pytest.org/en/stable/usage.html).')]
[string]$filter = "",
[Parameter(Mandatory = $false, HelpMessage = 'Marker for self tests, e.g. "static_analysis" (see https://docs.pytest.org/en/stable/how-to/mark.html).')]
[string]$marker = "",
[Parameter(Mandatory = $false, HelpMessage = 'Additional arguments for pytest, e.g. "--collect-only" (see https://docs.pytest.org/en/stable/reference/reference.html#command-line-flags).')]
[string]$pytestExtraArgs = "",
[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,
[Parameter(Mandatory = $false, HelpMessage = 'Wait for a key press before exiting. (Switch, default: false)')]
[switch]$waitForKey = $false
)
# 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
}
# Build documentation
function Invoke-Build-Docs {
Write-Host "Building documentation..."
# Check if sphinx-build is available
$sphinxBuild = Get-Command sphinx-build -ErrorAction SilentlyContinue
if (-not $sphinxBuild) {
Write-Host "Error: sphinx-build not found. Please run -install first."
exit 1
}
# Remove old build
if (Test-Path "build/html") {
Write-Host "Removing old documentation build..."
Remove-Item -Recurse -Force "build/html"
}
# Build documentation
Write-Host "Running sphinx-build..."
& sphinx-build -b html -j auto . build/html
if ($LASTEXITCODE -eq 0) {
Write-Host ""
Write-Host "Documentation built successfully!"
Write-Host "Open build/html/index.html in your browser to view the documentation."
} else {
Write-Host "Error: Documentation build failed."
exit 1
}
}
# 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 = $false)]
[string]$buildType = "",
[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('/') }
}
Foreach ($variant in $variantsSelected) {
$buildFolder = "build\$variant\$buildKit".Replace("/", "\")
if ($buildType -ne "") {
$buildFolder = "build\$variant\$buildKit\$buildType".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) {
if ($buildType -eq "") {
Write-Output "Building target '$target' with build kit '$buildKit' for variant '$variant' ..."
}
else {
Write-Output "Building target '$target' with build kit '$buildKit' and build type '$buildType' for variant '$variant' ..."
}
# CMake configure
$additionalConfig = "-DBUILD_KIT='$buildKit'"
if ($buildType -ne "") {
$additionalConfig += " -DBUILD_TYPE='$buildType'"
$additionalConfig += " -DCMAKE_BUILD_TYPE='$buildType'"
}
if ($buildKit -eq "test") {
$additionalConfig += " -DCMAKE_TOOLCHAIN_FILE='tools/toolchains/gcc/toolchain.cmake'"
}
Invoke-CommandLine -CommandLine "cmake -B '$buildFolder' -G Ninja -DVARIANT='$variant' $additionalConfig"
if (-Not $configureOnly) {
if ($buildType -eq "") {
$cmd = "cmake --build '$buildFolder' --target $target"
}
else {
$cmd = "cmake --build '$buildFolder' --config '$buildType' --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)]
[string]$filter = "",
[Parameter(Mandatory = $false)]
[string]$marker = ""
)
# Run python tests to test all relevant variants and platforms (build kits)
# (normally run in CI environment/Jenkins)
Write-Output "Running all self tests ..."
# Test result of pytest
$pytestJunitXml = "test/output/test-report.xml"
# Delete any old pytest result
Remove-Path $pytestJunitXml
$pytestArgs = @(
"--junitxml=$pytestJunitXml"
)
# Filter pytest test cases
$releaseBranchFilter = Get-ReleaseBranchPytestFilter
if ($releaseBranchFilter) {
$pytestArgs += "-k '$releaseBranchFilter'"
}
# otherwise consider command line option '-filter' if given
elseif ($filter) {
$pytestArgs += "-k '$filter'"
}
# Execute marker tests
if ($marker) {
$pytestArgs += "-m '$marker'"
}
# Add any extra pytest arguments given via command line
if ($pytestExtraArgs -and $pytestExtraArgs -ne "") {
$pytestArgs += $pytestExtraArgs
}
# Finally run pytest and ignore return value. Content of test-report.xml will be evaluated by CI system.
$commandLine = "pytest " + ($pytestArgs -join " ")
Invoke-CommandLine -CommandLine $commandLine -StopAtError $false
}
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 {
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) -installOptional: installation of optional dependencies")
Write-Information -Tags "Info:" -MessageData ("(3) -installVSCode: installation of Visual Studio Code")
Write-Information -Tags "Info:" -MessageData ("(4) -build: execute CMake build")
Write-Information -Tags "Info:" -MessageData ("(5) -docs: build HTML documentation")
Write-Information -Tags "Info:" -MessageData ("(6) -startVSCode: start Visual Studio Code")
Write-Information -Tags "Info:" -MessageData ("(7) quit: exit script")
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.17.2/install.ps1 | Invoke-Expression
# Execute bootstrap script
. .\.bootstrap\bootstrap.ps1
}
function Invoke-Clean-Workspace {
param (
[Parameter(Mandatory = $false)]
[bool]$install = $false,
[Parameter(Mandatory = $false)]
[bool]$selftests = $false
)
if ($install) {
Remove-Path ".venv"
}
if ($selftests) {
# Remove all build outputs in one step, this will remove obsolete variants, too.
Remove-Path "build"
}
}
## 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 ((-Not $install) -and (-Not $installOptional) -and (-Not $installVSCode) -and (-Not $build) -and (-Not $docs) -and (-Not $startVSCode) -and (-Not $command) -and (-Not $selftests)) {
$selectedOption = Get-User-Menu-Selection
switch ($selectedOption) {
'1' {
Write-Information -Tags "Info:" -MessageData "Installing mandatory dependencies ..."
$install = $true
}
'2' {
Write-Information -Tags "Info:" -MessageData "Installing optional dependencies ..."
$installOptional = $true
}
'3' {
Write-Information -Tags "Info:" -MessageData "Installing Visual Studio Code ..."
$installVSCode = $true
}
'4' {
Write-Information -Tags "Info:" -MessageData "Building ..."
$build = $true
}
'5' {
Write-Information -Tags "Info:" -MessageData "Building documentation ..."
$docs = $true
}
'6' {
Write-Information -Tags "Info:" -MessageData "Starting VS Code ..."
$startVSCode = $true
}
'7' {
Write-Information -Tags "Info:" -MessageData "Exiting ..."
exit
}
}
}
if ($install) {
# bootstrap environment
Invoke-Bootstrap
Write-Host -ForegroundColor Black -BackgroundColor Blue "For installation changes to take effect, please close and re-open your current terminal."
}
# Load bootstrap's utility functions
if (Test-Path ".\.bootstrap\utils.ps1") {
. .\.bootstrap\utils.ps1
}
if ($clean) {
Invoke-Clean-Workspace -install $install -selftests $selftests
}
# Skip pypeline and bootstrap setup for docs-only builds
if (-not ($docs -and -not $build -and -not $selftests)) {
Invoke-CommandLine ".venv\Scripts\pypeline run --step CollectPRChanges" -StopAtError $false
}
# Load environment setup script
if (Test-Path ".\build\env_setup.ps1") {
. .\build\env_setup.ps1
}
if ($installOptional) {
Import-ScoopFile "scoopfile-optional.json"
}
if ($installVSCode) {
Invoke-CommandLine "scoop bucket add extras" -StopAtError $false
Invoke-CommandLine "scoop install vscode"
Invoke-CommandLine "scoop update vscode" -StopAtError $false
}
if ($startVSCode) {
Write-Output "Starting Visual Studio Code..."
Invoke-CommandLine "code ." -StopAtError $false
}
if ($build) {
# Call build system to build variant(s)
Invoke-Build-System `
-clean $clean `
-build $build `
-target $target `
-buildKit $buildKit `
-buildType $buildType `
-variants $variants `
-reconfigure $reconfigure `
-configureOnly $configureOnly `
-ninjaArgs $ninjaArgs
}
if ($docs) {
Invoke-Build-Docs
}
if ($selftests) {
Invoke-Self-Tests -filter $filter -marker $marker
}
if ($command -ne '') {
Invoke-Expression "$command"
}
}
finally {
Pop-Location
if (-Not (Test-RunningInCIorTestEnvironment) -and $waitForKey) {
Read-Host -Prompt "Press Enter to continue ..."
}
}
## end of script