author | wenzelm |
Wed, 27 Mar 2024 17:04:37 +0100 | |
changeset 80038 | b1e2246147eb |
parent 80008 | 914c4a81027d |
child 80039 | 0732ee5c8ee1 |
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 |
|
80038 | 84 |
object Info { |
85 |
def check(infos: List[Info], spec: String): String = { |
|
86 |
val specs = Library.distinct(infos.map(_.family_name) ::: infos.map(_.platform)) |
|
87 |
if (specs.contains(spec)) spec |
|
88 |
else { |
|
89 |
error("Bad platform specification " + quote(spec) + |
|
90 |
"\n expected " + commas_quote(specs)) |
|
91 |
} |
|
92 |
} |
|
93 |
} |
|
94 |
||
80008
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
95 |
trait Info { |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
96 |
def platform: String |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
97 |
override def toString: String = platform |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
98 |
def path: Path = Path.explode(platform) |
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 |
val family: Family = Family.from_platform(platform) |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
101 |
def family_name: String = family.toString |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
102 |
|
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
103 |
def is_linux_arm: Boolean = family == Family.linux_arm |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
104 |
def is_linux: Boolean = family == Family.linux |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
105 |
def is_macos: Boolean = family == Family.macos |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
106 |
def is_windows: Boolean = family == Family.windows |
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 |
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
|
109 |
} |
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
110 |
|
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
wenzelm
parents:
79981
diff
changeset
|
111 |
|
61001 | 112 |
/* JVM version */ |
113 |
||
69112 | 114 |
private val Version = """1\.(\d+)\.0_(\d+)""".r |
71383 | 115 |
lazy val jvm_version: String = |
61001 | 116 |
System.getProperty("java.version") match { |
117 |
case Version(a, b) => a + "u" + b |
|
118 |
case a => a |
|
119 |
} |
|
120 |
||
121 |
||
41381 | 122 |
/* JVM name */ |
123 |
||
53582 | 124 |
val jvm_name: String = System.getProperty("java.vm.name", "") |
31825 | 125 |
} |