@@ -4,7 +4,7 @@ Compiles the Prefix runtime into a shared DLL, links the interpreter EXE
44against that DLL's import library, and compiles each discovered extension
55against the same shared runtime.
66
7- Requires: run from a Developer Command Prompt for Visual Studio where cl .exe is on PATH.
7+ Requires: clang .exe on PATH targeting x64 .
88Usage (from Prefix folder):
99 powershell -ExecutionPolicy Bypass -File .\build.ps1
1010#>
@@ -30,9 +30,22 @@ $buildDir = Join-Path $env:TEMP ("prefix-build-$stamp")
3030New-Item - ItemType Directory - Path $buildDir - Force | Out-Null
3131Write-Host " Build dir: $buildDir "
3232
33- $cl = Get-Command cl.exe - ErrorAction SilentlyContinue
34- if (-not $cl ) {
35- Write-Error " cl.exe not found. Run this script from a Developer Command Prompt for Visual Studio."
33+ $clang = Get-Command clang.exe - ErrorAction SilentlyContinue
34+ if (-not $clang ) {
35+ Write-Error " clang.exe not found on PATH."
36+ Remove-Item - Recurse - Force $buildDir - ErrorAction SilentlyContinue
37+ exit 1
38+ }
39+
40+ $clangTarget = (& clang.exe - dumpmachine 2> $null | Select-Object - First 1 )
41+ if (-not $clangTarget ) {
42+ Write-Error " Unable to determine clang.exe target triple."
43+ Remove-Item - Recurse - Force $buildDir - ErrorAction SilentlyContinue
44+ exit 1
45+ }
46+
47+ if ($clangTarget -notmatch ' ^x86_64-' ) {
48+ Write-Error " clang.exe must target baseline x64. Found target '$clangTarget '."
3649 Remove-Item - Recurse - Force $buildDir - ErrorAction SilentlyContinue
3750 exit 1
3851}
@@ -74,22 +87,53 @@ if ($platform::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Linux))
7487
7588Push-Location $buildDir
7689try {
77- $runtimeArgs = @ (
78- " /std:c17" , " /Gd" , " /O2" , " /Gy" , " /GF" , " /GL" , " /W4" , " /WX" , " /MP" , " /nologo" ,
90+ $isWindows = $platform ::IsOSPlatform([System.Runtime.InteropServices.OSPlatform ]::Windows)
91+
92+ # Use a single release profile for every artifact: portable baseline x64,
93+ # full-program optimization, and link-time dead stripping/folding.
94+ $clangArgs = @ (
95+ " --driver-mode=cl" ,
96+ " /clang:-march=x86-64" ,
97+ " /clang:-fuse-ld=lld" ,
98+ " /clang:-flto=full" ,
99+ " /clang:-ffunction-sections" ,
100+ " /clang:-fdata-sections"
101+ )
102+ $releaseCompileArgs = @ (
103+ " /std:c17" , " /Gd" , " /O2" , " /Ot" , " /Oi" , " /Ob2" , " /Gy" , " /Gw" , " /GF" , " /W4" , " /WX" , " /nologo"
104+ )
105+ # Only pass the `--gc-sections` linker option on non-Windows platforms;
106+ # on Windows we use MSVC-style linker options (/OPT:REF /OPT:ICF).
107+ if (-not $isWindows ) {
108+ $clangArgs += " /clang:-Wl,--gc-sections"
109+ }
110+
111+ # Libraries that extensions may need on Windows. Link via the build
112+ # system instead of source-level linker pragmas in the extension code.
113+ $linkLibs = @ ()
114+ if ($isWindows ) {
115+ $linkLibs = @ (" ole32.lib" , " ws2_32.lib" , " winhttp.lib" , " user32.lib" , " gdi32.lib" )
116+ $clangArgs += " /clang:-Wno-deprecated-declarations"
117+ }
118+
119+ $runtimeArgs = @ ($clangArgs + $releaseCompileArgs + @ (
79120 " /LD" , " /I$src " ,
80121 " /Fe:$runtimeDllName "
81- )
122+ ))
82123 $runtimeArgs += $runtimeSources
83- $runtimeArgs += @ (
84- " /link" ,
85- " /DEF:$runtimeDef " ,
86- " /IMPLIB:$runtimeLibName "
87- )
124+ $runtimeLinkFlags = @ (" /link" , " /DEF:$runtimeDef " , " /IMPLIB:$runtimeLibName " )
125+ if ($isWindows ) {
126+ $runtimeLinkFlags += " /OPT:REF"
127+ $runtimeLinkFlags += " /OPT:ICF"
128+ } else {
129+ $runtimeLinkFlags += " /clang:-Wl,--gc-sections"
130+ }
131+ $runtimeArgs += $runtimeLinkFlags
88132
89- Write-Host " Invoking: cl .exe $ ( $runtimeArgs -join ' ' ) "
90- & cl .exe @runtimeArgs
133+ Write-Host " Invoking: clang .exe $ ( $runtimeArgs -join ' ' ) "
134+ & clang .exe @runtimeArgs
91135 if ($LASTEXITCODE -ne 0 ) {
92- throw " cl .exe returned exit code $LASTEXITCODE while building shared runtime"
136+ throw " clang .exe returned exit code $LASTEXITCODE while building shared runtime"
93137 }
94138
95139 $runtimeDllPath = Join-Path $buildDir $runtimeDllName
@@ -111,18 +155,26 @@ try {
111155 Write-Host " Copied runtime DLL to: $runtimeDllDest "
112156 Write-Host " Copied runtime import library to: $runtimeLibDest "
113157
114- $exeArgs = @ (
115- " /std:c17" , " /Gd" , " /O2" , " /Gy" , " /GF" , " /GL" , " /W4" , " /WX" , " /MP" , " /nologo" ,
158+ $exeArgs = @ ($clangArgs + $releaseCompileArgs + @ (
116159 " /I$src " ,
117160 " /Fe:prefix.exe" ,
118161 $mainSource ,
119162 $runtimeLibPath
120- )
163+ ))
164+ $exeLinkFlags = @ ()
165+ if ($isWindows ) {
166+ $exeLinkFlags += " /link"
167+ $exeLinkFlags += " /OPT:REF"
168+ $exeLinkFlags += " /OPT:ICF"
169+ } else {
170+ $exeLinkFlags += " /clang:-Wl,--gc-sections"
171+ }
172+ $exeArgs += $exeLinkFlags
121173
122- Write-Host " Invoking: cl .exe $ ( $exeArgs -join ' ' ) "
123- & cl .exe @exeArgs
174+ Write-Host " Invoking: clang .exe $ ( $exeArgs -join ' ' ) "
175+ & clang .exe @exeArgs
124176 if ($LASTEXITCODE -ne 0 ) {
125- throw " cl .exe returned exit code $LASTEXITCODE while building interpreter"
177+ throw " clang .exe returned exit code $LASTEXITCODE while building interpreter"
126178 }
127179
128180 $outExe = Join-Path $buildDir " prefix.exe"
@@ -157,18 +209,29 @@ try {
157209 New-Item - ItemType Directory - Path $extBuildDir - Force | Out-Null
158210 Push-Location $extBuildDir
159211 try {
160- $extArgs = @ (
161- " /std:c17 " , " /Gd " , " /O2 " , " /W4 " , " /WX " , " /nologo " , " /LD " , " /LTCG " ,
212+ $extArgs = @ ($clangArgs + $releaseCompileArgs + @ (
213+ " /LD " ,
162214 " /I$src " ,
163215 " /Fe:$extOutName " ,
164- $extSourcePath ,
165- $runtimeLibPath
166- )
216+ $extSourcePath
217+ ))
218+ # Add OS-specific libraries required by some extensions.
219+ if ($linkLibs.Count -gt 0 ) { $extArgs += $linkLibs }
220+ $extArgs += $runtimeLibPath
221+ $extLinkFlags = @ ()
222+ if ($isWindows ) {
223+ $extLinkFlags += " /link"
224+ $extLinkFlags += " /OPT:REF"
225+ $extLinkFlags += " /OPT:ICF"
226+ } else {
227+ $extLinkFlags += " /clang:-Wl,--gc-sections"
228+ }
229+ $extArgs += $extLinkFlags
167230
168- Write-Host " Invoking: cl .exe $ ( $extArgs -join ' ' ) "
169- & cl .exe @extArgs
231+ Write-Host " Invoking: clang .exe $ ( $extArgs -join ' ' ) "
232+ & clang .exe @extArgs
170233 if ($LASTEXITCODE -ne 0 ) {
171- throw " cl .exe returned exit code $LASTEXITCODE while building extension '$extSourcePath '"
234+ throw " clang .exe returned exit code $LASTEXITCODE while building extension '$extSourcePath '"
172235 }
173236
174237 $extOutPath = Join-Path $extBuildDir $extOutName
0 commit comments