forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResolveReadyToRunCompilers.cs
452 lines (403 loc) · 19.2 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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// 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;
using NuGet.Versioning;
namespace Microsoft.NET.Build.Tasks
{
public class ResolveReadyToRunCompilers : TaskBase
{
public bool EmitSymbols { get; set; }
public bool ReadyToRunUseCrossgen2 { get; set; }
public string PerfmapFormatVersion { 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 string _targetRuntimeIdentifier;
private string _targetPlatform;
private string _hostRuntimeIdentifier;
private CrossgenToolInfo _crossgenTool;
private CrossgenToolInfo _crossgen2Tool;
private Architecture _targetArchitecture;
private bool _crossgen2IsVersion5;
protected override void ExecuteCore()
{
_runtimePack = GetNETCoreAppRuntimePack();
_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;
}
// In .NET 5 Crossgen2 did not support emitting native symbols, so we use Crossgen to emit them
if (_crossgen2IsVersion5 && 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 ||
!GetCrossgenComponentsPaths())
{
Log.LogError(Strings.ReadyToRunTargetNotSupportedError);
return false;
}
// Create tool task item
CrossgenTool = new TaskItem(_crossgenTool.ToolPath);
CrossgenTool.SetMetadata(MetadataKeys.JitPath, _crossgenTool.ClrJitPath);
if (!string.IsNullOrEmpty(_crossgenTool.DiaSymReaderPath))
{
CrossgenTool.SetMetadata(MetadataKeys.DiaSymReader, _crossgenTool.DiaSymReaderPath);
}
return true;
}
private bool ValidateCrossgen2Support()
{
ITaskItem crossgen2Pack = Crossgen2Packs?.FirstOrDefault();
_crossgen2Tool.PackagePath = crossgen2Pack?.GetMetadata(MetadataKeys.PackageDirectory);
if (string.IsNullOrEmpty(_crossgen2Tool.PackagePath) ||
!NuGetVersion.TryParse(crossgen2Pack.GetMetadata(MetadataKeys.NuGetPackageVersion), out NuGetVersion crossgen2PackVersion))
{
Log.LogError(Strings.ReadyToRunNoValidRuntimePackageError);
return false;
}
bool version5 = crossgen2PackVersion.Major < 6;
bool isSupportedTarget = ExtractTargetPlatformAndArchitecture(_targetRuntimeIdentifier, out _targetPlatform, out _targetArchitecture);
// In .NET 5 Crossgen2 supported only the following host->target compilation scenarios:
// win-x64 -> win-x64
// linux-x64 -> linux-x64
// linux-musl-x64 -> linux-musl-x64
string targetOS = null;
isSupportedTarget = isSupportedTarget &&
GetCrossgen2TargetOS(out targetOS) &&
(!version5 || _targetRuntimeIdentifier == _hostRuntimeIdentifier) &&
GetCrossgen2ComponentsPaths(version5);
if (!isSupportedTarget)
{
Log.LogError(Strings.ReadyToRunTargetNotSupportedError);
return false;
}
// Create tool task item
Crossgen2Tool = new TaskItem(_crossgen2Tool.ToolPath);
Crossgen2Tool.SetMetadata(MetadataKeys.IsVersion5, version5.ToString());
if (version5)
{
Crossgen2Tool.SetMetadata(MetadataKeys.JitPath, _crossgen2Tool.ClrJitPath);
}
else
{
Crossgen2Tool.SetMetadata(MetadataKeys.TargetOS, targetOS);
Crossgen2Tool.SetMetadata(MetadataKeys.TargetArch, ArchitectureToString(_targetArchitecture));
if (!string.IsNullOrEmpty(PerfmapFormatVersion))
{
Crossgen2Tool.SetMetadata(MetadataKeys.PerfmapFormatVersion, PerfmapFormatVersion);
}
}
_crossgen2IsVersion5 = version5;
return true;
}
private bool GetCrossgen2TargetOS(out string targetOS)
{
targetOS = null;
// Determine targetOS based on target rid.
// Use the runtime graph to support non-portable target rids.
var runtimeGraph = new RuntimeGraphCache(this).GetRuntimeGraph(RuntimeGraphPath);
string portablePlatform = NuGetUtils.GetBestMatchingRid(
runtimeGraph,
_targetPlatform,
new[] { "linux", "linux-musl", "osx", "win", "freebsd", "illumos" },
out _);
// For source-build, allow the bootstrap SDK rid to be unknown to the runtime repo graph.
if (portablePlatform == null && _targetRuntimeIdentifier == _hostRuntimeIdentifier)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
portablePlatform = "win";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
portablePlatform = "linux";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD")))
{
portablePlatform = "freebsd";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("ILLUMOS")))
{
portablePlatform = "illumos";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
portablePlatform = "osx";
}
}
targetOS = portablePlatform switch
{
"linux" => "linux",
"linux-musl" => "linux",
"osx" => "osx",
"win" => "windows",
"freebsd" => "freebsd",
"illumos" => "illumos",
_ => null
};
return targetOS != null;
}
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;
// This will split RID like "linux-musl-arm64" into "linux-musl" and "arm64" components
int separator = runtimeIdentifier.LastIndexOf('-');
if (separator < 0)
{
return false;
}
string architectureStr = runtimeIdentifier.Substring(separator + 1).ToLowerInvariant();
switch (architectureStr)
{
case "arm":
architecture = Architecture.Arm;
break;
case "arm64":
architecture = Architecture.Arm64;
break;
#if !NETFRAMEWORK
case "riscv64":
architecture = Architecture.RiscV64;
break;
case "loongarch64":
architecture = Architecture.LoongArch64;
break;
#endif
case "x64":
architecture = Architecture.X64;
break;
case "x86":
architecture = Architecture.X86;
break;
default:
return false;
}
platform = runtimeIdentifier.Substring(0, separator).ToLowerInvariant();
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", "x86_arm", "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", "x64_arm64", "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(bool version5)
{
string toolFileName, v5_clrJitFileNamePattern;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
toolFileName = "crossgen2.exe";
v5_clrJitFileNamePattern = "clrjit-{0}.dll";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
toolFileName = "crossgen2";
v5_clrJitFileNamePattern = "libclrjit-{0}.dylib";
}
else
{
// Generic Unix-like: linux, freebsd, and others.
toolFileName = "crossgen2";
v5_clrJitFileNamePattern = "libclrjit-{0}.so";
}
if (version5)
{
string clrJitFileName = string.Format(v5_clrJitFileNamePattern, GetTargetSpecForVersion5());
_crossgen2Tool.ClrJitPath = Path.Combine(_crossgen2Tool.PackagePath, "tools", clrJitFileName);
if (!File.Exists(_crossgen2Tool.ClrJitPath))
{
return false;
}
}
_crossgen2Tool.ToolPath = Path.Combine(_crossgen2Tool.PackagePath, "tools", toolFileName);
return File.Exists(_crossgen2Tool.ToolPath);
}
// Keep in sync with JitConfigProvider.GetTargetSpec in .NET 5
private string GetTargetSpecForVersion5()
{
string targetOSComponent = (_targetPlatform == "win" ? "win" : "unix");
string targetArchComponent = ArchitectureToString(_targetArchitecture);
return targetOSComponent + '-' + targetArchComponent;
}
private static string ArchitectureToString(Architecture architecture)
{
return architecture switch
{
Architecture.X86 => "x86",
Architecture.X64 => "x64",
Architecture.Arm => "arm",
Architecture.Arm64 => "arm64",
#if !NETFRAMEWORK
Architecture.RiscV64 => "riscv64",
Architecture.LoongArch64 => "loongarch64",
#endif
_ => null
};
}
}
}