src/Pure/System/mingw.scala
author wenzelm
Wed, 09 Apr 2025 22:29:11 +0200
changeset 82467 b0740dce1f1d
parent 82465 3cc075052033
child 82468 40a609d67b33
permissions -rw-r--r--
clarified signature: fewer warnings in IntelliJ IDEA;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72421
9a8bc089890d support for MSYS2/MinGW64 on Windows;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/System/mingw.scala
9a8bc089890d support for MSYS2/MinGW64 on Windows;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
9a8bc089890d support for MSYS2/MinGW64 on Windows;
wenzelm
parents:
diff changeset
     3
9a8bc089890d support for MSYS2/MinGW64 on Windows;
wenzelm
parents:
diff changeset
     4
Support for MSYS2/MinGW64 on Windows.
9a8bc089890d support for MSYS2/MinGW64 on Windows;
wenzelm
parents:
diff changeset
     5
*/
9a8bc089890d support for MSYS2/MinGW64 on Windows;
wenzelm
parents:
diff changeset
     6
9a8bc089890d support for MSYS2/MinGW64 on Windows;
wenzelm
parents:
diff changeset
     7
package isabelle
9a8bc089890d support for MSYS2/MinGW64 on Windows;
wenzelm
parents:
diff changeset
     8
9a8bc089890d support for MSYS2/MinGW64 on Windows;
wenzelm
parents:
diff changeset
     9
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    10
object MinGW {
82465
3cc075052033 tuned signature;
wenzelm
parents: 82463
diff changeset
    11
  def env_prefix(pre_path: String = "", post_path: String = ""): String = {
3cc075052033 tuned signature;
wenzelm
parents: 82463
diff changeset
    12
    val path =
3cc075052033 tuned signature;
wenzelm
parents: 82463
diff changeset
    13
      if_proper(pre_path, pre_path + ":") + "/usr/bin:/bin:/mingw64/bin" +
3cc075052033 tuned signature;
wenzelm
parents: 82463
diff changeset
    14
      if_proper(post_path, ":" + post_path)
3cc075052033 tuned signature;
wenzelm
parents: 82463
diff changeset
    15
    Bash.exports("PATH=" + path, "CONFIG_SITE=/mingw64/etc/config.site")
3cc075052033 tuned signature;
wenzelm
parents: 82463
diff changeset
    16
  }
72421
9a8bc089890d support for MSYS2/MinGW64 on Windows;
wenzelm
parents:
diff changeset
    17
72424
10c07d224035 tuned signature;
wenzelm
parents: 72421
diff changeset
    18
  val none: MinGW = new MinGW(None)
72431
b8b97c49e339 tuned signature;
wenzelm
parents: 72428
diff changeset
    19
  def apply(path: Path) = new MinGW(Some(path))
72424
10c07d224035 tuned signature;
wenzelm
parents: 72421
diff changeset
    20
}
10c07d224035 tuned signature;
wenzelm
parents: 72421
diff changeset
    21
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    22
class MinGW private(val root: Option[Path]) {
72424
10c07d224035 tuned signature;
wenzelm
parents: 72421
diff changeset
    23
  override def toString: String =
10c07d224035 tuned signature;
wenzelm
parents: 72421
diff changeset
    24
    root match {
10c07d224035 tuned signature;
wenzelm
parents: 72421
diff changeset
    25
      case None => "MinGW.none"
72431
b8b97c49e339 tuned signature;
wenzelm
parents: 72428
diff changeset
    26
      case Some(msys_root) => "MinGW(" + msys_root.toString + ")"
72424
10c07d224035 tuned signature;
wenzelm
parents: 72421
diff changeset
    27
    }
72421
9a8bc089890d support for MSYS2/MinGW64 on Windows;
wenzelm
parents:
diff changeset
    28
82463
3125fd1ee69c added option -G to build GMP library from sources;
wenzelm
parents: 75393
diff changeset
    29
  def standard_path(path: Path): String =
3125fd1ee69c added option -G to build GMP library from sources;
wenzelm
parents: 75393
diff changeset
    30
    root match {
3125fd1ee69c added option -G to build GMP library from sources;
wenzelm
parents: 75393
diff changeset
    31
      case Some(msys_root) if Platform.is_windows =>
3125fd1ee69c added option -G to build GMP library from sources;
wenzelm
parents: 75393
diff changeset
    32
        val command_line =
3125fd1ee69c added option -G to build GMP library from sources;
wenzelm
parents: 75393
diff changeset
    33
          java.util.List.of(
3125fd1ee69c added option -G to build GMP library from sources;
wenzelm
parents: 75393
diff changeset
    34
            File.platform_path(msys_root) + "\\usr\\bin\\cygpath", "-u", File.platform_path(path))
3125fd1ee69c added option -G to build GMP library from sources;
wenzelm
parents: 75393
diff changeset
    35
        val res = isabelle.setup.Environment.exec_process(command_line, null, null, false)
3125fd1ee69c added option -G to build GMP library from sources;
wenzelm
parents: 75393
diff changeset
    36
        if (res.ok) Library.trim_line(res.out)
3125fd1ee69c added option -G to build GMP library from sources;
wenzelm
parents: 75393
diff changeset
    37
        else error("Error: " + quote(Library.trim_line(res.err)))
3125fd1ee69c added option -G to build GMP library from sources;
wenzelm
parents: 75393
diff changeset
    38
      case _ => File.standard_path(path)
3125fd1ee69c added option -G to build GMP library from sources;
wenzelm
parents: 75393
diff changeset
    39
    }
3125fd1ee69c added option -G to build GMP library from sources;
wenzelm
parents: 75393
diff changeset
    40
82465
3cc075052033 tuned signature;
wenzelm
parents: 82463
diff changeset
    41
  def bash_script(script: String, env_prefix: String = MinGW.env_prefix()): String =
72424
10c07d224035 tuned signature;
wenzelm
parents: 72421
diff changeset
    42
    root match {
72428
b7351ffe0dbc clarified signature: allow complex bash script;
wenzelm
parents: 72427
diff changeset
    43
      case None => script
72424
10c07d224035 tuned signature;
wenzelm
parents: 72421
diff changeset
    44
      case Some(msys_root) =>
10c07d224035 tuned signature;
wenzelm
parents: 72421
diff changeset
    45
        File.bash_path(msys_root + Path.explode("usr/bin/bash")) +
82465
3cc075052033 tuned signature;
wenzelm
parents: 82463
diff changeset
    46
          " -c " + Bash.string(env_prefix + script)
72424
10c07d224035 tuned signature;
wenzelm
parents: 72421
diff changeset
    47
    }
72421
9a8bc089890d support for MSYS2/MinGW64 on Windows;
wenzelm
parents:
diff changeset
    48
72424
10c07d224035 tuned signature;
wenzelm
parents: 72421
diff changeset
    49
  def get_root: Path =
10c07d224035 tuned signature;
wenzelm
parents: 72421
diff changeset
    50
    if (!Platform.is_windows) error("Windows platform required")
72425
d0937d55eb90 clarified errors;
wenzelm
parents: 72424
diff changeset
    51
    else if (root.isEmpty) error("Windows platform requires msys/mingw root specification")
72424
10c07d224035 tuned signature;
wenzelm
parents: 72421
diff changeset
    52
    else root.get
72421
9a8bc089890d support for MSYS2/MinGW64 on Windows;
wenzelm
parents:
diff changeset
    53
82467
b0740dce1f1d clarified signature: fewer warnings in IntelliJ IDEA;
wenzelm
parents: 82465
diff changeset
    54
  def check(): Unit = {
72424
10c07d224035 tuned signature;
wenzelm
parents: 72421
diff changeset
    55
    if (Platform.is_windows) {
10c07d224035 tuned signature;
wenzelm
parents: 72421
diff changeset
    56
      get_root
72428
b7351ffe0dbc clarified signature: allow complex bash script;
wenzelm
parents: 72427
diff changeset
    57
      try { require(Isabelle_System.bash(bash_script("uname -s")).check.out.startsWith("MSYS")) }
72432
86f8fcdcff4a clarified message;
wenzelm
parents: 72431
diff changeset
    58
      catch { case ERROR(msg) => cat_error("Bad msys/mingw installation " + get_root, msg) }
72421
9a8bc089890d support for MSYS2/MinGW64 on Windows;
wenzelm
parents:
diff changeset
    59
    }
9a8bc089890d support for MSYS2/MinGW64 on Windows;
wenzelm
parents:
diff changeset
    60
  }
9a8bc089890d support for MSYS2/MinGW64 on Windows;
wenzelm
parents:
diff changeset
    61
}