forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResolveReadyToRunCompilers.cs
381 lines (339 loc) · 16.5 KB
/
ResolveReadyToRunCompilers.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
// 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 ResolveReadyToRunCompilers : TaskBase
{
public bool EmitSymbols { get; set; }
public bool ReadyToRunUseCrossgen2 { get; set; }
[Required]
public ITaskItem[] RuntimePacks { get; set; }
public ITaskItem[] Crossgen2Packs { get; set; }
[Required]
public ITaskItem[] TargetingPacks { get; set; }
[Required]
public string RuntimeGraphPath { get; set; }
[Required]
public string NETCoreSdkRuntimeIdentifier { get; set; }
[Output]
public ITaskItem CrossgenTool { get; set; }
[Output]
public ITaskItem Crossgen2Tool { get; set; }
internal struct CrossgenToolInfo
{
public string ToolPath;
public string PackagePath;
public string ClrJitPath;
public string DiaSymReaderPath;
}
private ITaskItem _runtimePack;
private ITaskItem _crossgen2Pack;
private string _targetRuntimeIdentifier;
private string _targetPlatform;
private string _hostRuntimeIdentifier;
private CrossgenToolInfo _crossgenTool;
private CrossgenToolInfo _crossgen2Tool;
private Architecture _targetArchitecture;
protected override void ExecuteCore()
{
_runtimePack = GetNETCoreAppRuntimePack();
_crossgen2Pack = Crossgen2Packs?.FirstOrDefault();
_targetRuntimeIdentifier = _runtimePack?.GetMetadata(MetadataKeys.RuntimeIdentifier);
// Get the list of runtime identifiers that we support and can target
ITaskItem targetingPack = GetNETCoreAppTargetingPack();
string supportedRuntimeIdentifiers = targetingPack?.GetMetadata(MetadataKeys.RuntimePackRuntimeIdentifiers);
var runtimeGraph = new RuntimeGraphCache(this).GetRuntimeGraph(RuntimeGraphPath);
var supportedRIDsList = supportedRuntimeIdentifiers == null ? Array.Empty<string>() : supportedRuntimeIdentifiers.Split(';');
// Get the best RID for the host machine, which will be used to validate that we can run crossgen for the target platform and architecture
_hostRuntimeIdentifier = NuGetUtils.GetBestMatchingRid(
runtimeGraph,
NETCoreSdkRuntimeIdentifier,
supportedRIDsList,
out _);
if (_hostRuntimeIdentifier == null || _targetRuntimeIdentifier == null)
{
Log.LogError(Strings.ReadyToRunNoValidRuntimePackageError);
return;
}
if (ReadyToRunUseCrossgen2)
{
if (!ValidateCrossgen2Support())
{
return;
}
// NOTE: Crossgen2 does not yet currently support emitting native symbols, and until this feature
// is implemented, we will use crossgen for it. This should go away in the future when crossgen2 supports the feature.
if (EmitSymbols && !ValidateCrossgenSupport())
{
return;
}
}
else
{
if (!ValidateCrossgenSupport())
{
return;
}
}
}
private bool ValidateCrossgenSupport()
{
_crossgenTool.PackagePath = _runtimePack?.GetMetadata(MetadataKeys.PackageDirectory);
if (_crossgenTool.PackagePath == null)
{
Log.LogError(Strings.ReadyToRunNoValidRuntimePackageError);
return false;
}
if (!ExtractTargetPlatformAndArchitecture(_targetRuntimeIdentifier, out _targetPlatform, out _targetArchitecture) ||
!ExtractTargetPlatformAndArchitecture(_hostRuntimeIdentifier, out string hostPlatform, out Architecture hostArchitecture) ||
_targetPlatform != hostPlatform)
{
Log.LogError(Strings.ReadyToRunTargetNotSupportedError);
return false;
}
if (!GetCrossgenComponentsPaths())
{
Log.LogError(Strings.ReadyToRunTargetNotSupportedError);
return false;
}
// Create tool task item
CrossgenTool = new TaskItem(_crossgenTool.ToolPath);
CrossgenTool.SetMetadata("JitPath", _crossgenTool.ClrJitPath);
if (!String.IsNullOrEmpty(_crossgenTool.DiaSymReaderPath))
{
CrossgenTool.SetMetadata("DiaSymReader", _crossgenTool.DiaSymReaderPath);
}
return true;
}
private bool ValidateCrossgen2Support()
{
_crossgen2Tool.PackagePath = _crossgen2Pack?.GetMetadata(MetadataKeys.PackageDirectory);
if (_crossgen2Tool.PackagePath == null)
{
Log.LogError(Strings.ReadyToRunNoValidRuntimePackageError);
return false;
}
// Crossgen2 only supports the following host->target compilation scenarios in net5.0:
// win-x64 -> win-x64
// linux-x64 -> linux-x64
// linux-musl-x64 -> linux-musl-x64
if (!ExtractTargetPlatformAndArchitecture(_targetRuntimeIdentifier, out _targetPlatform, out _targetArchitecture) ||
!GetTargetSpec(out string targetSpec) ||
_targetRuntimeIdentifier != _hostRuntimeIdentifier)
{
Log.LogError(Strings.ReadyToRunTargetNotSupportedError);
return false;
}
if (!GetCrossgen2ComponentsPaths(targetSpec))
{
Log.LogError(Strings.ReadyToRunTargetNotSupportedError);
return false;
}
// Create tool task item
Crossgen2Tool = new TaskItem(_crossgen2Tool.ToolPath);
Crossgen2Tool.SetMetadata("JitPath", _crossgen2Tool.ClrJitPath);
if (!String.IsNullOrEmpty(_crossgen2Tool.DiaSymReaderPath))
{
Crossgen2Tool.SetMetadata("DiaSymReader", _crossgen2Tool.DiaSymReaderPath);
}
return true;
}
private ITaskItem GetNETCoreAppRuntimePack()
{
return GetNETCoreAppPack(RuntimePacks, MetadataKeys.FrameworkName);
}
private ITaskItem GetNETCoreAppTargetingPack()
{
return GetNETCoreAppPack(TargetingPacks, MetadataKeys.RuntimeFrameworkName);
}
private static ITaskItem GetNETCoreAppPack(ITaskItem[] packs, string metadataKey)
{
return packs.SingleOrDefault(
pack => pack.GetMetadata(metadataKey)
.Equals("Microsoft.NETCore.App", StringComparison.OrdinalIgnoreCase));
}
private static bool ExtractTargetPlatformAndArchitecture(string runtimeIdentifier, out string platform, out Architecture architecture)
{
platform = null;
architecture = default;
int separator = runtimeIdentifier.LastIndexOf('-');
if (separator < 0 || separator >= runtimeIdentifier.Length)
{
return false;
}
platform = runtimeIdentifier.Substring(0, separator).ToLowerInvariant();
string architectureStr = runtimeIdentifier.Substring(separator + 1).ToLowerInvariant();
switch (architectureStr)
{
case "arm":
architecture = Architecture.Arm;
break;
case "arm64":
architecture = Architecture.Arm64;
break;
case "x64":
architecture = Architecture.X64;
break;
case "x86":
architecture = Architecture.X86;
break;
default:
return false;
}
return true;
}
private bool GetCrossgenComponentsPaths()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (_targetArchitecture == Architecture.Arm)
{
if (RuntimeInformation.OSArchitecture == _targetArchitecture)
{
// We can run native arm32 bits on an arm64 host in WOW mode
_crossgenTool.ToolPath = Path.Combine(_crossgenTool.PackagePath, "tools", "crossgen.exe");
_crossgenTool.ClrJitPath = Path.Combine(_crossgenTool.PackagePath, "runtimes", _targetRuntimeIdentifier, "native", "clrjit.dll");
_crossgenTool.DiaSymReaderPath = Path.Combine(_crossgenTool.PackagePath, "runtimes", _targetRuntimeIdentifier, "native", "Microsoft.DiaSymReader.Native.arm.dll");
}
else
{
// We can use the x86-hosted crossgen compiler to target ARM
_crossgenTool.ToolPath = Path.Combine(_crossgenTool.PackagePath, "tools", "x86_arm", "crossgen.exe");
_crossgenTool.ClrJitPath = Path.Combine(_crossgenTool.PackagePath, "runtimes", "x86_arm", "native", "clrjit.dll");
_crossgenTool.DiaSymReaderPath = Path.Combine(_crossgenTool.PackagePath, "runtimes", _targetRuntimeIdentifier, "native", "Microsoft.DiaSymReader.Native.x86.dll");
}
}
else if (_targetArchitecture == Architecture.Arm64)
{
if (RuntimeInformation.OSArchitecture == _targetArchitecture)
{
_crossgenTool.ToolPath = Path.Combine(_crossgenTool.PackagePath, "tools", "crossgen.exe");
_crossgenTool.ClrJitPath = Path.Combine(_crossgenTool.PackagePath, "runtimes", _targetRuntimeIdentifier, "native", "clrjit.dll");
_crossgenTool.DiaSymReaderPath = Path.Combine(_crossgenTool.PackagePath, "runtimes", _targetRuntimeIdentifier, "native", "Microsoft.DiaSymReader.Native.arm64.dll");
}
else
{
// We only have 64-bit hosted compilers for ARM64.
if (RuntimeInformation.OSArchitecture != Architecture.X64)
{
return false;
}
_crossgenTool.ToolPath = Path.Combine(_crossgenTool.PackagePath, "tools", "x64_arm64", "crossgen.exe");
_crossgenTool.ClrJitPath = Path.Combine(_crossgenTool.PackagePath, "runtimes", "x64_arm64", "native", "clrjit.dll");
_crossgenTool.DiaSymReaderPath = Path.Combine(_crossgenTool.PackagePath, "runtimes", _targetRuntimeIdentifier, "native", "Microsoft.DiaSymReader.Native.amd64.dll");
}
}
else
{
_crossgenTool.ToolPath = Path.Combine(_crossgenTool.PackagePath, "tools", "crossgen.exe");
_crossgenTool.ClrJitPath = Path.Combine(_crossgenTool.PackagePath, "runtimes", _targetRuntimeIdentifier, "native", "clrjit.dll");
if (_targetArchitecture == Architecture.X64)
{
_crossgenTool.DiaSymReaderPath = Path.Combine(_crossgenTool.PackagePath, "runtimes", _targetRuntimeIdentifier, "native", "Microsoft.DiaSymReader.Native.amd64.dll");
}
else
{
_crossgenTool.DiaSymReaderPath = Path.Combine(_crossgenTool.PackagePath, "runtimes", _targetRuntimeIdentifier, "native", "Microsoft.DiaSymReader.Native.x86.dll");
}
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
if (_targetArchitecture == Architecture.Arm || _targetArchitecture == Architecture.Arm64)
{
if (RuntimeInformation.OSArchitecture == _targetArchitecture)
{
_crossgenTool.ToolPath = Path.Combine(_crossgenTool.PackagePath, "tools", "crossgen");
_crossgenTool.ClrJitPath = Path.Combine(_crossgenTool.PackagePath, "runtimes", _targetRuntimeIdentifier, "native", "libclrjit.so");
}
else if (RuntimeInformation.OSArchitecture == Architecture.X64)
{
string xarchPath = (_targetArchitecture == Architecture.Arm ? "x64_arm" : "x64_arm64");
_crossgenTool.ToolPath = Path.Combine(_crossgenTool.PackagePath, "tools", xarchPath, "crossgen");
_crossgenTool.ClrJitPath = Path.Combine(_crossgenTool.PackagePath, "runtimes", xarchPath, "native", "libclrjit.so");
}
else
{
return false;
}
}
else
{
_crossgenTool.ToolPath = Path.Combine(_crossgenTool.PackagePath, "tools", "crossgen");
_crossgenTool.ClrJitPath = Path.Combine(_crossgenTool.PackagePath, "runtimes", _targetRuntimeIdentifier, "native", "libclrjit.so");
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
// Only x64 supported for OSX
if (_targetArchitecture != Architecture.X64 || RuntimeInformation.OSArchitecture != Architecture.X64)
{
return false;
}
_crossgenTool.ToolPath = Path.Combine(_crossgenTool.PackagePath, "tools", "crossgen");
_crossgenTool.ClrJitPath = Path.Combine(_crossgenTool.PackagePath, "runtimes", _targetRuntimeIdentifier, "native", "libclrjit.dylib");
}
else
{
// Unknown platform
return false;
}
return File.Exists(_crossgenTool.ToolPath) && File.Exists(_crossgenTool.ClrJitPath);
}
private bool GetCrossgen2ComponentsPaths(string targetSpec)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
_crossgen2Tool.ToolPath = Path.Combine(_crossgen2Tool.PackagePath, "tools", "crossgen2.exe");
_crossgen2Tool.ClrJitPath = Path.Combine(_crossgen2Tool.PackagePath, "tools", $"clrjit-{targetSpec}.dll");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
_crossgen2Tool.ToolPath = Path.Combine(_crossgen2Tool.PackagePath, "tools", "crossgen2");
_crossgen2Tool.ClrJitPath = Path.Combine(_crossgen2Tool.PackagePath, "tools", $"libclrjit-{targetSpec}.so");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
_crossgen2Tool.ToolPath = Path.Combine(_crossgen2Tool.PackagePath, "tools", "crossgen2");
_crossgen2Tool.ClrJitPath = Path.Combine(_crossgen2Tool.PackagePath, "tools", $"libclrjit-{targetSpec}.dylib");
}
else
{
// Unknown platform
return false;
}
return File.Exists(_crossgen2Tool.ToolPath) && File.Exists(_crossgen2Tool.ClrJitPath);
}
// Keep in sync with JitConfigProvider.GetTargetSpec
private bool GetTargetSpec(out string targetSpec)
{
string targetOSComponent = (_targetPlatform == "win" ? "win" : "unix");
string targetArchComponent = _targetArchitecture switch
{
Architecture.X86 => "x86",
Architecture.X64 => "x64",
Architecture.Arm => "arm",
Architecture.Arm64 => "arm64",
_ => null
};
if (targetArchComponent == null)
{
targetSpec = null;
return false;
}
targetSpec = targetOSComponent + '-' + targetArchComponent;
return true;
}
}
}