forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunReadyToRunCompiler.cs
406 lines (351 loc) · 15.6 KB
/
RunReadyToRunCompiler.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Microsoft.NET.Build.Tasks
{
public class RunReadyToRunCompiler : ToolTask
{
public ITaskItem CrossgenTool { get; set; }
public ITaskItem Crossgen2Tool { get; set; }
[Required]
public ITaskItem CompilationEntry { get; set; }
[Required]
public ITaskItem[] ImplementationAssemblyReferences { get; set; }
public ITaskItem[] ReadyToRunCompositeBuildReferences { get; set; }
public ITaskItem[] ReadyToRunCompositeBuildInput { get; set; }
public bool ShowCompilerWarnings { get; set; }
public bool UseCrossgen2 { get; set; }
public string Crossgen2ExtraCommandLineArgs { get; set; }
public ITaskItem[] Crossgen2PgoFiles { get; set; }
[Output]
public bool WarningsDetected { get; set; }
private bool _emitSymbols;
private string _inputAssembly;
private string _outputR2RImage;
private string _outputPDBImage;
private string _createPDBCommand;
private bool _createCompositeImage;
private bool IsPdbCompilation => !string.IsNullOrEmpty(_createPDBCommand);
private bool ActuallyUseCrossgen2 => UseCrossgen2 && !IsPdbCompilation;
private string DotNetHostPath => Crossgen2Tool?.GetMetadata(MetadataKeys.DotNetHostPath);
private bool Crossgen2IsVersion5
{
get
{
string version5 = Crossgen2Tool?.GetMetadata(MetadataKeys.IsVersion5);
return !string.IsNullOrEmpty(version5) && bool.Parse(version5);
}
}
protected override string ToolName
{
get
{
if (ActuallyUseCrossgen2)
{
string hostPath = DotNetHostPath;
if (!string.IsNullOrEmpty(hostPath))
{
return hostPath;
}
return Crossgen2Tool.ItemSpec;
}
return CrossgenTool.ItemSpec;
}
}
protected override string GenerateFullPathToTool() => ToolName;
private string DiaSymReader => CrossgenTool.GetMetadata(MetadataKeys.DiaSymReader);
public RunReadyToRunCompiler()
{
LogStandardErrorAsError = true;
}
protected override bool ValidateParameters()
{
string emitSymbolsMetadata = CompilationEntry.GetMetadata(MetadataKeys.EmitSymbols);
_emitSymbols = !string.IsNullOrEmpty(emitSymbolsMetadata) && bool.Parse(emitSymbolsMetadata);
_createPDBCommand = CompilationEntry.GetMetadata(MetadataKeys.CreatePDBCommand);
string createCompositeImageMetadata = CompilationEntry.GetMetadata(MetadataKeys.CreateCompositeImage);
_createCompositeImage = !string.IsNullOrEmpty(createCompositeImageMetadata) && bool.Parse(createCompositeImageMetadata);
if (IsPdbCompilation && CrossgenTool == null)
{
// PDB compilation is a step specific to Crossgen1 and 5.0 Crossgen2
// which didn't support PDB generation. 6.0 Crossgen2 produces symbols
// directly during native compilation.
Log.LogError(Strings.CrossgenToolMissingInPDBCompilationMode);
return false;
}
if (ActuallyUseCrossgen2)
{
if (Crossgen2Tool == null)
{
Log.LogError(Strings.Crossgen2ToolMissingWhenUseCrossgen2IsSet);
return false;
}
if (!File.Exists(Crossgen2Tool.ItemSpec))
{
Log.LogError(Strings.Crossgen2ToolExecutableNotFound, Crossgen2Tool.ItemSpec);
return false;
}
string hostPath = DotNetHostPath;
if (!string.IsNullOrEmpty(hostPath) && !File.Exists(hostPath))
{
Log.LogError(Strings.DotNetHostExecutableNotFound, hostPath);
return false;
}
string jitPath = Crossgen2Tool.GetMetadata(MetadataKeys.JitPath);
if (!string.IsNullOrEmpty(jitPath))
{
if (!File.Exists(jitPath))
{
Log.LogError(Strings.JitLibraryNotFound, jitPath);
return false;
}
}
else if (Crossgen2IsVersion5)
{
// We expect JitPath to be set for .NET 5 and {TargetOS, TargetArch} to be set for .NET 6 and later
Log.LogError(Strings.Crossgen2MissingRequiredMetadata, MetadataKeys.JitPath);
return false;
}
else
{
// For smooth switchover we accept both JitPath and TargetOS / TargetArch in .NET 6 Crossgen2
if (string.IsNullOrEmpty(Crossgen2Tool.GetMetadata(MetadataKeys.TargetOS)))
{
Log.LogError(Strings.Crossgen2MissingRequiredMetadata, MetadataKeys.TargetOS);
return false;
}
if (string.IsNullOrEmpty(Crossgen2Tool.GetMetadata(MetadataKeys.TargetArch)))
{
Log.LogError(Strings.Crossgen2MissingRequiredMetadata, MetadataKeys.TargetArch);
return false;
}
}
}
else
{
if (CrossgenTool == null)
{
Log.LogError(Strings.CrossgenToolMissingWhenUseCrossgen2IsNotSet);
return false;
}
if (!File.Exists(CrossgenTool.ItemSpec))
{
Log.LogError(Strings.CrossgenToolExecutableNotFound, CrossgenTool.ItemSpec);
return false;
}
if (!File.Exists(CrossgenTool.GetMetadata(MetadataKeys.JitPath)))
{
Log.LogError(Strings.JitLibraryNotFound, MetadataKeys.JitPath);
return false;
}
}
_outputPDBImage = CompilationEntry.GetMetadata(MetadataKeys.OutputPDBImage);
if (IsPdbCompilation)
{
_outputR2RImage = CompilationEntry.ItemSpec;
if (!string.IsNullOrEmpty(DiaSymReader) && !File.Exists(DiaSymReader))
{
Log.LogError(Strings.DiaSymReaderLibraryNotFound, DiaSymReader);
return false;
}
// R2R image has to be created before emitting native symbols (crossgen needs this as an input argument)
if (string.IsNullOrEmpty(_outputPDBImage))
{
Log.LogError(Strings.MissingOutputPDBImagePath);
}
if (!File.Exists(_outputR2RImage))
{
Log.LogError(Strings.PDBGeneratorInputExecutableNotFound, _outputR2RImage);
return false;
}
}
else
{
_outputR2RImage = CompilationEntry.GetMetadata(MetadataKeys.OutputR2RImage);
if (!_createCompositeImage)
{
_inputAssembly = CompilationEntry.ItemSpec;
if (!File.Exists(_inputAssembly))
{
Log.LogError(Strings.InputAssemblyNotFound, _inputAssembly);
return false;
}
}
else
{
_inputAssembly = "CompositeImage";
}
if (string.IsNullOrEmpty(_outputR2RImage))
{
Log.LogError(Strings.MissingOutputR2RImageFileName);
return false;
}
if (_emitSymbols && string.IsNullOrEmpty(_outputPDBImage))
{
Log.LogError(Strings.MissingOutputPDBImagePath);
}
}
return true;
}
private string GetAssemblyReferencesCommands()
{
StringBuilder result = new();
var references = _createCompositeImage ? ReadyToRunCompositeBuildReferences : ImplementationAssemblyReferences;
if (references != null)
{
foreach (var reference in (_createCompositeImage ? ReadyToRunCompositeBuildReferences : ImplementationAssemblyReferences))
{
// When generating PDBs, we must not add a reference to the IL version of the R2R image for which we're trying to generate a PDB
if (IsPdbCompilation && string.Equals(Path.GetFileName(reference.ItemSpec), Path.GetFileName(_outputR2RImage), StringComparison.OrdinalIgnoreCase))
continue;
if (UseCrossgen2 && !IsPdbCompilation)
{
result.AppendLine($"-r:\"{reference}\"");
}
else
{
result.AppendLine($"-r \"{reference}\"");
}
}
}
return result.ToString();
}
protected override string GenerateCommandLineCommands()
{
if (ActuallyUseCrossgen2 && !string.IsNullOrEmpty(DotNetHostPath))
{
return $"\"{Crossgen2Tool.ItemSpec}\"";
}
return null;
}
protected override string GenerateResponseFileCommands()
{
// Crossgen2 5.0 doesn't support PDB generation so Crossgen1 is used for that purpose.
if (ActuallyUseCrossgen2)
{
return GenerateCrossgen2ResponseFile();
}
else
{
return GenerateCrossgenResponseFile();
}
}
private string GenerateCrossgenResponseFile()
{
StringBuilder result = new();
result.AppendLine("/nologo");
if (IsPdbCompilation)
{
result.Append(GetAssemblyReferencesCommands());
if (!string.IsNullOrEmpty(DiaSymReader))
{
result.AppendLine($"/DiasymreaderPath \"{DiaSymReader}\"");
}
result.AppendLine(_createPDBCommand);
result.AppendLine($"\"{_outputR2RImage}\"");
}
else
{
result.AppendLine("/MissingDependenciesOK");
result.AppendLine($"/JITPath \"{CrossgenTool.GetMetadata(MetadataKeys.JitPath)}\"");
result.Append(GetAssemblyReferencesCommands());
result.AppendLine($"/out \"{_outputR2RImage}\"");
result.AppendLine($"\"{_inputAssembly}\"");
}
return result.ToString();
}
private string GenerateCrossgen2ResponseFile()
{
StringBuilder result = new();
string jitPath = Crossgen2Tool.GetMetadata(MetadataKeys.JitPath);
if (!string.IsNullOrEmpty(jitPath))
{
result.AppendLine($"--jitpath:\"{jitPath}\"");
}
else
{
result.AppendLine($"--targetos:{Crossgen2Tool.GetMetadata(MetadataKeys.TargetOS)}");
result.AppendLine($"--targetarch:{Crossgen2Tool.GetMetadata(MetadataKeys.TargetArch)}");
}
result.AppendLine("-O");
// 5.0 Crossgen2 doesn't support PDB generation.
if (!Crossgen2IsVersion5 && _emitSymbols)
{
if (Crossgen2Tool.GetMetadata(MetadataKeys.TargetOS) == "windows")
{
result.AppendLine("--pdb");
result.AppendLine($"--pdb-path:{Path.GetDirectoryName(_outputPDBImage)}");
}
else
{
result.AppendLine("--perfmap");
result.AppendLine($"--perfmap-path:{Path.GetDirectoryName(_outputPDBImage)}");
string perfmapFormatVersion = Crossgen2Tool.GetMetadata(MetadataKeys.PerfmapFormatVersion);
if (!string.IsNullOrEmpty(perfmapFormatVersion))
{
result.AppendLine($"--perfmap-format-version:{perfmapFormatVersion}");
}
}
}
if (Crossgen2PgoFiles != null)
{
foreach (var mibc in Crossgen2PgoFiles)
{
result.AppendLine($"-m:\"{mibc.ItemSpec}\"");
}
}
if (!string.IsNullOrEmpty(Crossgen2ExtraCommandLineArgs))
{
foreach (string extraArg in Crossgen2ExtraCommandLineArgs.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
{
result.AppendLine(extraArg);
}
}
if (_createCompositeImage)
{
result.AppendLine("--composite");
// Crossgen2 v5 only supported compilation with --inputbubble specified
if (Crossgen2IsVersion5)
result.AppendLine("--inputbubble");
result.AppendLine($"--out:\"{_outputR2RImage}\"");
result.Append(GetAssemblyReferencesCommands());
// Note: do not add double quotes around the input assembly, even if the file path contains spaces. The command line
// parsing logic will append this string to the working directory if it's a relative path, so any double quotes will result in errors.
foreach (var reference in ReadyToRunCompositeBuildInput)
{
result.AppendLine(reference.ItemSpec);
}
}
else
{
result.Append(GetAssemblyReferencesCommands());
result.AppendLine($"--out:\"{_outputR2RImage}\"");
// Note: do not add double quotes around the input assembly, even if the file path contains spaces. The command line
// parsing logic will append this string to the working directory if it's a relative path, so any double quotes will result in errors.
result.AppendLine($"{_inputAssembly}");
}
return result.ToString();
}
protected override int ExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands)
{
// Ensure output sub-directories exists - Crossgen does not create directories for output files. Any relative path used with the
// '/out' parameter has to have an existing directory.
Directory.CreateDirectory(Path.GetDirectoryName(_outputR2RImage));
WarningsDetected = false;
return base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
}
protected override void LogEventsFromTextOutput(string singleLine, MessageImportance messageImportance)
{
if (!ShowCompilerWarnings && singleLine.IndexOf("warning:", StringComparison.OrdinalIgnoreCase) != -1)
{
Log.LogMessage(MessageImportance.Normal, singleLine);
WarningsDetected = true;
}
else
{
base.LogEventsFromTextOutput(singleLine, messageImportance);
}
}
}
}