Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inferred runtime identifiers #7141

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public ITaskItem[] RuntimeGroups
set;
}

/// <summary>
/// Additional runtime identifiers to add to the graph.
/// </summary>
public string[] InferRuntimeIdentifiers
{
get;
set;
}

/// <summary>
/// Optional source Runtime.json to use as a starting point when merging additional RuntimeGroups
/// </summary>
Expand Down Expand Up @@ -133,7 +142,11 @@ public override bool Execute()
runtimeGraph = new RuntimeGraph();
}

foreach (var runtimeGroup in RuntimeGroups.NullAsEmpty().Select(i => new RuntimeGroup(i)))
List<RuntimeGroup> runtimeGroups = RuntimeGroups.NullAsEmpty().Select(i => new RuntimeGroup(i)).ToList();

AddInferredRuntimeIdentifiers(runtimeGroups, InferRuntimeIdentifiers.NullAsEmpty());

foreach (var runtimeGroup in runtimeGroups)
{
runtimeGraph = SafeMerge(runtimeGraph, runtimeGroup);
}
Expand Down Expand Up @@ -270,6 +283,79 @@ private void ValidateImports(RuntimeGraph runtimeGraph, IDictionary<string, stri
}
}
}
private void AddInferredRuntimeIdentifiers(ICollection<RuntimeGroup> runtimeGroups, IEnumerable<string> runtimeIdentifiers)
{
var runtimeGroupsByBaseRID = runtimeGroups.GroupBy(rg => rg.BaseRID).ToDictionary(g => g.Key, g => new List<RuntimeGroup>(g.AsEnumerable()));

foreach(var runtimeIdentifer in runtimeIdentifiers)
{
RID rid = RID.Parse(runtimeIdentifer);

if (!rid.HasArchitecture() && !rid.HasVersion())
{
Log.LogError($"Cannot add Runtime {rid} to any existing group since it has no architcture nor version.");
continue;
}

if (runtimeGroupsByBaseRID.TryGetValue(rid.BaseRID, out var candidateRuntimeGroups))
{
RuntimeGroup closestGroup = null;
RuntimeVersion closestVersion = null;

foreach(var candidate in candidateRuntimeGroups)
{
if (rid.HasArchitecture() && !candidate.Architectures.Contains(rid.Architecture))
{
continue;
}

foreach(var version in candidate.Versions)
{
if (closestVersion == null ||
((version <= rid.Version) &&
(version > closestVersion)))
{
closestVersion = version;
closestGroup = candidate;
}
}
}

if (closestGroup == null)
{
// couldn't find a close group, create a new one for just this arch/version
RuntimeGroup templateGroup = candidateRuntimeGroups.First();
RuntimeGroup runtimeGroup = RuntimeGroup.CreateFromTemplate(templateGroup);

if (rid.HasArchitecture())
{
runtimeGroup.Architectures.Add(rid.Architecture);
}

if (rid.HasVersion())
{
runtimeGroup.Versions.Add(rid.Version);
}

// add to overall list
runtimeGroups.Add(runtimeGroup);

// add to our base-RID specific list from the dictionary so that further iterations see it.
candidateRuntimeGroups.Add(runtimeGroup);

}
else if (closestVersion != rid.Version)
{
closestGroup.Versions.Add(rid.Version);
}

}
else
{
Log.LogError($"Cannot find a group to add Runtime {rid} ({rid.BaseRID}) from {string.Join(",", runtimeGroupsByBaseRID.Keys)}");
}
}
}

private static IDictionary<string, IEnumerable<string>> GetCompatibilityMap(RuntimeGraph graph)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
<BeforePack>$(BeforePack);AddRuntimeJson</BeforePack>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="Microsoft.DotNet.Build.Tasks.Packaging.Tests" />
</ItemGroup>

<ItemGroup>
<!-- nuget pack needs an actual empty file to use for placeholders for empty directories
so we must provide one.
Expand Down
165 changes: 160 additions & 5 deletions src/Microsoft.DotNet.Build.Tasks.Packaging/src/RID.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.Text;

namespace Microsoft.DotNet.Build.Tasks.Packaging
{
internal class RID
{
internal const char VersionDelimiter = '.';
internal const char ArchitectureDelimiter = '-';
internal const char QualifierDelimiter = '-';

public string BaseRID { get; set; }
public string VersionDelimiter { get; set; }
public string Version { get; set; }
public string ArchitectureDelimiter { get; set; }
public bool OmitVersionDelimiter { get; set; }
public RuntimeVersion Version { get; set; }
public string Architecture { get; set; }
public string QualifierDelimiter { get; set; }
public string Qualifier { get; set; }

public override string ToString()
Expand All @@ -21,7 +25,10 @@ public override string ToString()

if (HasVersion())
{
builder.Append(VersionDelimiter);
if (!OmitVersionDelimiter)
{
builder.Append(VersionDelimiter);
}
builder.Append(Version);
}

Expand All @@ -40,6 +47,120 @@ public override string ToString()
return builder.ToString();
}


enum RIDPart : int
{
Base = 0,
Version,
Architcture,
Qualifier,
Max = Qualifier
}

public static RID Parse(string runtimeIdentifier)
{
string[] parts = new string[(int)RIDPart.Max + 1];
bool omitVersionDelimiter = true;
RIDPart parseState = RIDPart.Base;

int partStart = 0, partLength = 0;

// qualifier is indistinguishable from arch so we cannot distinguish it for parsing purposes
Debug.Assert(ArchitectureDelimiter == QualifierDelimiter);

for (int i = 0; i < runtimeIdentifier.Length; i++)
{
char current = runtimeIdentifier[i];
partLength = i - partStart;

switch (parseState)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider using a regular expression for parsing instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I had a regex and it was much more difficult to read once it was complicated enough to handle all cases and capture all fields that we needed. This simple state machine is easier to read and debug (and happens to be more efficient though that's not a real concern for this one shot task).

{
case RIDPart.Base:
// treat any number as the start of the version
if (current == VersionDelimiter || (current >= '0' && current <= '9'))
{
SetPart();
partStart = i;
if (current == VersionDelimiter)
{
omitVersionDelimiter = false;
partStart = i + 1;
}
parseState = RIDPart.Version;
}
// version might be omitted
else if (current == ArchitectureDelimiter)
{
// ensure there's no version later in the string
if (-1 != runtimeIdentifier.IndexOf(VersionDelimiter, i))
{
break;
}
SetPart();
partStart = i + 1; // skip delimiter
parseState = RIDPart.Architcture;
}
break;
case RIDPart.Version:
if (current == ArchitectureDelimiter)
{
SetPart();
partStart = i + 1; // skip delimiter
parseState = RIDPart.Architcture;
}
break;
case RIDPart.Architcture:
if (current == QualifierDelimiter)
{
SetPart();
partStart = i + 1; // skip delimiter
parseState = RIDPart.Qualifier;
}
break;
default:
break;
}
}

partLength = runtimeIdentifier.Length - partStart;
if (partLength > 0)
{
SetPart();
}

string GetPart(RIDPart part)
{
return parts[(int)part];
}

void SetPart()
{
if (partLength == 0)
{
throw new ArgumentException($"unexpected delimiter at position {partStart} in {runtimeIdentifier}");
}

parts[(int)parseState] = runtimeIdentifier.Substring(partStart, partLength);
}

string version = GetPart(RIDPart.Version);

if (version == null)
{
omitVersionDelimiter = false;
}

return new RID()
{
BaseRID = GetPart(RIDPart.Base),
OmitVersionDelimiter = omitVersionDelimiter,
Version = version == null ? null : new RuntimeVersion(version),
Architecture = GetPart(RIDPart.Architcture),
Qualifier = GetPart(RIDPart.Qualifier)
};
}


public bool HasVersion()
{
return Version != null;
Expand All @@ -54,5 +175,39 @@ public bool HasQualifier()
{
return Qualifier != null;
}

public override bool Equals(object obj)
{
return Equals(obj as RID);
}

public bool Equals(RID obj)
{
return object.ReferenceEquals(obj, this) ||
(!(obj is null) &&
BaseRID == obj.BaseRID &&
OmitVersionDelimiter == obj.OmitVersionDelimiter &&
Version == obj.Version &&
Architecture == obj.Architecture &&
Qualifier == obj.Qualifier);

}

public override int GetHashCode()
{
#if NETFRAMEWORK
return BaseRID.GetHashCode();
#else
HashCode hashCode = new HashCode();
hashCode.Add(BaseRID);
hashCode.Add(VersionDelimiter);
hashCode.Add(Version);
hashCode.Add(ArchitectureDelimiter);
hashCode.Add(Architecture);
hashCode.Add(QualifierDelimiter);
hashCode.Add(Qualifier);
return hashCode.ToHashCode();
#endif
}
}
}
Loading