|
| 1 | +# Copyright 2019-2025 namazso <admin@namazso.eu> |
| 2 | +# This file is part of OpenHashTab. |
| 3 | +# |
| 4 | +# OpenHashTab is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# OpenHashTab is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with OpenHashTab. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +[CmdletBinding()] param () |
| 18 | + |
| 19 | +function Get-Environment { |
| 20 | + <# |
| 21 | + .SYNOPSIS |
| 22 | + Captures the current environment variables. |
| 23 | + .DESCRIPTION |
| 24 | + Returns a hashtable of all current environment variables. |
| 25 | + .EXAMPLE |
| 26 | + $SavedEnv = Get-Environment |
| 27 | + .OUTPUTS |
| 28 | + System.Collections.Hashtable |
| 29 | + #> |
| 30 | + $env = @{} |
| 31 | + Get-ChildItem Env: | ForEach-Object { $env[$_.Name] = $_.Value } |
| 32 | + return $env |
| 33 | +} |
| 34 | + |
| 35 | +function Set-Environment { |
| 36 | + <# |
| 37 | + .SYNOPSIS |
| 38 | + Restores environment variables from a saved state. |
| 39 | + .DESCRIPTION |
| 40 | + Clears current environment variables and sets them to the provided saved state. |
| 41 | + .PARAMETER Environment |
| 42 | + Hashtable of environment variables previously captured by Get-Environment. |
| 43 | + .EXAMPLE |
| 44 | + Set-Environment -Environment $SavedEnv |
| 45 | + #> |
| 46 | + param( |
| 47 | + [Parameter(Mandatory=$true)] |
| 48 | + [System.Collections.Hashtable]$Environment |
| 49 | + ) |
| 50 | + |
| 51 | + # Clear current environment variables |
| 52 | + Remove-Item -Path Env:* -ErrorAction SilentlyContinue |
| 53 | + |
| 54 | + # Restore saved environment variables |
| 55 | + foreach ($key in $Environment.Keys) { |
| 56 | + Set-Item "Env:$key" $Environment[$key] |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +function Reset-Environment { |
| 61 | + <# |
| 62 | + .SYNOPSIS |
| 63 | + Resets the environment by keeping only default Windows environment variables. |
| 64 | + .DESCRIPTION |
| 65 | + Filters an environment hashtable to keep only default Windows environment variables. |
| 66 | + Returns a reset environment hashtable. |
| 67 | + .PARAMETER Environment |
| 68 | + Hashtable of environment variables to reset, as provided by Get-Environment. |
| 69 | + .PARAMETER AdditionalVariables |
| 70 | + Optional array of additional environment variable names to preserve. |
| 71 | + .EXAMPLE |
| 72 | + $ResetEnv = Reset-Environment -Environment $SavedEnv |
| 73 | + .EXAMPLE |
| 74 | + $ResetEnv = Reset-Environment -Environment (Get-Environment) -AdditionalVariables @("JAVA_HOME") |
| 75 | + .OUTPUTS |
| 76 | + System.Collections.Hashtable |
| 77 | + #> |
| 78 | + param( |
| 79 | + [Parameter(Mandatory=$true)] |
| 80 | + [System.Collections.Hashtable]$Environment, |
| 81 | + |
| 82 | + [Parameter(Mandatory=$false)] |
| 83 | + [string[]]$AdditionalVariables = @() |
| 84 | + ) |
| 85 | + |
| 86 | + # List of default Windows environment variables to preserve |
| 87 | + $defaultVars = @( |
| 88 | + "ALLUSERSPROFILE", "APPDATA", "CommonProgramFiles", "CommonProgramFiles(x86)", |
| 89 | + "CommonProgramW6432", "COMPUTERNAME", "ComSpec", "HOMEDRIVE", "HOMEPATH", |
| 90 | + "LOCALAPPDATA", "LOGONSERVER", "NUMBER_OF_PROCESSORS", "OS", "Path", "PATHEXT", |
| 91 | + "PROCESSOR_ARCHITECTURE", "PROCESSOR_IDENTIFIER", "PROCESSOR_LEVEL", "PROCESSOR_REVISION", |
| 92 | + "ProgramData", "ProgramFiles", "ProgramFiles(x86)", "ProgramW6432", "PROMPT", |
| 93 | + "PSModulePath", "PUBLIC", "SystemDrive", "SystemRoot", "TEMP", "TMP", |
| 94 | + "USERDOMAIN", "USERDOMAIN_ROAMINGPROFILE", "USERNAME", "USERPROFILE", "windir" |
| 95 | + ) |
| 96 | + |
| 97 | + # Combine default variables with additional ones |
| 98 | + $varsToKeep = $defaultVars + $AdditionalVariables |
| 99 | + |
| 100 | + # Create a new hashtable with only the variables to keep |
| 101 | + $resetEnvironment = @{} |
| 102 | + foreach ($key in $Environment.Keys) { |
| 103 | + if ($varsToKeep -contains $key -or $varsToKeep -contains $key.ToUpper()) { |
| 104 | + $resetEnvironment[$key] = $Environment[$key] |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + $SystemRoot = $resetEnvironment["SystemRoot"] |
| 109 | + $resetEnvironment["Path"] = "$SystemRoot\system32;$SystemRoot;$SystemRoot\System32\Wbem;$SystemRoot\System32\WindowsPowerShell\v1.0;" |
| 110 | + |
| 111 | + return $resetEnvironment |
| 112 | +} |
| 113 | +function Get-CmdEnvironment { |
| 114 | + <# |
| 115 | + .SYNOPSIS |
| 116 | + Captures environment variables from a cmd.exe script execution. |
| 117 | + .DESCRIPTION |
| 118 | + Executes a specified script/batch file using cmd.exe and returns all environment |
| 119 | + variables after execution as a hashtable. |
| 120 | + .PARAMETER ScriptPath |
| 121 | + The path to the command, batch file, or script to execute. |
| 122 | + .PARAMETER Arguments |
| 123 | + Optional arguments to pass to the script. |
| 124 | + .EXAMPLE |
| 125 | + $VsEnv = Get-CmdEnvironment -ScriptPath "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat" |
| 126 | + .OUTPUTS |
| 127 | + System.Collections.Hashtable |
| 128 | + #> |
| 129 | + param( |
| 130 | + [Parameter(Mandatory=$true)] |
| 131 | + [String]$ScriptPath, |
| 132 | + |
| 133 | + [Parameter(Mandatory=$false)] |
| 134 | + [String]$Arguments = "" |
| 135 | + ) |
| 136 | + |
| 137 | + if (-not (Test-Path $ScriptPath)) { |
| 138 | + Write-Error "Script not found: $ScriptPath" |
| 139 | + return $null |
| 140 | + } |
| 141 | + |
| 142 | + # Use full path and proper quoting |
| 143 | + $scriptPathFull = (Resolve-Path $ScriptPath).Path |
| 144 | + $cmdLine = "`"$scriptPathFull`" $Arguments && set" |
| 145 | + |
| 146 | + Write-Verbose "Executing: cmd.exe /c $cmdLine" |
| 147 | + |
| 148 | + $env = @{} |
| 149 | + |
| 150 | + & cmd.exe /c "$cmdLine" | |
| 151 | + Select-String '^([^=]*)=(.*)$' | ForEach-Object { |
| 152 | + $varName = $_.Matches[0].Groups[1].Value |
| 153 | + $varValue = $_.Matches[0].Groups[2].Value |
| 154 | + $env[$varName] = $varValue |
| 155 | + } |
| 156 | + |
| 157 | + return $env |
| 158 | +} |
| 159 | + |
| 160 | +$OriginalEnvironment = Get-Environment; |
| 161 | + |
| 162 | +$VSRoot = If ($Env:VSINSTALLDIR) {$Env:VSINSTALLDIR} Else { & vswhere -property installationPath }; |
| 163 | + |
| 164 | +If (-not $VSRoot) { |
| 165 | + Throw "Visual Studio not found." |
| 166 | +} |
| 167 | + |
| 168 | +$WinSdkVer = If ($Env:WindowsTargetPlatformVersion) { $Env:WindowsTargetPlatformVersion } Else { $(Get-Item "hklm:\SOFTWARE\Microsoft\Microsoft SDKs\Windows").GetValue("CurrentVersion") }; |
| 169 | +$WinSdkDir = If ($Env:WindowsSdkDir) { $Env:WindowsSdkDir } Else { $(Get-Item "hklm:\SOFTWARE\Microsoft\Microsoft SDKs\Windows").GetValue("CurrentInstallFolder") }; |
| 170 | + |
| 171 | +$Cmake = Get-Command "cmake"; |
| 172 | +$Dotnet = Get-Command "dotnet"; |
| 173 | +$Ninja = (Get-Command "ninja").Path; |
| 174 | +$LlvmPath = (Get-Item (Get-Command "clang").Path).Directory.FullName; |
| 175 | + |
| 176 | +If (Test-Path "Env:CI_VERSION") { |
| 177 | + $OhtVersion = $Env:CI_VERSION; |
| 178 | + $OhtVersionMajor = $Env:CI_VERSION_MAJOR; |
| 179 | + $OhtVersionMinor = $Env:CI_VERSION_MINOR; |
| 180 | + $OhtVersionPatch = $Env:CI_VERSION_PATCH; |
| 181 | +} Else { |
| 182 | + $OhtVersion = "(unknown)"; |
| 183 | + $OhtVersionMajor = 0; |
| 184 | + $OhtVersionMinor = 0; |
| 185 | + $OhtVersionPatch = 0; |
| 186 | +} |
| 187 | + |
| 188 | +$CleanEnv = Reset-Environment $OriginalEnvironment -AdditionalVariables @("AUTHENTICODE_SIGN"); |
| 189 | +Set-Environment -Environment $CleanEnv; |
| 190 | + |
| 191 | +Write-Host "Using Visual Studio at: $VSRoot" |
| 192 | + |
| 193 | +$x86_Environment = Get-CmdEnvironment -ScriptPath "$VSRoot\VC\Auxiliary\Build\vcvarsamd64_x86.bat"; |
| 194 | +$x86_Environment["CFLAGS"] = "--target=i686-pc-windows-msvc"; |
| 195 | +$x86_Environment["CXXFLAGS"] = "--target=i686-pc-windows-msv"; |
| 196 | +$x86_Environment["LDFLAGS"] = "--target=i686-pc-windows-msv"; |
| 197 | +$x64_Environment = Get-CmdEnvironment -ScriptPath "$VSRoot\VC\Auxiliary\Build\vcvars64.bat"; |
| 198 | +$x64_Environment["CFLAGS"] = "--target=x86_64-pc-windows-msvc"; |
| 199 | +$x64_Environment["CXXFLAGS"] = "--target=x86_64-pc-windows-msvc"; |
| 200 | +$x64_Environment["LDFLAGS"] = "--target=x86_64-pc-windows-msvc"; |
| 201 | +$ARM64_Environment = Get-CmdEnvironment -ScriptPath "$VSRoot\VC\Auxiliary\Build\vcvarsamd64_arm64.bat"; |
| 202 | +$ARM64_Environment["CFLAGS"] = "--target=arm64-pc-windows-msvc"; |
| 203 | +$ARM64_Environment["CXXFLAGS"] = "--target=arm64-pc-windows-msvc"; |
| 204 | +$ARM64_Environment["LDFLAGS"] = "--target=arm64-pc-windows-msvc"; |
| 205 | + |
| 206 | +foreach ($env in @($x86_Environment, $x64_Environment, $ARM64_Environment)) { |
| 207 | + $Platform = $env["Platform"]; |
| 208 | + $env["CC"] = "$LlvmPath\clang-cl.exe"; |
| 209 | + $env["CXX"] = "$LlvmPath\clang-cl.exe"; |
| 210 | + $env["INCLUDE"] += ";$WinSdkDir\Include\$WinSdkVer\shared;$WinSdkDir\Include\$WinSdkVer\ucrt;$WinSdkDir\Include\$WinSdkVer\um"; |
| 211 | + $env["LIB"] += ";$WinSdkDir\Lib\$WinSdkVer\ucrt\$Platform;$WinSdkDir\Lib\$WinSdkVer\um\$Platform"; |
| 212 | + $env["LIBPATH"] += ";$WinSdkDir\Lib\$WinSdkVer\ucrt\$Platform;$WinSdkDir\Lib\$WinSdkVer\um\$Platform"; |
| 213 | + $env["RC"] = "$LlvmPath\llvm-rc.exe"; |
| 214 | + $env["Path"] += ";$WinSdkDir\bin\$WinSdkVer\x64"; |
| 215 | +} |
| 216 | + |
| 217 | +Write-Host "bleh" |
| 218 | + |
| 219 | +New-Item -Path "install" -ItemType Directory -Force |
| 220 | +New-Item -Path "install\algorithms" -ItemType Directory -Force |
| 221 | +$AlgorithmsInstallDir = (Get-Item "install\algorithms").FullName; |
| 222 | + |
| 223 | +"x86", "SSE2", "AVX2", "AVX512", "ARM64" | ForEach-Object { |
| 224 | + If ($_ -eq "x86") { |
| 225 | + $Environment = $x86_Environment; |
| 226 | + } Elseif ($_ -eq "ARM64") { |
| 227 | + $Environment = $ARM64_Environment; |
| 228 | + } Else { |
| 229 | + $Environment = $x64_Environment; |
| 230 | + } |
| 231 | + |
| 232 | + $BuildDir = "build\algorithms-$_"; |
| 233 | + |
| 234 | + Set-Environment $Environment; |
| 235 | + $Mt = (Get-Command "mt").Path; |
| 236 | + & $Cmake ` |
| 237 | + -G Ninja ` |
| 238 | + -S Algorithms ` |
| 239 | + -B $BuildDir ` |
| 240 | + --install-prefix $AlgorithmsInstallDir ` |
| 241 | + "-DOHT_FLAVOR=$_" ` |
| 242 | + "-DCMAKE_BUILD_TYPE=Release" ` |
| 243 | + "-DCMAKE_MAKE_PROGRAM=$Ninja" ` |
| 244 | + "-DCMAKE_MT=$Mt" |
| 245 | + & $Cmake --build $BuildDir |
| 246 | + & $Cmake --install $BuildDir |
| 247 | + |
| 248 | + If ($Env:AUTHENTICODE_SIGN) { |
| 249 | + & $Env:AUTHENTICODE_SIGN "$((Get-Item "$AlgorithmsInstallDir\AlgorithmsDll_$_.dll").FullName)" |
| 250 | + } |
| 251 | +} |
| 252 | + |
| 253 | +$OhtVersionNumeric = "$OhtVersionMajor.$OhtVersionMinor.$OhtVersionPatch"; |
| 254 | + |
| 255 | +"x86", "x64", "arm64" | ForEach-Object { |
| 256 | + If ($_ -eq "x86") { |
| 257 | + $Environment = $x86_Environment; |
| 258 | + } Elseif ($_ -eq "arm64") { |
| 259 | + $Environment = $ARM64_Environment; |
| 260 | + } Else { |
| 261 | + $Environment = $x64_Environment; |
| 262 | + } |
| 263 | + |
| 264 | + $Platform = $Environment["Platform"]; |
| 265 | + |
| 266 | + $BuildDir = "build\oht-$_"; |
| 267 | + |
| 268 | + Set-Environment $Environment; |
| 269 | + $Mt = (Get-Command "mt").Path; |
| 270 | + & $Cmake ` |
| 271 | + -G Ninja ` |
| 272 | + -S OpenHashTab ` |
| 273 | + -B $BuildDir ` |
| 274 | + "-DCMAKE_BUILD_TYPE=Release" ` |
| 275 | + "-DCMAKE_MAKE_PROGRAM=$Ninja" ` |
| 276 | + "-DCMAKE_MT=$Mt" ` |
| 277 | + "-DOHT_ALGORITHMS_INSTALL_DIR=$AlgorithmsInstallDir" ` |
| 278 | + "-DOHT_ALGORITHMS_INCLUDE_DIR=$((Get-Item .\Algorithms).FullName)" ` |
| 279 | + "-DOHT_LOCALES_DIR=$((Get-Item .\Localization).FullName)" ` |
| 280 | + "-DCI_VERSION=$OhtVersion" ` |
| 281 | + "-DCI_VERSION_MAJOR=$OhtVersionMajor" ` |
| 282 | + "-DCI_VERSION_MINOR=$OhtVersionMinor" ` |
| 283 | + "-DCI_VERSION_PATCH=$OhtVersionPatch" |
| 284 | + & $Cmake --build $BuildDir |
| 285 | + |
| 286 | + If ($Env:AUTHENTICODE_SIGN) { |
| 287 | + & $Env:AUTHENTICODE_SIGN "$((Get-Item "$BuildDir\OpenHashTab*.dll").FullName)" |
| 288 | + & $Env:AUTHENTICODE_SIGN "$((Get-Item "$BuildDir\StandaloneStub.exe").FullName)" |
| 289 | + & $Env:AUTHENTICODE_SIGN "$((Get-Item "$BuildDir\Benchmark.exe").FullName)" |
| 290 | + } |
| 291 | + |
| 292 | + $BuildDir32 = "build\oht-x86"; |
| 293 | + |
| 294 | + "User", "Machine" | ForEach-Object { |
| 295 | + & $Dotnet build ` |
| 296 | + -c $_ ` |
| 297 | + "-property:ProductVersion=$OhtVersionNumeric" ` |
| 298 | + "-property:BuildDirectory32=$((Get-Item $BuildDir32).FullName)" ` |
| 299 | + "-property:BuildDirectory=$((Get-Item $BuildDir).FullName)" ` |
| 300 | + "-property:AlgorithmsDllsDirectory=$AlgorithmsInstallDir" ` |
| 301 | + } |
| 302 | +} |
| 303 | + |
| 304 | +Set-Environment -Environment $OriginalEnvironment; |
0 commit comments