Skip to content

Commit

Permalink
Add RuntimeIdMap
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Jun 29, 2018
1 parent 993e64c commit b6271ac
Show file tree
Hide file tree
Showing 7 changed files with 3,622 additions and 35 deletions.
3 changes: 2 additions & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
-->

<!-- Libs -->
<LibGit2SharpVersion>0.26.0-preview-0017</LibGit2SharpVersion>
<LibGit2SharpVersion>0.26.0-preview-0027</LibGit2SharpVersion>
<MicrosoftBuildVersion>15.6.82</MicrosoftBuildVersion>
<MicrosoftBuildTasksCore>15.6.82</MicrosoftBuildTasksCore>
<MicrosoftTeamFoundationServerExtendedClientVersion>15.112.1</MicrosoftTeamFoundationServerExtendedClientVersion>
<MicrosoftDotNetPlatformAbstractionsVersion>2.1.0</MicrosoftDotNetPlatformAbstractionsVersion>

<MicrosoftSourceLinkVersion>1.0.0-beta-63001-01</MicrosoftSourceLinkVersion>
</PropertyGroup>
Expand Down
133 changes: 133 additions & 0 deletions scripts/RuntimeIdMapGenerator.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#r "Newtonsoft.Json.dll"

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using Newtonsoft.Json.Linq;

// The RIDs supported by LibGit2 - these match the directory names (runtimes/{name}/native).
var availableRids = new[]
{
"alpine-x64",
"debian.9-x64",
"fedora-x64",
"linux-x64",
"osx",
"rhel-x64",
"win-x64",
"win-x86",
};

// The path to runtime.json file retrieved from https://raw.githubusercontent.com/dotnet/corefx/master/pkg/Microsoft.NETCore.Platforms/runtime.json.
var runtimeJsonPath = Path.Combine(GetScriptDir(), "runtime.json");

var runtimes = BuildRuntimeGraph();

var map = new Dictionary<string, string>();

foreach (var entry in runtimes)
{
var rid = entry.Key;
var compatibleRids = GetCompatibleRuntimeIdentifiers(runtimes, rid);
foreach (var availableRid in availableRids)
{
if (compatibleRids.Contains(availableRid))
{
// use the first rid, it is the most specific:
if (!map.TryGetValue(rid, out var existing))
{
map.Add(rid, availableRid);
}
}
}
}

var orderedMap = map.OrderBy(e => e.Key);

Console.WriteLine("private static readonly string[] s_rids = new[]");
Console.WriteLine("{");

foreach (var entry in orderedMap)
{
Console.WriteLine($" \"{entry.Key}\",");
}

Console.WriteLine("}");
Console.WriteLine();
Console.WriteLine("private static readonly string[] s_directories = new[]");
Console.WriteLine("{");

foreach (var entry in orderedMap)
{
Console.WriteLine($" \"{entry.Value}\",");
}

Console.WriteLine("}");

string GetScriptDir([CallerFilePath] string path = null) => Path.GetDirectoryName(path);

Dictionary<string, Runtime> BuildRuntimeGraph()
{
var rids = new Dictionary<string, Runtime>();

var json = JObject.Parse(File.ReadAllText(runtimeJsonPath));
var runtimes = (JObject)json["runtimes"];

foreach (var runtime in runtimes)
{
var imports = (JArray)((JObject)runtime.Value)["#import"];
rids.Add(runtime.Key, new Runtime(runtime.Key, imports.Select(import => (string)import).ToArray()));
}

return rids;
}

struct Runtime
{
public string RuntimeIdentifier { get; }
public string[] ImportedRuntimeIdentifiers { get; }

public Runtime(string runtimeIdentifier, string[] importedRuntimeIdentifiers)
{
RuntimeIdentifier = runtimeIdentifier;
ImportedRuntimeIdentifiers = importedRuntimeIdentifiers;
}
}

List<string> GetCompatibleRuntimeIdentifiers(Dictionary<string, Runtime> runtimes, string runtimeIdentifier)
{
var result = new List<string>();

if (runtimes.TryGetValue(runtimeIdentifier, out var initialRuntime))
{
var queue = new Queue<Runtime>();
var hash = new HashSet<string>();

hash.Add(runtimeIdentifier);
queue.Enqueue(initialRuntime);

while (queue.Count > 0)
{
var runtime = queue.Dequeue();
result.Add(runtime.RuntimeIdentifier);

foreach (var item in runtime.ImportedRuntimeIdentifiers)
{
if (hash.Add(item))
{
queue.Enqueue(runtimes[item]);
}
}
}
}
else
{
result.Add(runtimeIdentifier);
}

return result;
}
Loading

0 comments on commit b6271ac

Please sign in to comment.