-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ps1
More file actions
259 lines (228 loc) · 9.05 KB
/
build.ps1
File metadata and controls
259 lines (228 loc) · 9.05 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
<#
build.ps1
Compiles the Prefix runtime into a shared DLL, links the interpreter EXE
against that DLL's import library, and compiles each discovered extension
against the same shared runtime.
Requires: clang.exe on PATH targeting x64.
Usage (from Prefix folder):
powershell -ExecutionPolicy Bypass -File .\build.ps1
#>
$script = Split-Path -Parent $MyInvocation.MyCommand.Definition
$src = Join-Path $script "src"
$runtimeDef = Join-Path $src "prefix_runtime.def"
$runtimeDllName = "prefix_runtime.dll"
$runtimeLibName = "prefix_runtime.lib"
$runtimePdbName = "prefix_runtime.pdb"
$runtimeDllDest = Join-Path $script $runtimeDllName
$runtimeLibDest = Join-Path $script $runtimeLibName
$runtimePdbDest = Join-Path $script $runtimePdbName
$extRoots = @(
(Join-Path $script "ext"),
(Join-Path $script "lib"),
(Join-Path $script "tests")
)
Write-Host "Preparing temp build directory under`$env:TEMP..."
$stamp = Get-Date -Format "yyyyMMdd-HHmmss"
$buildDir = Join-Path $env:TEMP ("prefix-build-$stamp")
New-Item -ItemType Directory -Path $buildDir -Force | Out-Null
Write-Host "Build dir: $buildDir"
$clang = Get-Command clang.exe -ErrorAction SilentlyContinue
if (-not $clang) {
Write-Error "clang.exe not found on PATH."
Remove-Item -Recurse -Force $buildDir -ErrorAction SilentlyContinue
exit 1
}
$clangTarget = (& clang.exe -dumpmachine 2>$null | Select-Object -First 1)
if (-not $clangTarget) {
Write-Error "Unable to determine clang.exe target triple."
Remove-Item -Recurse -Force $buildDir -ErrorAction SilentlyContinue
exit 1
}
if ($clangTarget -notmatch '^x86_64-') {
Write-Error "clang.exe 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 (-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
}
$platform = [System.Runtime.InteropServices.RuntimeInformation]
$extSuffix = ".dll"
if ($platform::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Linux)) {
$extSuffix = ".so"
} elseif ($platform::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::OSX)) {
$extSuffix = ".dylib"
}
Push-Location $buildDir
try {
$runningOnWindows = $platform::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)
# Use a single release profile for every artifact: portable baseline x64,
# full-program optimization, and link-time dead stripping/folding.
$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"
)
# Only pass the `--gc-sections` linker option on non-Windows platforms;
# on Windows we use MSVC-style linker options (/OPT:REF /OPT:ICF).
if (-not $runningOnWindows) {
$clangArgs += "/clang:-Wl,--gc-sections"
}
# Libraries that extensions may need on Windows. Link via the build
# system instead of source-level linker pragmas in the extension code.
$linkLibs = @()
if ($runningOnWindows) {
$linkLibs = @("ole32.lib", "ws2_32.lib", "winhttp.lib", "user32.lib", "gdi32.lib")
$clangArgs += "/clang:-Wno-deprecated-declarations"
}
$runtimeArgs = @($clangArgs + $releaseCompileArgs + @(
"/LD", "/I$src",
"/Fe:$runtimeDllName"
))
$runtimeArgs += $runtimeSources
$runtimeLinkFlags = @("/link", "/DEF:$runtimeDef", "/IMPLIB:$runtimeLibName")
if ($runningOnWindows) {
$runtimeLinkFlags += "/OPT:REF"
$runtimeLinkFlags += "/OPT:ICF"
} else {
$runtimeLinkFlags += "/clang:-Wl,--gc-sections"
}
$runtimeArgs += $runtimeLinkFlags
Write-Host "Invoking: clang.exe $($runtimeArgs -join ' ')"
& clang.exe @runtimeArgs
if ($LASTEXITCODE -ne 0) {
throw "clang.exe returned exit code $LASTEXITCODE while building shared runtime"
}
$runtimeDllPath = Join-Path $buildDir $runtimeDllName
$runtimeLibPath = Join-Path $buildDir $runtimeLibName
$runtimePdbPath = Join-Path $buildDir $runtimePdbName
if (-not (Test-Path $runtimeDllPath)) {
throw "Expected runtime DLL not found: $runtimeDllPath"
}
if (-not (Test-Path $runtimeLibPath)) {
throw "Expected runtime import library not found: $runtimeLibPath"
}
Copy-Item -Path $runtimeDllPath -Destination $runtimeDllDest -Force
Copy-Item -Path $runtimeLibPath -Destination $runtimeLibDest -Force
if (Test-Path $runtimePdbPath) {
Copy-Item -Path $runtimePdbPath -Destination $runtimePdbDest -Force
}
Write-Host "Copied runtime DLL to: $runtimeDllDest"
Write-Host "Copied runtime import library to: $runtimeLibDest"
$exeArgs = @($clangArgs + $releaseCompileArgs + @(
"/I$src",
"/Fe:prefix.exe",
$mainSource,
$runtimeLibPath
))
$exeLinkFlags = @()
if ($runningOnWindows) {
$exeLinkFlags += "/link"
$exeLinkFlags += "/OPT:REF"
$exeLinkFlags += "/OPT:ICF"
} else {
$exeLinkFlags += "/clang:-Wl,--gc-sections"
}
$exeArgs += $exeLinkFlags
Write-Host "Invoking: clang.exe $($exeArgs -join ' ')"
& clang.exe @exeArgs
if ($LASTEXITCODE -ne 0) {
throw "clang.exe returned exit code $LASTEXITCODE while building interpreter"
}
$outExe = Join-Path $buildDir "prefix.exe"
if (-not (Test-Path $outExe)) {
throw "Expected output EXE not found: $outExe"
}
$exeDest = Join-Path $script "prefix.exe"
Copy-Item -Path $outExe -Destination $exeDest -Force
Write-Host "Copied EXE to: $exeDest"
$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-" + [guid]::NewGuid().ToString("N"))
New-Item -ItemType Directory -Path $extBuildDir -Force | Out-Null
Push-Location $extBuildDir
try {
$extArgs = @($clangArgs + $releaseCompileArgs + @(
"/LD",
"/I$src",
"/Fe:$extOutName",
$extSourcePath
))
# Add OS-specific libraries required by some extensions.
if ($linkLibs.Count -gt 0) { $extArgs += $linkLibs }
$extArgs += $runtimeLibPath
$extLinkFlags = @()
if ($runningOnWindows) {
$extLinkFlags += "/link"
$extLinkFlags += "/OPT:REF"
$extLinkFlags += "/OPT:ICF"
} else {
$extLinkFlags += "/clang:-Wl,--gc-sections"
}
$extArgs += $extLinkFlags
Write-Host "Invoking: clang.exe $($extArgs -join ' ')"
& clang.exe @extArgs
if ($LASTEXITCODE -ne 0) {
throw "clang.exe 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 'prefix.exe'), $runtimeDllDest, $runtimeLibDest"
exit 0