11<#
22build.ps1
3- Compiles the interpreter in a temporary directory and copies the resulting EXE
4- back into this repo. Also discovers extension C sources under ext/ and lib/
5- and compiles each one into a dynamic library next to its source file .
3+ Compiles the Prefix runtime into a shared DLL, links the interpreter EXE
4+ against that DLL's import library, and compiles each discovered extension
5+ against the same shared runtime .
66
77Requires: run from a Developer Command Prompt for Visual Studio where cl.exe is on PATH.
88Usage (from Prefix-C folder):
@@ -11,6 +11,13 @@ Usage (from Prefix-C folder):
1111
1212$script = Split-Path - Parent $MyInvocation.MyCommand.Definition
1313$src = Join-Path $script " src"
14+ $runtimeDef = Join-Path $src " prefix_runtime.def"
15+ $runtimeDllName = " prefix_runtime.dll"
16+ $runtimeLibName = " prefix_runtime.lib"
17+ $runtimePdbName = " prefix_runtime.pdb"
18+ $runtimeDllDest = Join-Path $script $runtimeDllName
19+ $runtimeLibDest = Join-Path $script $runtimeLibName
20+ $runtimePdbDest = Join-Path $script $runtimePdbName
1421$extRoots = @ (
1522 (Join-Path $script " ext" ),
1623 (Join-Path $script " lib" ),
@@ -37,6 +44,26 @@ if ($cFiles.Count -eq 0) {
3744 exit 1
3845}
3946
47+ if (-not (Test-Path $runtimeDef )) {
48+ Write-Error " Runtime export definition not found: $runtimeDef "
49+ Remove-Item - Recurse - Force $buildDir - ErrorAction SilentlyContinue
50+ exit 1
51+ }
52+
53+ $mainSource = Join-Path $src " main.c"
54+ if (-not (Test-Path $mainSource )) {
55+ Write-Error " Main source not found: $mainSource "
56+ Remove-Item - Recurse - Force $buildDir - ErrorAction SilentlyContinue
57+ exit 1
58+ }
59+
60+ $runtimeSources = @ ($cFiles | Where-Object { $_ -ne $mainSource })
61+ if ($runtimeSources.Count -eq 0 ) {
62+ Write-Error " No runtime sources found after excluding '$mainSource '"
63+ Remove-Item - Recurse - Force $buildDir - ErrorAction SilentlyContinue
64+ exit 1
65+ }
66+
4067$platform = [System.Runtime.InteropServices.RuntimeInformation ]
4168$extSuffix = " .dll"
4269if ($platform ::IsOSPlatform([System.Runtime.InteropServices.OSPlatform ]::Linux)) {
@@ -47,11 +74,50 @@ if ($platform::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Linux))
4774
4875Push-Location $buildDir
4976try {
77+ $runtimeArgs = @ (
78+ " /std:c17" , " /Gd" , " /O2" , " /Gy" , " /GF" , " /GL" , " /W4" , " /WX" , " /MP" , " /nologo" ,
79+ " /LD" , " /I$src " ,
80+ " /Fe:$runtimeDllName "
81+ )
82+ $runtimeArgs += $runtimeSources
83+ $runtimeArgs += @ (
84+ " /link" ,
85+ " /DEF:$runtimeDef " ,
86+ " /IMPLIB:$runtimeLibName "
87+ )
88+
89+ Write-Host " Invoking: cl.exe $ ( $runtimeArgs -join ' ' ) "
90+ & cl.exe @runtimeArgs
91+ if ($LASTEXITCODE -ne 0 ) {
92+ throw " cl.exe returned exit code $LASTEXITCODE while building shared runtime"
93+ }
94+
95+ $runtimeDllPath = Join-Path $buildDir $runtimeDllName
96+ $runtimeLibPath = Join-Path $buildDir $runtimeLibName
97+ $runtimePdbPath = Join-Path $buildDir $runtimePdbName
98+
99+ if (-not (Test-Path $runtimeDllPath )) {
100+ throw " Expected runtime DLL not found: $runtimeDllPath "
101+ }
102+ if (-not (Test-Path $runtimeLibPath )) {
103+ throw " Expected runtime import library not found: $runtimeLibPath "
104+ }
105+
106+ Copy-Item - Path $runtimeDllPath - Destination $runtimeDllDest - Force
107+ Copy-Item - Path $runtimeLibPath - Destination $runtimeLibDest - Force
108+ if (Test-Path $runtimePdbPath ) {
109+ Copy-Item - Path $runtimePdbPath - Destination $runtimePdbDest - Force
110+ }
111+ Write-Host " Copied runtime DLL to: $runtimeDllDest "
112+ Write-Host " Copied runtime import library to: $runtimeLibDest "
113+
50114 $exeArgs = @ (
51115 " /std:c17" , " /Gd" , " /O2" , " /Gy" , " /GF" , " /GL" , " /W4" , " /WX" , " /MP" , " /nologo" ,
52- " /Fe:prefix.exe"
116+ " /I$src " ,
117+ " /Fe:prefix.exe" ,
118+ $mainSource ,
119+ $runtimeLibPath
53120 )
54- $exeArgs += $cFiles
55121
56122 Write-Host " Invoking: cl.exe $ ( $exeArgs -join ' ' ) "
57123 & cl.exe @exeArgs
@@ -95,16 +161,10 @@ try {
95161 " /std:c17" , " /Gd" , " /O2" , " /W4" , " /WX" , " /nologo" , " /LD" , " /LTCG" ,
96162 " /I$src " ,
97163 " /Fe:$extOutName " ,
98- $extSourcePath
164+ $extSourcePath ,
165+ $runtimeLibPath
99166 )
100167
101- # Link the object files produced when building the main interpreter
102- # so extensions can resolve internal runtime symbols (value_* helpers).
103- $objFiles = Get-ChildItem - Path $buildDir - Filter * .obj - File - Recurse | ForEach-Object { $_.FullName }
104- if ($objFiles.Count -gt 0 ) {
105- $extArgs += $objFiles
106- }
107-
108168 Write-Host " Invoking: cl.exe $ ( $extArgs -join ' ' ) "
109169 & cl.exe @extArgs
110170 if ($LASTEXITCODE -ne 0 ) {
@@ -132,5 +192,5 @@ try {
132192 Remove-Item - Recurse - Force $buildDir - ErrorAction SilentlyContinue
133193}
134194
135- Write-Host " Build succeeded and exe copied to: $ ( Join-Path $script ' prefix.exe' ) "
195+ Write-Host " Build succeeded and artifacts copied to: $ ( Join-Path $script ' prefix.exe' ) , $runtimeDllDest , $runtimeLibDest "
136196exit 0
0 commit comments