|
| 1 | +#requires -Version 5.1 |
| 2 | +<# |
| 3 | +.COPYRIGHT |
| 4 | + Copyright 2013-2026 Alexander Peslyak |
| 5 | + Copyright 2026 CPUchain |
| 6 | + All rights reserved. |
| 7 | +
|
| 8 | + Redistribution and use in source and binary forms, with or without |
| 9 | + modification, are permitted. |
| 10 | +
|
| 11 | + THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 12 | + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 13 | + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 14 | + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 15 | + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 16 | + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 17 | + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 18 | + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 19 | + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 20 | + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 21 | + SUCH DAMAGE. |
| 22 | +
|
| 23 | +.SYNOPSIS |
| 24 | + Build yespower with the MSVC toolchain (port of the supplied GNU Makefile). |
| 25 | +
|
| 26 | +.DESCRIPTION |
| 27 | + Locates a Visual Studio / Build Tools installation with the C++ compiler |
| 28 | + using vswhere.exe, imports its x64 environment, and compiles the yespower |
| 29 | + "tests" and "benchmark" programs with cl.exe / link.exe. |
| 30 | +
|
| 31 | + Targets (mirroring the Makefile): |
| 32 | + build.ps1 # build tests.exe and benchmark.exe (optimized) |
| 33 | + build.ps1 -Target check # build and run tests.exe, diff against TESTS-OK |
| 34 | + build.ps1 -Target benchmark # build and run benchmark.exe |
| 35 | + build.ps1 -Target ref # build using the reference implementation |
| 36 | + build.ps1 -Target check-ref |
| 37 | + build.ps1 -Target clean # remove build artifacts |
| 38 | +
|
| 39 | +.NOTES |
| 40 | + Requires Visual Studio 2026 (Community is fine) with the |
| 41 | + "Desktop development with C++" workload installed. See the README section |
| 42 | + "How to test yespower for proper operation." for details. |
| 43 | +#> |
| 44 | +[CmdletBinding()] |
| 45 | +param( |
| 46 | + [ValidateSet('all', 'tests', 'benchmark', 'check', 'ref', 'check-ref', 'clean')] |
| 47 | + [string]$Target = 'all' |
| 48 | +) |
| 49 | + |
| 50 | +$ErrorActionPreference = 'Stop' |
| 51 | +Set-Location -LiteralPath $PSScriptRoot |
| 52 | + |
| 53 | +# --- Toolchain detection via vswhere.exe ------------------------------------ |
| 54 | + |
| 55 | +function Find-VsWhere { |
| 56 | + $candidates = @( |
| 57 | + (Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio\Installer\vswhere.exe'), |
| 58 | + (Join-Path $env:ProgramFiles 'Microsoft Visual Studio\Installer\vswhere.exe') |
| 59 | + ) |
| 60 | + foreach ($c in $candidates) { |
| 61 | + if ($c -and (Test-Path -LiteralPath $c)) { return $c } |
| 62 | + } |
| 63 | + $cmd = Get-Command vswhere.exe -ErrorAction SilentlyContinue |
| 64 | + if ($cmd) { return $cmd.Source } |
| 65 | + throw "vswhere.exe not found. Install Visual Studio 2026 (it ships vswhere)." |
| 66 | +} |
| 67 | + |
| 68 | +function Get-VcVarsPath { |
| 69 | + $vswhere = Find-VsWhere |
| 70 | + # Require the x64/x86 C++ compiler toolset. |
| 71 | + $installPath = & $vswhere -latest -products * ` |
| 72 | + -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 ` |
| 73 | + -property installationPath |
| 74 | + if (-not $installPath) { |
| 75 | + throw "No Visual Studio installation with the C++ toolset was found. " + |
| 76 | + "Install the 'Desktop development with C++' workload." |
| 77 | + } |
| 78 | + $vcvars = Join-Path $installPath 'VC\Auxiliary\Build\vcvars64.bat' |
| 79 | + if (-not (Test-Path -LiteralPath $vcvars)) { |
| 80 | + throw "vcvars64.bat not found under '$installPath'." |
| 81 | + } |
| 82 | + return $vcvars |
| 83 | +} |
| 84 | + |
| 85 | +# Import the MSVC x64 environment (PATH, INCLUDE, LIB, ...) into this session. |
| 86 | +function Import-VcEnvironment { |
| 87 | + $vcvars = Get-VcVarsPath |
| 88 | + Write-Host "Using MSVC environment: $vcvars" |
| 89 | + $marker = '___VCVARS_ENV___' |
| 90 | + # vcvars64.bat may emit benign stderr noise; don't let it abort the script. |
| 91 | + $lines = & cmd /c "`"$vcvars`" 2>nul && echo $marker && set" |
| 92 | + $seen = $false |
| 93 | + foreach ($line in $lines) { |
| 94 | + if (-not $seen) { |
| 95 | + if ($line.Trim() -eq $marker) { $seen = $true } |
| 96 | + continue |
| 97 | + } |
| 98 | + if ($line -match '^([^=]+)=(.*)$') { |
| 99 | + Set-Item -Path ("Env:" + $matches[1]) -Value $matches[2] |
| 100 | + } |
| 101 | + } |
| 102 | + if (-not $seen) { throw "Failed to import the MSVC environment." } |
| 103 | +} |
| 104 | + |
| 105 | +# --- Compiler / linker settings (port of Makefile CFLAGS/LDFLAGS) ----------- |
| 106 | + |
| 107 | +# /O2 -> -O2 |
| 108 | +# /D__SSE2__ -> enable the SSE2 intrinsic code path (MSVC x64 supports |
| 109 | +# <emmintrin.h>; this path is free of GCC inline asm). |
| 110 | +# /DUSE_OPENMP -> intentionally NOT set: benchmark.c's OpenMP section relies |
| 111 | +# on POSIX-only APIs and VLAs that MSVC cannot build. |
| 112 | +$CFLAGS = @('/nologo', '/O2', '/MT', '/D__SSE2__', '/wd4146', '/wd4244') |
| 113 | + |
| 114 | +$OBJS_COMMON = @('sha256.obj') |
| 115 | +$OBJS_CORE_OPT = 'yespower-opt.obj' |
| 116 | +$OBJS_CORE_REF = 'yespower-ref.obj' |
| 117 | + |
| 118 | +function Invoke-Tool { |
| 119 | + param([string]$Exe, [string[]]$Arguments) |
| 120 | + Write-Host ">> $Exe $($Arguments -join ' ')" |
| 121 | + # Send tool output to the host so it doesn't pollute function return values. |
| 122 | + & $Exe @Arguments | Out-Host |
| 123 | + if ($LASTEXITCODE -ne 0) { |
| 124 | + throw "$Exe failed with exit code $LASTEXITCODE" |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +function Compile-One { |
| 129 | + param([string]$Source, [string[]]$ExtraFlags = @()) |
| 130 | + $obj = [System.IO.Path]::GetFileNameWithoutExtension($Source) + '.obj' |
| 131 | + Invoke-Tool 'cl' (@('/c') + $CFLAGS + $ExtraFlags + @("/Fo$obj", $Source)) |
| 132 | + return $obj |
| 133 | +} |
| 134 | + |
| 135 | +function Link-Exe { |
| 136 | + param([string]$Out, [string[]]$Objs) |
| 137 | + Invoke-Tool 'link' (@('/nologo', "/OUT:$Out") + $Objs) |
| 138 | +} |
| 139 | + |
| 140 | +function Build-Tests { |
| 141 | + param([string]$Core = $OBJS_CORE_OPT) |
| 142 | + $coreSrc = if ($Core -eq $OBJS_CORE_REF) { 'yespower-ref.c' } else { 'yespower-opt.c' } |
| 143 | + $objs = @() |
| 144 | + $objs += Compile-One $coreSrc |
| 145 | + $objs += Compile-One 'sha256.c' |
| 146 | + $objs += Compile-One 'tests.c' |
| 147 | + Link-Exe 'tests.exe' $objs |
| 148 | + Write-Host "Built tests.exe" |
| 149 | +} |
| 150 | + |
| 151 | +function Build-Benchmark { |
| 152 | + param([string]$Core = $OBJS_CORE_OPT) |
| 153 | + $coreSrc = if ($Core -eq $OBJS_CORE_REF) { 'yespower-ref.c' } else { 'yespower-opt.c' } |
| 154 | + $objs = @() |
| 155 | + $objs += Compile-One $coreSrc |
| 156 | + $objs += Compile-One 'sha256.c' |
| 157 | + $objs += Compile-One 'benchmark.c' |
| 158 | + Link-Exe 'benchmark.exe' $objs |
| 159 | + Write-Host "Built benchmark.exe" |
| 160 | +} |
| 161 | + |
| 162 | +function Invoke-Benchmark { |
| 163 | + param([string]$Core = $OBJS_CORE_OPT) |
| 164 | + Build-Benchmark -Core $Core |
| 165 | + Write-Host 'Running benchmark' |
| 166 | + & .\benchmark.exe | Out-Host |
| 167 | + if ($LASTEXITCODE -ne 0) { throw "benchmark.exe failed with exit code $LASTEXITCODE" } |
| 168 | +} |
| 169 | + |
| 170 | +function Invoke-Check { |
| 171 | + param([string]$Core = $OBJS_CORE_OPT) |
| 172 | + Build-Tests -Core $Core |
| 173 | + Write-Host 'Running tests' |
| 174 | + & .\tests.exe | Out-File -Encoding ascii -FilePath 'TESTS-OUT' |
| 175 | + if ($LASTEXITCODE -ne 0) { throw "tests.exe failed with exit code $LASTEXITCODE" } |
| 176 | + # Compare against the reference output, ignoring CRLF/LF differences. |
| 177 | + $expected = (Get-Content -Raw 'TESTS-OK') -replace "`r`n", "`n" |
| 178 | + $actual = (Get-Content -Raw 'TESTS-OUT') -replace "`r`n", "`n" |
| 179 | + if ($expected.TrimEnd("`n") -eq $actual.TrimEnd("`n")) { |
| 180 | + Write-Host 'PASSED' -ForegroundColor Green |
| 181 | + } else { |
| 182 | + Write-Host 'FAILED' -ForegroundColor Red |
| 183 | + Compare-Object ($expected -split "`n") ($actual -split "`n") | |
| 184 | + Format-Table -AutoSize | Out-String | Write-Host |
| 185 | + exit 1 |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +function Invoke-Clean { |
| 190 | + $patterns = @('*.obj', 'tests.exe', 'benchmark.exe', 'TESTS-OUT', |
| 191 | + '*.ilk', '*.pdb', '_probe*', '_run.ps1', '_vcout.txt') |
| 192 | + foreach ($p in $patterns) { |
| 193 | + Get-ChildItem -LiteralPath $PSScriptRoot -Filter $p -ErrorAction SilentlyContinue | |
| 194 | + Remove-Item -Force -ErrorAction SilentlyContinue |
| 195 | + } |
| 196 | + Write-Host 'Cleaned build artifacts.' |
| 197 | +} |
| 198 | + |
| 199 | +# --- Dispatch --------------------------------------------------------------- |
| 200 | + |
| 201 | +switch ($Target) { |
| 202 | + 'clean' { Invoke-Clean; break } |
| 203 | + default { |
| 204 | + Import-VcEnvironment |
| 205 | + switch ($Target) { |
| 206 | + 'all' { Build-Tests; Build-Benchmark } |
| 207 | + 'tests' { Build-Tests } |
| 208 | + 'benchmark' { Invoke-Benchmark } |
| 209 | + 'check' { Invoke-Check } |
| 210 | + 'ref' { Build-Tests -Core $OBJS_CORE_REF; Build-Benchmark -Core $OBJS_CORE_REF } |
| 211 | + 'check-ref' { Invoke-Check -Core $OBJS_CORE_REF } |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments