-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ps1
More file actions
344 lines (312 loc) · 11.2 KB
/
Copy pathbuild.ps1
File metadata and controls
344 lines (312 loc) · 11.2 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
<#
.SYNOPSIS
Build the native self-hosted MiniLang compiler.
.DESCRIPTION
Uses an existing native compiler when available, otherwise bootstraps from the
sibling Python compiler. Output is staged safely, smoke-tested and then moved
to the requested destination. The object pipeline keeps peak memory bounded.
#>
param(
[string]$Compiler = "",
[string]$Output = "",
[string]$Python = "",
[switch]$NoReplace,
[switch]$SkipSmoke,
[switch]$KeepObjects,
[switch]$NoBootstrapProbe
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$Root = Split-Path -Parent $PSCommandPath
$script:CompilerIsPython = $false
$script:PythonExe = ""
$script:PythonPrefixArgs = @()
$script:SelectedCompilerExitCode = 1
$script:StageDir = ""
function Resolve-BuildPath {
# Resolve caller-supplied paths relative to this repository.
param([string]$Path)
if ([System.IO.Path]::IsPathRooted($Path)) {
return [System.IO.Path]::GetFullPath($Path)
}
return [System.IO.Path]::GetFullPath((Join-Path $Root $Path))
}
function Test-PathContainedBy {
# Require a complete child path component; raw StartsWith checks also accept
# siblings such as "MiniLangCompilerML-backup".
param(
[string]$Path,
[string]$Parent
)
$fullPath = [System.IO.Path]::GetFullPath($Path).TrimEnd(
[System.IO.Path]::DirectorySeparatorChar,
[System.IO.Path]::AltDirectorySeparatorChar)
$fullParent = [System.IO.Path]::GetFullPath($Parent).TrimEnd(
[System.IO.Path]::DirectorySeparatorChar,
[System.IO.Path]::AltDirectorySeparatorChar)
if ([string]::Equals($fullPath, $fullParent, [System.StringComparison]::OrdinalIgnoreCase)) {
return $false
}
return $fullPath.StartsWith(
$fullParent + [System.IO.Path]::DirectorySeparatorChar,
[System.StringComparison]::OrdinalIgnoreCase)
}
function Resolve-CommandOrFile {
# Accept either an explicit file or a command discoverable through PATH.
param(
[string]$Value,
[string]$Label
)
if (Test-Path -LiteralPath $Value -PathType Leaf) {
return [System.IO.Path]::GetFullPath($Value)
}
$command = Get-Command $Value -ErrorAction SilentlyContinue | Select-Object -First 1
if ($null -ne $command -and -not [string]::IsNullOrWhiteSpace($command.Path)) {
return $command.Path
}
throw "$Label not found: $Value"
}
function Invoke-SelectedCompiler {
# Normalize invocation and exit-code capture for native and Python compilers.
param(
[string]$CompilerPath,
[string[]]$Arguments
)
if ($script:CompilerIsPython) {
$prefixArgs = @($script:PythonPrefixArgs)
& $script:PythonExe @prefixArgs $CompilerPath @Arguments
} else {
& $CompilerPath @Arguments
}
$script:SelectedCompilerExitCode = [int]$LASTEXITCODE
}
function Remove-CompilerObjects {
# Remove only the validated object directory associated with one output.
param([string]$ExePath)
if ($KeepObjects) { return }
$objDir = Get-CompilerObjectDir $ExePath
if ($objDir -and (Test-Path -LiteralPath $objDir)) {
Remove-Item -LiteralPath $objDir -Recurse -Force
}
}
function Get-CompilerObjectDir {
# Only the private stage directory belongs to this invocation. Output-adjacent
# directories selected by a caller must never become recursive-delete targets.
param([string]$ExePath)
$outDir = Split-Path -Parent $ExePath
$stem = [System.IO.Path]::GetFileNameWithoutExtension($ExePath)
$objDir = Join-Path (Join-Path $outDir "tmp") $stem
$full = [System.IO.Path]::GetFullPath($objDir)
if (-not [string]::IsNullOrWhiteSpace($script:StageDir) -and
(Test-PathContainedBy $full $script:StageDir)) {
return $full
}
return ""
}
function Invoke-LinkFallback {
# Reuse completed object emission when only the fresh linker process failed.
param(
[string]$CompilerPath,
[string]$EntryPath,
[string]$StageExePath
)
$objDir = Get-CompilerObjectDir $StageExePath
if ($objDir -eq "" -or -not (Test-Path -LiteralPath $objDir)) {
return $false
}
$mloCount = @(Get-ChildItem -LiteralPath $objDir -Filter *.mlo -File -ErrorAction SilentlyContinue).Count
if ($mloCount -le 0) {
return $false
}
$supportObjects = @(Get-ChildItem -LiteralPath $objDir -Filter "*_support.mlo" -File -ErrorAction SilentlyContinue)
if ($supportObjects.Count -ne 1) {
return $false
}
Write-Host ""
Write-Host "Retrying link from existing object directory..."
Write-Host "Object dir: $objDir"
Write-Host "Objects: $mloCount"
$linkArgs = @($EntryPath, $StageExePath, "--link-obj-dir", $objDir, "--subsystem", "console", "--gc-limit", "1536m")
Invoke-SelectedCompiler -CompilerPath $CompilerPath -Arguments $linkArgs
$linkExit = $script:SelectedCompilerExitCode
if ($linkExit -ne 0) {
throw "Fallback link failed with exit code $linkExit"
}
return $true
}
if ($Compiler -eq "") {
$nativeCompiler = Join-Path $Root "build\mlc_win64.exe"
$pythonBootstrap = Join-Path (Split-Path -Parent $Root) "MiniLangCompilerPy\mlc_win64.py"
if (Test-Path -LiteralPath $nativeCompiler -PathType Leaf) {
$Compiler = $nativeCompiler
} elseif (Test-Path -LiteralPath $pythonBootstrap -PathType Leaf) {
$Compiler = $pythonBootstrap
} else {
throw "No bootstrap compiler found. Pass -Compiler PATH to a MiniLang compiler executable or to mlc_win64.py."
}
}
if ($Output -eq "") {
$Output = Join-Path $Root "build\mlc_win64.exe"
}
$Compiler = Resolve-BuildPath $Compiler
$FinalOutput = Resolve-BuildPath $Output
if (-not (Test-Path -LiteralPath $Compiler)) {
throw "Compiler not found: $Compiler"
}
$script:CompilerIsPython = [System.IO.Path]::GetExtension($Compiler) -ieq ".py"
if ($script:CompilerIsPython) {
if (-not [string]::IsNullOrWhiteSpace($Python)) {
$script:PythonExe = Resolve-CommandOrFile $Python "Python interpreter"
if ([System.IO.Path]::GetFileNameWithoutExtension($script:PythonExe) -ieq "py") {
$script:PythonPrefixArgs = @("-3")
}
} else {
$pyLauncher = Get-Command "py" -ErrorAction SilentlyContinue | Select-Object -First 1
if ($null -ne $pyLauncher) {
$script:PythonExe = $pyLauncher.Path
$script:PythonPrefixArgs = @("-3")
} else {
$script:PythonExe = Resolve-CommandOrFile "python" "Python interpreter"
}
}
}
$outDir = Split-Path -Parent $FinalOutput
New-Item -ItemType Directory -Force -Path $outDir | Out-Null
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($FinalOutput)
$stageToken = ("" + $PID + "." + [System.Guid]::NewGuid().ToString("N"))
$stageDir = Join-Path ([System.IO.Path]::GetTempPath()) ("mlc_build_" + $stageToken)
$script:StageDir = $stageDir
$stageOutput = Join-Path $stageDir ($baseName + ".next.exe")
$replaceFinal = -not $NoReplace
if ($NoReplace) {
if ([string]::Equals($FinalOutput, $Compiler, [System.StringComparison]::OrdinalIgnoreCase)) {
$FinalOutput = Join-Path $outDir ($baseName + ".next.exe")
}
}
New-Item -ItemType Directory -Force -Path $stageDir | Out-Null
$smokeTimer = $null
try {
if (Test-Path -LiteralPath $stageOutput) {
Remove-Item -LiteralPath $stageOutput -Force
}
$entry = Join-Path $Root "mlc_win64.ml"
$buildArgs = @(
$entry,
$stageOutput,
"-I", $Root,
# Reserving address space is cheap on x64. The larger compiler heap lets the
# resulting compiler handle very large monolithic targets such as MiniQuake;
# committed memory starts at 512 MiB, grows on demand and can be trimmed to
# 16 MiB after GC. This keeps the waiting object-pipeline coordinator small
# without adding commit churn to the allocation-heavy emitter startup.
"--heap-reserve", "8g",
"--heap-commit", "512m",
"--heap-shrink",
"--heap-shrink-min", "16m",
"--gc-limit", "1536m",
"--object-pipeline"
)
$enableBootstrapProbe = -not $NoBootstrapProbe -and -not $script:CompilerIsPython
if ($enableBootstrapProbe) {
$buildArgs += "--mem-probe"
}
Write-Host "Compiler: $Compiler"
Write-Host "Entry: $entry"
Write-Host "Stage: $stageOutput"
if ($replaceFinal) {
Write-Host "Output: $FinalOutput"
} else {
Write-Host "Output: $FinalOutput"
}
if ($enableBootstrapProbe) {
Write-Host "Bootstrap: mem-probe enabled"
} elseif (-not $NoBootstrapProbe -and $script:CompilerIsPython) {
Write-Host "Bootstrap: mem-probe omitted for Python compiler"
}
$buildTimer = [System.Diagnostics.Stopwatch]::StartNew()
Invoke-SelectedCompiler -CompilerPath $Compiler -Arguments $buildArgs 2>&1 | ForEach-Object {
$line = "" + $_
if ($line -notmatch '^\[mem\]') {
Write-Host $line
}
}
$buildExit = $script:SelectedCompilerExitCode
if ($buildExit -ne 0) {
$linked = Invoke-LinkFallback $Compiler $entry $stageOutput
if (-not $linked) {
throw "Compiler build failed with exit code $buildExit"
}
}
$buildTimer.Stop()
if (-not (Test-Path -LiteralPath $stageOutput)) {
throw "Compiler build did not produce output: $stageOutput"
}
if (-not $SkipSmoke) {
$smokeSrc = Join-Path $stageDir "smoke.ml"
$smokeExe = Join-Path $stageDir "smoke.exe"
Set-Content -LiteralPath $smokeSrc -Encoding ASCII -Value @(
'print "hello"',
'x = 1',
'print "x=" + x'
)
if (Test-Path -LiteralPath $smokeExe) {
Remove-Item -LiteralPath $smokeExe -Force
}
$smokeTimer = [System.Diagnostics.Stopwatch]::StartNew()
& $stageOutput $smokeSrc $smokeExe "--heap-reserve" "4g" "--heap-commit" "256m" "--gc-limit" "128m"
$smokeCompileExit = $LASTEXITCODE
if ($smokeCompileExit -ne 0) {
throw "Smoke compile failed with exit code $smokeCompileExit"
}
& $smokeExe
$smokeRunExit = $LASTEXITCODE
$smokeTimer.Stop()
if ($smokeRunExit -ne 0) {
throw "Smoke executable failed with exit code $smokeRunExit"
}
Remove-CompilerObjects $smokeExe
Remove-Item -LiteralPath $smokeSrc -Force -ErrorAction SilentlyContinue
Remove-Item -LiteralPath $smokeExe -Force -ErrorAction SilentlyContinue
}
if ($replaceFinal) {
$backup = Join-Path ([System.IO.Path]::GetTempPath()) ("mlc_win64_previous_" + $PID + ".exe")
if (Test-Path -LiteralPath $backup) {
Remove-Item -LiteralPath $backup -Force
}
try {
if (Test-Path -LiteralPath $FinalOutput) {
Move-Item -LiteralPath $FinalOutput -Destination $backup -Force
}
Move-Item -LiteralPath $stageOutput -Destination $FinalOutput -Force
Remove-Item -LiteralPath $backup -Force -ErrorAction SilentlyContinue
} catch {
if ((Test-Path -LiteralPath $backup) -and -not (Test-Path -LiteralPath $FinalOutput)) {
Move-Item -LiteralPath $backup -Destination $FinalOutput -Force
}
throw
}
} else {
if (Test-Path -LiteralPath $FinalOutput) {
Remove-Item -LiteralPath $FinalOutput -Force
}
Move-Item -LiteralPath $stageOutput -Destination $FinalOutput -Force
}
Remove-CompilerObjects $stageOutput
} finally {
# The GUID-named stage is owned exclusively by this invocation. Clean it on
# compile, smoke, publication and success paths alike unless the caller
# explicitly requested the retained MLO files for investigation.
if (-not $KeepObjects -and (Test-Path -LiteralPath $stageDir)) {
Remove-Item -LiteralPath $stageDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
Write-Host ""
Write-Host ("Build complete in {0:n3}s." -f $buildTimer.Elapsed.TotalSeconds)
if (-not $SkipSmoke) {
Write-Host ("Smoke test complete in {0:n3}s." -f $smokeTimer.Elapsed.TotalSeconds)
}
if ($replaceFinal) {
Write-Host "Wrote: $FinalOutput"
} else {
Write-Host "Wrote: $FinalOutput"
}