forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunReadyToRunCompiler.cs
224 lines (186 loc) · 8.65 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
// 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.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Microsoft.NET.Build.Tasks
{
public class RunReadyToRunCompiler : ToolTask
{
[Required]
public ITaskItem CrossgenTool { get; set; }
[Required]
public ITaskItem CompilationEntry { get; set; }
[Required]
public ITaskItem[] ImplementationAssemblies { get; set; }
private string _crossgenPath;
private string _clrjitPath;
private string _diasymreaderPath;
private string _inputAssembly;
private string _outputR2RImage;
private string _outputPDBImage;
private string _createPDBCommand;
private bool IsPdbCompilation => !String.IsNullOrEmpty(_createPDBCommand);
protected override string ToolName => _crossgenPath;
protected override string GenerateFullPathToTool() => _crossgenPath;
public RunReadyToRunCompiler()
{
LogStandardErrorAsError = true;
}
protected override bool ValidateParameters()
{
_crossgenPath = CrossgenTool.ItemSpec;
_clrjitPath = CrossgenTool.GetMetadata("JitPath");
_diasymreaderPath = CrossgenTool.GetMetadata("DiaSymReader");
if (!File.Exists(_crossgenPath) || !File.Exists(_clrjitPath))
{
return false;
}
_createPDBCommand = CompilationEntry.GetMetadata("CreatePDBCommand");
if (IsPdbCompilation)
{
_outputR2RImage = CompilationEntry.ItemSpec;
_outputPDBImage = CompilationEntry.GetMetadata("OutputPDBImage");
if (!String.IsNullOrEmpty(_diasymreaderPath) && !File.Exists(_diasymreaderPath))
{
return false;
}
// R2R image has to be created before emitting native symbols (crossgen needs this as an input argument)
if (String.IsNullOrEmpty(_outputPDBImage) || !File.Exists(_outputR2RImage))
{
return false;
}
}
else
{
_inputAssembly = CompilationEntry.ItemSpec;
_outputR2RImage = CompilationEntry.GetMetadata("OutputR2RImage");
if (!File.Exists(_inputAssembly))
{
return false;
}
}
return true;
}
// TEMP LOGIC - SDK Should provide correct list - When fixing https://github.com/dotnet/sdk/issues/3110, delete
// both IsManagedAssemblyToUseAsCrossgenReference and IsReferenceAssembly
bool IsManagedAssemblyToUseAsCrossgenReference(ITaskItem file)
{
// Reference only managed assemblies that will be published to the root directory.
string relativeOutputPath = file.GetMetadata(MetadataKeys.RelativePath);
if (!String.IsNullOrEmpty(Path.GetDirectoryName(relativeOutputPath)))
{
return false;
}
using (FileStream fs = new FileStream(file.ItemSpec, FileMode.Open, FileAccess.Read))
{
try
{
using (var pereader = new PEReader(fs))
{
if (!pereader.HasMetadata)
return false;
MetadataReader mdReader = pereader.GetMetadataReader();
if (!mdReader.IsAssembly)
return false;
// Reference assemblies should never be given to crossgen
if (IsReferenceAssembly(mdReader))
return false;
}
}
catch (BadImageFormatException)
{
// Not a valid assembly file
return false;
}
}
return true;
}
private bool IsReferenceAssembly(MetadataReader mdReader)
{
foreach (var attributeHandle in mdReader.GetAssemblyDefinition().GetCustomAttributes())
{
StringHandle attributeTypeName = default;
StringHandle attributeTypeNamespace = default;
EntityHandle attributeCtor = mdReader.GetCustomAttribute(attributeHandle).Constructor;
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 string GetAssemblyReferencesCommands()
{
StringBuilder result = new StringBuilder();
// Add all runtime libraries to the list of references passed to crossgen
foreach (var runtimeAssembly in ImplementationAssemblies)
{
// 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(runtimeAssembly.ItemSpec), Path.GetFileName(_outputR2RImage), StringComparison.OrdinalIgnoreCase))
continue;
// TODO: Delete check when fixing https://github.com/dotnet/sdk/issues/3110
if (!IsManagedAssemblyToUseAsCrossgenReference(runtimeAssembly))
continue;
result.AppendLine($"/r \"{runtimeAssembly}\"");
}
return result.ToString();
}
protected override string GenerateResponseFileCommands()
{
StringBuilder result = new StringBuilder();
result.AppendLine("/nologo");
if (IsPdbCompilation)
{
result.Append(GetAssemblyReferencesCommands());
if (!String.IsNullOrEmpty(_diasymreaderPath))
{
result.AppendLine($"/DiasymreaderPath \"{_diasymreaderPath}\"");
}
result.AppendLine(_createPDBCommand);
result.AppendLine($"\"{_outputR2RImage}\"");
}
else
{
result.AppendLine("/MissingDependenciesOK");
result.AppendLine($"/JITPath \"{_clrjitPath}\"");
result.Append(GetAssemblyReferencesCommands());
result.AppendLine($"/out \"{_outputR2RImage}\"");
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));
return base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
}
}
}