-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathFwBuildEnvironment.psm1
More file actions
358 lines (306 loc) · 12.4 KB
/
FwBuildEnvironment.psm1
File metadata and controls
358 lines (306 loc) · 12.4 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<#
.SYNOPSIS
Visual Studio and build tool environment helpers for FieldWorks.
.DESCRIPTION
Provides VS environment initialization, MSBuild execution, and
VSTest path discovery.
.NOTES
Used by FwBuildHelpers.psm1 - do not import directly.
#>
# =============================================================================
# VS Environment Functions
# =============================================================================
function Get-PreferredVcToolBinPath {
<#
.SYNOPSIS
Returns the preferred HostX64\x64 MSVC tool bin directory.
#>
if (-not $env:VCINSTALLDIR) {
return $null
}
$toolsRoot = Join-Path $env:VCINSTALLDIR 'Tools\MSVC'
if (-not (Test-Path $toolsRoot)) {
return $null
}
$versionSort = {
$parsedVersion = [version]'0.0'
$isVersion = [version]::TryParse($_.Name, [ref]$parsedVersion)
if ($isVersion) {
return $parsedVersion
}
return [version]'0.0'
}
$sortProperties = @(
@{ Expression = {
$parsedVersion = [version]'0.0'
[version]::TryParse($_.Name, [ref]$parsedVersion)
}; Descending = $true }
@{ Expression = $versionSort; Descending = $true }
@{ Expression = { $_.Name }; Descending = $true }
)
$preferred = Get-ChildItem -Path $toolsRoot -Directory -ErrorAction SilentlyContinue |
Sort-Object -Property $sortProperties |
ForEach-Object { Join-Path $_.FullName 'bin\HostX64\x64' } |
Where-Object { Test-Path $_ } |
Select-Object -First 1
return $preferred
}
function Ensure-PreferredVcToolPath {
<#
.SYNOPSIS
Moves the preferred HostX64\x64 MSVC bin directory to the front of PATH.
#>
$preferred = Get-PreferredVcToolBinPath
if (-not $preferred) {
return
}
$pathEntries = @()
if (-not [string]::IsNullOrWhiteSpace($env:PATH)) {
$pathEntries = $env:PATH -split ';' | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
}
$filteredEntries = $pathEntries | Where-Object {
-not [string]::Equals($_.TrimEnd('\'), $preferred.TrimEnd('\'), [System.StringComparison]::OrdinalIgnoreCase)
}
$env:PATH = (@($preferred) + $filteredEntries) -join ';'
}
function Initialize-VsDevEnvironment {
<#
.SYNOPSIS
Initializes the Visual Studio Developer environment.
.DESCRIPTION
Sets up environment variables for native C++ compilation (x64 only).
Safe to call multiple times - will skip if already initialized.
#>
if ($env:OS -ne 'Windows_NT') {
return
}
if ($env:VCINSTALLDIR) {
Ensure-PreferredVcToolPath
Write-Host '[OK] Visual Studio environment already initialized' -ForegroundColor Green
return
}
Write-Host 'Initializing Visual Studio Developer environment...' -ForegroundColor Yellow
$vswhereCandidates = @()
if ($env:ProgramFiles) {
$pfVswhere = Join-Path -Path $env:ProgramFiles -ChildPath 'Microsoft Visual Studio\Installer\vswhere.exe'
if (Test-Path $pfVswhere) { $vswhereCandidates += $pfVswhere }
}
$programFilesX86 = ${env:ProgramFiles(x86)}
if ($programFilesX86) {
$pf86Vswhere = Join-Path -Path $programFilesX86 -ChildPath 'Microsoft Visual Studio\Installer\vswhere.exe'
if (Test-Path $pf86Vswhere) { $vswhereCandidates += $pf86Vswhere }
}
if (-not $vswhereCandidates) {
Write-Host ''
Write-Host '[ERROR] Visual Studio 2017+ not found' -ForegroundColor Red
Write-Host ' Install from: https://visualstudio.microsoft.com/downloads/' -ForegroundColor Yellow
throw 'Visual Studio not found'
}
$vsInstallPath = & $vswhereCandidates[0] -latest -requires Microsoft.Component.MSBuild Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -products * -property installationPath
if (-not $vsInstallPath) {
Write-Host ''
Write-Host '[ERROR] Visual Studio found but missing required C++ tools' -ForegroundColor Red
Write-Host ' Please install the "Desktop development with C++" workload' -ForegroundColor Yellow
throw 'Visual Studio C++ tools not found'
}
$vsDevCmd = Join-Path -Path $vsInstallPath -ChildPath 'Common7\Tools\VsDevCmd.bat'
if (-not (Test-Path $vsDevCmd)) {
throw "Unable to locate VsDevCmd.bat under '$vsInstallPath'."
}
# x64-only build
$arch = 'amd64'
$vsVersion = Split-Path (Split-Path (Split-Path (Split-Path $vsInstallPath))) -Leaf
Write-Host " Found Visual Studio $vsVersion at: $vsInstallPath" -ForegroundColor Gray
Write-Host " Setting up environment for $arch..." -ForegroundColor Gray
$cmdArgs = "`"$vsDevCmd`" -no_logo -arch=$arch -host_arch=$arch && set"
$envOutput = & cmd.exe /c $cmdArgs 2>&1
if ($LASTEXITCODE -ne 0) {
throw 'Failed to initialize Visual Studio environment'
}
foreach ($line in $envOutput) {
$parts = $line -split '=', 2
if ($parts.Length -eq 2 -and $parts[0]) {
Set-Item -Path "Env:$($parts[0])" -Value $parts[1]
}
}
if (-not $env:VCINSTALLDIR) {
throw 'Visual Studio C++ environment not configured'
}
Ensure-PreferredVcToolPath
Write-Host '[OK] Visual Studio environment initialized successfully' -ForegroundColor Green
Write-Host " VCINSTALLDIR: $env:VCINSTALLDIR" -ForegroundColor Gray
}
function Get-CvtresDiagnostics {
<#
.SYNOPSIS
Returns details about the cvtres.exe resolved in the current session.
#>
$result = [ordered]@{
Path = $null
IsVcToolset = $false
IsDotNetFramework = $false
}
$cmd = Get-Command "cvtres.exe" -ErrorAction SilentlyContinue
if ($cmd) {
$result.Path = $cmd.Source
$lower = $result.Path.ToLowerInvariant()
$result.IsVcToolset = $lower -match "[\\/]vc[\\/]tools[\\/]msvc[\\/][^\\/]+[\\/]bin[\\/]hostx64[\\/]x64[\\/]cvtres\.exe$"
$result.IsDotNetFramework = $lower -match "windows[\\/]microsoft\.net[\\/]framework"
return $result
}
if ($env:VCINSTALLDIR) {
$candidates = Get-ChildItem -Path (Join-Path $env:VCINSTALLDIR "Tools\MSVC\*") -Filter cvtres.exe -Recurse -ErrorAction SilentlyContinue |
Sort-Object FullName -Descending
if ($candidates -and $candidates.Count -gt 0) {
$result.Path = $candidates[0].FullName
$lower = $result.Path.ToLowerInvariant()
$result.IsVcToolset = $lower -match "[\\/]vc[\\/]tools[\\/]msvc[\\/][^\\/]+[\\/]bin[\\/]hostx64[\\/]x64[\\/]cvtres\.exe$"
$result.IsDotNetFramework = $lower -match "windows[\\/]microsoft\.net[\\/]framework"
}
}
return $result
}
function Test-CvtresCompatibility {
<#
.SYNOPSIS
Emits warnings if cvtres.exe resolves to a non-VC toolset binary.
#>
$diag = Get-CvtresDiagnostics
if (-not $diag.Path) {
Write-Host "[WARN] cvtres.exe not found after VS environment setup. Toolchain may be incomplete." -ForegroundColor Yellow
return
}
if ($diag.IsDotNetFramework) {
Write-Host "[WARN] cvtres.exe resolves to a .NET Framework path. Prefer the VC toolset version (Hostx64\\x64). $($diag.Path)" -ForegroundColor Yellow
}
elseif (-not $diag.IsVcToolset) {
Write-Host "[WARN] cvtres.exe is not from the VC toolset Hostx64\\x64 folder. Confirm PATH ordering. $($diag.Path)" -ForegroundColor Yellow
}
}
# =============================================================================
# MSBuild Helper Functions
# =============================================================================
function Get-MSBuildPath {
<#
.SYNOPSIS
Gets the path to MSBuild.exe.
.DESCRIPTION
Returns the MSBuild command, either from PATH or 'msbuild' as fallback.
#>
$msbuildCmd = Get-Command msbuild -ErrorAction SilentlyContinue
if ($msbuildCmd) {
return $msbuildCmd.Source
}
return 'msbuild'
}
function Invoke-MSBuild {
<#
.SYNOPSIS
Executes MSBuild with proper error handling.
.DESCRIPTION
Runs MSBuild with the specified arguments and handles errors appropriately.
.PARAMETER Arguments
Array of arguments to pass to MSBuild.
.PARAMETER Description
Human-readable description of the build step.
.PARAMETER LogPath
Optional path to write build output to a log file.
.PARAMETER TailLines
If specified, only displays the last N lines of output.
#>
param(
[Parameter(Mandatory)]
[string[]]$Arguments,
[Parameter(Mandatory)]
[string]$Description,
[string]$LogPath = '',
[int]$TailLines = 0
)
$msbuildCmd = Get-MSBuildPath
Write-Host "Running $Description..." -ForegroundColor Cyan
if ($TailLines -gt 0) {
# Capture all output, optionally log to file, then display tail
$output = & $msbuildCmd $Arguments 2>&1 | ForEach-Object { $_.ToString() }
$exitCode = $LASTEXITCODE
if ($LogPath) {
$logDir = Split-Path -Parent $LogPath
if ($logDir -and -not (Test-Path $logDir)) {
New-Item -Path $logDir -ItemType Directory -Force | Out-Null
}
$output | Out-File -FilePath $LogPath -Encoding utf8
}
# Display last N lines
$totalLines = $output.Count
if ($totalLines -gt $TailLines) {
Write-Host "... ($($totalLines - $TailLines) lines omitted, showing last $TailLines) ..." -ForegroundColor DarkGray
$output | Select-Object -Last $TailLines | ForEach-Object { Write-Host $_ }
}
else {
$output | ForEach-Object { Write-Host $_ }
}
$LASTEXITCODE = $exitCode
}
elseif ($LogPath) {
$logDir = Split-Path -Parent $LogPath
if ($logDir -and -not (Test-Path $logDir)) {
New-Item -Path $logDir -ItemType Directory -Force | Out-Null
}
& $msbuildCmd $Arguments | Tee-Object -FilePath $LogPath
}
else {
& $msbuildCmd $Arguments
}
if ($LASTEXITCODE -ne 0) {
$errorMsg = "MSBuild failed during $Description with exit code $LASTEXITCODE"
if ($LASTEXITCODE -eq -1073741819) {
$errorMsg += " (0xC0000005 - Access Violation). This indicates a crash in native code during build."
}
throw $errorMsg
}
}
# =============================================================================
# VSTest Helper Functions
# =============================================================================
function Get-VSTestPath {
<#
.SYNOPSIS
Finds vstest.console.exe in PATH or known locations.
.DESCRIPTION
First checks PATH, then falls back to known VS installation paths.
#>
# Try PATH first (setup scripts add vstest to PATH)
$vstestFromPath = Get-Command "vstest.console.exe" -ErrorAction SilentlyContinue
if ($vstestFromPath) {
return $vstestFromPath.Source
}
# Fall back to known installation paths
$programFilesX86 = ${env:ProgramFiles(x86)}
if (-not $programFilesX86) { $programFilesX86 = "C:\Program Files (x86)" }
$vstestCandidates = @(
# BuildTools
"$programFilesX86\Microsoft Visual Studio\2022\BuildTools\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe",
"C:\BuildTools\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe",
# TestAgent (sometimes installed separately)
"$programFilesX86\Microsoft Visual Studio\2022\TestAgent\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe",
# Full VS installations
"${env:ProgramFiles}\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe",
"${env:ProgramFiles}\Microsoft Visual Studio\2022\Professional\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe",
"${env:ProgramFiles}\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe"
)
foreach ($candidate in $vstestCandidates) {
if (Test-Path $candidate) {
return $candidate
}
}
return $null
}
# =============================================================================
# Module Exports
# =============================================================================
Export-ModuleMember -Function @(
'Initialize-VsDevEnvironment',
'Test-CvtresCompatibility',
'Get-MSBuildPath',
'Invoke-MSBuild',
'Get-VSTestPath'
)