src/Pure/System/cygwin.scala
author wenzelm
Tue, 29 Nov 2011 21:29:53 +0100
changeset 45673 cd41e3903fbf
parent 45667 546d78f0d81f
child 47679 93e0dada1266
permissions -rw-r--r--
separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31499
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/System/cygwin.scala
45673
cd41e3903fbf separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
wenzelm
parents: 45667
diff changeset
     2
    Module:     PIDE
31499
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
     4
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
     5
Accessing the Cygwin installation.
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
     6
*/
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
     7
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
     8
package isabelle
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
     9
43520
cec9b95fa35d explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents: 41333
diff changeset
    10
import java.lang.System
31499
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    11
import java.lang.reflect.Method
31826
7f311da87d5a some more Cygwin checks;
wenzelm
parents: 31499
diff changeset
    12
import java.io.File
34219
d37cfca69887 Standard_System.raw_execute: optional cwd;
wenzelm
parents: 34203
diff changeset
    13
import java.net.URL
d37cfca69887 Standard_System.raw_execute: optional cwd;
wenzelm
parents: 34203
diff changeset
    14
import java.awt.Component
31499
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    15
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    16
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    17
object Cygwin
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    18
{
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    19
  /* registry access */
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    20
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    21
  // Some black magic involving private WindowsPreferences from Sun, cf.
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    22
  // http://www.docjar.com/html/api/java/util/prefs/WindowsPreferences.java.html
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    23
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    24
  private val WindowsPreferences = Class.forName("java.util.prefs.WindowsPreferences")
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    25
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    26
  private val HKEY_CURRENT_USER = 0x80000001
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    27
  private val HKEY_LOCAL_MACHINE = 0x80000002
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    28
  private val KEY_READ = 0x20019
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    29
  private val NATIVE_HANDLE = 0
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    30
  private val ERROR_CODE = 1
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    31
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    32
  private def C_string(s: String): Array[Byte] =
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    33
    (s + "\0").getBytes("US-ASCII")
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    34
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    35
  private def J_string(bs: Array[Byte]): String =
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    36
    new String(bs, 0, bs.length - 1, "US-ASCII")
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    37
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    38
  private val INT = Integer.TYPE
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    39
  private val BYTES = (new Array[Byte](0)).getClass
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    40
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    41
  private def open_key(handle: Int, subkey: Array[Byte], mask: Int): Array[Int] =
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    42
  {
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    43
    val m = WindowsPreferences.getDeclaredMethod("WindowsRegOpenKey", INT, BYTES, INT)
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    44
    m.setAccessible(true)
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    45
    m.invoke(null, handle.asInstanceOf[Object], subkey.asInstanceOf[Object],
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    46
      mask.asInstanceOf[Object]).asInstanceOf[Array[Int]]
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    47
  }
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    48
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    49
  private def close_key(handle: Int): Int =
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    50
  {
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    51
    val m = WindowsPreferences.getDeclaredMethod("WindowsRegCloseKey", INT)
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    52
    m.setAccessible(true)
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    53
    m.invoke(null, handle.asInstanceOf[Object]).asInstanceOf[Int]
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    54
  }
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    55
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    56
  private def query(handle: Int, name: Array[Byte]) =
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    57
  {
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    58
    val m = WindowsPreferences.getDeclaredMethod("WindowsRegQueryValueEx", INT, BYTES)
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    59
    m.setAccessible(true)
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    60
    m.invoke(null, handle.asInstanceOf[Object], name.asInstanceOf[Object]).
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    61
      asInstanceOf[Array[Byte]]
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    62
  }
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    63
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    64
  def query_registry(sys: Boolean, path: String, name: String): Option[String] =
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    65
  {
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    66
    val handle = if (sys) HKEY_LOCAL_MACHINE else HKEY_CURRENT_USER
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    67
    val result = open_key(handle, C_string(path), KEY_READ)
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    68
    if (result(ERROR_CODE) != 0) None
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    69
    else {
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    70
      val res = query(result(NATIVE_HANDLE), C_string(name))
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    71
      if (res == null) None
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    72
      else Some(J_string(res))
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    73
    }
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    74
  }
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    75
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    76
  def query_registry(path: String, name: String): Option[String] =
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    77
    query_registry(false, path, name) orElse
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    78
      query_registry(true, path, name)
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    79
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    80
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    81
  /* Cygwin installation */
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    82
31826
7f311da87d5a some more Cygwin checks;
wenzelm
parents: 31499
diff changeset
    83
  private val CYGWIN_SETUP1 = "Software\\Cygwin\\setup"
34043
7129fab1fe4f more robust Cygwin.config: actually check Wow6432Node, prefer explicit CYGWIN_ROOT in any case;
wenzelm
parents: 31826
diff changeset
    84
  private val CYGWIN_SETUP2 = "Software\\Wow6432Node\\Cygwin\\setup"
31499
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
    85
34220
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    86
  private def sanity_check(root: File)
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    87
  {
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    88
    if (!new File(root, "bin\\bash.exe").isFile ||
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    89
        !new File(root, "bin\\env.exe").isFile ||
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    90
        !new File(root, "bin\\tar.exe").isFile)
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    91
      error("Bad Cygwin installation: " + root.toString)
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    92
  }
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
    93
34045
bc71778a327d simplified Cygwin setup, assuming 1.7 registry layout (version 1.5 suffers from upcaseenv problem anyway);
wenzelm
parents: 34043
diff changeset
    94
  def check_root(): String =
31826
7f311da87d5a some more Cygwin checks;
wenzelm
parents: 31499
diff changeset
    95
  {
43520
cec9b95fa35d explicit import java.lang.System to prevent odd scope problems;
wenzelm
parents: 41333
diff changeset
    96
    val this_cygwin = System.getenv("THIS_CYGWIN")
34045
bc71778a327d simplified Cygwin setup, assuming 1.7 registry layout (version 1.5 suffers from upcaseenv problem anyway);
wenzelm
parents: 34043
diff changeset
    97
    val root =
36194
8e61560ded89 THIS_CYGWIN;
wenzelm
parents: 34258
diff changeset
    98
      if (this_cygwin != null && this_cygwin != "") this_cygwin
34045
bc71778a327d simplified Cygwin setup, assuming 1.7 registry layout (version 1.5 suffers from upcaseenv problem anyway);
wenzelm
parents: 34043
diff changeset
    99
      else
bc71778a327d simplified Cygwin setup, assuming 1.7 registry layout (version 1.5 suffers from upcaseenv problem anyway);
wenzelm
parents: 34043
diff changeset
   100
        query_registry(CYGWIN_SETUP1, "rootdir") orElse
bc71778a327d simplified Cygwin setup, assuming 1.7 registry layout (version 1.5 suffers from upcaseenv problem anyway);
wenzelm
parents: 34043
diff changeset
   101
        query_registry(CYGWIN_SETUP2, "rootdir") getOrElse
bc71778a327d simplified Cygwin setup, assuming 1.7 registry layout (version 1.5 suffers from upcaseenv problem anyway);
wenzelm
parents: 34043
diff changeset
   102
        error("Failed to determine Cygwin installation -- version 1.7 required")
34220
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
   103
    sanity_check(new File(root))
34045
bc71778a327d simplified Cygwin setup, assuming 1.7 registry layout (version 1.5 suffers from upcaseenv problem anyway);
wenzelm
parents: 34043
diff changeset
   104
    root
31826
7f311da87d5a some more Cygwin checks;
wenzelm
parents: 31499
diff changeset
   105
  }
34203
dd2f49d88b47 crude Cygwin.setup;
wenzelm
parents: 34045
diff changeset
   106
34219
d37cfca69887 Standard_System.raw_execute: optional cwd;
wenzelm
parents: 34203
diff changeset
   107
  def setup(parent: Component, root: File)
34203
dd2f49d88b47 crude Cygwin.setup;
wenzelm
parents: 34045
diff changeset
   108
  {
34258
e936d3c35ce0 Standard_System.raw_exec;
wenzelm
parents: 34256
diff changeset
   109
    if (!root.isDirectory && !root.mkdirs) error("Failed to create root directory: " + root)
34219
d37cfca69887 Standard_System.raw_execute: optional cwd;
wenzelm
parents: 34203
diff changeset
   110
d37cfca69887 Standard_System.raw_execute: optional cwd;
wenzelm
parents: 34203
diff changeset
   111
    val download = new File(root, "download")
d37cfca69887 Standard_System.raw_execute: optional cwd;
wenzelm
parents: 34203
diff changeset
   112
    if (!download.mkdir) error("Failed to create download directory: " + download)
d37cfca69887 Standard_System.raw_execute: optional cwd;
wenzelm
parents: 34203
diff changeset
   113
d37cfca69887 Standard_System.raw_execute: optional cwd;
wenzelm
parents: 34203
diff changeset
   114
    val setup_exe = new File(root, "setup.exe")
34220
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
   115
39703
545cc67324d8 tuned signatures and messages;
wenzelm
parents: 37175
diff changeset
   116
    try {
545cc67324d8 tuned signatures and messages;
wenzelm
parents: 37175
diff changeset
   117
      Download.file(parent, "Downloading", new URL("http://www.cygwin.com/setup.exe"), setup_exe)
545cc67324d8 tuned signatures and messages;
wenzelm
parents: 37175
diff changeset
   118
    }
43650
f00da558b78e imitate exception ERROR of Isabelle/ML;
wenzelm
parents: 43520
diff changeset
   119
    catch { case ERROR(_) => error("Failed to download Cygwin setup program") }
34219
d37cfca69887 Standard_System.raw_execute: optional cwd;
wenzelm
parents: 34203
diff changeset
   120
34258
e936d3c35ce0 Standard_System.raw_exec;
wenzelm
parents: 34256
diff changeset
   121
    val (_, rc) = Standard_System.raw_exec(root, null, true,
e936d3c35ce0 Standard_System.raw_exec;
wenzelm
parents: 34256
diff changeset
   122
        setup_exe.toString, "-R", root.toString, "-l", download.toString,
41333
2a12d91a6ab7 Cygwin: Poly/ML 5.4.0 requires libgmp3;
wenzelm
parents: 39703
diff changeset
   123
          "-P", "libgmp3,make,perl,python", "-q", "-n")
34219
d37cfca69887 Standard_System.raw_execute: optional cwd;
wenzelm
parents: 34203
diff changeset
   124
    if (rc != 0) error("Cygwin setup failed!")
34220
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
   125
f7a0088518e1 tuned error handling;
wenzelm
parents: 34219
diff changeset
   126
    sanity_check(root)
34203
dd2f49d88b47 crude Cygwin.setup;
wenzelm
parents: 34045
diff changeset
   127
  }
31499
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
   128
}
4345173ee386 Accessing the Cygwin installation.
wenzelm
parents:
diff changeset
   129