Repository navigation
Expand file tree
/
Copy pathbuild-rust-mingw.ps1
More file actions
303 lines (262 loc) · 11.1 KB
/
Copy pathbuild-rust-mingw.ps1
File metadata and controls
303 lines (262 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# PowerShell script to build ScaleIT Bridge with MinGW toolchain
# This script properly configures MinGW environment and builds the Rust project
function Stop-ExistingRustBuildProcesses {
$processNames = @("cargo", "rustc", "x86_64-w64-mingw32-gcc")
foreach ($processName in $processNames) {
try {
$running = Get-Process -Name $processName -ErrorAction SilentlyContinue
} catch {
continue
}
foreach ($proc in $running) {
Write-Host "Stopping lingering process: $($proc.ProcessName) (PID $($proc.Id))" -ForegroundColor Yellow
try {
Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue
} catch {
Write-Host "Unable to stop $($proc.ProcessName) (PID $($proc.Id)), continuing..." -ForegroundColor Red
}
}
}
}
function Reset-TargetDirectory {
param([string]$RootPath)
$targetDir = Join-Path $RootPath "src-rust\target"
if (-not (Test-Path $targetDir)) {
return
}
Write-Host "Resetting attributes under $targetDir to avoid permission issues..." -ForegroundColor Yellow
Get-ChildItem -Path $targetDir -Recurse -Force -ErrorAction SilentlyContinue | ForEach-Object {
if ($_.Attributes -band [System.IO.FileAttributes]::ReadOnly) {
$_.Attributes = $_.Attributes -band (-bnot [System.IO.FileAttributes]::ReadOnly)
}
}
Write-Host "Removing stale target directory before clean build..." -ForegroundColor Yellow
try {
Remove-Item -Recurse -Force -ErrorAction Stop $targetDir
Write-Host "Stale target directory removed successfully" -ForegroundColor Green
} catch {
Write-Host "Unable to remove target directory cleanly; cargo clean will continue." -ForegroundColor Yellow
}
}
Write-Host "Building ScaleIT Bridge with MinGW toolchain..." -ForegroundColor Green
Write-Host ""
# Ensure we run from the script directory even if the caller uses an absolute path
$repoRoot = Split-Path -Parent $MyInvocation.MyCommand.Definition
Set-Location $repoRoot
function Stop-AvgFirewall {
$serviceName = "AVG Firewall"
$service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
if ($service -and $service.Status -ne "Stopped") {
Write-Host "Stopping $serviceName to avoid permission locks..." -ForegroundColor Yellow
try {
Stop-Service -Name $serviceName -Force -ErrorAction Stop
} catch {
Write-Host ("Failed to stop {0}: {1}" -f $serviceName, $_.Exception.Message) -ForegroundColor Red
}
}
}
Stop-AvgFirewall
Stop-ExistingRustBuildProcesses
Reset-TargetDirectory -RootPath $repoRoot
# Set MinGW paths
$mingwPath = "D:\msys64\mingw64"
$mingwBinPath = "$mingwPath\bin"
$mingwCrossBinPath = "$mingwPath\x86_64-w64-mingw32\bin"
# Check if MSYS2 MinGW paths exist
if (-not (Test-Path $mingwPath)) {
Write-Host "Error: MSYS2 MinGW path not found: $mingwPath" -ForegroundColor Red
Write-Host "Please ensure MSYS2 is installed with MinGW64 environment." -ForegroundColor Red
exit 1
}
if (-not (Test-Path $mingwCrossBinPath)) {
Write-Host "Error: MinGW cross-compile tools not found: $mingwCrossBinPath" -ForegroundColor Red
Write-Host "Please install mingw-w64 toolchain in MSYS2:" -ForegroundColor Red
Write-Host " pacman -S mingw-w64-x86_64-toolchain" -ForegroundColor Yellow
exit 1
}
# Set environment variables for MinGW build
$env:PATH = "$mingwBinPath;$mingwCrossBinPath;$env:PATH"
$env:CC = "$mingwBinPath\gcc.exe"
$env:CXX = "$mingwBinPath\g++.exe"
$env:AR = "$mingwCrossBinPath\ar.exe"
$env:RANLIB = "$mingwCrossBinPath\ranlib.exe"
$env:CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER = "$mingwBinPath\gcc.exe"
$env:RUSTFLAGS = "-C target-cpu=native"
Write-Host "MinGW environment configured:" -ForegroundColor Yellow
Write-Host " MINGW_PATH: $mingwPath" -ForegroundColor Gray
Write-Host " CC: $env:CC" -ForegroundColor Gray
Write-Host " AR: $env:AR" -ForegroundColor Gray
Write-Host " LINKER: $env:CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER" -ForegroundColor Gray
Write-Host ""
# Verify tools are available
Write-Host "Verifying MinGW tools..." -ForegroundColor Yellow
$tools = @(
@{ Name = "gcc"; Path = "$mingwBinPath\gcc.exe" },
@{ Name = "dlltool"; Path = "$mingwCrossBinPath\dlltool.exe" },
@{ Name = "ar"; Path = "$mingwCrossBinPath\ar.exe" },
@{ Name = "ranlib"; Path = "$mingwCrossBinPath\ranlib.exe" }
)
foreach ($tool in $tools) {
if (Test-Path $tool.Path) {
Write-Host " checkmark $($tool.Name) found" -ForegroundColor Green
} else {
Write-Host " X $($tool.Name) not found at $($tool.Path)" -ForegroundColor Red
exit 1
}
}
Write-Host ""
# Set Rust toolchain to GNU
Write-Host "Setting Rust toolchain to GNU..." -ForegroundColor Yellow
try {
rustup default stable-x86_64-pc-windows-gnu
Write-Host "Rust GNU toolchain activated" -ForegroundColor Green
} catch {
Write-Host "Error setting Rust toolchain. Please run manually:" -ForegroundColor Red
Write-Host " rustup toolchain install stable-x86_64-pc-windows-gnu" -ForegroundColor White
Write-Host " rustup default stable-x86_64-pc-windows-gnu" -ForegroundColor White
exit 1
}
Write-Host ""
# Navigate to Rust source directory
if (-not (Test-Path "src-rust")) {
Write-Host "Error: src-rust directory not found" -ForegroundColor Red
exit 1
}
Set-Location "src-rust"
Write-Host "Changed to src-rust directory" -ForegroundColor Gray
# Clean previous build to ensure fresh build
Write-Host "Cleaning previous build..." -ForegroundColor Yellow
cargo clean
Write-Host " [OK] Build cache cleared" -ForegroundColor Green
# Remove existing executable to force rebuild
if ($buildMode -eq "release") {
$exeToRemove = "target\release\scaleit-bridge.exe"
} else {
$exeToRemove = "target\debug\scaleit-bridge.exe"
}
if (Test-Path $exeToRemove) {
Remove-Item $exeToRemove -Force -ErrorAction SilentlyContinue
Write-Host " [OK] Removed existing executable to force rebuild" -ForegroundColor Green
}
# Check for release build flag
$buildMode = "debug"
if ($args -contains "--release" -or $args -contains "-r") {
$buildMode = "release"
Write-Host "Building in RELEASE mode (optimized)" -ForegroundColor Cyan
} else {
Write-Host "Building in DEBUG mode (faster compilation)" -ForegroundColor Cyan
}
Write-Host ""
# Build the project
Write-Host "Building Rust project..." -ForegroundColor Yellow
Write-Host "This may take a few minutes on first build..." -ForegroundColor Gray
Write-Host ""
if ($buildMode -eq "release") {
cargo build --release | Out-Null
} else {
cargo build | Out-Null
}
if ($LASTEXITCODE -ne 0) {
Write-Host ""
Write-Host "Build failed!" -ForegroundColor Red
Write-Host "Check the error messages above for details." -ForegroundColor Yellow
Write-Host ""
Write-Host "Common solutions:" -ForegroundColor Cyan
Write-Host "1. Ensure MSYS2 MinGW64 is fully installed" -ForegroundColor White
Write-Host "2. Run: pacman -S mingw-w64-x86_64-toolchain" -ForegroundColor White
Write-Host "3. Check that no antivirus is blocking build files" -ForegroundColor White
Write-Host "4. Try stopping AVG Firewall: .\AVG_OFF.bat" -ForegroundColor White
exit 1
}
Write-Host ""
Write-Host "Build successful!" -ForegroundColor Green
Write-Host ""
# Run tests (unless --skip-tests flag is provided)
if ($args -notcontains "--skip-tests" -and $args -notcontains "--no-tests") {
Write-Host "Running tests..." -ForegroundColor Yellow
cargo test | Out-Null
$testExitCode = $LASTEXITCODE
if ($testExitCode -ne 0) {
Write-Host ""
Write-Host "Some tests failed, but build is complete." -ForegroundColor Yellow
Write-Host "Review test output above for details." -ForegroundColor Yellow
} else {
Write-Host ""
Write-Host "All tests passed! ✅" -ForegroundColor Green
}
# Reset exit code to 0 if build succeeded (tests are optional)
$LASTEXITCODE = 0
} else {
Write-Host "Skipping tests (--skip-tests flag provided)" -ForegroundColor Gray
Write-Host ""
}
Write-Host ""
Write-Host "════════════════════════════════════════" -ForegroundColor Green
Write-Host "Build complete!" -ForegroundColor Green
Write-Host "════════════════════════════════════════" -ForegroundColor Green
Write-Host ""
# Get version from Cargo.toml
$cargoFile = Join-Path $repoRoot "src-rust\Cargo.toml"
$version = "0.1.0"
if (Test-Path $cargoFile) {
$cargoContent = Get-Content $cargoFile
foreach ($line in $cargoContent) {
if ($line -match '^\s*version\s*=\s*"([^"]+)"') {
$version = $Matches[1]
break
}
}
}
# Get current branch
$currentBranch = git branch --show-current 2>$null
if (-not $currentBranch) {
$currentBranch = "unknown"
}
# Determine if we're on a non-main branch
$isMainBranch = ($currentBranch -eq "main" -or $currentBranch -eq "master")
$branchSuffix = ""
if (-not $isMainBranch) {
# Sanitize branch name for filename (remove special characters)
$sanitizedBranch = $currentBranch -replace '[^\w\-]', '-'
$branchSuffix = "-$sanitizedBranch"
}
# Build executable name with version and branch
$versionSuffix = "-v$version"
$exeBaseName = "scaleit-bridge$versionSuffix$branchSuffix"
if ($buildMode -eq "release") {
$originalExePath = "target\release\scaleit-bridge.exe"
$newExePath = "target\release\$exeBaseName.exe"
} else {
$originalExePath = "target\debug\scaleit-bridge.exe"
$newExePath = "target\debug\$exeBaseName.exe"
}
if (Test-Path $originalExePath) {
# Verify the executable was just built (check timestamp)
$exeInfo = Get-Item $originalExePath
$buildTime = Get-Date
$timeDiff = ($buildTime - $exeInfo.LastWriteTime).TotalSeconds
if ($timeDiff -gt 60) {
Write-Host "WARNING: Executable appears to be older than 60 seconds!" -ForegroundColor Yellow
Write-Host " Last modified: $($exeInfo.LastWriteTime)" -ForegroundColor Yellow
Write-Host " Current time: $buildTime" -ForegroundColor Yellow
Write-Host " This may indicate the build used cached files." -ForegroundColor Yellow
}
# Copy executable with new name
Copy-Item $originalExePath $newExePath -Force
$fileInfo = Get-Item $newExePath
Write-Host "Executable: $newExePath" -ForegroundColor Cyan
Write-Host "Size: $([math]::Round($fileInfo.Length / 1MB, 2)) MB" -ForegroundColor Cyan
Write-Host "Version: $version" -ForegroundColor Cyan
Write-Host "Branch: $currentBranch" -ForegroundColor Cyan
Write-Host "Last modified: $($fileInfo.LastWriteTime)" -ForegroundColor Cyan
Write-Host ""
}
Write-Host "Next steps:" -ForegroundColor Cyan
Write-Host " • Run server: cargo run" -ForegroundColor White
Write-Host " • Run with script: ..\run-backend.ps1" -ForegroundColor White
if ($buildMode -ne "release") {
Write-Host " • Build release: .\build-rust-mingw.ps1 --release" -ForegroundColor White
}
Write-Host ""
Write-Host "Server will be available at: http://localhost:8080" -ForegroundColor Yellow
Write-Host ""