install_build_tools_and_verify.ps1 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <#
  2. Install Visual C++ Build Tools (if missing), ensure OpenSSL env vars are set,
  3. and run cargo clean && cargo check in the src-tauri folder.
  4. Usage: Open an elevated (Administrator) PowerShell and run:
  5. .\install_build_tools_and_verify.ps1
  6. This script attempts to be idempotent and logs steps to the console.
  7. #>
  8. Set-StrictMode -Version Latest
  9. function Write-Log {
  10. param($msg)
  11. $t = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
  12. Write-Host "[$t] $msg"
  13. }
  14. function Ensure-Administrator {
  15. $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
  16. if (-not $isAdmin) {
  17. Write-Error "This script must be run as Administrator. Please re-open PowerShell as Administrator and re-run."
  18. exit 1
  19. }
  20. }
  21. function Ensure-OpenSSL-Installed {
  22. param($opensslRoot = "C:\Program Files\OpenSSL-Win64")
  23. if (-not (Test-Path $opensslRoot)) {
  24. Write-Error "OpenSSL root not found at $opensslRoot. Please install the full Win64 OpenSSL and re-run."
  25. exit 1
  26. }
  27. if (-not (Test-Path (Join-Path $opensslRoot "include\openssl"))) {
  28. Write-Error "OpenSSL include directory missing under $opensslRoot\include. Please install the Full OpenSSL distribution with developer files."
  29. exit 1
  30. }
  31. if (-not (Test-Path (Join-Path $opensslRoot "lib"))) {
  32. Write-Error "OpenSSL lib directory missing under $opensslRoot\lib. Please install the Full OpenSSL distribution with libraries."
  33. exit 1
  34. }
  35. return $opensslRoot
  36. }
  37. function Ensure-Choco {
  38. if (-not (Get-Command choco -ErrorAction SilentlyContinue)) {
  39. Write-Log "Chocolatey not found. Installing Chocolatey (will take a few minutes)..."
  40. Set-ExecutionPolicy Bypass -Scope Process -Force
  41. $script = 'https://community.chocolatey.org/install.ps1'
  42. try {
  43. iex ((New-Object System.Net.WebClient).DownloadString($script))
  44. } catch {
  45. Write-Error "Failed to install Chocolatey. Please install it manually and re-run this script."
  46. exit 1
  47. }
  48. # Refresh environment for choco
  49. $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
  50. } else {
  51. Write-Log "Chocolatey detected."
  52. }
  53. }
  54. function Install-VSBuildTools {
  55. # Install Visual Studio 2022 Build Tools with C++ workload
  56. if (Get-Command cl -ErrorAction SilentlyContinue) {
  57. Write-Log "MSVC (cl) already available."
  58. return
  59. }
  60. Write-Log "Installing Visual Studio 2022 Build Tools (Desktop development with C++) via Chocolatey..."
  61. $pkg = "visualstudio2022buildtools"
  62. $params = '--package-parameters "--add Microsoft.VisualStudio.Workload.VCTools --includeRecommended"'
  63. $args = "install $pkg $params -y"
  64. $exit = & choco $args
  65. if ($LASTEXITCODE -ne 0) {
  66. Write-Error "Chocolatey installation of Visual Studio Build Tools failed. Please install manually from https://visualstudio.microsoft.com/visual-cpp-build-tools/"
  67. exit 1
  68. }
  69. Write-Log "Visual Studio Build Tools installation requested. It may take several minutes to complete."
  70. }
  71. function Find-VcVars {
  72. # Common path for VS Build Tools 2022
  73. $candidates = @(
  74. "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat",
  75. "C:\Program Files (x86)\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat",
  76. "C:\Program Files (x86)\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvars64.bat",
  77. "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
  78. )
  79. foreach ($p in $candidates) {
  80. if (Test-Path $p) { return $p }
  81. }
  82. return $null
  83. }
  84. function Configure-OpenSSL-Env {
  85. param($opensslRoot)
  86. Write-Log "Setting OpenSSL environment variables (system-wide)..."
  87. setx OPENSSL_DIR $opensslRoot | Out-Null
  88. setx OPENSSL_LIB_DIR (Join-Path $opensslRoot "lib") | Out-Null
  89. setx OPENSSL_INCLUDE_DIR (Join-Path $opensslRoot "include") | Out-Null
  90. # Append bin to PATH if not present
  91. $bin = (Join-Path $opensslRoot "bin")
  92. $currentPath = [Environment]::GetEnvironmentVariable("Path","Machine")
  93. if ($currentPath -notlike "*$bin*") {
  94. Write-Log "Adding $bin to system PATH"
  95. setx PATH "$currentPath;$bin" | Out-Null
  96. } else {
  97. Write-Log "OpenSSL bin already in PATH"
  98. }
  99. }
  100. function Run-Cargo-Under-VcVars {
  101. param($vcvarsPath, $projectDir)
  102. if (-not (Test-Path $vcvarsPath)) {
  103. Write-Error "vcvars batch not found at $vcvarsPath"
  104. exit 1
  105. }
  106. if (-not (Test-Path $projectDir)) {
  107. Write-Error "Project dir not found: $projectDir"
  108. exit 1
  109. }
  110. Write-Log "Running cargo clean && cargo check inside Developer Command Prompt context..."
  111. $cmd = "cmd /c `"$vcvarsPath`" && cd /d `"$projectDir`" && cargo clean && cargo check"
  112. Write-Log "Executing: $cmd"
  113. & cmd /c "`"$vcvarsPath`" && cd /d `"$projectDir`" && cargo clean && cargo check"
  114. if ($LASTEXITCODE -ne 0) {
  115. Write-Error "cargo check failed. See output above."
  116. exit 1
  117. }
  118. Write-Log "cargo check succeeded."
  119. }
  120. ### Main
  121. Ensure-Administrator
  122. $opensslRoot = Ensure-OpenSSL-Installed
  123. Configure-OpenSSL-Env -opensslRoot $opensslRoot
  124. Ensure-Choco
  125. Install-VSBuildTools
  126. # Try to find vcvars soon after installation
  127. $vcvars = Find-VcVars
  128. if (-not $vcvars) {
  129. Write-Log "vcvars64.bat not found automatically. You should open 'x64 Native Tools Command Prompt for VS' manually and run the cargo commands."
  130. Write-Log "Script attempted to install Build Tools; if installation is still running or requires reboot, please restart your machine and then open 'x64 Native Tools Command Prompt for VS'."
  131. exit 0
  132. }
  133. # Run cargo inside MSVC environment
  134. $proj = "D:\code\Buff\Buff\client-tauri\src-tauri"
  135. Run-Cargo-Under-VcVars -vcvarsPath $vcvars -projectDir $proj
  136. Write-Log "All done. If cargo check passed, try 'npm run tauri:dev' from project root."