param( [ValidateSet('Debug', 'Release', 'RelWithDebInfo')] [string]$Configuration = 'Release', [string]$Msys2Root = $env:SETTLEMENT_MSYS2_ROOT, [string]$VoidLibrarySource ) $ErrorActionPreference = 'Stop' $projectRoot = $PSScriptRoot if (-not $Msys2Root) { $Msys2Root = Join-Path $projectRoot '.tools\toolchain\msys64' } $bin = Join-Path $Msys2Root 'ucrt64\bin' foreach ($tool in @('gcc.exe', 'g++.exe', 'cmake.exe', 'ctest.exe', 'ninja.exe', 'windres.exe')) { if (-not (Test-Path -LiteralPath (Join-Path $bin $tool) -PathType Leaf)) { throw "MSYS2 UCRT64 tool missing: $tool. Run .\setup-toolchain.ps1." } } $bin = (Get-Item -LiteralPath $bin).FullName.Replace('\', '/') $toolPaths = @($bin) $voidSource = '' $gitOption = @() if ($VoidLibrarySource) { $voidSource = (Resolve-Path -LiteralPath $VoidLibrarySource).ProviderPath.Replace('\', '/') if (-not (Test-Path -LiteralPath (Join-Path $voidSource 'CMakeLists.txt') -PathType Leaf)) { throw "Void Library CMakeLists.txt was not found in $voidSource." } } else { $git = Join-Path $Msys2Root 'usr\bin\git.exe' if (Test-Path -LiteralPath $git -PathType Leaf) { $bundledGit = Get-Item -LiteralPath $git $git = $bundledGit.FullName $toolPaths += $bundledGit.DirectoryName } else { $git = (Get-Command git.exe -CommandType Application -ErrorAction SilentlyContinue).Source } if (-not $git) { throw 'Git is required to fetch Void Library. Run .\setup-toolchain.ps1 or pass -VoidLibrarySource.' } $gitOption = @("-DGIT_EXECUTABLE:FILEPATH=$($git.Replace('\', '/'))") } $buildRoot = Join-Path $projectRoot 'build' $buildDirectory = Join-Path $buildRoot 'msys2-ucrt64' $previousPath = $env:Path try { $env:Path = ($toolPaths + $previousPath) -join ';' $version = & (Join-Path $bin 'g++.exe') -dumpfullversion if ($LASTEXITCODE -ne 0 -or $version -notmatch '^\d+\.\d+(\.\d+)?$' -or [version]$version -lt [version]'16.0') { throw "MSYS2 UCRT64 GCC 16 or later is required. Found: $version" } $cVersion = & (Join-Path $bin 'gcc.exe') -dumpfullversion if ($LASTEXITCODE -ne 0 -or $cVersion -ne $version) { throw "gcc.exe and g++.exe must match. Found: $cVersion and $version" } $cmake = Join-Path $bin 'cmake.exe' $cmakeVersion = & $cmake --version if ($LASTEXITCODE -ne 0 -or $cmakeVersion[0] -notmatch '^cmake version (\d+\.\d+\.\d+)' -or [version]$Matches[1] -lt [version]'4.0') { throw 'CMake 4.0 or later is required. Run .\setup-toolchain.ps1.' } Write-Output "Building with MSYS2 UCRT64 GCC $version" $options = @('-S', $projectRoot, '-B', $buildDirectory, '-G', 'Ninja', "-DCMAKE_C_COMPILER=$bin/gcc.exe", "-DCMAKE_CXX_COMPILER=$bin/g++.exe", "-DCMAKE_RC_COMPILER=$bin/windres.exe", "-DCMAKE_MAKE_PROGRAM=$bin/ninja.exe", "-DCMAKE_BUILD_TYPE=$Configuration", '-DBUILD_TESTING=ON', "-DFETCHCONTENT_SOURCE_DIR_VOID_LIBRARY:PATH=$voidSource") + $gitOption & $cmake @options if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } & $cmake --build $buildDirectory --parallel if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } & (Join-Path $bin 'ctest.exe') --test-dir $buildDirectory --output-on-failure if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } foreach ($runtime in @('libstdc++-6.dll', 'libgcc_s_seh-1.dll', 'libwinpthread-1.dll')) { Copy-Item -LiteralPath (Join-Path $bin $runtime) -Destination $buildRoot -Force } foreach ($program in @('settlement_viewer', 'settlement_sim', 'settlement_colony_benchmark', 'settlement_core_tests', 'settlement_lan_tests')) { Copy-Item -LiteralPath (Join-Path $buildDirectory "$program.exe") -Destination $buildRoot -Force } } finally { $env:Path = $previousPath }