src/Pure/System/platform.scala
author wenzelm
Fri, 01 Apr 2022 17:06:10 +0200
changeset 75393 87ebf5a50283
parent 75212 7870cdaa3f1f
child 78610 fd1fec53665b
permissions -rw-r--r--
clarified formatting, for the sake of scala3;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31825
d47a9dc1f064 moved platform identification to platform.scala;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/System/platform.scala
d47a9dc1f064 moved platform identification to platform.scala;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
d47a9dc1f064 moved platform identification to platform.scala;
wenzelm
parents:
diff changeset
     3
69112
5b749aa452c6 misc tuning and modernization;
wenzelm
parents: 64493
diff changeset
     4
System platform identification.
31825
d47a9dc1f064 moved platform identification to platform.scala;
wenzelm
parents:
diff changeset
     5
*/
d47a9dc1f064 moved platform identification to platform.scala;
wenzelm
parents:
diff changeset
     6
d47a9dc1f064 moved platform identification to platform.scala;
wenzelm
parents:
diff changeset
     7
package isabelle
d47a9dc1f064 moved platform identification to platform.scala;
wenzelm
parents:
diff changeset
     8
31828
31584cf201cc sane platform look-and-feel;
wenzelm
parents: 31825
diff changeset
     9
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75212
diff changeset
    10
object Platform {
69112
5b749aa452c6 misc tuning and modernization;
wenzelm
parents: 64493
diff changeset
    11
  /* platform family */
31828
31584cf201cc sane platform look-and-feel;
wenzelm
parents: 31825
diff changeset
    12
73906
f627ffab387b support for Isabelle setup in pure Java;
wenzelm
parents: 73904
diff changeset
    13
  val is_windows: Boolean = isabelle.setup.Environment.is_windows()
71383
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 69726
diff changeset
    14
  val is_linux: Boolean = System.getProperty("os.name", "") == "Linux"
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 69726
diff changeset
    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
d47a9dc1f064 moved platform identification to platform.scala;
wenzelm
parents:
diff changeset
    17
73637
f3a356c64193 support for platform family "linux_arm";
wenzelm
parents: 73602
diff changeset
    18
  def is_arm: Boolean = cpu_arch.startsWith("arm")
f3a356c64193 support for platform family "linux_arm";
wenzelm
parents: 73602
diff changeset
    19
69410
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    20
  def family: Family.Value =
73637
f3a356c64193 support for platform family "linux_arm";
wenzelm
parents: 73602
diff changeset
    21
    if (is_linux && is_arm) Family.linux_arm
f3a356c64193 support for platform family "linux_arm";
wenzelm
parents: 73602
diff changeset
    22
    else if (is_linux) Family.linux
69410
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    23
    else if (is_macos) Family.macos
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    24
    else if (is_windows) Family.windows
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    25
    else error("Failed to determine current platform family")
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    26
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75212
diff changeset
    27
  object Family extends Enumeration {
73637
f3a356c64193 support for platform family "linux_arm";
wenzelm
parents: 73602
diff changeset
    28
    val linux_arm, linux, macos, windows = Value
73642
ac6f8fff036b clarified default_platform_families (again);
wenzelm
parents: 73639
diff changeset
    29
    val list0: List[Value] = List(linux, windows, macos)
75212
7870cdaa3f1f clarified order;
wenzelm
parents: 75083
diff changeset
    30
    val list: List[Value] = List(linux, linux_arm, windows, macos)
69410
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    31
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    32
    def unapply(name: String): Option[Value] =
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    33
      try { Some(withName(name)) }
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    34
      catch { case _: NoSuchElementException => None }
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    35
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    36
    def parse(name: String): Value =
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    37
      unapply(name) getOrElse error("Bad platform family: " + quote(name))
73637
f3a356c64193 support for platform family "linux_arm";
wenzelm
parents: 73602
diff changeset
    38
f3a356c64193 support for platform family "linux_arm";
wenzelm
parents: 73602
diff changeset
    39
    def standard(platform: Value): String =
f3a356c64193 support for platform family "linux_arm";
wenzelm
parents: 73602
diff changeset
    40
      if (platform == linux_arm) "arm64-linux"
f3a356c64193 support for platform family "linux_arm";
wenzelm
parents: 73602
diff changeset
    41
      else if (platform == linux) "x86_64-linux"
f3a356c64193 support for platform family "linux_arm";
wenzelm
parents: 73602
diff changeset
    42
      else if (platform == macos) "x86_64-darwin"
f3a356c64193 support for platform family "linux_arm";
wenzelm
parents: 73602
diff changeset
    43
      else if (platform == windows) "x86_64-cygwin"
73638
a6a9162f3ec1 tuned message;
wenzelm
parents: 73637
diff changeset
    44
      else error("Bad platform family: " + quote(platform.toString))
75083
35a5c4b16024 setup VSCode from VSCodium distribution;
wenzelm
parents: 73906
diff changeset
    45
35a5c4b16024 setup VSCode from VSCodium distribution;
wenzelm
parents: 73906
diff changeset
    46
    def native(platform: Value): String =
35a5c4b16024 setup VSCode from VSCodium distribution;
wenzelm
parents: 73906
diff changeset
    47
      if (platform == macos) "arm64-darwin"
35a5c4b16024 setup VSCode from VSCodium distribution;
wenzelm
parents: 73906
diff changeset
    48
      else if (platform == windows) "x86_64-windows"
35a5c4b16024 setup VSCode from VSCodium distribution;
wenzelm
parents: 73906
diff changeset
    49
      else standard(platform)
69410
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    50
  }
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    51
31828
31584cf201cc sane platform look-and-feel;
wenzelm
parents: 31825
diff changeset
    52
69112
5b749aa452c6 misc tuning and modernization;
wenzelm
parents: 64493
diff changeset
    53
  /* platform identifiers */
31828
31584cf201cc sane platform look-and-feel;
wenzelm
parents: 31825
diff changeset
    54
69112
5b749aa452c6 misc tuning and modernization;
wenzelm
parents: 64493
diff changeset
    55
  private val X86_64 = """amd64|x86_64""".r
72344
728da67527b9 detect arm64-linux platform;
wenzelm
parents: 72250
diff changeset
    56
  private val Arm64 = """arm64|aarch64""".r
31825
d47a9dc1f064 moved platform identification to platform.scala;
wenzelm
parents:
diff changeset
    57
69726
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    58
  def cpu_arch: String =
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    59
    System.getProperty("os.arch", "") match {
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    60
      case X86_64() => "x86_64"
72344
728da67527b9 detect arm64-linux platform;
wenzelm
parents: 72250
diff changeset
    61
      case Arm64() => "arm64"
69726
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    62
      case _ => error("Failed to determine CPU architecture")
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    63
    }
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    64
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    65
  def os_name: String =
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    66
    family match {
73639
e1432539df35 proper jvm_platform, notably for org.sqlite.lib.path;
wenzelm
parents: 73638
diff changeset
    67
      case Family.linux_arm => "linux"
69726
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    68
      case Family.macos => "darwin"
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    69
      case _ => family.toString
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    70
    }
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    71
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    72
  lazy val jvm_platform: String = cpu_arch + "-" + os_name
31828
31584cf201cc sane platform look-and-feel;
wenzelm
parents: 31825
diff changeset
    73
31584cf201cc sane platform look-and-feel;
wenzelm
parents: 31825
diff changeset
    74
61001
ea38a1922a0b more version information;
wenzelm
parents: 55618
diff changeset
    75
  /* JVM version */
ea38a1922a0b more version information;
wenzelm
parents: 55618
diff changeset
    76
69112
5b749aa452c6 misc tuning and modernization;
wenzelm
parents: 64493
diff changeset
    77
  private val Version = """1\.(\d+)\.0_(\d+)""".r
71383
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 69726
diff changeset
    78
  lazy val jvm_version: String =
61001
ea38a1922a0b more version information;
wenzelm
parents: 55618
diff changeset
    79
    System.getProperty("java.version") match {
ea38a1922a0b more version information;
wenzelm
parents: 55618
diff changeset
    80
      case Version(a, b) => a + "u" + b
ea38a1922a0b more version information;
wenzelm
parents: 55618
diff changeset
    81
      case a => a
ea38a1922a0b more version information;
wenzelm
parents: 55618
diff changeset
    82
    }
ea38a1922a0b more version information;
wenzelm
parents: 55618
diff changeset
    83
ea38a1922a0b more version information;
wenzelm
parents: 55618
diff changeset
    84
41381
77990a6cd558 more explicit jvm_name;
wenzelm
parents: 36786
diff changeset
    85
  /* JVM name */
77990a6cd558 more explicit jvm_name;
wenzelm
parents: 36786
diff changeset
    86
53582
8533b4cb8dd7 more robust System.getProperty with default;
wenzelm
parents: 51617
diff changeset
    87
  val jvm_name: String = System.getProperty("java.vm.name", "")
31825
d47a9dc1f064 moved platform identification to platform.scala;
wenzelm
parents:
diff changeset
    88
}