-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCompileCommandsJson.cs
229 lines (211 loc) · 8.47 KB
/
CompileCommandsJson.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Web;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
/// <summary>
/// MSBuild logger to emit a compile_commands.json file from a C++ project build.
/// </summary>
/// <remarks>
/// Based on the work of:
/// * Kirill Osenkov and the MSBuildStructuredLog project.
/// * Dave Glick's MsBuildPipeLogger.
///
/// Ref for MSBuild Logger API:
/// https://docs.microsoft.com/en-us/visualstudio/msbuild/build-loggers
/// Format spec:
/// https://clang.llvm.org/docs/JSONCompilationDatabase.html
/// </remarks>
public class CompileCommandsJson : Logger
{
public override void Initialize(IEventSource eventSource)
{
// Default to writing compile_commands.json in the current directory,
// but permit it to be overridden by a parameter.
//
string outputFilePath = String.IsNullOrEmpty(Parameters) ? "compile_commands.json" : Parameters;
try
{
const bool append = false;
Encoding utf8WithoutBom = new UTF8Encoding(false);
this.streamWriter = new StreamWriter(outputFilePath, append, utf8WithoutBom);
this.firstLine = true;
streamWriter.WriteLine("[");
}
catch (Exception ex)
{
if (ex is UnauthorizedAccessException
|| ex is ArgumentNullException
|| ex is PathTooLongException
|| ex is DirectoryNotFoundException
|| ex is NotSupportedException
|| ex is ArgumentException
|| ex is SecurityException
|| ex is IOException)
{
throw new LoggerException("Failed to create " + outputFilePath + ": " + ex.Message);
}
else
{
// Unexpected failure
throw;
}
}
eventSource.AnyEventRaised += EventSource_AnyEventRaised;
}
private void EventSource_AnyEventRaised(object sender, BuildEventArgs args)
{
if (args is TaskCommandLineEventArgs taskArgs && taskArgs.TaskName == "CL")
{
// taskArgs.CommandLine begins with the full path to the compiler, but that path is
// *not* escaped/quoted for a shell, and may contain spaces, such as C:\Program Files
// (x86)\Microsoft Visual Studio\... As a workaround for this misfeature, find the
// end of the path by searching for CL.exe. (This will fail if a user renames the
// compiler binary, or installs their tools to a path that includes "CL.exe ".)
const string clExe = "cl.exe ";
int clExeIndex = taskArgs.CommandLine.ToLower().IndexOf(clExe);
if (clExeIndex == -1)
{
throw new LoggerException("Unexpected lack of CL.exe in " + taskArgs.CommandLine);
}
string compilerPath = taskArgs.CommandLine.Substring(0, clExeIndex + clExe.Length - 1);
string argsString = taskArgs.CommandLine.Substring(clExeIndex + clExe.Length).TrimStart();
string[] cmdArgs = CommandLineToArgs(argsString);
// Options that consume the following argument.
string[] optionsWithParam = {
"D", "I", "F", "U", "FI", "FU",
"analyze:log", "analyze:stacksize", "analyze:max_paths",
"analyze:ruleset", "analyze:plugin"};
List<string> maybeFilenames = new List<string>();
List<string> filenames = new List<string>();
bool allFilenamesAreSources = false;
for (int i = 0; i < cmdArgs.Length; i++)
{
bool isOption = cmdArgs[i].StartsWith("/") || cmdArgs[i].StartsWith("-");
string option = isOption ? cmdArgs[i].Substring(1) : "";
if (isOption && Array.Exists(optionsWithParam, e => e == option))
{
i++; // skip next arg
}
else if (option == "Tc" || option == "Tp")
{
// next arg is definitely a source file
if (i + 1 < cmdArgs.Length)
{
filenames.Add(cmdArgs[i + 1]);
}
}
else if (option.StartsWith("Tc") || option.StartsWith("Tp"))
{
// rest of this arg is definitely a source file
filenames.Add(option.Substring(2));
}
else if (option == "TC" || option == "TP")
{
// all inputs are treated as source files
allFilenamesAreSources = true;
}
else if (option == "link")
{
break; // only linker options follow
}
else if (isOption || cmdArgs[i].StartsWith("@"))
{
// other argument, ignore it
}
else
{
// non-argument, add it to our list of potential sources
maybeFilenames.Add(cmdArgs[i]);
}
}
// Iterate over potential sources, and decide (based on the filename)
// whether they are source inputs.
foreach (string filename in maybeFilenames)
{
if (allFilenamesAreSources)
{
filenames.Add(filename);
}
else
{
int suffixPos = filename.LastIndexOf('.');
if (suffixPos != -1)
{
string ext = filename.Substring(suffixPos + 1).ToLowerInvariant();
if (ext == "c" || ext == "cxx" || ext == "cpp")
{
filenames.Add(filename);
}
}
}
}
// simplify the compile command to avoid .. etc.
string compileCommand = '"' + Path.GetFullPath(compilerPath) + "\" " + argsString;
string dirname = Path.GetDirectoryName(taskArgs.ProjectFile);
// For each source file, emit a JSON entry
foreach (string filename in filenames)
{
// Terminate the preceding entry
if (firstLine)
{
firstLine = false;
}
else
{
streamWriter.WriteLine(",");
}
// Write one entry
streamWriter.WriteLine(String.Format(
"{{\"directory\": \"{0}\",",
HttpUtility.JavaScriptStringEncode(dirname)));
streamWriter.WriteLine(String.Format(
" \"command\": \"{0}\",",
HttpUtility.JavaScriptStringEncode(compileCommand)));
streamWriter.Write(String.Format(
" \"file\": \"{0}\"}}",
HttpUtility.JavaScriptStringEncode(filename)));
}
}
}
[DllImport("shell32.dll", SetLastError = true)]
static extern IntPtr CommandLineToArgvW(
[MarshalAs(UnmanagedType.LPWStr)] string lpCmdLine, out int pNumArgs);
static string[] CommandLineToArgs(string commandLine)
{
int argc;
var argv = CommandLineToArgvW(commandLine, out argc);
if (argv == IntPtr.Zero)
throw new System.ComponentModel.Win32Exception();
try
{
var args = new string[argc];
for (var i = 0; i < args.Length; i++)
{
var p = Marshal.ReadIntPtr(argv, i * IntPtr.Size);
args[i] = Marshal.PtrToStringUni(p);
}
return args;
}
finally
{
Marshal.FreeHGlobal(argv);
}
}
public override void Shutdown()
{
if (!firstLine)
{
streamWriter.WriteLine();
}
streamWriter.WriteLine("]");
streamWriter.Close();
base.Shutdown();
}
private StreamWriter streamWriter;
private bool firstLine;
}