-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ps1
More file actions
196 lines (170 loc) · 6.71 KB
/
build.ps1
File metadata and controls
196 lines (170 loc) · 6.71 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
<#
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: run from a Developer Command Prompt for Visual Studio where cl.exe is on PATH.
Usage (from Prefix-C 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"
$cl = Get-Command cl.exe -ErrorAction SilentlyContinue
if (-not $cl) {
Write-Error "cl.exe not found. Run this script from a Developer Command Prompt for Visual Studio."
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 {
$runtimeArgs = @(
"/std:c17", "/Gd", "/O2", "/Gy", "/GF", "/GL", "/W4", "/WX", "/MP", "/nologo",
"/LD", "/I$src",
"/Fe:$runtimeDllName"
)
$runtimeArgs += $runtimeSources
$runtimeArgs += @(
"/link",
"/DEF:$runtimeDef",
"/IMPLIB:$runtimeLibName"
)
Write-Host "Invoking: cl.exe $($runtimeArgs -join ' ')"
& cl.exe @runtimeArgs
if ($LASTEXITCODE -ne 0) {
throw "cl.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 = @(
"/std:c17", "/Gd", "/O2", "/Gy", "/GF", "/GL", "/W4", "/WX", "/MP", "/nologo",
"/I$src",
"/Fe:prefix.exe",
$mainSource,
$runtimeLibPath
)
Write-Host "Invoking: cl.exe $($exeArgs -join ' ')"
& cl.exe @exeArgs
if ($LASTEXITCODE -ne 0) {
throw "cl.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 = @(
"/std:c17", "/Gd", "/O2", "/W4", "/WX", "/nologo", "/LD", "/LTCG",
"/I$src",
"/Fe:$extOutName",
$extSourcePath,
$runtimeLibPath
)
Write-Host "Invoking: cl.exe $($extArgs -join ' ')"
& cl.exe @extArgs
if ($LASTEXITCODE -ne 0) {
throw "cl.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