-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompilation_justcompile.ps1
More file actions
177 lines (157 loc) · 6.89 KB
/
compilation_justcompile.ps1
File metadata and controls
177 lines (157 loc) · 6.89 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
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Configure and build the project using CMake.
.DESCRIPTION
Replacement for the old `compile.ps1` script. Configure and build the project
using CMake. It will remove previous build cache if present, run
`cmake -S . -B build`, then build `cmake --build build --config Release` and
optionally run `cmake --install` (best-effort).
.USAGE
./compilation_justcompile.ps1 -- -DULTRALIGHT_SDK_ROOT="/path/to/sdk"
#>
[CmdletBinding()]
param(
[Parameter(ValueFromRemainingArguments=$true)]
[string[]]$RemainingArgs = @()
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
if ($RemainingArgs) { $RemainingArgs = $RemainingArgs | ForEach-Object { $_.Trim('"') } }
if ($RemainingArgs -and ($RemainingArgs.Count -eq 1) -and ($RemainingArgs[0] -eq 'System.String')) { $RemainingArgs = @() }
Write-Host "Starting compilation_justcompile.ps1: PowerShell $($PSVersionTable.PSVersion) Host: $($Host.Name) PID: $($PID)" -ForegroundColor Magenta
Write-Host "Working directory: " (Get-Location).Path -ForegroundColor Magenta
Write-Host "Script args (remaining): $($RemainingArgs -join ' ')" -ForegroundColor Magenta
$scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Definition
Set-Location $scriptRoot
function Write-Log([string]$Level, [string]$Message) {
$ts = (Get-Date).ToString('HH:mm:ss')
switch ($Level) {
'INFO' { Write-Host "[$ts] $Message" -ForegroundColor Cyan }
'WARN' { Write-Warning "[$ts] $Message" }
'ERR' { Write-Error "[$ts] $Message" }
default { Write-Host "[$ts] $Message" }
}
}
function Invoke-Tool([string]$Exe, [string[]]$Arguments, [int]$TimeoutSeconds = 3600, [int]$StallTimeoutSeconds = 300) {
Write-Log INFO "Running: $Exe $($Arguments -join ' ')"
$outFile = [System.IO.Path]::GetTempFileName()
$errFile = [System.IO.Path]::GetTempFileName()
$proc = $null
try {
$proc = Start-Process -FilePath $Exe -ArgumentList $Arguments -NoNewWindow -RedirectStandardOutput $outFile -RedirectStandardError $errFile -PassThru -ErrorAction Stop
} catch {
Write-Log WARN "Start-Process failed to start: $_. Falling back to direct invocation."
$orig = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
& $Exe @Arguments 2>&1 | ForEach-Object { Write-Log INFO $_ }
return $LASTEXITCODE
} finally { $ErrorActionPreference = $orig }
}
$lastSize = 0
$lastOutputTime = Get-Date
$start = Get-Date
while (-not $proc.HasExited) {
Start-Sleep -Milliseconds 300
try {
$size = (Get-Item $outFile -ErrorAction SilentlyContinue).Length
if ($null -ne $size -and $size -ne $lastSize) {
$lastSize = $size
$lastOutputTime = Get-Date
Get-Content -LiteralPath $outFile -Tail 100 -ErrorAction SilentlyContinue | ForEach-Object { Write-Log INFO $_ }
}
else {
if ((Get-Date) -gt $lastOutputTime.AddSeconds(60)) {
Get-Content -LiteralPath $outFile -Tail 20 -ErrorAction SilentlyContinue | ForEach-Object { Write-Log INFO $_ }
}
}
} catch { }
if ((Get-Date) -gt $start.AddSeconds($TimeoutSeconds)) {
Write-Log ERR "Process timed out after $TimeoutSeconds seconds. Killing process..."
try { Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue } catch { }
break
}
if ((Get-Date) -gt $lastOutputTime.AddSeconds($StallTimeoutSeconds)) {
Write-Log WARN "No output for $StallTimeoutSeconds seconds. Killing process to avoid hang..."
try { Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue } catch { }
break
}
}
Start-Sleep -Milliseconds 200
try {
if (Test-Path $outFile) { Get-Content -LiteralPath $outFile -ErrorAction SilentlyContinue | ForEach-Object { Write-Log INFO $_ } }
if (Test-Path $errFile) { Get-Content -LiteralPath $errFile -ErrorAction SilentlyContinue | ForEach-Object { Write-Log ERR $_ } }
} catch { }
$exit = 1
try { $exit = $proc.ExitCode } catch { }
Remove-Item -LiteralPath $outFile,$errFile -ErrorAction SilentlyContinue
return $exit
}
if (-not (Get-Command cmake -ErrorAction SilentlyContinue)) {
Write-Error "cmake not found in PATH. Please install CMake and add it to PATH or run this script from an environment that can run cmake."
exit 2
}
$buildPath = Join-Path $scriptRoot 'build'
$cachePath = Join-Path $buildPath 'CMakeCache.txt'
if (Test-Path $cachePath) {
Write-Host "Removing previous CMake cache/build to avoid generator mismatch..." -ForegroundColor Yellow
try {
Remove-Item -LiteralPath $buildPath -Recurse -Force -ErrorAction Stop
} catch {
Write-Warning "Failed to remove build directory: $_"
}
}
# Configure
$cmakeArgs = @('-S', '.', '-B', 'build')
function Join-Args([string[]]$argsList) {
if (-not $argsList) { return @() }
$out = @()
for ($i = 0; $i -lt $argsList.Length; $i++) {
$arg = $argsList[$i].Trim('"')
if ($arg -match '^-D[^=]+$' -and ($i + 1) -lt $argsList.Length -and -not ($argsList[$i+1] -like '-*')) {
$next = $argsList[$i+1].Trim('"')
$out += ($arg + '=' + $next)
$i++
} else {
$out += $arg
}
}
return $out
}
if ($RemainingArgs) {
$normArgs = Join-Args $RemainingArgs
Write-Host "Normalized remaining args for CMake: $($normArgs -join ' ')" -ForegroundColor Yellow
$cmakeArgs += $normArgs
} else {
$normArgs = @()
}
# If ULTRALIGHT_SDK_ROOT isn't set via a -D flag, but an environment variable exists, use it by default
if (-not ($normArgs -match '^-DULTRALIGHT_SDK_ROOT=' ) -and ($env:ULTRALIGHT_SDK_ROOT)) {
$cmakeArgs += ('-DULTRALIGHT_SDK_ROOT=' + $env:ULTRALIGHT_SDK_ROOT)
Write-Host "Using ULTRALIGHT_SDK_ROOT from environment: $env:ULTRALIGHT_SDK_ROOT" -ForegroundColor Yellow
}
$code = Invoke-Tool 'cmake' $cmakeArgs
if ($code -ne 0) { exit $code }
# Build
$buildArgs = @('--build', 'build', '--config', 'Release')
# Add parallel build args by default unless the user passed explicit flags
$hasParallel = $false
foreach ($a in $normArgs) { if ($a -match '^-j' -or $a -match '^/m' -or $a -eq '--' ) { $hasParallel = $true; break } }
$procs = [Environment]::ProcessorCount
if (-not $hasParallel) {
if ($IsWindows) { $buildArgs += '--'; $buildArgs += ("/m:$procs") }
else { $buildArgs += '--'; $buildArgs += ("-j $procs") }
}
$code = Invoke-Tool 'cmake' $buildArgs
if ($code -ne 0) { exit $code }
# Optional install (best-effort)
try {
$code = Invoke-Tool 'cmake' @('--install', 'build', '--config', 'Release')
if ($code -ne 0) {
Write-Warning "cmake --install returned exit code $code"
}
} catch {
Write-Warning "cmake --install threw an exception: $_"
}
Write-Host "Build complete." -ForegroundColor Green