src/Pure/System/platform.scala
author wenzelm
Wed, 27 Mar 2024 17:04:37 +0100
changeset 80038 b1e2246147eb
parent 80008 914c4a81027d
child 80039 0732ee5c8ee1
permissions -rw-r--r--
clarified signature;
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
78610
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    20
  def family: Family =
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
78610
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    27
  object Family {
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    28
    val list: List[Family] = List(Family.linux, Family.linux_arm, Family.windows, Family.macos)
69410
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    29
78610
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    30
    def unapply(name: String): Option[Family] =
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    31
      try { Some(Family.valueOf(name)) }
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    32
      catch { case _: IllegalArgumentException => None }
69410
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    33
78610
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    34
    def parse(name: String): Family =
69410
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    35
      unapply(name) getOrElse error("Bad platform family: " + quote(name))
73637
f3a356c64193 support for platform family "linux_arm";
wenzelm
parents: 73602
diff changeset
    36
78610
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    37
    val standard: Family => String =
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    38
      {
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    39
        case Family.linux_arm => "arm64-linux"
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    40
        case Family.linux => "x86_64-linux"
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    41
        case Family.macos => "x86_64-darwin"
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    42
        case Family.windows => "x86_64-cygwin"
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    43
      }
75083
35a5c4b16024 setup VSCode from VSCodium distribution;
wenzelm
parents: 73906
diff changeset
    44
78610
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    45
    val native: Family => String =
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    46
      {
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    47
        case Family.macos => "arm64-darwin"
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    48
        case Family.windows => "x86_64-windows"
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    49
        case platform => standard(platform)
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    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
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    55
  }
c071fcec4323 more explicit Platform.Family;
wenzelm
parents: 69112
diff changeset
    56
78610
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    57
  enum Family { case linux_arm, linux, macos, windows }
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 75393
diff changeset
    58
31828
31584cf201cc sane platform look-and-feel;
wenzelm
parents: 31825
diff changeset
    59
69112
5b749aa452c6 misc tuning and modernization;
wenzelm
parents: 64493
diff changeset
    60
  /* platform identifiers */
31828
31584cf201cc sane platform look-and-feel;
wenzelm
parents: 31825
diff changeset
    61
69112
5b749aa452c6 misc tuning and modernization;
wenzelm
parents: 64493
diff changeset
    62
  private val X86_64 = """amd64|x86_64""".r
72344
728da67527b9 detect arm64-linux platform;
wenzelm
parents: 72250
diff changeset
    63
  private val Arm64 = """arm64|aarch64""".r
31825
d47a9dc1f064 moved platform identification to platform.scala;
wenzelm
parents:
diff changeset
    64
69726
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    65
  def cpu_arch: String =
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    66
    System.getProperty("os.arch", "") match {
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    67
      case X86_64() => "x86_64"
72344
728da67527b9 detect arm64-linux platform;
wenzelm
parents: 72250
diff changeset
    68
      case Arm64() => "arm64"
69726
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    69
      case _ => error("Failed to determine CPU architecture")
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
  def os_name: String =
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    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
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    75
      case Family.macos => "darwin"
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    76
      case _ => family.toString
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    77
    }
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    78
461f0615faa3 clarified signature;
wenzelm
parents: 69410
diff changeset
    79
  lazy val jvm_platform: String = cpu_arch + "-" + os_name
31828
31584cf201cc sane platform look-and-feel;
wenzelm
parents: 31825
diff changeset
    80
31584cf201cc sane platform look-and-feel;
wenzelm
parents: 31825
diff changeset
    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
b1e2246147eb clarified signature;
wenzelm
parents: 80008
diff changeset
    84
  object Info {
b1e2246147eb clarified signature;
wenzelm
parents: 80008
diff changeset
    85
    def check(infos: List[Info], spec: String): String = {
b1e2246147eb clarified signature;
wenzelm
parents: 80008
diff changeset
    86
      val specs = Library.distinct(infos.map(_.family_name) ::: infos.map(_.platform))
b1e2246147eb clarified signature;
wenzelm
parents: 80008
diff changeset
    87
      if (specs.contains(spec)) spec
b1e2246147eb clarified signature;
wenzelm
parents: 80008
diff changeset
    88
      else {
b1e2246147eb clarified signature;
wenzelm
parents: 80008
diff changeset
    89
        error("Bad platform specification " + quote(spec) +
b1e2246147eb clarified signature;
wenzelm
parents: 80008
diff changeset
    90
          "\n  expected " + commas_quote(specs))
b1e2246147eb clarified signature;
wenzelm
parents: 80008
diff changeset
    91
      }
b1e2246147eb clarified signature;
wenzelm
parents: 80008
diff changeset
    92
    }
b1e2246147eb clarified signature;
wenzelm
parents: 80008
diff changeset
    93
  }
b1e2246147eb clarified signature;
wenzelm
parents: 80008
diff changeset
    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
ea38a1922a0b more version information;
wenzelm
parents: 55618
diff changeset
   112
  /* JVM version */
ea38a1922a0b more version information;
wenzelm
parents: 55618
diff changeset
   113
69112
5b749aa452c6 misc tuning and modernization;
wenzelm
parents: 64493
diff changeset
   114
  private val Version = """1\.(\d+)\.0_(\d+)""".r
71383
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 69726
diff changeset
   115
  lazy val jvm_version: String =
61001
ea38a1922a0b more version information;
wenzelm
parents: 55618
diff changeset
   116
    System.getProperty("java.version") match {
ea38a1922a0b more version information;
wenzelm
parents: 55618
diff changeset
   117
      case Version(a, b) => a + "u" + b
ea38a1922a0b more version information;
wenzelm
parents: 55618
diff changeset
   118
      case a => a
ea38a1922a0b more version information;
wenzelm
parents: 55618
diff changeset
   119
    }
ea38a1922a0b more version information;
wenzelm
parents: 55618
diff changeset
   120
ea38a1922a0b more version information;
wenzelm
parents: 55618
diff changeset
   121
41381
77990a6cd558 more explicit jvm_name;
wenzelm
parents: 36786
diff changeset
   122
  /* JVM name */
77990a6cd558 more explicit jvm_name;
wenzelm
parents: 36786
diff changeset
   123
53582
8533b4cb8dd7 more robust System.getProperty with default;
wenzelm
parents: 51617
diff changeset
   124
  val jvm_name: String = System.getProperty("java.vm.name", "")
31825
d47a9dc1f064 moved platform identification to platform.scala;
wenzelm
parents:
diff changeset
   125
}