-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.ps1
More file actions
312 lines (279 loc) · 10.3 KB
/
Copy pathbuild.ps1
File metadata and controls
312 lines (279 loc) · 10.3 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
<#
build.ps1
Compiles the Prefix runtime into a shared library, links the interpreter
against that library, and compiles each discovered extension against the
same shared runtime.
Requires: clang on PATH targeting x64.
Usage (from Prefix folder):
pwsh -File ./build.ps1
#>
$script = Split-Path -Parent $MyInvocation.MyCommand.Definition
$src = Join-Path $script "src"
$runtimeDef = Join-Path $src "prefix_runtime.def"
$extRoots = @(
(Join-Path $script "ext"),
(Join-Path $script "lib"),
(Join-Path $script "tests")
)
$runningOnWindows = -not $IsLinux -and -not $IsMacOS
# Platform-specific names
if ($runningOnWindows) {
$clangCmd = "clang.exe"
$runtimeDllName = "prefix_runtime.dll"
$runtimeLibName = "prefix_runtime.lib"
$runtimePdbName = "prefix_runtime.pdb"
$exeName = "prefix.exe"
} elseif ($IsMacOS) {
$clangCmd = "clang"
$runtimeDllName = "libprefix_runtime.dylib"
$runtimeLibName = ""
$runtimePdbName = ""
$exeName = "prefix"
} else {
$clangCmd = "clang"
$runtimeDllName = "libprefix_runtime.so"
$runtimeLibName = ""
$runtimePdbName = ""
$exeName = "prefix"
}
$runtimeDllDest = Join-Path $script $runtimeDllName
$runtimeLibDest = if ($runtimeLibName) { Join-Path $script $runtimeLibName } else { $null }
$runtimePdbDest = if ($runtimePdbName) { Join-Path $script $runtimePdbName } else { $null }
$extSuffix = ".dll"
if ($IsLinux) {
$extSuffix = ".so"
} elseif ($IsMacOS) {
$extSuffix = ".dylib"
}
Write-Host "Preparing temp build directory..."
$stamp = Get-Date -Format "yyyyMMdd-HHmmss"
if ($runningOnWindows) {
$buildDir = Join-Path $env:TEMP ("prefix-build-$stamp")
} else {
$buildDir = Join-Path "/tmp" ("prefix-build-$stamp")
}
New-Item -ItemType Directory -Path $buildDir -Force | Out-Null
Write-Host "Build dir: $buildDir"
$clang = Get-Command $clangCmd -ErrorAction SilentlyContinue
if (-not $clang) {
Write-Error "$clangCmd not found on PATH."
Remove-Item -Recurse -Force $buildDir -ErrorAction SilentlyContinue
exit 1
}
$clangTarget = (& $clangCmd -dumpmachine 2>$null | Select-Object -First 1)
if (-not $clangTarget) {
Write-Error "Unable to determine $clangCmd target triple."
Remove-Item -Recurse -Force $buildDir -ErrorAction SilentlyContinue
exit 1
}
if ($clangTarget -notmatch '^x86_64-') {
Write-Error "$clangCmd must target baseline x64. Found target '$clangTarget'."
Remove-Item -Recurse -Force $buildDir -ErrorAction SilentlyContinue
exit 1
}
$cFiles = Get-ChildItem -Path $src -Filter *.c -File -Recurse | ForEach-Object { $_.FullName }
if ($cFiles.Count -eq 0) {
Write-Error "No .c files found in '$src'"
Remove-Item -Recurse -Force $buildDir -ErrorAction SilentlyContinue
exit 1
}
if ($runningOnWindows -and -not (Test-Path $runtimeDef)) {
Write-Error "Runtime export definition not found: $runtimeDef"
Remove-Item -Recurse -Force $buildDir -ErrorAction SilentlyContinue
exit 1
}
$mainSource = Join-Path $src "main.c"
if (-not (Test-Path $mainSource)) {
Write-Error "Main source not found: $mainSource"
Remove-Item -Recurse -Force $buildDir -ErrorAction SilentlyContinue
exit 1
}
$runtimeSources = @($cFiles | Where-Object { $_ -ne $mainSource })
if ($runtimeSources.Count -eq 0) {
Write-Error "No runtime sources found after excluding '$mainSource'"
Remove-Item -Recurse -Force $buildDir -ErrorAction SilentlyContinue
exit 1
}
Push-Location $buildDir
try {
if ($runningOnWindows) {
# MSVC-compatible driver mode for Windows
$clangArgs = @(
"--driver-mode=cl",
"/clang:-march=x86-64",
"/clang:-fuse-ld=lld",
"/clang:-flto=full",
"/clang:-ffunction-sections",
"/clang:-fdata-sections"
)
$releaseCompileArgs = @(
"/std:c17", "/Gd", "/O2", "/Ot", "/Oi", "/Ob2", "/Gy", "/Gw", "/GF", "/W4", "/WX", "/nologo"
)
$linkLibs = @("ole32.lib", "ws2_32.lib", "winhttp.lib", "user32.lib", "gdi32.lib")
$clangArgs += "/clang:-Wno-deprecated-declarations"
} else {
# GCC-compatible flags for Linux/macOS
$clangArgs = @(
"-march=x86-64",
"-flto=full",
"-ffunction-sections",
"-fdata-sections",
"-fPIC"
)
$releaseCompileArgs = @(
"-std=c17", "-O2", "-Wall", "-Wextra", "-Werror",
"-D_GNU_SOURCE"
)
$linkLibs = @()
if ($IsLinux) {
$linkLibs = @("-ldl", "-lpthread", "-lm")
} elseif ($IsMacOS) {
$linkLibs = @("-ldl", "-lm")
}
}
# --- Build runtime shared library ---
if ($runningOnWindows) {
$runtimeArgs = @($clangArgs + $releaseCompileArgs + @(
"/LD", "/I$src",
"/Fe:$runtimeDllName"
))
$runtimeArgs += $runtimeSources
$runtimeLinkFlags = @("/link", "/DEF:$runtimeDef", "/IMPLIB:$runtimeLibName", "/OPT:REF", "/OPT:ICF")
$runtimeArgs += $runtimeLinkFlags
} else {
$runtimeArgs = @($clangArgs + $releaseCompileArgs + @(
"-shared",
"-I$src",
"-o", $runtimeDllName
))
$runtimeArgs += $runtimeSources
$runtimeArgs += @("-Wl,--gc-sections")
$runtimeArgs += $linkLibs
}
Write-Host "Invoking: $clangCmd $($runtimeArgs -join ' ')"
& $clangCmd @runtimeArgs
if ($LASTEXITCODE -ne 0) {
throw "$clangCmd returned exit code $LASTEXITCODE while building shared runtime"
}
$runtimeDllPath = Join-Path $buildDir $runtimeDllName
if (-not (Test-Path $runtimeDllPath)) {
throw "Expected runtime library not found: $runtimeDllPath"
}
Copy-Item -Path $runtimeDllPath -Destination $runtimeDllDest -Force
Write-Host "Copied runtime library to: $runtimeDllDest"
if ($runningOnWindows) {
$runtimeLibPath = Join-Path $buildDir $runtimeLibName
$runtimePdbPath = Join-Path $buildDir $runtimePdbName
if (-not (Test-Path $runtimeLibPath)) {
throw "Expected runtime import library not found: $runtimeLibPath"
}
Copy-Item -Path $runtimeLibPath -Destination $runtimeLibDest -Force
if (Test-Path $runtimePdbPath) {
Copy-Item -Path $runtimePdbPath -Destination $runtimePdbDest -Force
}
Write-Host "Copied runtime import library to: $runtimeLibDest"
}
# --- Build interpreter executable ---
if ($runningOnWindows) {
$runtimeLibPath = Join-Path $buildDir $runtimeLibName
$exeArgs = @($clangArgs + $releaseCompileArgs + @(
"/I$src",
"/Fe:$exeName",
$mainSource,
$runtimeLibPath
))
$exeLinkFlags = @("/link", "/OPT:REF", "/OPT:ICF")
$exeArgs += $exeLinkFlags
} else {
$exeArgs = @($clangArgs + $releaseCompileArgs + @(
"-I$src",
"-o", $exeName,
$mainSource,
"-L$buildDir",
"-lprefix_runtime",
"-Wl,-rpath,`$ORIGIN",
"-Wl,--gc-sections"
))
$exeArgs += $linkLibs
}
Write-Host "Invoking: $clangCmd $($exeArgs -join ' ')"
& $clangCmd @exeArgs
if ($LASTEXITCODE -ne 0) {
throw "$clangCmd returned exit code $LASTEXITCODE while building interpreter"
}
$outExe = Join-Path $buildDir $exeName
if (-not (Test-Path $outExe)) {
throw "Expected output executable not found: $outExe"
}
$exeDest = Join-Path $script $exeName
Copy-Item -Path $outExe -Destination $exeDest -Force
Write-Host "Copied executable to: $exeDest"
# --- Build extensions ---
$extSources = @()
foreach ($root in $extRoots) {
if (Test-Path $root) {
$extSources += Get-ChildItem -Path $root -Filter *.c -File -Recurse
}
}
if ($extSources.Count -eq 0) {
Write-Host "No extension C sources found under ext/, lib/"
} else {
Write-Host "Found $($extSources.Count) extension source file(s)."
}
foreach ($extSource in $extSources) {
$extSourcePath = $extSource.FullName
$extName = [System.IO.Path]::GetFileNameWithoutExtension($extSourcePath)
$extOutName = "$extName$extSuffix"
$extDest = Join-Path $extSource.DirectoryName $extOutName
$extBuildDir = Join-Path $buildDir "ext-$extName"
New-Item -ItemType Directory -Path $extBuildDir -Force | Out-Null
Push-Location $extBuildDir
try {
if ($runningOnWindows) {
$extArgs = @($clangArgs + $releaseCompileArgs + @(
"/LD",
"/I$src",
"/Fe:$extOutName",
$extSourcePath
))
if ($linkLibs.Count -gt 0) { $extArgs += $linkLibs }
$extArgs += (Join-Path $buildDir $runtimeLibName)
$extArgs += @("/link", "/OPT:REF", "/OPT:ICF")
} else {
$extArgs = @($clangArgs + $releaseCompileArgs + @(
"-shared",
"-I$src",
"-o", $extOutName,
$extSourcePath,
"-L$buildDir",
"-lprefix_runtime",
"-Wl,--gc-sections"
))
if ($linkLibs.Count -gt 0) { $extArgs += $linkLibs }
}
Write-Host "Invoking: $clangCmd $($extArgs -join ' ')"
& $clangCmd @extArgs
if ($LASTEXITCODE -ne 0) {
throw "$clangCmd returned exit code $LASTEXITCODE while building extension '$extSourcePath'"
}
$extOutPath = Join-Path $extBuildDir $extOutName
if (-not (Test-Path $extOutPath)) {
throw "Expected output extension not found: $extOutPath"
}
Copy-Item -Path $extOutPath -Destination $extDest -Force
Write-Host "Copied extension to: $extDest"
} finally {
Pop-Location
Remove-Item -Recurse -Force $extBuildDir -ErrorAction SilentlyContinue
}
}
} catch {
Write-Error "Build failed: $_"
exit 1
} finally {
Pop-Location
Write-Host "Cleaning up temp build dir: $buildDir"
Remove-Item -Recurse -Force $buildDir -ErrorAction SilentlyContinue
}
Write-Host "Build succeeded and artifacts copied to: $(Join-Path $script $exeName), $runtimeDllDest"
exit 0