ISABELLE_JDK_HOME settings variable points to JDK with javac and jar (not just JRE);
update for prospective jdk1.7.x component;
1 /* Title: Pure/System/isabelle_system.scala
4 Fundamental Isabelle system environment: quasi-static module with
5 optional init operation.
10 import java.lang.System
11 import java.util.regex.Pattern
12 import java.util.Locale
13 import java.io.{InputStream, OutputStream, File, BufferedReader, InputStreamReader,
14 BufferedWriter, OutputStreamWriter, IOException}
15 import java.awt.{GraphicsEnvironment, Font}
16 import java.awt.font.TextAttribute
18 import scala.io.Source
19 import scala.util.matching.Regex
20 import scala.collection.mutable
23 object Isabelle_System
25 /** implicit state **/
27 private case class State(standard_system: Standard_System, settings: Map[String, String])
29 @volatile private var _state: Option[State] = None
31 private def check_state(): State =
33 if (_state.isEmpty) init() // unsynchronized check
37 def standard_system: Standard_System = check_state().standard_system
38 def settings: Map[String, String] = check_state().settings
41 isabelle_home precedence:
42 (1) this_isabelle_home as explicit argument
43 (2) ISABELLE_HOME process environment variable (e.g. inherited from running isabelle tool)
44 (3) isabelle.home system property (e.g. via JVM application boot process)
46 def init(this_isabelle_home: String = null) = synchronized {
48 import scala.collection.JavaConversions._
50 val standard_system = new Standard_System
53 val env = Map(System.getenv.toList: _*) +
54 ("ISABELLE_JDK_HOME" -> standard_system.this_jdk_home())
57 if (this_isabelle_home != null) this_isabelle_home
59 env.get("ISABELLE_HOME") match {
60 case None | Some("") =>
61 val path = System.getProperty("isabelle.home")
62 if (path == null || path == "") error("Unknown Isabelle home directory")
64 case Some(path) => path
67 Standard_System.with_tmp_file("settings") { dump =>
69 if (Platform.is_windows) List(standard_system.platform_root + "\\bin\\bash", "-l")
72 shell_prefix ::: List(isabelle_home + "/bin/isabelle", "getenv", "-d", dump.toString)
73 val (output, rc) = Standard_System.raw_exec(null, env, true, cmdline: _*)
74 if (rc != 0) error(output)
77 for (entry <- Source.fromFile(dump).mkString split "\0" if entry != "") yield {
78 val i = entry.indexOf('=')
79 if (i <= 0) (entry -> "")
80 else (entry.substring(0, i) -> entry.substring(i + 1))
83 ("HOME" -> System.getenv("HOME")) +
84 ("PATH" -> System.getenv("PATH"))
87 _state = Some(State(standard_system, settings))
94 def getenv(name: String): String =
95 settings.getOrElse(if (name == "HOME") "HOME_JVM" else name, "")
97 def getenv_strict(name: String): String =
99 val value = getenv(name)
100 if (value != "") value else error("Undefined environment variable: " + name)
104 /** file-system operations **/
106 /* path specifications */
108 def standard_path(path: Path): String = path.expand.implode
110 def platform_path(path: Path): String = standard_system.jvm_path(standard_path(path))
111 def platform_file(path: Path): File = new File(platform_path(path))
113 def platform_file_url(raw_path: Path): String =
115 val path = raw_path.expand
116 require(path.is_absolute)
117 val s = platform_path(path).replaceAll(" ", "%20")
118 if (!Platform.is_windows) "file://" + s
119 else if (s.startsWith("\\\\")) "file:" + s.replace('\\', '/')
120 else "file:///" + s.replace('\\', '/')
123 def posix_path(jvm_path: String): String = standard_system.posix_path(jvm_path)
128 def try_read(paths: Seq[Path]): String =
130 val buf = new StringBuilder
133 file = platform_file(path) if file.isFile
134 c <- (Source.fromFile(file) ++ Iterator.single('\n'))
142 private def try_file(file: File) = if (file.isFile) Some(file) else None
144 def source_file(path: Path): Option[File] =
146 if (path.is_absolute || path.is_current)
147 try_file(platform_file(path))
149 val pure_file = platform_file(Path.explode("~~/src/Pure") + path)
150 if (pure_file.isFile) Some(pure_file)
151 else if (getenv("ML_SOURCES") != "")
152 try_file(platform_file(Path.explode("$ML_SOURCES") + path))
159 /** external processes **/
163 def execute(redirect: Boolean, args: String*): Process =
166 if (Platform.is_windows) List(standard_system.platform_root + "\\bin\\env.exe") ++ args
168 Standard_System.raw_execute(null, settings, redirect, cmdline: _*)
172 /* managed process */
174 class Managed_Process(redirect: Boolean, args: String*)
177 List(standard_path(Path.explode("~~/lib/scripts/process")), "group", "-", "no_script")
178 private val proc = execute(redirect, (params ++ args):_*)
183 val stdin: BufferedWriter =
184 new BufferedWriter(new OutputStreamWriter(proc.getOutputStream, Standard_System.charset))
186 val stdout: BufferedReader =
187 new BufferedReader(new InputStreamReader(proc.getInputStream, Standard_System.charset))
189 val stderr: BufferedReader =
190 new BufferedReader(new InputStreamReader(proc.getErrorStream, Standard_System.charset))
195 private val pid = stdout.readLine
197 private def kill(signal: String): Boolean =
199 execute(true, "kill", "-" + signal, "-" + pid).waitFor
200 execute(true, "kill", "-0", "-" + pid).waitFor == 0
203 private def multi_kill(signal: String): Boolean =
207 while (running && count > 0) {
217 def interrupt() { multi_kill("INT") }
218 def terminate() { multi_kill("INT") && multi_kill("TERM") && kill("KILL"); proc.destroy }
223 private val shutdown_hook = new Thread { override def run = terminate() }
225 try { Runtime.getRuntime.addShutdownHook(shutdown_hook) }
226 catch { case _: IllegalStateException => }
228 private def cleanup() =
229 try { Runtime.getRuntime.removeShutdownHook(shutdown_hook) }
230 catch { case _: IllegalStateException => }
235 def join: Int = { val rc = proc.waitFor; cleanup(); rc }
241 def bash(script: String): (String, String, Int) =
243 Standard_System.with_tmp_file("isabelle_script") { script_file =>
244 Standard_System.write_file(script_file, script)
245 val proc = new Managed_Process(false, "bash", posix_path(script_file.getPath))
248 val stdout = Simple_Thread.future("bash_stdout") { Standard_System.slurp(proc.stdout) }
249 val stderr = Simple_Thread.future("bash_stderr") { Standard_System.slurp(proc.stderr) }
253 catch { case e: InterruptedException => Thread.interrupted; proc.terminate; 130 }
254 (stdout.join, stderr.join, rc)
261 def isabelle_tool(name: String, args: String*): (String, Int) =
263 Path.split(getenv_strict("ISABELLE_TOOLS")).find { dir =>
264 val file = platform_file(dir + Path.basic(name))
266 file.isFile && file.canRead && file.canExecute &&
267 !name.endsWith("~") && !name.endsWith(".orig")
269 catch { case _: SecurityException => false }
272 val file = standard_path(dir + Path.basic(name))
273 Standard_System.process_output(execute(true, (List(file) ++ args): _*))
274 case None => ("Unknown Isabelle tool: " + name, 2)
280 /** Isabelle resources **/
284 def components(): List[Path] =
285 Path.split(getenv_strict("ISABELLE_COMPONENTS"))
290 def find_logics(): List[String] =
292 val ml_ident = getenv_strict("ML_IDENTIFIER")
293 val logics = new mutable.ListBuffer[String]
294 for (dir <- Path.split(getenv_strict("ISABELLE_PATH"))) {
295 val files = platform_file(dir + Path.explode(ml_ident)).listFiles()
297 for (file <- files if file.isFile) logics += file.getName
306 def get_font(family: String = "IsabelleText", size: Int = 1, bold: Boolean = false): Font =
307 new Font(family, if (bold) Font.BOLD else Font.PLAIN, size)
311 val ge = GraphicsEnvironment.getLocalGraphicsEnvironment()
312 for (font <- Path.split(getenv_strict("ISABELLE_FONTS")))
313 ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, platform_file(font)))