From 362c12db7bb85db6574d397437704854776005c9 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 2 Jan 2025 13:49:51 -0800 Subject: [PATCH] Don't require there to be a supported runtime pack runtime identifier for crossgen2 (#45487) --- .../ProcessFrameworkReferences.cs | 2 + .../ResolveReadyToRunCompilers.cs | 52 +++++++++++++------ 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/ProcessFrameworkReferences.cs b/src/Tasks/Microsoft.NET.Build.Tasks/ProcessFrameworkReferences.cs index 245aa9bdf5aa..b9704a7e11c8 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/ProcessFrameworkReferences.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/ProcessFrameworkReferences.cs @@ -814,6 +814,8 @@ private ToolPackSupport AddToolPack( runtimePackToDownload.SetMetadata(MetadataKeys.Version, packVersion); } + runtimePackItem.SetMetadata(MetadataKeys.RuntimeIdentifier, hostRuntimeIdentifier); + switch (toolPackType) { case ToolPackType.Crossgen2: diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/ResolveReadyToRunCompilers.cs b/src/Tasks/Microsoft.NET.Build.Tasks/ResolveReadyToRunCompilers.cs index af35bd3bcd0b..4f2c34b8e54d 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/ResolveReadyToRunCompilers.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/ResolveReadyToRunCompilers.cs @@ -52,21 +52,7 @@ 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() : 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) + if (_targetRuntimeIdentifier == null) { Log.LogError(Strings.ReadyToRunNoValidRuntimePackageError); return; @@ -96,6 +82,13 @@ protected override void ExecuteCore() private bool ValidateCrossgenSupport() { + _hostRuntimeIdentifier = GetHostRuntimeIdentifierForCrossgen(); + if (_hostRuntimeIdentifier == null) + { + Log.LogError(Strings.ReadyToRunNoValidRuntimePackageError); + return false; + } + _crossgenTool.PackagePath = _runtimePack?.GetMetadata(MetadataKeys.PackageDirectory); if (_crossgenTool.PackagePath == null) { @@ -121,12 +114,39 @@ private bool ValidateCrossgenSupport() } return true; + + string GetHostRuntimeIdentifierForCrossgen() + { + // Crossgen's host RID comes from the runtime pack that Crossgen will be loaded from. + + // 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() : 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 + return NuGetUtils.GetBestMatchingRid( + runtimeGraph, + NETCoreSdkRuntimeIdentifier, + supportedRIDsList, + out _); + } } private bool ValidateCrossgen2Support() { ITaskItem crossgen2Pack = Crossgen2Packs?.FirstOrDefault(); - _crossgen2Tool.PackagePath = crossgen2Pack?.GetMetadata(MetadataKeys.PackageDirectory); + + _hostRuntimeIdentifier = crossgen2Pack?.GetMetadata(MetadataKeys.RuntimeIdentifier); + if (_hostRuntimeIdentifier == null) + { + Log.LogError(Strings.ReadyToRunNoValidRuntimePackageError); + return false; + } + + _crossgen2Tool.PackagePath = crossgen2Pack.GetMetadata(MetadataKeys.PackageDirectory); if (string.IsNullOrEmpty(_crossgen2Tool.PackagePath) || !NuGetVersion.TryParse(crossgen2Pack.GetMetadata(MetadataKeys.NuGetPackageVersion), out NuGetVersion crossgen2PackVersion))