-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathRunReadyToRunCompiler.cs
269 lines (229 loc) · 10.1 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
// 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.IO;
using System.Runtime.InteropServices;
using System.Text;
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 bool ShowCompilerWarnings { get; set; }
public bool UseCrossgen2 { get; set; }
public bool Crossgen2Composite { get; set; }
public string Crossgen2ExtraCommandLineArgs { get; set; }
[Output]
public bool WarningsDetected { get; set; }
private string _inputAssembly;
private string _outputR2RImage;
private string _outputPDBImage;
private string _createPDBCommand;
private bool IsPdbCompilation => !String.IsNullOrEmpty(_createPDBCommand);
protected override string ToolName
{
get
{
// NOTE: Crossgen2 does not yet support emitting native symbols. We use crossgen instead for now.
if (UseCrossgen2)
{
return IsPdbCompilation ? CrossgenTool.ItemSpec : Crossgen2Tool.ItemSpec;
}
else
{
return CrossgenTool.ItemSpec;
}
}
}
protected override string GenerateFullPathToTool() => ToolName;
// NOTE: Crossgen2 does not yet support emitting native symbols. We use crossgen instead for now.
private string DiaSymReader => CrossgenTool.GetMetadata(MetadataKeys.DiaSymReader);
public RunReadyToRunCompiler()
{
LogStandardErrorAsError = true;
}
protected override bool ValidateParameters()
{
_createPDBCommand = CompilationEntry.GetMetadata(MetadataKeys.CreatePDBCommand);
if (CrossgenTool == null && Crossgen2Tool == null)
{
return false;
}
if (IsPdbCompilation && CrossgenTool == null)
{
// We need the crossgen tool for now to emit native symbols. Crossgen2 does not yet support this feature
return false;
}
if (CrossgenTool != null)
{
if (!File.Exists(CrossgenTool.ItemSpec) || !File.Exists(CrossgenTool.GetMetadata(MetadataKeys.JitPath)))
{
return false;
}
}
if (Crossgen2Tool != null)
{
// We expect JitPath to be set for .NET 5 and {TargetOS, TargetArch} to be set for .NET 6 and later
string jitPath = Crossgen2Tool.GetMetadata(MetadataKeys.JitPath);
if (!File.Exists(Crossgen2Tool.ItemSpec) ||
(!String.IsNullOrEmpty(jitPath) && !File.Exists(jitPath)) ||
(String.IsNullOrEmpty(jitPath) && (
String.IsNullOrEmpty(Crossgen2Tool.GetMetadata(MetadataKeys.TargetOS)) ||
String.IsNullOrEmpty(Crossgen2Tool.GetMetadata(MetadataKeys.TargetArch)))))
{
return false;
}
}
if (IsPdbCompilation)
{
_outputR2RImage = CompilationEntry.ItemSpec;
_outputPDBImage = CompilationEntry.GetMetadata(MetadataKeys.OutputPDBImage);
if (!String.IsNullOrEmpty(DiaSymReader) && !File.Exists(DiaSymReader))
{
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(MetadataKeys.OutputR2RImage);
if (!File.Exists(_inputAssembly))
{
return false;
}
}
return true;
}
private string GetAssemblyReferencesCommands()
{
StringBuilder result = new StringBuilder();
foreach (var reference in 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 GenerateResponseFileCommands()
{
// NOTE: Crossgen2 does not yet support emitting native symbols. We use crossgen instead for now.
if (IsPdbCompilation)
{
return GenerateCrossgenResponseFile();
}
else
{
return UseCrossgen2 ? GenerateCrossgen2ResponseFile() : GenerateCrossgenResponseFile();
}
}
private string GenerateCrossgenResponseFile()
{
StringBuilder result = new StringBuilder();
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 StringBuilder();
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");
if (Crossgen2Composite)
{
result.AppendLine("--composite");
result.AppendLine("--inputbubble");
result.AppendLine($"--out:\"{_outputR2RImage}\"");
if (!String.IsNullOrEmpty(Crossgen2ExtraCommandLineArgs))
{
result.AppendLine(Crossgen2ExtraCommandLineArgs);
}
// 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 ImplementationAssemblyReferences)
{
result.AppendLine(reference.ItemSpec);
}
}
else
{
result.Append(GetAssemblyReferencesCommands());
result.AppendLine($"--out:\"{_outputR2RImage}\"");
if (!String.IsNullOrEmpty(Crossgen2ExtraCommandLineArgs))
{
result.AppendLine(Crossgen2ExtraCommandLineArgs);
}
// 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);
}
}
}
}