author | wenzelm |
Tue, 26 Mar 2024 17:06:23 +0100 | |
changeset 80008 | 914c4a81027d |
parent 79981 | bdea4eccd8d5 |
child 80038 | b1e2246147eb |
permissions | -rw-r--r-- |
31825 | 1 |
/* Title: Pure/System/platform.scala |
2 |
Author: Makarius |
|
3 |
||
69112 | 4 |
System platform identification. |
31825 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
31828 | 9 |
|
75393 | 10 |
object Platform { |
69112 | 11 |
/* platform family */ |
31828 | 12 |
|
73906 | 13 |
val is_windows: Boolean = isabelle.setup.Environment.is_windows() |
71383 | 14 |
val is_linux: Boolean = System.getProperty("os.name", "") == "Linux" |
15 |
val is_macos: Boolean = System.getProperty("os.name", "") == "Mac OS X" |
|
73602
37243ad3ecb6
fast approximation of test for process group (NB: initial process might already be terminated, while background processes are still running);
wenzelm
parents:
73193
diff
changeset
|
16 |
val is_unix: Boolean = is_linux || is_macos |
31825 | 17 |
|
73637 | 18 |
def is_arm: Boolean = cpu_arch.startsWith("arm") |
19 |
||
78610 | 20 |
def family: Family = |
73637 | 21 |
if (is_linux && is_arm) Family.linux_arm |
22 |
else if (is_linux) Family.linux |
|
69410 | 23 |
else if (is_macos) Family.macos |
24 |
else if (is_windows) Family.windows |
|
25 |
else error("Failed to determine current platform family") |
|
26 |
||
78610 | 27 |
object Family { |
28 |
val list: List[Family] = List(Family.linux, Family.linux_arm, Family.windows, Family.macos) |
|
69410 | 29 |
|
78610 | 30 |
def unapply(name: String): Option[Family] = |
31 |
try { Some(Family.valueOf(name)) } |
|
32 |
catch { case _: IllegalArgumentException => None } |
|
69410 | 33 |
|
78610 | 34 |
def parse(name: String): Family = |
69410 | 35 |
unapply(name) getOrElse error("Bad platform family: " + quote(name)) |
73637 | 36 |
|
78610 | 37 |
val standard: Family => String = |
38 |
{ |
|
39 |
case Family.linux_arm => "arm64-linux" |
|
40 |
case Family.linux => "x86_64-linux" |
|
41 |
case Family.macos => "x86_64-darwin" |
|
42 |
case Family.windows => "x86_64-cygwin" |
|
43 |
} |
|
75083 | 44 |
|
78610 | 45 |
val native: Family => String = |
46 |
{ |
|
47 |
case Family.macos => "arm64-darwin" |
|
48 |
case Family.windows => "x86_64-windows" |
|
49 |
case platform => standard(platform) |
|
50 |
} |
|
79981
bdea4eccd8d5
support for etc/platform.props, to specify multi-platform directory structure more accurately;
wenzelm
parents:
79536
diff
changeset
|
51 |
|
bdea4eccd8d5
support for etc/platform.props, to specify multi-platform directory structure more accurately;
wenzelm
parents:
79536
diff
changeset
|
52 |
def from_platform(platform: String): Family = |
bdea4eccd8d5
support for etc/platform.props, to specify multi-platform directory structure more accurately;
wenzelm
parents:
79536
diff
changeset
|
53 |
list.find(family => platform == standard(family) || platform == native(family)) |
bdea4eccd8d5
support for etc/platform.props, to specify multi-platform directory structure more accurately;
wenzelm
parents:
79536
diff
changeset
|
54 |
.getOrElse(error("Bad platform " + quote(platform))) |
69410 | 55 |
} |
56 |
||
78610 | 57 |
enum Family { case linux_arm, linux, macos, windows } |
58 |
||
31828 | 59 |
|
69112 | 60 |
/* platform identifiers */ |
31828 | 61 |
|
69112 | 62 |
private val X86_64 = """amd64|x86_64""".r |
72344 | 63 |
private val Arm64 = """arm64|aarch64""".r |
31825 | 64 |
|
69726 | 65 |
def cpu_arch: String = |
66 |
System.getProperty("os.arch", "") match { |
|
67 |
case X86_64() => "x86_64" |
|
72344 | 68 |
case Arm64() => "arm64" |
69726 | 69 |
case _ => error("Failed to determine CPU architecture") |
70 |
} |
|
71 |
||
72 |
def os_name: String = |
|
73 |
family match { |
|
73639
e1432539df35
proper jvm_platform, notably for org.sqlite.lib.path;
wenzelm
parents:
73638
diff
changeset
|
74 |
case Family.linux_arm => "linux" |
69726 | 75 |
case Family.macos => "darwin" |
76 |
case _ => family.toString |
|
77 |
} |
|
78 |
||
79 |
lazy val jvm_platform: String = cpu_arch + "-" + os_name |
|
31828 | 80 |
|
81 |
||
80008
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
82 |
/* platform info */ |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
83 |
|
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
84 |
trait Info { |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
85 |
def platform: String |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
86 |
override def toString: String = platform |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
87 |
def path: Path = Path.explode(platform) |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
88 |
|
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
89 |
val family: Family = Family.from_platform(platform) |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
90 |
def family_name: String = family.toString |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
91 |
|
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
92 |
def is_linux_arm: Boolean = family == Family.linux_arm |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
93 |
def is_linux: Boolean = family == Family.linux |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
94 |
def is_macos: Boolean = family == Family.macos |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
95 |
def is_windows: Boolean = family == Family.windows |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
96 |
|
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
97 |
def is(spec: String): Boolean = platform == spec || family_name == spec |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
98 |
} |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
99 |
|
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
100 |
def check_spec(infos: List[Info], spec: String): String = { |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
101 |
val specs = Library.distinct(infos.map(_.family_name) ::: infos.map(_.platform)) |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
102 |
if (specs.contains(spec)) spec |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
103 |
else { |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
104 |
error("Bad platform specification " + quote(spec) + |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
105 |
"\n expected " + commas_quote(specs)) |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
106 |
} |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
107 |
} |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
108 |
|
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
109 |
|
61001 | 110 |
/* JVM version */ |
111 |
||
69112 | 112 |
private val Version = """1\.(\d+)\.0_(\d+)""".r |
71383 | 113 |
lazy val jvm_version: String = |
61001 | 114 |
System.getProperty("java.version") match { |
115 |
case Version(a, b) => a + "u" + b |
|
116 |
case a => a |
|
117 |
} |
|
118 |
||
119 |
||
41381 | 120 |
/* JVM name */ |
121 |
||
53582 | 122 |
val jvm_name: String = System.getProperty("java.vm.name", "") |
31825 | 123 |
} |