src/Pure/System/mingw.scala
author wenzelm
Wed, 09 Apr 2025 22:23:59 +0200
changeset 82466 d5ef492dd673
parent 82465 3cc075052033
child 82467 b0740dce1f1d
permissions -rw-r--r--
tuned: prefer explicit Bash.exports;

/*  Title:      Pure/System/mingw.scala
    Author:     Makarius

Support for MSYS2/MinGW64 on Windows.
*/

package isabelle


object MinGW {
  def env_prefix(pre_path: String = "", post_path: String = ""): String = {
    val path =
      if_proper(pre_path, pre_path + ":") + "/usr/bin:/bin:/mingw64/bin" +
      if_proper(post_path, ":" + post_path)
    Bash.exports("PATH=" + path, "CONFIG_SITE=/mingw64/etc/config.site")
  }

  val none: MinGW = new MinGW(None)
  def apply(path: Path) = new MinGW(Some(path))
}

class MinGW private(val root: Option[Path]) {
  override def toString: String =
    root match {
      case None => "MinGW.none"
      case Some(msys_root) => "MinGW(" + msys_root.toString + ")"
    }

  def standard_path(path: Path): String =
    root match {
      case Some(msys_root) if Platform.is_windows =>
        val command_line =
          java.util.List.of(
            File.platform_path(msys_root) + "\\usr\\bin\\cygpath", "-u", File.platform_path(path))
        val res = isabelle.setup.Environment.exec_process(command_line, null, null, false)
        if (res.ok) Library.trim_line(res.out)
        else error("Error: " + quote(Library.trim_line(res.err)))
      case _ => File.standard_path(path)
    }

  def bash_script(script: String, env_prefix: String = MinGW.env_prefix()): String =
    root match {
      case None => script
      case Some(msys_root) =>
        File.bash_path(msys_root + Path.explode("usr/bin/bash")) +
          " -c " + Bash.string(env_prefix + script)
    }

  def get_root: Path =
    if (!Platform.is_windows) error("Windows platform required")
    else if (root.isEmpty) error("Windows platform requires msys/mingw root specification")
    else root.get

  def check: Unit = {
    if (Platform.is_windows) {
      get_root
      try { require(Isabelle_System.bash(bash_script("uname -s")).check.out.startsWith("MSYS")) }
      catch { case ERROR(msg) => cat_error("Bad msys/mingw installation " + get_root, msg) }
    }
  }
}