# pcbuild.ps1 - build a Power C program with the improved toolchain.
#
# powershell -NoProfile -File reference\powerc-re\tools\pcbuild.ps1 -Src a.c,b.c -Name prog [-Dir build] [-Run 'keys\n'] [-NoOpt]
#
# 1. compiles every source with the ORIGINAL Power C compiler running in VICE (copy of POWERc.d81)
# 2. runs pcopt.py on each object file (peephole optimizer; -NoOpt skips this)
# 3. links with the original linker, using the improved runtime objects from optlib\
# (faster multiply / divide, fixed unsigned >=), again on a copy of the disk
# 4. with -Run, loads the program in BASIC, runs it and types the given keys; the final
# screen is saved as
\run.png
# Prerequisite (once): python reference\powerc-re\tools\setup.py
# The original reference\POWERc.d81 is never written.
param([string[]]$Src, [string]$Name = "prog", [string]$Dir = "build", [string]$Run = "", [switch]$NoOpt,
[int]$Settle = 30)
$ErrorActionPreference = "Stop"
$here = $PSScriptRoot
$root = (Resolve-Path (Join-Path $here "..")).Path
$Src = @($Src | % { $_ -split "," } | % { (Resolve-Path $_).Path })
New-Item -ItemType Directory -Force -Path $Dir | Out-Null
$Dir = (Resolve-Path $Dir).Path
$obj = Join-Path $Dir "obj"; $opt = Join-Path $Dir "opt"
New-Item -ItemType Directory -Force -Path $obj, $opt | Out-Null
if (-not (Test-Path (Join-Path $root "optlib\cmulti.obj"))) { throw "run tools\setup.py first" }
& (Join-Path $here "pc-compile.ps1") -Src $Src -Out $obj | Where-Object { $_ -match "^(ok|FAIL)" }
$names = $Src | % { [IO.Path]::GetFileNameWithoutExtension($_) }
foreach ($n in $names) { if (-not (Test-Path (Join-Path $obj "$n.o"))) { throw "$n.c did not compile - see work\last.png" } }
$final = @()
foreach ($n in $names) {
$in = Join-Path $obj "$n.o"
if ($NoOpt) { $final += $in; continue }
$out = Join-Path $opt "$n.o"
& python (Join-Path $here "pcopt.py") $in $out --lib (Join-Path $root "lib")
$final += $out
}
$lib = (Get-ChildItem (Join-Path $root "optlib") -Filter *.obj | % { $_.FullName }) -join ","
$p = @{ Obj = ($final -join ","); Name = $Name; Dir = $Dir; Settle = $Settle }
if (-not $NoOpt) { $p.LibObj = $lib }
if ($Run -ne "") { $p.Keys = $Run }
& (Join-Path $here "pc-run.ps1") @p | Where-Object { $_ -match "linked|FAILED" }