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.'