/
ArchitectureExtensions.cs
75 lines (61 loc) · 2.74 KB
/
ArchitectureExtensions.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
70
71
72
73
74
75
// Copyright Bastian Eicher et al.
// Licensed under the GNU Lesser Public License
namespace ZeroInstall.Model;
/// <summary>
/// Contains extension methods for <see cref="Architecture"/>, <see cref="OS"/> and <see cref="Cpu"/>.
/// </summary>
public static class ArchitectureExtensions
{
/// <summary>
/// Determines whether an implementation for <paramref name="architecture"/> can run on <paramref name="target"/>.
/// </summary>
public static bool RunsOn(this Architecture architecture, Architecture target)
=> architecture.OS.RunsOn(target.OS) && architecture.Cpu.RunsOn(target.Cpu)
|| architecture is {Cpu: >= Cpu.I386 and <= Cpu.X64} && target is {Cpu: Cpu.AArch64, OS: OS.Windows or OS.MacOSX}; // x86/64 emulation on ARM
/// <summary>
/// Determines whether an implementation for <paramref name="os"/> can run on <paramref name="target"/>.
/// </summary>
public static bool RunsOn(this OS os, OS target)
=> os switch
{
OS.Unknown or OS.Unknown => false,
// Exact match
_ when os == target => true,
// Platform-neutral implementation
OS.All => true,
_ when target == OS.All => true,
// Compatible supersets
OS.Windows when target == OS.Cygwin => true,
OS.Darwin when target == OS.MacOSX => true,
OS.Posix when target <= OS.Cygwin => true,
_ => false
};
/// <summary>
/// Determines whether an implementation for <paramref name="cpu"/> can run on <paramref name="target"/>.
/// </summary>
public static bool RunsOn(this Cpu cpu, Cpu target)
=> cpu switch
{
Cpu.Unknown or Cpu.Unknown => false,
// Exact match
_ when cpu == target => true,
// Platform-neutral implementation
Cpu.All => true,
_ when target == Cpu.All => true,
// Compatible supersets
>= Cpu.I386 and <= Cpu.X64 when target >= cpu && target <= Cpu.X64 => true,
>= Cpu.Ppc and <= Cpu.Ppc64 when target >= cpu && target <= Cpu.Ppc64 => true,
>= Cpu.ArmV6L and <= Cpu.AArch64 when target >= cpu && target <= Cpu.AArch64 => true,
_ => false
};
/// <summary>
/// Indicates whether the CPU architecture is 32-bit.
/// </summary>
public static bool Is32Bit(this Cpu cpu)
=> cpu is ((>= Cpu.I386 and <= Cpu.I686) or Cpu.Ppc or Cpu.ArmV6L or Cpu.ArmV7L);
/// <summary>
/// Indicates whether the CPU architecture is 64-bit.
/// </summary>
public static bool Is64Bit(this Cpu cpu)
=> cpu is (Cpu.X64 or Cpu.Ppc64 or Cpu.AArch64);
}