<# Copyright (c) Stefano Sinigardi MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #> $utils_psm1_version = "0.0.4" $IsWindowsPowerShell = switch ( $PSVersionTable.PSVersion.Major ) { 5 { $true } 4 { $true } 3 { $true } 2 { $true } default { $false } } $cuda_version_full = "11.7.0" $cuda_version_short = "11.7" $cuda_version_full_dashed = $cuda_version_full.replace('.', '-') $cuda_version_short_dashed = $cuda_version_short.replace('.', '-') function getProgramFiles32bit() { $out = ${env:PROGRAMFILES(X86)} if ($null -eq $out) { $out = ${env:PROGRAMFILES} } if ($null -eq $out) { MyThrow("Could not find [Program Files 32-bit]") } return $out } function getLatestVisualStudioWithDesktopWorkloadPath() { $programFiles = getProgramFiles32bit $vswhereExe = "$programFiles\Microsoft Visual Studio\Installer\vswhere.exe" if (Test-Path $vswhereExe) { $output = & $vswhereExe -products * -latest -requires Microsoft.VisualStudio.Workload.NativeDesktop -format xml [xml]$asXml = $output foreach ($instance in $asXml.instances.instance) { $installationPath = $instance.InstallationPath -replace "\\$" # Remove potential trailing backslash } if (!$installationPath) { Write-Host "Warning: no full Visual Studio setup has been found, extending search to include also partial installations" -ForegroundColor Yellow $output = & $vswhereExe -products * -latest -format xml [xml]$asXml = $output foreach ($instance in $asXml.instances.instance) { $installationPath = $instance.InstallationPath -replace "\\$" # Remove potential trailing backslash } } if (!$installationPath) { Write-Host "Warning: no full Visual Studio setup has been found, extending search to include also pre-release installations" -ForegroundColor Yellow $output = & $vswhereExe -prerelease -products * -latest -format xml [xml]$asXml = $output foreach ($instance in $asXml.instances.instance) { $installationPath = $instance.InstallationPath -replace "\\$" # Remove potential trailing backslash } } if (!$installationPath) { MyThrow("Could not locate any installation of Visual Studio") } } else { MyThrow("Could not locate vswhere at $vswhereExe") } return $installationPath } function getLatestVisualStudioWithDesktopWorkloadVersion() { $programFiles = getProgramFiles32bit $vswhereExe = "$programFiles\Microsoft Visual Studio\Installer\vswhere.exe" if (Test-Path $vswhereExe) { $output = & $vswhereExe -products * -latest -requires Microsoft.VisualStudio.Workload.NativeDesktop -format xml [xml]$asXml = $output foreach ($instance in $asXml.instances.instance) { $installationVersion = $instance.InstallationVersion } if (!$installationVersion) { Write-Host "Warning: no full Visual Studio setup has been found, extending search to include also partial installations" -ForegroundColor Yellow $output = & $vswhereExe -products * -latest -format xml [xml]$asXml = $output foreach ($instance in $asXml.instances.instance) { $installationVersion = $instance.installationVersion } } if (!$installationVersion) { Write-Host "Warning: no full Visual Studio setup has been found, extending search to include also pre-release installations" -ForegroundColor Yellow $output = & $vswhereExe -prerelease -products * -latest -format xml [xml]$asXml = $output foreach ($instance in $asXml.instances.instance) { $installationVersion = $instance.installationVersion } } if (!$installationVersion) { MyThrow("Could not locate any installation of Visual Studio") } } else { MyThrow("Could not locate vswhere at $vswhereExe") } return $installationVersion } function DownloadNinja() { Write-Host "Unable to find Ninja, downloading a portable version on-the-fly" -ForegroundColor Yellow Remove-Item -Force -Recurse -ErrorAction SilentlyContinue ninja Remove-Item -Force -ErrorAction SilentlyContinue ninja.zip if ($IsWindows -or $IsWindowsPowerShell) { $url = "https://github.com/ninja-build/ninja/releases/download/v1.10.2/ninja-win.zip" } elseif ($IsLinux) { $url = "https://github.com/ninja-build/ninja/releases/download/v1.10.2/ninja-linux.zip" } elseif ($IsMacOS) { $url = "https://github.com/ninja-build/ninja/releases/download/v1.10.2/ninja-mac.zip" } else { MyThrow("Unknown OS, unsupported") } Invoke-RestMethod -Uri $url -Method Get -ContentType application/zip -OutFile "ninja.zip" Expand-Archive -Path ninja.zip Remove-Item -Force -ErrorAction SilentlyContinue ninja.zip } Function MyThrow ($Message) { if ($global:DisableInteractive) { Write-Host $Message -ForegroundColor Red throw } else { # Check if running in PowerShell ISE if ($psISE) { # "ReadKey" not supported in PowerShell ISE. # Show MessageBox UI $Shell = New-Object -ComObject "WScript.Shell" $Shell.Popup($Message, 0, "OK", 0) throw } $Ignore = 16, # Shift (left or right) 17, # Ctrl (left or right) 18, # Alt (left or right) 20, # Caps lock 91, # Windows key (left) 92, # Windows key (right) 93, # Menu key 144, # Num lock 145, # Scroll lock 166, # Back 167, # Forward 168, # Refresh 169, # Stop 170, # Search 171, # Favorites 172, # Start/Home 173, # Mute 174, # Volume Down 175, # Volume Up 176, # Next Track 177, # Previous Track 178, # Stop Media 179, # Play 180, # Mail 181, # Select Media 182, # Application 1 183 # Application 2 Write-Host $Message -ForegroundColor Red Write-Host -NoNewline "Press any key to continue..." while (($null -eq $KeyInfo.VirtualKeyCode) -or ($Ignore -contains $KeyInfo.VirtualKeyCode)) { $KeyInfo = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown") } Write-Host "" throw } } Export-ModuleMember -Variable utils_psm1_version Export-ModuleMember -Variable IsWindowsPowerShell Export-ModuleMember -Variable cuda_version_full Export-ModuleMember -Variable cuda_version_short Export-ModuleMember -Variable cuda_version_full_dashed Export-ModuleMember -Variable cuda_version_short_dashed Export-ModuleMember -Function getProgramFiles32bit Export-ModuleMember -Function getLatestVisualStudioWithDesktopWorkloadPath Export-ModuleMember -Function getLatestVisualStudioWithDesktopWorkloadVersion Export-ModuleMember -Function DownloadNinja Export-ModuleMember -Function MyThrow Export-ModuleMember -Function CopyTexFile