IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
RuntimeUtil.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Runtime.InteropServices;
5using System.Text.Json;
6
7namespace IKVM.Runtime
8{
9
13 public static class RuntimeUtil
14 {
15
16 static JsonElement? runtimeJsonElement;
17 static string runtimeIdentifier;
18
22 public static bool IsMono { get; } = Type.GetType("Mono.Runtime") != null;
23
27 public static bool IsWindows { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
28
32 public static bool IsLinux { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
33
37 public static bool IsOSX { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
38
42 public static string RuntimeIdentifier => runtimeIdentifier ??= GetRuntimeIdentifier();
43
48 static string GetRuntimeIdentifier()
49 {
50 // allow overrides with environment
51 if (Environment.GetEnvironmentVariable("DOTNET_RUNTIME_ID") is string rid1 && string.IsNullOrWhiteSpace(rid1) == false)
52 if (RuntimeJson.TryGetProperty(rid1, out _))
53 return rid1;
54
55#if NETCOREAPP
56 if (RuntimeInformation.RuntimeIdentifier is string rid2 && string.IsNullOrWhiteSpace(rid2) == false)
57 if (RuntimeJson.TryGetProperty(rid2, out _))
58 return rid2;
59#endif
60
61 return GetFallbackRuntimeIdentifier();
62 }
63
68 static string GetFallbackRuntimeIdentifier()
69 {
70 var arch = RuntimeInformation.ProcessArchitecture switch
71 {
72 Architecture.X86 => "x86",
73 Architecture.X64 => "x64",
74 Architecture.Arm => "arm",
75 Architecture.Arm64 => "arm64",
76 _ => null,
77 };
78
79 if (IsWindows)
80 return $"win-{arch}";
81
82 if (IsLinux)
83 return $"linux-{arch}";
84
85 if (IsOSX)
86 return $"osx-{arch}";
87
88 return null;
89 }
90
94 static JsonElement RuntimeJson => runtimeJsonElement ??= GetRuntimeJson();
95
100 static JsonElement GetRuntimeJson()
101 {
102 using var stream = typeof(RuntimeUtil).Assembly.GetManifestResourceStream("runtime.json");
103 var g = JsonDocument.Parse(stream);
104 return g.RootElement.GetProperty("runtimes");
105 }
106
110 public static IEnumerable<string> SupportedRuntimeIdentifiers => GetSupportedRuntimeIdentifiers();
111
116 static IEnumerable<string> GetSupportedRuntimeIdentifiers()
117 {
118 return GetRuntimeIdentifierIterator(RuntimeIdentifier).Distinct();
119 }
120
126 static IEnumerable<string> GetRuntimeIdentifierIterator(string rid)
127 {
128 if (RuntimeJson.TryGetProperty(rid, out var node) == false)
129 yield break;
130
131 // initiate a breadth first search across runtime.json from specified RID
132 var v = new HashSet<string>();
133 var q = new Queue<(string, JsonElement)>();
134 v.Add(rid);
135 q.Enqueue((rid, node));
136
137 // continue until end is reached
138 while (q.Count > 0)
139 {
140 // dequeue next item and yield
141 var (thisRid, thisNode) = q.Dequeue();
142 yield return thisRid;
143
144 // enqueue referenced rids
145 if (thisNode.TryGetProperty("#import", out var import))
146 foreach (var imports in import.EnumerateArray())
147 if (imports.GetString() is string nextRid && string.IsNullOrWhiteSpace(nextRid) == false)
148 if (v.Add(nextRid))
149 if (RuntimeJson.TryGetProperty(nextRid, out var nextNode))
150 q.Enqueue((nextRid, nextNode));
151 }
152 }
153
154 }
155
156}
IKVM.Reflection.Type Type
Maintains various information about the current runtime environment.
static IEnumerable< string > SupportedRuntimeIdentifiers
Gets the supported RIDs, returned in most specific order.
static bool IsMono
Returns true if the current runtime is Mono.
static bool IsOSX
Returns true if the current platform is Mac OS X.
static bool IsWindows
Returns true if the current platform is Windows.
static bool IsLinux
Returns true if the current platform is Linux.
static string RuntimeIdentifier
Gets the current runtime identifier.