diff --git a/build.ps1 b/build.ps1 new file mode 100644 index 0000000..f27b088 --- /dev/null +++ b/build.ps1 @@ -0,0 +1,59 @@ +param( + [ValidateSet('Debug', 'Release', 'RelWithDebInfo')] + [string]$Configuration = 'Release' +) + +$ErrorActionPreference = 'Stop' +$projectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path +$toolRoot = Join-Path $projectRoot '.tools\toolchain' + +if (-not (Test-Path -LiteralPath $toolRoot -PathType Container)) { + throw 'Portable tools are missing. Run .\setup-toolchain.ps1 first.' +} + +$cmakeExe = Get-ChildItem -LiteralPath (Join-Path $toolRoot 'cmake') -Filter 'cmake.exe' -File -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1 +$clangxxExe = Get-ChildItem -LiteralPath (Join-Path $toolRoot 'llvm-mingw') -Filter 'clang++.exe' -File -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1 +$ninjaExe = Get-ChildItem -LiteralPath (Join-Path $toolRoot 'llvm-mingw') -Filter 'ninja.exe' -File -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1 +$makeExe = Get-ChildItem -LiteralPath (Join-Path $toolRoot 'llvm-mingw') -Filter 'mingw32-make.exe' -File -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1 + +if ($null -eq $cmakeExe -or $null -eq $clangxxExe) { + throw 'CMake or Clang is missing. Run .\setup-toolchain.ps1 again.' +} + +$compilerBin = Split-Path -Parent $clangxxExe.FullName +$cmakeBin = Split-Path -Parent $cmakeExe.FullName +$env:Path = "$compilerBin;$cmakeBin;$env:Path" +$buildDirectory = Join-Path $projectRoot 'build' +$generatorArguments = @() +if ($null -ne $ninjaExe) { + $generatorArguments = @('-G', 'Ninja', "-DCMAKE_MAKE_PROGRAM=$($ninjaExe.FullName)") +} elseif ($null -ne $makeExe) { + $generatorArguments = @('-G', 'MinGW Makefiles', "-DCMAKE_MAKE_PROGRAM=$($makeExe.FullName)") +} else { + throw 'The LLVM-MinGW archive contains neither Ninja nor mingw32-make.' +} + +& $cmakeExe.FullName -S $projectRoot -B $buildDirectory @generatorArguments -U CMAKE_C_COMPILER ` + "-DCMAKE_CXX_COMPILER=$($clangxxExe.FullName)" ` + "-DCMAKE_BUILD_TYPE=$Configuration" +if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + +& $cmakeExe.FullName --build $buildDirectory --parallel --config $Configuration +if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + +# LLVM-MinGW links libc++ dynamically. Copy its runtime beside the executables +# so they can be launched from Explorer without editing the system PATH. +foreach ($runtimeName in @('libc++.dll', 'libunwind.dll')) { + $runtimeSource = Join-Path $compilerBin $runtimeName + if (-not (Test-Path -LiteralPath $runtimeSource -PathType Leaf)) { + throw "Required LLVM-MinGW runtime was not found: $runtimeSource" + } + Copy-Item -LiteralPath $runtimeSource -Destination $buildDirectory -Force +} + +$ctestExe = Join-Path $cmakeBin 'ctest.exe' +if (-not (Test-Path -LiteralPath $ctestExe -PathType Leaf)) { + throw 'CTest was not found beside CMake.' +} +& $ctestExe --test-dir $buildDirectory -C $Configuration --output-on-failure +exit $LASTEXITCODE diff --git a/setup-toolchain.ps1 b/setup-toolchain.ps1 new file mode 100644 index 0000000..cdf0930 --- /dev/null +++ b/setup-toolchain.ps1 @@ -0,0 +1,111 @@ +param() + +$ErrorActionPreference = 'Stop' +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + +$projectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path +$toolRoot = Join-Path $projectRoot '.tools\toolchain' +$downloadRoot = Join-Path $projectRoot '.tools\downloads' +$cmakeVersion = '4.5.1' +$llvmVersion = '20261117' +$cmakeArchiveName = "cmake-$cmakeVersion-windows-x86_64.zip" +$cmakeHashName = "cmake-$cmakeVersion-SHA-256.txt" +$llvmArchiveName = "llvm-mingw-$llvmVersion-ucrt-x86_64.zip" + +if (-not [Environment]::Is64BitOperatingSystem) { + throw 'This portable toolchain setup requires 64-bit Windows.' +} + +New-Item -ItemType Directory -Path $downloadRoot -Force | Out-Null +New-Item -ItemType Directory -Path $toolRoot -Force | Out-Null + +function Get-Download { + param( + [Parameter(Mandatory = $true)][string]$Uri, + [Parameter(Mandatory = $true)][string]$Destination + ) + + if (-not (Test-Path -LiteralPath $Destination -PathType Leaf)) { + Write-Host "Downloading $([System.IO.Path]::GetFileName($Destination))" + Invoke-WebRequest -Uri $Uri -OutFile $Destination -UseBasicParsing + } +} + +function Expand-IfMissing { + param( + [Parameter(Mandatory = $true)][string]$Archive, + [Parameter(Mandatory = $true)][string]$Destination, + [Parameter(Mandatory = $true)][string]$ExpectedExecutable + ) + + $existingExecutable = Get-ChildItem -LiteralPath $Destination -Filter $ExpectedExecutable -File -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1 + if ($null -eq $existingExecutable) { + New-Item -ItemType Directory -Path $Destination -Force | Out-Null + Expand-Archive -LiteralPath $Archive -DestinationPath $Destination -Force + } +} + +$cmakeArchive = Join-Path $downloadRoot $cmakeArchiveName +$cmakeHashFile = Join-Path $downloadRoot $cmakeHashName +$cmakeBase = "https://github.com/Kitware/CMake/releases/download/v$cmakeVersion" +Get-Download -Uri "$cmakeBase/$cmakeArchiveName" -Destination $cmakeArchive +Get-Download -Uri "$cmakeBase/$cmakeHashName" -Destination $cmakeHashFile + +$escapedCmakeName = [regex]::Escape($cmakeArchiveName) +$hashText = Get-Content -LiteralPath $cmakeHashFile -Raw +$hashPatterns = @( + "(?im)^\s*([0-9a-f]{64})\s+\*?$escapedCmakeName\s*$", + "(?im)^\s*SHA-?256\s*\(\s*$escapedCmakeName\s*\)\s*[:=]\s*([0-9a-f]{64})\s*$", + "(?im)^\s*$escapedCmakeName\s*[: ]+\s*([0-9a-f]{64})\s*$" +) +$expectedCmakeHash = $null +foreach ($pattern in $hashPatterns) { + $match = [regex]::Match($hashText, $pattern) + if ($match.Success) { + $expectedCmakeHash = $match.Groups[1].Value.ToLowerInvariant() + break + } +} +if (-not $expectedCmakeHash) { + throw "Could not read the official SHA-256 for $cmakeArchiveName. The archive was not extracted." +} +$actualCmakeHash = (Get-FileHash -LiteralPath $cmakeArchive -Algorithm SHA256).Hash.ToLowerInvariant() +if ($actualCmakeHash -ne $expectedCmakeHash) { + throw "CMake archive SHA-256 mismatch. Expected $expectedCmakeHash, got $actualCmakeHash." +} + +$llvmArchive = Join-Path $downloadRoot $llvmArchiveName +$llvmReleaseUri = "https://api.github.com/repos/mstorsjo/llvm-mingw/releases/tags/$llvmVersion" +$llvmRelease = Invoke-RestMethod -Uri $llvmReleaseUri -Headers @{ 'User-Agent' = 'stonehearth-sim-toolchain-setup' } +$llvmAsset = $llvmRelease.assets | Where-Object { $_.name -eq $llvmArchiveName } | Select-Object -First 1 +if ($null -eq $llvmAsset) { + throw "The official llvm-mingw release $llvmVersion does not contain $llvmArchiveName." +} +Get-Download -Uri $llvmAsset.browser_download_url -Destination $llvmArchive + +if ($llvmAsset.digest -and $llvmAsset.digest -match '^sha256:([0-9a-fA-F]{64})$') { + $expectedLlvmHash = $Matches[1].ToLowerInvariant() + $actualLlvmHash = (Get-FileHash -LiteralPath $llvmArchive -Algorithm SHA256).Hash.ToLowerInvariant() + if ($actualLlvmHash -ne $expectedLlvmHash) { + throw "LLVM-MinGW archive SHA-256 mismatch. Expected $expectedLlvmHash, got $actualLlvmHash." + } +} else { + Write-Warning 'GitHub did not publish a SHA-256 digest for this LLVM-MinGW asset; download is from the official release over HTTPS.' +} + +$cmakeInstall = Join-Path $toolRoot 'cmake' +$llvmInstall = Join-Path $toolRoot 'llvm-mingw' +Expand-IfMissing -Archive $cmakeArchive -Destination $cmakeInstall -ExpectedExecutable 'cmake.exe' +Expand-IfMissing -Archive $llvmArchive -Destination $llvmInstall -ExpectedExecutable 'clang++.exe' + +$cmakeExe = Get-ChildItem -LiteralPath $cmakeInstall -Filter 'cmake.exe' -File -Recurse | Select-Object -First 1 +$clangxxExe = Get-ChildItem -LiteralPath $llvmInstall -Filter 'clang++.exe' -File -Recurse | Select-Object -First 1 +if ($null -eq $cmakeExe -or $null -eq $clangxxExe) { + throw 'Extraction completed, but CMake or Clang++ was not found in the toolchain folders.' +} + +Write-Host "CMake: $($cmakeExe.FullName)" +& $cmakeExe.FullName --version | Select-Object -First 1 +Write-Host "C++ compiler: $($clangxxExe.FullName)" +& $clangxxExe.FullName --version | Select-Object -First 1 +Write-Host 'Portable toolchain is ready. Run .\build.ps1 to configure, build, and run CTest.'