-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathOutOfProcessExtractor.cs
More file actions
1194 lines (1064 loc) · 53.1 KB
/
Copy pathOutOfProcessExtractor.cs
File metadata and controls
1194 lines (1064 loc) · 53.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Microsoft.Build.Logging.StructuredLogger;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Xml.Linq;
namespace MSBuild.CompileCommands.Extractor
{
public enum MsBuildLauncher { Auto, Cmd, Direct, Dotnet }
public enum IncludePathOrder { Auto, Prepend, Append }
public class OutOfProcessExtractor : ICompileCommandsExtractor
{
private readonly string _msbuildPath;
private readonly string _projectPath;
private readonly string _configuration;
private readonly string _platform;
private readonly bool _enableLogger;
private readonly string? _solutionDir;
private readonly string? _vcToolsInstallDir;
private readonly string? _vcTargetsPath;
private readonly string? _clPath;
private readonly IReadOnlyDictionary<string, string> _userProperties;
private readonly IReadOnlyDictionary<string, string> _userEnv;
private readonly MsBuildLauncher _launcher;
private readonly IncludePathOrder _includePathOrder;
private readonly bool _emitDefaults;
private readonly bool _mergeDefaults;
public OutOfProcessExtractor(
string msbuildPath,
string projectPath,
string configuration = "Debug",
string platform = "x64",
bool enableLogger = false,
string? solutionDir = null,
string? vcToolsInstallDir = null,
string? vcTargetsPath = null,
string? clPath = null,
IReadOnlyDictionary<string, string>? msbuildProperties = null,
IReadOnlyDictionary<string, string>? msbuildEnv = null,
MsBuildLauncher launcher = MsBuildLauncher.Auto,
IncludePathOrder includePathOrder = IncludePathOrder.Auto,
bool emitDefaults = false,
bool mergeDefaults = false)
{
_msbuildPath = msbuildPath;
_projectPath = Path.GetFullPath(projectPath);
_configuration = configuration;
_platform = platform;
_enableLogger = enableLogger;
_solutionDir = solutionDir;
_vcToolsInstallDir = vcToolsInstallDir?.TrimEnd('\\');
if (_vcToolsInstallDir != null && !_vcToolsInstallDir.StartsWith(@"\\"))
_vcToolsInstallDir = _vcToolsInstallDir.Replace(@"\\", @"\");
_vcTargetsPath = vcTargetsPath;
_clPath = clPath;
_userProperties = msbuildProperties ?? new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
_userEnv = msbuildEnv ?? new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
_launcher = launcher;
_includePathOrder = includePathOrder;
_emitDefaults = emitDefaults;
_mergeDefaults = mergeDefaults;
if (!File.Exists(_msbuildPath))
throw new FileNotFoundException($"MSBuild not found: {_msbuildPath}");
if (_msbuildPath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
{
// MSBuild.dll from a .NET SDK can't be launched directly, use dotnet exec
var dotnetPath = FindDotnetExe();
if (dotnetPath == null)
throw new FileNotFoundException(
$"MSBuild path '{_msbuildPath}' is a .dll and requires 'dotnet' to execute, but dotnet was not found on PATH.");
_dotnetExePath = dotnetPath;
}
if (!File.Exists(_projectPath))
throw new FileNotFoundException($"Project file not found: {_projectPath}");
}
private readonly string? _dotnetExePath;
private string[] _externalIncludePaths = [];
private string[] _includePaths = [];
private IReadOnlyDictionary<string, string>? _evaluatedProperties;
private static readonly char[] CmdMetaChars =
{ ' ', '\t', ';', '&', '|', '^', '<', '>', '(', ')' };
/// <summary>
/// Quotes a command-line argument for cmd.exe. Handles whitespace, cmd
/// metacharacters (; & | ^ < > ( )), and trailing backslashes
/// that would otherwise escape the closing quote.
/// </summary>
private static string QuoteCmdArg(string arg)
{
if (arg.StartsWith('"') && arg.EndsWith('"')) return arg;
if (arg.IndexOfAny(CmdMetaChars) < 0) return arg;
int trailingBackslashes = 0;
for (int i = arg.Length - 1; i >= 0 && arg[i] == '\\'; i--) trailingBackslashes++;
return "\"" + arg + new string('\\', trailingBackslashes) + "\"";
}
private static string? TryResolveMsBuildExpression(string expr)
{
var match = Regex.Match(expr, @"\$\(\[MSBuild\]::NormalizePath\('([^']+)'(?:.*?)(?:\)\))?(.*)$");
if (match.Success)
{
var basePath = match.Groups[1].Value;
var suffix = match.Groups[2].Value.TrimStart('\\', '\'', ',', ' ');
try
{
var resolved = Path.GetFullPath(Path.Combine(basePath, suffix));
if (Directory.Exists(resolved))
return resolved;
}
catch { }
}
return null;
}
private static IEnumerable<string> ResolveIncludePathEntries(IEnumerable<string> entries)
{
foreach (var entry in entries)
{
if (string.IsNullOrWhiteSpace(entry)) continue;
if (!entry.StartsWith("$("))
{
string resolved;
try { resolved = Uri.UnescapeDataString(entry.Trim()); } catch { resolved = entry.Trim(); }
if (!string.IsNullOrEmpty(resolved))
yield return resolved;
}
else
{
var resolved = TryResolveMsBuildExpression(entry);
if (resolved != null)
yield return resolved;
}
}
}
private static IEnumerable<string> GetVsAuxiliaryIncludePaths(string? vcToolsInstallDir)
{
if (string.IsNullOrEmpty(vcToolsInstallDir)) return [];
try
{
var vcDir = Path.GetFullPath(Path.Combine(vcToolsInstallDir, "..", "..", ".."));
var results = new List<string>();
var auxInclude = Path.Combine(vcDir, "Auxiliary", "VS", "include");
if (Directory.Exists(auxInclude))
results.Add(auxInclude);
var auxUnitTest = Path.Combine(vcDir, "Auxiliary", "VS", "UnitTest", "include");
if (Directory.Exists(auxUnitTest))
results.Add(auxUnitTest);
return results;
}
catch { return []; }
}
private static string? FindDotnetExe()
{
var pathVar = Environment.GetEnvironmentVariable("PATH") ?? "";
var exeName = OperatingSystem.IsWindows() ? "dotnet.exe" : "dotnet";
foreach (var dir in pathVar.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries))
{
var candidate = Path.Combine(dir.Trim(), exeName);
if (File.Exists(candidate))
return candidate;
}
return null;
}
public List<CompileCommand> ExtractCompileCommands()
{
var entries = GetClCommandLines();
var commands = entries.Count > 0 ? ToCompileCommands(entries) : new List<CompileCommand>();
if (commands.Count > 0)
return commands;
// Fallback for GN-generated projects (Chromium, Crashpad, WebRTC)
var fallback = TryExtractFromItemDefinitionGroup();
if (fallback.Count > 0)
{
Console.Error.WriteLine($"Info: Used ItemDefinitionGroup fallback for {_projectPath} ({fallback.Count} entries)");
return fallback;
}
Console.Error.WriteLine($"Warning: No compile commands found in {_projectPath}");
return new List<CompileCommand>();
}
public void WriteCompileCommandsJson(string? outputPath = null)
{
var commands = ExtractCompileCommands();
outputPath ??= Path.Combine(Path.GetDirectoryName(_projectPath)!, "compile_commands.json");
var json = JsonSerializer.Serialize(commands, new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
File.WriteAllText(outputPath, json);
}
private List<ClCommandLineEntry> GetClCommandLines()
{
var binlogPath = Path.Combine(Path.GetTempPath(), $"msbuild_{Guid.NewGuid():N}.binlog");
try
{
RunMSBuild(binlogPath);
return ParseBinlog(binlogPath);
}
catch (Exception ex)
{
// GN-generated projects may fail the design-time build.
// Return empty to trigger the ItemDefinitionGroup fallback.
if (_enableLogger)
Console.Error.WriteLine($"Design-time build failed for {_projectPath}: {ex.Message} (will try fallback)");
return new List<ClCommandLineEntry>();
}
finally
{
try { if (File.Exists(binlogPath) && !_enableLogger) File.Delete(binlogPath); }
catch { /* cleanup failure is not fatal */ }
}
}
private void RunMSBuild(string binlogPath)
{
var properties = new List<KeyValuePair<string, string>>
{
new("Configuration", _configuration),
new("Platform", _platform),
new("DesignTimeBuild", "true"),
new("BuildingInsideVisualStudio", "true"),
// Design-time builds should not build referenced projects.
new("BuildProjectReferences", "false"),
// Stable locale for MSBuild error strings.
new("LangID", "1033")
};
// Pass MicrosoftBuildCppTasksCommonPath if VCTargetsPath is set (needed for custom MSBuild environments)
if (_vcTargetsPath != null)
{
properties.Add(new("MicrosoftBuildCppTasksCommonPath", _vcTargetsPath.EndsWith('\\') ? _vcTargetsPath : _vcTargetsPath + "\\"));
}
if (_solutionDir != null)
{
var dir = _solutionDir.EndsWith('\\') ? _solutionDir : _solutionDir + "\\";
properties.Add(new("SolutionDir", dir));
}
// Skip VCToolsInstallDir when MicrosoftBuildCppTasksCommonPath is set
// (custom MSBuild environments manage their own tool paths)
if (_vcToolsInstallDir != null && !properties.Any(p => p.Key == "MicrosoftBuildCppTasksCommonPath"))
{
var dir = _vcToolsInstallDir.StartsWith(@"\\")
? _vcToolsInstallDir
: _vcToolsInstallDir.Replace(@"\\", @"\");
dir = dir.EndsWith('\\') ? dir : dir + "\\";
properties.Add(new("VCToolsInstallDir", dir));
// Derive VCInstallDir from VCToolsInstallDir (3 levels up)
// to prevent unresolved fallback values in repo props files.
try
{
var vcInstallDir = Path.GetFullPath(Path.Combine(dir, "..", "..", ".."));
if (Directory.Exists(vcInstallDir))
{
var vcNorm = vcInstallDir.EndsWith('\\') ? vcInstallDir : vcInstallDir + "\\";
properties.Add(new("VCInstallDir", vcNorm));
}
}
catch { }
}
// Detect and set NETFXKitsDir to prevent unresolved fallback values
var netfxKitsDir = InProcessExtractor.FindNETFXKitsDir();
if (netfxKitsDir != null && !properties.Any(p => p.Key.Equals("NETFXKitsDir", StringComparison.OrdinalIgnoreCase)))
properties.Add(new("NETFXKitsDir", netfxKitsDir));
// Apply user-supplied --msbuild-property overrides last so they win over our defaults.
foreach (var kv in _userProperties)
{
properties.RemoveAll(p => string.Equals(p.Key, kv.Key, StringComparison.OrdinalIgnoreCase));
properties.Add(new(kv.Key, kv.Value));
}
// Don't set WindowsTargetPlatformVersion as a global property. It would
// override projects with explicit full versions (e.g. 10.0.19041.0).
// Instead, set as environment variable which has lower precedence in MSBuild.
var latestSdk = VsWhereHelper.FindLatestWindowsSdkVersion();
if (latestSdk != null)
{
Environment.SetEnvironmentVariable("WindowsTargetPlatformVersion", latestSdk);
}
// Use ArgumentList to avoid shell quoting issues
// with paths containing spaces and trailing backslashes
var startInfo = new System.Diagnostics.ProcessStartInfo
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
// For .cmd/.bat MSBuild wrappers, use cmd.exe /c to ensure proper
// batch script execution (init scripts, environment setup, etc.)
// The default (auto) sniffs the file extension; users may override
// via --msbuild-launcher cmd|direct|dotnet to force a specific mode.
bool isCmdWrapper;
switch (_launcher)
{
case MsBuildLauncher.Cmd:
isCmdWrapper = true;
break;
case MsBuildLauncher.Direct:
isCmdWrapper = false;
break;
case MsBuildLauncher.Dotnet:
// Force dotnet exec path; treat like a .dll (handled below).
isCmdWrapper = false;
break;
default: // Auto
isCmdWrapper = _msbuildPath.EndsWith(".cmd", StringComparison.OrdinalIgnoreCase)
|| _msbuildPath.EndsWith(".bat", StringComparison.OrdinalIgnoreCase);
break;
}
if (_dotnetExePath != null || _launcher == MsBuildLauncher.Dotnet)
{
// MSBuild.dll: run via "dotnet exec MSBuild.dll ..."
var dotnet = _dotnetExePath ?? FindDotnetExe()
?? throw new FileNotFoundException("--msbuild-launcher dotnet was requested but 'dotnet' was not found on PATH.");
startInfo.FileName = dotnet;
startInfo.ArgumentList.Add("exec");
startInfo.ArgumentList.Add(_msbuildPath);
}
else if (isCmdWrapper)
{
// For .cmd wrappers: build a single command string for cmd.exe /c
// This ensures batch init scripts run correctly
startInfo.FileName = "cmd.exe";
}
else
{
startInfo.FileName = _msbuildPath;
}
var msbuildArgs = new List<string>();
msbuildArgs.Add(_projectPath);
// Batch design-time targets. Even if some are unknown in older or custom .targets chains,
// MSBuild skips missing targets with a warning rather than failing,
// as long as they appear in a semicolon list after a known target.
// ComputeReferenceCLInput is kept first so that referenced public include directories
// are folded into ClCompile metadata before GetClCommandLines evaluates.
msbuildArgs.Add("/t:ComputeReferenceCLInput;GetProjectDirectories;GetClCommandLines");
// Use separate /p: per property to avoid issues with semicolons
// or special characters in property values
foreach (var prop in properties)
msbuildArgs.Add($"/p:{prop.Key}={prop.Value}");
msbuildArgs.Add($"/bl:{binlogPath}");
msbuildArgs.Add("/nologo");
msbuildArgs.Add("/v:quiet");
if (isCmdWrapper)
{
// Build single command string for cmd.exe /c.
var quotedArgs = msbuildArgs.Select(QuoteCmdArg);
// Wrap the whole command in outer quotes: cmd /c "..." with multiple quoted
// args inside. Per `cmd /?`, when more than two quotes are present cmd strips
// the first and last quote and passes the rest through verbatim, so the outer
// pair protects the inner quoting.
startInfo.Arguments = $"/c \"\"{_msbuildPath}\" {string.Join(" ", quotedArgs)}\"";
// When using a .cmd/.bat wrapper that launches .NET Framework MSBuild,
// clear .NET SDK environment variables inherited from the dotnet host process.
// These vars (MSBuildExtensionsPath, MSBUILD_EXE_PATH, etc.) cause .NET Framework
// MSBuild to resolve targets from the .NET SDK instead of its own directory,
// breaking target resolution (e.g., ResolveReferences not found).
var dotnetSdkVarsToClean = new[]
{
"MSBuildExtensionsPath",
"MSBuildSDKsPath",
"MSBUILD_EXE_PATH",
"MSBuildLoadMicrosoftTargetsReadOnly",
"MSBUILDFAILONDRIVEENUMERATINGWILDCARD",
"_MSBUILDTLENABLED",
"DOTNET_HOST_PATH",
"VCTargetsPath"
};
foreach (var varName in dotnetSdkVarsToClean)
{
startInfo.Environment.Remove(varName);
}
}
else
{
foreach (var arg in msbuildArgs)
startInfo.ArgumentList.Add(arg);
}
// When the host process is a .NET SDK app (like this extractor),
// clear .NET SDK environment variables that would cause .NET Framework MSBuild
// to resolve targets from the wrong location.
// This applies to both .cmd wrappers and direct MSBuild.exe launches.
if (!isCmdWrapper)
{
var dotnetSdkVarsToClean = new[]
{
"MSBuildExtensionsPath",
"MSBuildSDKsPath",
"MSBUILD_EXE_PATH",
"MSBuildLoadMicrosoftTargetsReadOnly",
"MSBUILDFAILONDRIVEENUMERATINGWILDCARD",
"_MSBUILDTLENABLED",
"DOTNET_HOST_PATH"
};
foreach (var varName in dotnetSdkVarsToClean)
{
startInfo.Environment[varName] = "";
}
}
// Set environment variables for non-.cmd cases
if (!isCmdWrapper)
{
// Only set VCTargetsPath env var if not using MicrosoftBuildCppTasksCommonPath
if (_vcTargetsPath != null && !properties.Any(p => p.Key == "MicrosoftBuildCppTasksCommonPath"))
{
var path = _vcTargetsPath.EndsWith('\\') ? _vcTargetsPath : _vcTargetsPath + "\\";
startInfo.Environment["VCTargetsPath"] = path;
}
if (_vcToolsInstallDir != null)
{
var dir = _vcToolsInstallDir.StartsWith(@"\\")
? _vcToolsInstallDir
: _vcToolsInstallDir.Replace(@"\\", @"\");
dir = dir.EndsWith('\\') ? dir : dir + "\\";
startInfo.Environment["VCToolsInstallDir"] = dir;
}
}
// Apply user-supplied --msbuild-env overrides last so they win over our defaults
// (including the .NET-SDK clear-list above and VCTargetsPath/VCToolsInstallDir).
foreach (var kv in _userEnv)
{
startInfo.Environment[kv.Key] = kv.Value;
}
if (_enableLogger)
{
if (isCmdWrapper)
{
Console.WriteLine($"Running: cmd.exe {startInfo.Arguments}");
}
else
{
var cmdLine = _dotnetExePath != null
? $"dotnet exec \"{_msbuildPath}\""
: $"\"{_msbuildPath}\"";
var args = string.Join(" ", startInfo.ArgumentList
.Skip(_dotnetExePath != null ? 2 : 0)
.Select(a => a.Contains(' ') ? $"\"{a}\"" : a));
Console.WriteLine($"Running: {cmdLine} {args}");
}
}
using var process = System.Diagnostics.Process.Start(startInfo);
if (process == null)
throw new Exception("Failed to start MSBuild process");
// Read both streams as tasks to avoid deadlock when both buffers fill
var stdoutTask = process.StandardOutput.ReadToEndAsync();
var errorTask = process.StandardError.ReadToEndAsync();
// Enforce timeout FIRST. ReadToEnd blocks forever if the process hangs
if (!process.WaitForExit(300000))
{
try { process.Kill(true); } catch { }
throw new Exception("MSBuild timed out after 5 minutes");
}
// Now safe to read (process has exited)
var output = stdoutTask.Result;
var error = errorTask.Result;
if (_enableLogger)
{
if (!string.IsNullOrWhiteSpace(output)) Console.WriteLine(output);
if (!string.IsNullOrWhiteSpace(error)) Console.Error.WriteLine(error);
Console.WriteLine($"Binlog: {binlogPath}");
}
if (process.ExitCode != 0)
throw new Exception($"MSBuild failed with exit code {process.ExitCode}:\n{error}\n{output}");
}
private List<ClCommandLineEntry> ParseBinlog(string binlogPath)
{
var entries = new List<ClCommandLineEntry>();
var build = BinaryLog.ReadBuild(binlogPath);
var target = build.FindFirstDescendant<Target>(t => t.Name == "GetClCommandLines");
// Snapshot evaluated property values (last-wins) so residual $(...) references that
// survive into the captured command lines can be expanded in ToCompileCommands.
var propertyMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (var prop in build.FindChildrenRecursive<Property>())
{
if (!string.IsNullOrEmpty(prop.Name) && !string.IsNullOrEmpty(prop.Value))
propertyMap[prop.Name] = prop.Value;
}
_evaluatedProperties = propertyMap;
// Prefer GetProjectDirectories target output over raw property reads,
// it accounts for UseEnv=true and Makefile configuration resolution that
// raw property values miss.
string? includePath = null, externalIncludePath = null, vcToolsDirValue = null;
var projDirsTarget = build.FindFirstDescendant<Target>(t => t.Name == "GetProjectDirectories");
if (projDirsTarget != null)
{
var outputsFolder = projDirsTarget.FindFirstDescendant<Folder>(f => f.Name == "TargetOutputs");
var directoriesItem = outputsFolder?.Children.OfType<Item>().FirstOrDefault();
if (directoriesItem != null)
{
foreach (var md in directoriesItem.Children.OfType<Metadata>())
{
switch (md.Name)
{
case "IncludePath": includePath = md.Value; break;
case "ExternalIncludePath": externalIncludePath = md.Value; break;
}
}
}
}
// Fall back to Property nodes when GetProjectDirectories didn't run
// (older targets, unknown-target warning).
if (string.IsNullOrEmpty(externalIncludePath))
{
externalIncludePath = build.FindChildrenRecursive<Property>()
.LastOrDefault(p => p.Name == "ExternalIncludePath" && !string.IsNullOrEmpty(p.Value))?.Value;
}
if (string.IsNullOrEmpty(includePath))
{
includePath = build.FindChildrenRecursive<Property>()
.LastOrDefault(p => p.Name == "IncludePath" && !string.IsNullOrEmpty(p.Value))?.Value;
}
if (!string.IsNullOrEmpty(externalIncludePath))
{
_externalIncludePaths = ResolveIncludePathEntries(
externalIncludePath.Split(';', StringSplitOptions.RemoveEmptyEntries)).ToArray();
}
if (!string.IsNullOrEmpty(includePath))
{
var externalSet = new HashSet<string>(_externalIncludePaths, StringComparer.OrdinalIgnoreCase);
_includePaths = ResolveIncludePathEntries(
includePath.Split(';', StringSplitOptions.RemoveEmptyEntries))
.Where(p => !externalSet.Contains(p))
.ToArray();
}
// Add VS auxiliary include paths from VCToolsInstallDir
vcToolsDirValue = build.FindChildrenRecursive<Property>()
.LastOrDefault(p => p.Name == "VCToolsInstallDir" && !string.IsNullOrEmpty(p.Value))?.Value;
var vcToolsDir = _vcToolsInstallDir ?? vcToolsDirValue?.TrimEnd('\\');
var allIncludes = new HashSet<string>(_externalIncludePaths.Concat(_includePaths), StringComparer.OrdinalIgnoreCase);
var auxPaths = GetVsAuxiliaryIncludePaths(vcToolsDir)
.Where(p => !allIncludes.Contains(p))
.ToArray();
if (auxPaths.Length > 0)
_includePaths = _includePaths.Concat(auxPaths).ToArray();
if (target == null)
{
if (_enableLogger)
Console.WriteLine("Warning: GetClCommandLines target not found in binlog");
return entries;
}
// Try AddItem nodes first (older binlog format)
foreach (var child in target.Children)
{
if (child is AddItem addItem)
{
var entry = ParseAddItem(addItem);
if (entry != null) entries.Add(entry);
}
}
// If no AddItem nodes found, try Item nodes in TargetOutputs folder (newer binlog format)
if (entries.Count == 0)
{
var outputFolder = target.FindFirstDescendant<Folder>(f => f.Name == "TargetOutputs");
if (outputFolder != null)
{
foreach (var child in outputFolder.Children)
{
if (child is Item item)
{
var entry = ParseItem(item);
if (entry != null) entries.Add(entry);
}
}
}
}
return entries;
}
private static bool DetectFxCompile(IReadOnlyList<string> files)
{
// The CLCommandLine task runs separately for @(ClCompile) and @(FxCompile),
// and both outputs land in the same @(ClCommandLines) item group.
// Detect HLSL by file extensions, since fxc.exe flags are not consumable by clangd,
// so FxCompile entries must be dropped from standard output.
if (files.Count == 0) return false;
foreach (var f in files)
{
var ext = Path.GetExtension(f);
if (!string.Equals(ext, ".hlsl", StringComparison.OrdinalIgnoreCase)
&& !string.Equals(ext, ".fx", StringComparison.OrdinalIgnoreCase)
&& !string.Equals(ext, ".hlsli", StringComparison.OrdinalIgnoreCase))
return false;
}
return true;
}
private ClCommandLineEntry? ParseAddItem(AddItem addItem)
{
var commandLine = addItem.Name ?? "";
var workingDir = "";
var toolPath = "";
var configOptions = "";
var files = new List<string>();
foreach (var metadata in addItem.Children.OfType<Metadata>())
{
switch (metadata.Name)
{
case "WorkingDirectory": workingDir = metadata.Value; break;
case "ToolPath": toolPath = metadata.Value; break;
case "ConfigurationOptions": configOptions = metadata.Value; break;
case "Files":
files.AddRange(metadata.Value.Split(';', StringSplitOptions.RemoveEmptyEntries).Select(f => f.Trim()));
break;
}
}
if (files.Count == 0) return null;
return new ClCommandLineEntry(
CommandLine: commandLine,
WorkingDirectory: workingDir,
ToolPath: toolPath,
Files: files.ToArray(),
IsConfigurationDefault: string.Equals(configOptions, "true", StringComparison.OrdinalIgnoreCase),
IsFxCompile: DetectFxCompile(files));
}
private ClCommandLineEntry? ParseItem(Item item)
{
var commandLine = item.Name ?? "";
var workingDir = "";
var toolPath = "";
var configOptions = "";
var files = new List<string>();
foreach (var metadata in item.Children.OfType<Metadata>())
{
switch (metadata.Name)
{
case "WorkingDirectory": workingDir = metadata.Value; break;
case "ToolPath": toolPath = metadata.Value; break;
case "ConfigurationOptions": configOptions = metadata.Value; break;
case "Files":
files.AddRange(metadata.Value.Split(';', StringSplitOptions.RemoveEmptyEntries).Select(f => f.Trim()));
break;
}
}
if (files.Count == 0) return null;
return new ClCommandLineEntry(
CommandLine: commandLine,
WorkingDirectory: workingDir,
ToolPath: toolPath,
Files: files.ToArray(),
IsConfigurationDefault: string.Equals(configOptions, "true", StringComparison.OrdinalIgnoreCase),
IsFxCompile: DetectFxCompile(files));
}
private List<CompileCommand> ToCompileCommands(List<ClCommandLineEntry> entries)
{
var commands = new List<CompileCommand>();
var projectName = Path.GetFileNameWithoutExtension(_projectPath);
string[]? defaultArgs = null;
foreach (var entry in entries)
{
// Drop FxCompile (HLSL) entries. fxc.exe flags are not
// cl.exe-compatible and clangd cannot consume them.
if (entry.IsFxCompile)
continue;
var toolPath = ResolveToolPath(entry.ToolPath);
var baseArgs = new List<string> { toolPath };
// Inject --target so clangd uses the correct architecture triple.
var target = GetClangTargetTriple();
if (target != null)
baseArgs.Add($"--target={target}");
// Disable clang's error limit to prevent cascading failures.
// MSVC STL headers can trigger errors that clang recovers from,
// but the default limit (20) causes fatal_too_many_errors which
// stops parsing and cascades into hundreds of false diagnostics.
baseArgs.Add("-ferror-limit=0");
// Decide where to place system include paths (from IncludePath / ExternalIncludePath MSBuild properties)
// relative to the project's /I flags from AdditionalIncludeDirectories.
//
// The default (Auto) classifies each path:
// - paths inside the project tree are prepended,
// preserving the historical behavior for projects that place source dirs in IncludePath,
// - paths outside the project tree are appended,
// matching cl.exe semantics where INCLUDE env-var paths are searched after explicit /I,
// which avoids header shadowing when a system header has the same name as a generated header.
var allSystemIncludes = _externalIncludePaths.Concat(_includePaths).ToArray();
var (prependIncludes, appendIncludes) = ClassifyIncludePaths(allSystemIncludes);
foreach (var p in prependIncludes)
baseArgs.Add($"/I{p}");
baseArgs.AddRange(CommandLineTokenizer.TokenizeWithResponseFiles(entry.CommandLine, entry.WorkingDirectory));
foreach (var p in appendIncludes)
baseArgs.Add($"/I{p}");
// Merge two-arg /external:I flags into single token.
// MSBuild emits "/external:I" "<path>" as separate tokens but
// cl.exe also accepts the concatenated form "/external:I<path>".
var mergedArgs = new List<string>();
for (int i = 0; i < baseArgs.Count; i++)
{
if (i + 1 < baseArgs.Count &&
baseArgs[i].Equals("/external:I", StringComparison.OrdinalIgnoreCase))
{
mergedArgs.Add($"/external:I{baseArgs[i + 1]}");
i++; // skip next
}
else
{
mergedArgs.Add(baseArgs[i]);
}
}
baseArgs = mergedArgs;
// Strip build-output flags irrelevant for IntelliSense
baseArgs.RemoveAll(a =>
a.StartsWith("/Fo", StringComparison.OrdinalIgnoreCase) ||
a.StartsWith("/Fd", StringComparison.OrdinalIgnoreCase) ||
a.StartsWith("/errorReport", StringComparison.OrdinalIgnoreCase));
foreach (var file in entry.Files)
{
bool isTemporaryCpp = string.Equals(Path.GetFileName(file), "__temporary.cpp", StringComparison.OrdinalIgnoreCase);
if (isTemporaryCpp)
{
if (_emitDefaults)
{
var defaultsFile = Path.Combine(entry.WorkingDirectory, "__project_defaults.cpp");
defaultsFile = MsBuildPropertyExpander.ExpandMsBuildProperties(defaultsFile, _evaluatedProperties);
var defaultsArgs = new List<string>(baseArgs) { defaultsFile };
var expandedDefaults = MsBuildPropertyExpander.ExpandMsBuildProperties(defaultsArgs.ToArray(), _evaluatedProperties);
var sanitized = InProcessExtractor.SanitizeFallbackPaths(expandedDefaults, _vcToolsInstallDir);
commands.Add(new CompileCommand(
File: defaultsFile,
Arguments: sanitized,
Directory: MsBuildPropertyExpander.ExpandMsBuildProperties(entry.WorkingDirectory, _evaluatedProperties),
ProjectPath: _projectPath,
ProjectName: projectName,
Configuration: _configuration,
Platform: _platform
));
}
if (_mergeDefaults)
defaultArgs = baseArgs.ToArray();
continue;
}
var fileArgs = new List<string>(baseArgs) { file };
if (_mergeDefaults && defaultArgs != null)
InProcessExtractor.MergeDefaultFlags(fileArgs, defaultArgs);
var expandedFileArgs = MsBuildPropertyExpander.ExpandMsBuildProperties(fileArgs.ToArray(), _evaluatedProperties);
var commandLine = InProcessExtractor.SanitizeFallbackPaths(expandedFileArgs, _vcToolsInstallDir);
commands.Add(new CompileCommand(
File: MsBuildPropertyExpander.ExpandMsBuildProperties(file, _evaluatedProperties),
Arguments: commandLine,
Directory: MsBuildPropertyExpander.ExpandMsBuildProperties(entry.WorkingDirectory, _evaluatedProperties),
ProjectPath: _projectPath,
ProjectName: projectName,
Configuration: _configuration,
Platform: _platform
));
}
}
return commands;
}
private (string[] prepend, string[] append) ClassifyIncludePaths(string[] all)
{
switch (_includePathOrder)
{
case IncludePathOrder.Prepend:
return (all, Array.Empty<string>());
case IncludePathOrder.Append:
return (Array.Empty<string>(), all);
default: // Auto: per-path classification by project-tree containment
{
var projectDir = Path.GetDirectoryName(_projectPath);
if (string.IsNullOrEmpty(projectDir))
return (Array.Empty<string>(), all);
var projectDirNorm = NormalizePath(projectDir);
var prepend = new List<string>();
var append = new List<string>();
foreach (var p in all)
{
var n = NormalizePath(p);
if (n.StartsWith(projectDirNorm, StringComparison.OrdinalIgnoreCase))
prepend.Add(p);
else
append.Add(p);
}
return (prepend.ToArray(), append.ToArray());
}
}
}
private static string NormalizePath(string p)
{
try { return Path.GetFullPath(p).TrimEnd('\\') + "\\"; }
catch { return p.TrimEnd('\\') + "\\"; }
}
private string ResolveToolPath(string toolPath)
{
// User-specified --cl-path takes highest priority
if (!string.IsNullOrEmpty(_clPath) && File.Exists(_clPath))
return _clPath;
if (toolPath.Contains("system32", StringComparison.OrdinalIgnoreCase) &&
toolPath.EndsWith("CL.exe", StringComparison.OrdinalIgnoreCase))
{
var resolved = VsWhereHelper.ResolveClExePath(_vcToolsInstallDir, _platform);
if (resolved != null)
return resolved;
}
return toolPath;
}
/// <summary>
/// Maps the MSBuild Platform to a clang target triple so that clangd
/// compiles with the correct pointer size, predefined macros (_WIN64,
/// _M_X64, etc.), and ABI.
/// </summary>
private string? GetClangTargetTriple()
{
return _platform.ToLowerInvariant() switch
{
"win32" or "x86" => "i686-pc-windows-msvc",
"x64" or "x86_64" or "amd64" => "x86_64-pc-windows-msvc",
"arm" => "thumbv7-pc-windows-msvc",
"arm64" or "aarch64" => "aarch64-pc-windows-msvc",
_ => null
};
}
private static readonly HashSet<string> CppExtensions = new(StringComparer.OrdinalIgnoreCase)
{ ".cpp", ".cc", ".cxx", ".c", ".c++", ".cp" };
/// <summary>
/// Scans disk for C/C++ source files when the project XML contains none.
/// Uses header references as directory hints, falling back to the project directory.
/// </summary>
private static List<string> FindSourceFilesOnDisk(string projectDir, XDocument doc, XNamespace ns)
{
var files = new List<string>();
// Collect directories from ClInclude and None items (header references)
var headerPaths = doc.Root?
.Elements(ns + "ItemGroup")
.SelectMany(ig => ig.Elements(ns + "ClInclude").Concat(ig.Elements(ns + "None")))
.Select(e => e.Attribute("Include")?.Value)
.Where(v => v != null)
.Cast<string>()
.Select(v => Path.GetDirectoryName(Path.GetFullPath(Path.Combine(projectDir, v))))
.Where(d => d != null && Directory.Exists(d))
.Cast<string>()
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList() ?? new List<string>();
if (headerPaths.Count > 0)
{
foreach (var dir in headerPaths)
{
try
{
files.AddRange(Directory.EnumerateFiles(dir!, "*.*", SearchOption.TopDirectoryOnly)
.Where(f => CppExtensions.Contains(Path.GetExtension(f))));
}
catch (Exception) { /* permission denied, etc. */ }
}
}
else
{
try
{
files.AddRange(Directory.EnumerateFiles(projectDir, "*.*", SearchOption.TopDirectoryOnly)
.Where(f => CppExtensions.Contains(Path.GetExtension(f))));
}
catch (Exception) { /* permission denied, etc. */ }
}
return files.Distinct(StringComparer.OrdinalIgnoreCase).ToList();
}
/// <summary>
/// Fallback extraction for GN-generated projects (Chromium, Crashpad, WebRTC).
/// Parses the vcxproj XML directly to read ItemDefinitionGroup/ClCompile settings
/// and CustomBuild source file items.
/// </summary>
private List<CompileCommand> TryExtractFromItemDefinitionGroup()
{
XDocument doc;
try
{
doc = XDocument.Load(_projectPath);
}
catch
{
return new List<CompileCommand>();
}
var ns = doc.Root?.GetDefaultNamespace() ?? XNamespace.None;
var projectDir = Path.GetDirectoryName(_projectPath)!;
var projectName = Path.GetFileNameWithoutExtension(_projectPath);
// Find ClCompile settings in ItemDefinitionGroup
var clCompileDefElement = doc.Root?
.Elements(ns + "ItemDefinitionGroup")
.Elements(ns + "ClCompile")
.FirstOrDefault();
if (clCompileDefElement == null)
return new List<CompileCommand>();
string GetElementValue(string name) =>
clCompileDefElement.Element(ns + name)?.Value ?? "";
var additionalIncludes = GetElementValue("AdditionalIncludeDirectories");
var preprocessorDefs = GetElementValue("PreprocessorDefinitions");
var additionalOptions = GetElementValue("AdditionalOptions");
var languageStandard = GetElementValue("LanguageStandard");
var disabledWarnings = GetElementValue("DisableSpecificWarnings");
// Collect source files from CustomBuild items (GN pattern) or ClCompile items
var sourceFiles = doc.Root?
.Elements(ns + "ItemGroup")
.Elements(ns + "CustomBuild")
.Select(e => e.Attribute("Include")?.Value)
.Where(v => v != null && CppExtensions.Contains(Path.GetExtension(v)))
.Cast<string>()
.ToList() ?? new List<string>();
if (sourceFiles.Count == 0)
{
sourceFiles = doc.Root?
.Elements(ns + "ItemGroup")
.Elements(ns + "ClCompile")
.Select(e => e.Attribute("Include")?.Value)
.Where(v => v != null)
.Cast<string>()
.ToList() ?? new List<string>();
}