forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrepareForReadyToRunCompilation.cs
324 lines (279 loc) · 15.3 KB
/
PrepareForReadyToRunCompilation.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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection.PortableExecutable;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Reflection.Metadata;
using System.Reflection;
namespace Microsoft.NET.Build.Tasks
{
public class PrepareForReadyToRunCompilation : TaskBase
{
[Required]
public ITaskItem MainAssembly { get; set; }
public ITaskItem[] Assemblies { get; set; }
public string[] ExcludeList { get; set; }
public bool EmitSymbols { get; set; }
public bool ReadyToRunUseCrossgen2 { get; set; }
public bool Crossgen2Composite { get; set; }
[Required]
public string OutputPath { get; set; }
[Required]
public bool IncludeSymbolsInSingleFile { get; set; }
public ITaskItem CrossgenTool { get; set; }
public ITaskItem Crossgen2Tool { get; set; }
// Output lists of files to compile. Currently crossgen has to run in two steps, the first to generate the R2R image
// and the second to create native PDBs for the compiled images (the output of the first step is an input to the second step)
[Output]
public ITaskItem[] ReadyToRunCompileList => _compileList.ToArray();
[Output]
public ITaskItem[] ReadyToRunSymbolsCompileList => _symbolsCompileList.ToArray();
// Output files to publish after compilation. These lists are equivalent to the input list, but contain the new
// paths to the compiled R2R images and native PDBs.
[Output]
public ITaskItem[] ReadyToRunFilesToPublish => _r2rFiles.ToArray();
[Output]
public ITaskItem[] ReadyToRunAssembliesToReference => _r2rReferences.ToArray();
private List<ITaskItem> _compileList = new List<ITaskItem>();
private List<ITaskItem> _symbolsCompileList = new List<ITaskItem>();
private List<ITaskItem> _r2rFiles = new List<ITaskItem>();
private List<ITaskItem> _r2rReferences = new List<ITaskItem>();
protected override void ExecuteCore()
{
// Future: when crossgen2 supports generating PDBs, update this to check crossgen2 when we are using crossgen2.
string diaSymReaderPath = CrossgenTool?.GetMetadata("DiaSymReader");
bool hasValidDiaSymReaderLib = !string.IsNullOrEmpty(diaSymReaderPath) && File.Exists(diaSymReaderPath);
// Process input lists of files
ProcessInputFileList(Assemblies, _compileList, _symbolsCompileList, _r2rFiles, _r2rReferences, hasValidDiaSymReaderLib);
}
private void ProcessInputFileList(
ITaskItem[] inputFiles,
List<ITaskItem> imageCompilationList,
List<ITaskItem> symbolsCompilationList,
List<ITaskItem> r2rFilesPublishList,
List<ITaskItem> r2rReferenceList,
bool hasValidDiaSymReaderLib)
{
if (inputFiles == null)
{
return;
}
// TODO: ExcludeList for composite mode
var exclusionSet = ExcludeList == null || Crossgen2Composite ? null : new HashSet<string>(ExcludeList, StringComparer.OrdinalIgnoreCase);
foreach (var file in inputFiles)
{
var eligibility = GetInputFileEligibility(file, exclusionSet);
if (eligibility == Eligibility.None)
{
continue;
}
r2rReferenceList.Add(file);
if (!Crossgen2Composite && (eligibility == Eligibility.ReferenceOnly))
{
continue;
}
var outputR2RImageRelativePath = file.GetMetadata(MetadataKeys.RelativePath);
var outputR2RImage = Path.Combine(OutputPath, outputR2RImageRelativePath);
if (!Crossgen2Composite)
{
// This TaskItem is the IL->R2R entry, for an input assembly that needs to be compiled into a R2R image. This will be used as
// an input to the ReadyToRunCompiler task
TaskItem r2rCompilationEntry = new TaskItem(file);
r2rCompilationEntry.SetMetadata("OutputR2RImage", outputR2RImage);
r2rCompilationEntry.RemoveMetadata(MetadataKeys.OriginalItemSpec);
imageCompilationList.Add(r2rCompilationEntry);
}
else if (file.ItemSpec == MainAssembly.ItemSpec)
{
// Create a TaskItem for <MainAssembly>.r2r.dll
var compositeR2RImageRelativePath = file.GetMetadata(MetadataKeys.RelativePath);
compositeR2RImageRelativePath = Path.ChangeExtension(compositeR2RImageRelativePath, "r2r" + Path.GetExtension(compositeR2RImageRelativePath));
var compositeR2RImage = Path.Combine(OutputPath, compositeR2RImageRelativePath);
TaskItem r2rCompilationEntry = new TaskItem(file);
r2rCompilationEntry.SetMetadata("OutputR2RImage", compositeR2RImage);
r2rCompilationEntry.RemoveMetadata(MetadataKeys.OriginalItemSpec);
imageCompilationList.Add(r2rCompilationEntry);
// Publish it
TaskItem compositeR2RFileToPublish = new TaskItem(file);
compositeR2RFileToPublish.ItemSpec = compositeR2RImage;
compositeR2RFileToPublish.RemoveMetadata(MetadataKeys.OriginalItemSpec);
compositeR2RFileToPublish.SetMetadata(MetadataKeys.RelativePath, compositeR2RImageRelativePath);
r2rFilesPublishList.Add(compositeR2RFileToPublish);
}
// This TaskItem corresponds to the output R2R image. It is equivalent to the input TaskItem, only the ItemSpec for it points to the new path
// for the newly created R2R image
TaskItem r2rFileToPublish = new TaskItem(file);
r2rFileToPublish.ItemSpec = outputR2RImage;
r2rFileToPublish.RemoveMetadata(MetadataKeys.OriginalItemSpec);
r2rFilesPublishList.Add(r2rFileToPublish);
// Note: ReadyToRun PDB/Map files are not needed for debugging. They are only used for profiling, therefore the default behavior is to not generate them
// unless an explicit PublishReadyToRunEmitSymbols flag is enabled by the app developer. There is also another way to profile that the runtime supports, which does
// not rely on the native PDBs/Map files, so creating them is really an opt-in option, typically used by advanced users.
// For debugging, only the IL PDBs are required.
if (EmitSymbols)
{
string outputPDBImageRelativePath = null, outputPDBImage = null, createPDBCommand = null;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && hasValidDiaSymReaderLib)
{
outputPDBImage = Path.ChangeExtension(outputR2RImage, "ni.pdb");
outputPDBImageRelativePath = Path.ChangeExtension(outputR2RImageRelativePath, "ni.pdb");
createPDBCommand = $"/CreatePDB \"{Path.GetDirectoryName(outputPDBImage)}\"";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
using (FileStream fs = new FileStream(file.ItemSpec, FileMode.Open, FileAccess.Read))
{
PEReader pereader = new PEReader(fs);
MetadataReader mdReader = pereader.GetMetadataReader();
Guid mvid = mdReader.GetGuid(mdReader.GetModuleDefinition().Mvid);
outputPDBImage = Path.ChangeExtension(outputR2RImage, "ni.{" + mvid + "}.map");
outputPDBImageRelativePath = Path.ChangeExtension(outputR2RImageRelativePath, "ni.{" + mvid + "}.map");
createPDBCommand = $"/CreatePerfMap \"{Path.GetDirectoryName(outputPDBImage)}\"";
}
}
if (outputPDBImage != null)
{
// This TaskItem is the R2R->R2RPDB entry, for a R2R image that was just created, and for which we need to create native PDBs. This will be used as
// an input to the ReadyToRunCompiler task
TaskItem pdbCompilationEntry = new TaskItem(file);
pdbCompilationEntry.ItemSpec = outputR2RImage;
pdbCompilationEntry.SetMetadata("OutputPDBImage", outputPDBImage);
pdbCompilationEntry.SetMetadata("CreatePDBCommand", createPDBCommand);
symbolsCompilationList.Add(pdbCompilationEntry);
// This TaskItem corresponds to the output PDB image. It is equivalent to the input TaskItem, only the ItemSpec for it points to the new path
// for the newly created PDB image.
TaskItem r2rSymbolsFileToPublish = new TaskItem(file);
r2rSymbolsFileToPublish.ItemSpec = outputPDBImage;
r2rSymbolsFileToPublish.SetMetadata(MetadataKeys.RelativePath, outputPDBImageRelativePath);
r2rSymbolsFileToPublish.RemoveMetadata(MetadataKeys.OriginalItemSpec);
if (!IncludeSymbolsInSingleFile)
{
r2rSymbolsFileToPublish.SetMetadata(MetadataKeys.ExcludeFromSingleFile, "true");
}
r2rFilesPublishList.Add(r2rSymbolsFileToPublish);
}
}
}
}
private enum Eligibility
{
None,
ReferenceOnly,
CompileAndReference
};
private static Eligibility GetInputFileEligibility(ITaskItem file, HashSet<string> exclusionSet)
{
// Check to see if this is a valid ILOnly image that we can compile
using (FileStream fs = new FileStream(file.ItemSpec, FileMode.Open, FileAccess.Read))
{
try
{
using (var pereader = new PEReader(fs))
{
if (!pereader.HasMetadata)
{
return Eligibility.None;
}
MetadataReader mdReader = pereader.GetMetadataReader();
if (!mdReader.IsAssembly)
{
return Eligibility.None;
}
if (IsReferenceAssembly(mdReader))
{
// crossgen can only take implementation assemblies, even as references
return Eligibility.None;
}
if ((pereader.PEHeaders.CorHeader.Flags & CorFlags.ILOnly) != CorFlags.ILOnly)
{
return Eligibility.ReferenceOnly;
}
if (file.HasMetadataValue(MetadataKeys.ReferenceOnly, "true"))
{
return Eligibility.ReferenceOnly;
}
if (exclusionSet != null && exclusionSet.Contains(Path.GetFileName(file.ItemSpec)))
{
return Eligibility.ReferenceOnly;
}
// save these most expensive checks for last. We don't want to scan all references for IL code
if (ReferencesWinMD(mdReader) || !HasILCode(pereader, mdReader))
{
return Eligibility.ReferenceOnly;
}
return Eligibility.CompileAndReference;
}
}
catch (BadImageFormatException)
{
// Not a valid assembly file
return Eligibility.None;
}
}
}
private static bool IsReferenceAssembly(MetadataReader mdReader)
{
foreach (var attributeHandle in mdReader.GetAssemblyDefinition().GetCustomAttributes())
{
EntityHandle attributeCtor = mdReader.GetCustomAttribute(attributeHandle).Constructor;
StringHandle attributeTypeName = default;
StringHandle attributeTypeNamespace = default;
if (attributeCtor.Kind == HandleKind.MemberReference)
{
EntityHandle attributeMemberParent = mdReader.GetMemberReference((MemberReferenceHandle)attributeCtor).Parent;
if (attributeMemberParent.Kind == HandleKind.TypeReference)
{
TypeReference attributeTypeRef = mdReader.GetTypeReference((TypeReferenceHandle)attributeMemberParent);
attributeTypeName = attributeTypeRef.Name;
attributeTypeNamespace = attributeTypeRef.Namespace;
}
}
else if (attributeCtor.Kind == HandleKind.MethodDefinition)
{
TypeDefinitionHandle attributeTypeDefHandle = mdReader.GetMethodDefinition((MethodDefinitionHandle)attributeCtor).GetDeclaringType();
TypeDefinition attributeTypeDef = mdReader.GetTypeDefinition(attributeTypeDefHandle);
attributeTypeName = attributeTypeDef.Name;
attributeTypeNamespace = attributeTypeDef.Namespace;
}
if (!attributeTypeName.IsNil &&
!attributeTypeNamespace.IsNil &&
mdReader.StringComparer.Equals(attributeTypeName, "ReferenceAssemblyAttribute") &&
mdReader.StringComparer.Equals(attributeTypeNamespace, "System.Runtime.CompilerServices"))
{
return true;
}
}
return false;
}
private static bool ReferencesWinMD(MetadataReader mdReader)
{
foreach (var assemblyRefHandle in mdReader.AssemblyReferences)
{
AssemblyReference assemblyRef = mdReader.GetAssemblyReference(assemblyRefHandle);
if ((assemblyRef.Flags & AssemblyFlags.WindowsRuntime) == AssemblyFlags.WindowsRuntime)
{
return true;
}
}
return false;
}
private static bool HasILCode(PEReader peReader, MetadataReader mdReader)
{
foreach (var methoddefHandle in mdReader.MethodDefinitions)
{
MethodDefinition methodDef = mdReader.GetMethodDefinition(methoddefHandle);
if (methodDef.RelativeVirtualAddress > 0)
{
return true;
}
}
return false;
}
}
}