| author | wenzelm | 
| Fri, 04 Apr 2025 16:35:05 +0200 | |
| changeset 82432 | 314d6b215f90 | 
| parent 80039 | 0732ee5c8ee1 | 
| child 82611 | cf64152e9f51 | 
| 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: 
73193diff
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: 
79536diff
changeset | 51 | |
| 
bdea4eccd8d5
support for etc/platform.props, to specify multi-platform directory structure more accurately;
 wenzelm parents: 
79536diff
changeset | 52 | def from_platform(platform: String): Family = | 
| 
bdea4eccd8d5
support for etc/platform.props, to specify multi-platform directory structure more accurately;
 wenzelm parents: 
79536diff
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: 
79536diff
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: 
73638diff
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: 
79981diff
changeset | 82 | /* platform info */ | 
| 
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
 wenzelm parents: 
79981diff
changeset | 83 | |
| 80038 | 84 |   object Info {
 | 
| 80039 | 85 | val ALL = "all" | 
| 86 | ||
| 80038 | 87 |     def check(infos: List[Info], spec: String): String = {
 | 
| 80039 | 88 | val specs = Library.distinct(ALL :: infos.map(_.family_name) ::: infos.map(_.platform)) | 
| 80038 | 89 | if (specs.contains(spec)) spec | 
| 90 |       else {
 | |
| 91 |         error("Bad platform specification " + quote(spec) +
 | |
| 92 | "\n expected " + commas_quote(specs)) | |
| 93 | } | |
| 94 | } | |
| 95 | } | |
| 96 | ||
| 80008 
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
 wenzelm parents: 
79981diff
changeset | 97 |   trait Info {
 | 
| 
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
 wenzelm parents: 
79981diff
changeset | 98 | def platform: String | 
| 
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
 wenzelm parents: 
79981diff
changeset | 99 | override def toString: String = platform | 
| 
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
 wenzelm parents: 
79981diff
changeset | 100 | def path: Path = Path.explode(platform) | 
| 
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
 wenzelm parents: 
79981diff
changeset | 101 | |
| 
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
 wenzelm parents: 
79981diff
changeset | 102 | val family: Family = Family.from_platform(platform) | 
| 
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
 wenzelm parents: 
79981diff
changeset | 103 | def family_name: String = family.toString | 
| 
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
 wenzelm parents: 
79981diff
changeset | 104 | |
| 
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
 wenzelm parents: 
79981diff
changeset | 105 | def is_linux_arm: Boolean = family == Family.linux_arm | 
| 
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
 wenzelm parents: 
79981diff
changeset | 106 | def is_linux: Boolean = family == Family.linux | 
| 
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
 wenzelm parents: 
79981diff
changeset | 107 | def is_macos: Boolean = family == Family.macos | 
| 
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
 wenzelm parents: 
79981diff
changeset | 108 | def is_windows: Boolean = family == Family.windows | 
| 
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
 wenzelm parents: 
79981diff
changeset | 109 | |
| 80039 | 110 | def is(spec: String): Boolean = | 
| 111 | Info.ALL == spec || platform == spec || family_name == spec | |
| 80008 
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
 wenzelm parents: 
79981diff
changeset | 112 | } | 
| 
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
 wenzelm parents: 
79981diff
changeset | 113 | |
| 
914c4a81027d
clarified signature: explicit type Platform.Info with derived operations;
 wenzelm parents: 
79981diff
changeset | 114 | |
| 61001 | 115 | /* JVM version */ | 
| 116 | ||
| 69112 | 117 | private val Version = """1\.(\d+)\.0_(\d+)""".r | 
| 71383 | 118 | lazy val jvm_version: String = | 
| 61001 | 119 |     System.getProperty("java.version") match {
 | 
| 120 | case Version(a, b) => a + "u" + b | |
| 121 | case a => a | |
| 122 | } | |
| 123 | ||
| 124 | ||
| 41381 | 125 | /* JVM name */ | 
| 126 | ||
| 53582 | 127 |   val jvm_name: String = System.getProperty("java.vm.name", "")
 | 
| 31825 | 128 | } |