forked from ForNeVeR/Cesium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TargetRuntime.cs
69 lines (61 loc) · 2.39 KB
/
TargetRuntime.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
using System.Runtime.Versioning;
using Cesium.Core;
using Mono.Cecil;
namespace Cesium.CodeGen;
public enum SystemAssemblyKind
{
MsCorLib,
SystemRuntime,
NetStandard,
}
public record TargetRuntimeDescriptor(
SystemAssemblyKind Kind,
Version SystemLibraryVersion,
Version TargetFrameworkVersion)
{
public static readonly TargetRuntimeDescriptor Net60 = new(
SystemAssemblyKind.SystemRuntime,
Version.Parse("6.0"),
Version.Parse("6.0"));
public static readonly TargetRuntimeDescriptor NetStandard20 = new(
SystemAssemblyKind.NetStandard,
Version.Parse("2.0"),
Version.Parse("2.0"));
public static readonly TargetRuntimeDescriptor Net48 = new(
SystemAssemblyKind.NetStandard,
Version.Parse("4.0"),
Version.Parse("4.8"));
public AssemblyNameReference GetSystemAssemblyReference()
{
var (assemblyName, publicKeyToken) = Kind switch
{
SystemAssemblyKind.MsCorLib =>
("mscorlib", new byte[] { 0xb7, 0x7a, 0x5c, 0x56, 0x19, 0x34, 0xe0, 0x89 }),
SystemAssemblyKind.SystemRuntime =>
("System.Runtime", new byte[] { 0xb0, 0x3f, 0x5f, 0x7f, 0x11, 0xd5, 0x0a, 0x3a }),
SystemAssemblyKind.NetStandard =>
("netstandard", new byte[] { 0xb0, 0x3f, 0x5f, 0x7f, 0x11, 0xd5, 0x0a, 0x3a }),
_ => throw new CompilationException($"Unknown assembly kind: {Kind}")
};
return new AssemblyNameReference(assemblyName, SystemLibraryVersion)
{
PublicKeyToken = publicKeyToken
};
}
public CustomAttribute GetTargetFrameworkAttribute(ModuleDefinition module)
{
var frameworkName = Kind switch
{
SystemAssemblyKind.MsCorLib => ".NETFramework",
SystemAssemblyKind.SystemRuntime => ".NETCoreApp",
SystemAssemblyKind.NetStandard => ".NETStandard",
_ => throw new CompilationException($"Unknown target runtime kind: {Kind}")
} + $",Version=v{TargetFrameworkVersion}";
var constructor = typeof(TargetFrameworkAttribute).GetConstructor(new[] { typeof(string) });
var constructorRef = module.ImportReference(constructor);
return new CustomAttribute(constructorRef)
{
ConstructorArguments = { new CustomAttributeArgument(module.TypeSystem.String, frameworkName) }
};
}
}