Repository navigation
Expand file tree
/
Copy pathVsWhereHelper.cs
More file actions
345 lines (293 loc) · 12.8 KB
/
Copy pathVsWhereHelper.cs
File metadata and controls
345 lines (293 loc) · 12.8 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
using System.Runtime.InteropServices;
using System.Text.Json;
namespace MSBuild.CompileCommands.Extractor
{
public class VsWhereResult
{
public string? InstallationPath { get; set; }
public string? VCTargetsPath { get; set; }
public string? VCToolsInstallDir { get; set; }
}
public record VsInstance(
string InstanceId,
string DisplayName,
string InstallationPath,
string Version,
bool HasVcTools,
string? VCTargetsPath,
string? VCToolsInstallDir
);
/// <summary>
/// Discovers Visual Studio installations and VC++ tool paths using vswhere.exe.
/// Also resolves the real cl.exe path from VCToolsInstallDir.
/// </summary>
public static class VsWhereHelper
{
private static readonly string[] VCTargetVersionDirs = ["v180", "v170", "v160", "v150"];
/// <summary>
/// Lists all Visual Studio installations found by vswhere.exe.
/// </summary>
public static List<VsInstance> ListInstances(bool enableLogger = false)
{
var results = new List<VsInstance>();
var vswhereExe = FindVsWhere();
if (vswhereExe == null)
{
if (enableLogger)
Console.WriteLine("VsWhere: vswhere.exe not found");
return results;
}
try
{
var startInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = vswhereExe,
Arguments = "-all -prerelease -format json",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using var process = System.Diagnostics.Process.Start(startInfo);
if (process == null) return results;
var stdoutTask = process.StandardOutput.ReadToEndAsync();
var stderrTask = process.StandardError.ReadToEndAsync();
if (!process.WaitForExit(10000))
{
try { process.Kill(true); } catch { }
return results;
}
var output = stdoutTask.Result;
// Consume stderr to prevent hang
_ = stderrTask.Result;
if (process.ExitCode != 0 || string.IsNullOrWhiteSpace(output))
return results;
using var doc = JsonDocument.Parse(output);
var root = doc.RootElement;
if (root.ValueKind != JsonValueKind.Array)
return results;
foreach (var entry in root.EnumerateArray())
{
var instanceId = entry.TryGetProperty("instanceId", out var idEl) ? idEl.GetString() : null;
var displayName = entry.TryGetProperty("displayName", out var nameEl) ? nameEl.GetString() : null;
var installPath = entry.TryGetProperty("installationPath", out var pathEl) ? pathEl.GetString() : null;
var version = entry.TryGetProperty("installationVersion", out var verEl) ? verEl.GetString() : null;
if (instanceId == null || installPath == null)
continue;
var vcTargetsPath = FindVCTargetsPath(installPath);
var vcToolsInstallDir = FindVCToolsInstallDir(installPath);
var hasVcTools = vcTargetsPath != null || vcToolsInstallDir != null;
results.Add(new VsInstance(
InstanceId: instanceId,
DisplayName: displayName ?? "(unknown)",
InstallationPath: installPath,
Version: version ?? "(unknown)",
HasVcTools: hasVcTools,
VCTargetsPath: vcTargetsPath,
VCToolsInstallDir: vcToolsInstallDir
));
}
}
catch (Exception ex)
{
if (enableLogger)
Console.WriteLine($"VsWhere: Error listing instances: {ex.Message}");
}
return results;
}
/// <summary>
/// Auto-detects a Visual Studio installation with C++ tools via vswhere.exe.
/// Returns null if vswhere is not found or no suitable installation exists.
/// </summary>
public static VsWhereResult? DetectVisualStudio(bool enableLogger = false)
{
var vswhereExe = FindVsWhere();
if (vswhereExe == null)
{
if (enableLogger)
Console.WriteLine("VsWhere: vswhere.exe not found");
return null;
}
var installPath = RunVsWhere(vswhereExe, enableLogger);
if (installPath == null)
{
if (enableLogger)
Console.WriteLine("VsWhere: No Visual Studio installation with C++ tools found");
return null;
}
var result = new VsWhereResult { InstallationPath = installPath };
result.VCTargetsPath = FindVCTargetsPath(installPath);
result.VCToolsInstallDir = FindVCToolsInstallDir(installPath);
if (enableLogger)
{
Console.WriteLine("VsWhere Detection:");
Console.WriteLine($" InstallationPath: {result.InstallationPath}");
Console.WriteLine($" VCTargetsPath: {result.VCTargetsPath ?? "(not found)"}");
Console.WriteLine($" VCToolsInstallDir: {result.VCToolsInstallDir ?? "(not found)"}");
Console.WriteLine();
}
return result;
}
private static string? FindVsWhere()
{
var programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
if (!string.IsNullOrEmpty(programFilesX86))
{
var path = Path.Combine(programFilesX86, "Microsoft Visual Studio", "Installer", "vswhere.exe");
if (File.Exists(path))
return path;
}
// Fallback: check PATH
try
{
var startInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = "where",
Arguments = "vswhere.exe",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using var process = System.Diagnostics.Process.Start(startInfo);
if (process != null)
{
var output = process.StandardOutput.ReadLine();
process.WaitForExit(5000);
if (process.ExitCode == 0 && !string.IsNullOrEmpty(output) && File.Exists(output))
return output;
}
}
catch { }
return null;
}
private static string? RunVsWhere(string vswhereExe, bool enableLogger)
{
try
{
var startInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = vswhereExe,
Arguments = "-products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -latest -prerelease -format json",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using var process = System.Diagnostics.Process.Start(startInfo);
if (process == null) return null;
var stdoutTask = process.StandardOutput.ReadToEndAsync();
var stderrTask = process.StandardError.ReadToEndAsync();
if (!process.WaitForExit(10000))
{
try { process.Kill(true); } catch { }
return null;
}
var output = stdoutTask.Result;
if (enableLogger)
Console.WriteLine($"VsWhere: Exit code {process.ExitCode}");
if (process.ExitCode != 0 || string.IsNullOrWhiteSpace(output))
return null;
using var doc = JsonDocument.Parse(output);
var root = doc.RootElement;
if (root.ValueKind == JsonValueKind.Array && root.GetArrayLength() > 0)
{
if (root[0].TryGetProperty("installationPath", out var pathElement))
return pathElement.GetString();
}
}
catch (Exception ex)
{
if (enableLogger)
Console.WriteLine($"VsWhere: Error running vswhere: {ex.Message}");
}
return null;
}
public static string? FindVCTargetsPath(string vsInstallPath)
{
foreach (var version in VCTargetVersionDirs)
{
var path = Path.Combine(vsInstallPath, "MSBuild", "Microsoft", "VC", version);
if (Directory.Exists(path))
return path;
}
var legacyPath = Path.Combine(vsInstallPath, "Common7", "IDE", "VC", "VCTargets");
if (Directory.Exists(legacyPath))
return legacyPath;
return null;
}
public static string? FindVCToolsInstallDir(string vsInstallPath)
{
var msvcRoot = Path.Combine(vsInstallPath, "VC", "Tools", "MSVC");
if (!Directory.Exists(msvcRoot))
return null;
var versions = Directory.GetDirectories(msvcRoot)
.Select(d => new { Path = d, Name = Path.GetFileName(d) })
.Where(d => Version.TryParse(d.Name, out _))
.OrderByDescending(d => Version.Parse(d.Name))
.ToList();
return versions.Count > 0 ? versions[0].Path : null;
}
/// <summary>
/// Resolves the real cl.exe path from VCToolsInstallDir and target platform.
/// Maps the platform name to a target architecture and probes for cl.exe
/// under the appropriate Host<arch>/<target> directory.
/// </summary>
public static string? ResolveClExePath(string? vcToolsInstallDir, string platform)
{
if (string.IsNullOrEmpty(vcToolsInstallDir))
return null;
var hostArch = RuntimeInformation.OSArchitecture switch
{
Architecture.X64 => "Hostx64",
Architecture.X86 => "Hostx86",
Architecture.Arm64 => "HostARM64",
_ => "Hostx64"
};
var targetArch = MapPlatformToTarget(platform);
// Try the natural host/target combination first
var clPath = Path.Combine(vcToolsInstallDir, "bin", hostArch, targetArch, "cl.exe");
if (File.Exists(clPath))
return clPath;
// Fallback: try other host architectures
foreach (var host in new[] { "Hostx64", "Hostx86", "HostARM64" })
{
if (host == hostArch) continue;
clPath = Path.Combine(vcToolsInstallDir, "bin", host, targetArch, "cl.exe");
if (File.Exists(clPath))
return clPath;
}
return null;
}
private static string MapPlatformToTarget(string platform)
{
return platform.ToLowerInvariant() switch
{
"x64" => "x64",
"win32" => "x86",
"x86" => "x86",
"arm" => "arm",
"arm64" => "arm64",
"arm64ec" => "arm64",
_ => "x64"
};
}
/// <summary>
/// Finds the latest installed Windows 10/11 SDK version by scanning
/// the Include directory under "Windows Kits\10".
/// Returns null if no SDK is installed.
/// </summary>
public static string? FindLatestWindowsSdkVersion()
{
var sdkInclude = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
"Windows Kits", "10", "Include");
if (!Directory.Exists(sdkInclude))
return null;
return Directory.GetDirectories(sdkInclude)
.Select(d => Path.GetFileName(d))
.Where(v => v != null && v.StartsWith("10.0.") && Version.TryParse(v, out _))
.OrderByDescending(v => Version.Parse(v!))
.FirstOrDefault();
}
}
}