1 /* Title: Pure/System/isabelle_system.scala |
1 /* Title: Pure/System/isabelle_system.scala |
2 Author: Makarius |
2 Author: Makarius |
3 |
3 |
4 Fundamental Isabelle system environment: quasi-static module with |
4 Miscellaneous Isabelle system operations. |
5 optional init operation. |
|
6 */ |
5 */ |
7 |
6 |
8 package isabelle |
7 package isabelle |
9 |
8 |
10 |
9 |
11 import java.io.{File => JFile, IOException} |
10 import java.io.{File => JFile, IOException} |
12 import java.nio.file.{Path => JPath, Files, SimpleFileVisitor, FileVisitResult, |
11 import java.nio.file.{Path => JPath, Files, SimpleFileVisitor, FileVisitResult, |
13 StandardCopyOption, FileSystemException} |
12 StandardCopyOption, FileSystemException} |
14 import java.nio.file.attribute.BasicFileAttributes |
13 import java.nio.file.attribute.BasicFileAttributes |
15 |
14 |
16 import scala.jdk.CollectionConverters._ |
|
17 |
|
18 |
15 |
19 object Isabelle_System |
16 object Isabelle_System |
20 { |
17 { |
21 /** bootstrap information **/ |
18 /* services */ |
22 |
19 |
23 def jdk_home(): String = |
20 type Service = Isabelle_Env.Service |
24 { |
21 |
25 val java_home = System.getProperty("java.home", "") |
22 def services(): List[Class[Service]] = Isabelle_Env.services() |
26 val home = new JFile(java_home) |
|
27 val parent = home.getParent |
|
28 if (home.getName == "jre" && parent != null && |
|
29 (new JFile(new JFile(parent, "bin"), "javac")).exists) parent |
|
30 else java_home |
|
31 } |
|
32 |
|
33 def bootstrap_directory( |
|
34 preference: String, envar: String, property: String, description: String): String = |
|
35 { |
|
36 val value = |
|
37 proper_string(preference) orElse // explicit argument |
|
38 proper_string(System.getenv(envar)) orElse // e.g. inherited from running isabelle tool |
|
39 proper_string(System.getProperty(property)) getOrElse // e.g. via JVM application boot process |
|
40 error("Unknown " + description + " directory") |
|
41 |
|
42 if ((new JFile(value)).isDirectory) value |
|
43 else error("Bad " + description + " directory " + quote(value)) |
|
44 } |
|
45 |
|
46 |
|
47 |
|
48 /** implicit settings environment **/ |
|
49 |
|
50 abstract class Service |
|
51 |
|
52 @volatile private var _settings: Option[Map[String, String]] = None |
|
53 @volatile private var _services: Option[List[Class[Service]]] = None |
|
54 |
|
55 def settings(): Map[String, String] = |
|
56 { |
|
57 if (_settings.isEmpty) init() // unsynchronized check |
|
58 _settings.get |
|
59 } |
|
60 |
|
61 def services(): List[Class[Service]] = |
|
62 { |
|
63 if (_services.isEmpty) init() // unsynchronized check |
|
64 _services.get |
|
65 } |
|
66 |
23 |
67 def make_services[C](c: Class[C]): List[C] = |
24 def make_services[C](c: Class[C]): List[C] = |
68 for { c1 <- services() if Library.is_subclass(c1, c) } |
25 for { c1 <- services() if Library.is_subclass(c1, c) } |
69 yield c1.getDeclaredConstructor().newInstance().asInstanceOf[C] |
26 yield c1.getDeclaredConstructor().newInstance().asInstanceOf[C] |
70 |
27 |
71 def init(isabelle_root: String = "", cygwin_root: String = ""): Unit = synchronized |
28 |
72 { |
29 /* settings */ |
73 if (_settings.isEmpty || _services.isEmpty) { |
30 |
74 val isabelle_root1 = |
31 def settings(): Map[String, String] = Isabelle_Env.settings() |
75 bootstrap_directory(isabelle_root, "ISABELLE_ROOT", "isabelle.root", "Isabelle root") |
|
76 |
|
77 val cygwin_root1 = |
|
78 if (Platform.is_windows) |
|
79 bootstrap_directory(cygwin_root, "CYGWIN_ROOT", "cygwin.root", "Cygwin root") |
|
80 else "" |
|
81 |
|
82 if (Platform.is_windows) Cygwin.init(isabelle_root1, cygwin_root1) |
|
83 |
|
84 def set_cygwin_root(): Unit = |
|
85 { |
|
86 if (Platform.is_windows) |
|
87 _settings = Some(_settings.getOrElse(Map.empty) + ("CYGWIN_ROOT" -> cygwin_root1)) |
|
88 } |
|
89 |
|
90 set_cygwin_root() |
|
91 |
|
92 def default(env: Map[String, String], entry: (String, String)): Map[String, String] = |
|
93 if (env.isDefinedAt(entry._1) || entry._2 == "") env |
|
94 else env + entry |
|
95 |
|
96 val env = |
|
97 { |
|
98 val temp_windows = |
|
99 { |
|
100 val temp = if (Platform.is_windows) System.getenv("TEMP") else null |
|
101 if (temp != null && temp.contains('\\')) temp else "" |
|
102 } |
|
103 val user_home = System.getProperty("user.home", "") |
|
104 val isabelle_app = System.getProperty("isabelle.app", "") |
|
105 |
|
106 default( |
|
107 default( |
|
108 default(sys.env + ("ISABELLE_JDK_HOME" -> File.standard_path(jdk_home())), |
|
109 "TEMP_WINDOWS" -> temp_windows), |
|
110 "HOME" -> user_home), |
|
111 "ISABELLE_APP" -> isabelle_app) |
|
112 } |
|
113 |
|
114 val settings = |
|
115 { |
|
116 val dump = JFile.createTempFile("settings", null) |
|
117 dump.deleteOnExit |
|
118 try { |
|
119 val cmd1 = |
|
120 if (Platform.is_windows) |
|
121 List(cygwin_root1 + "\\bin\\bash", "-l", |
|
122 File.standard_path(isabelle_root1 + "\\bin\\isabelle")) |
|
123 else |
|
124 List(isabelle_root1 + "/bin/isabelle") |
|
125 val cmd = cmd1 ::: List("getenv", "-d", dump.toString) |
|
126 |
|
127 val (output, rc) = process_output(process(cmd, env = env, redirect = true)) |
|
128 if (rc != 0) error(output) |
|
129 |
|
130 val entries = |
|
131 space_explode('\u0000', File.read(dump)).flatMap( |
|
132 { |
|
133 case Properties.Eq(a, b) => Some(a -> b) |
|
134 case s => if (s.isEmpty || s.startsWith("=")) None else Some(s -> "") |
|
135 }).toMap |
|
136 entries + ("PATH" -> entries("PATH_JVM")) - "PATH_JVM" |
|
137 } |
|
138 finally { dump.delete } |
|
139 } |
|
140 _settings = Some(settings) |
|
141 set_cygwin_root() |
|
142 |
|
143 val variable = "ISABELLE_SCALA_SERVICES" |
|
144 val services = |
|
145 for (name <- space_explode(':', settings.getOrElse(variable, getenv_error(variable)))) |
|
146 yield { |
|
147 def err(msg: String): Nothing = |
|
148 error("Bad entry " + quote(name) + " in " + variable + "\n" + msg) |
|
149 try { Class.forName(name).asInstanceOf[Class[Service]] } |
|
150 catch { |
|
151 case _: ClassNotFoundException => err("Class not found") |
|
152 case exn: Throwable => err(Exn.message(exn)) |
|
153 } |
|
154 } |
|
155 _services = Some(services) |
|
156 } |
|
157 } |
|
158 |
|
159 |
|
160 /* getenv -- dynamic process environment */ |
|
161 |
|
162 private def getenv_error(name: String): Nothing = |
|
163 error("Undefined Isabelle environment variable: " + quote(name)) |
|
164 |
32 |
165 def getenv(name: String, env: Map[String, String] = settings()): String = |
33 def getenv(name: String, env: Map[String, String] = settings()): String = |
166 env.getOrElse(name, "") |
34 env.getOrElse(name, "") |
167 |
35 |
168 def getenv_strict(name: String, env: Map[String, String] = settings()): String = |
36 def getenv_strict(name: String, env: Map[String, String] = settings()): String = |
456 |
324 |
457 |
325 |
458 |
326 |
459 /** external processes **/ |
327 /** external processes **/ |
460 |
328 |
461 /* raw process */ |
|
462 |
|
463 def process(command_line: List[String], |
|
464 cwd: JFile = null, |
|
465 env: Map[String, String] = settings(), |
|
466 redirect: Boolean = false): Process = |
|
467 { |
|
468 val proc = new ProcessBuilder |
|
469 |
|
470 // fragile on Windows: |
|
471 // see https://docs.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?view=msvc-160 |
|
472 proc.command(command_line.asJava) |
|
473 |
|
474 if (cwd != null) proc.directory(cwd) |
|
475 if (env != null) { |
|
476 proc.environment.clear() |
|
477 for ((x, y) <- env) proc.environment.put(x, y) |
|
478 } |
|
479 proc.redirectErrorStream(redirect) |
|
480 proc.start |
|
481 } |
|
482 |
|
483 def process_output(proc: Process): (String, Int) = |
|
484 { |
|
485 proc.getOutputStream.close() |
|
486 |
|
487 val output = File.read_stream(proc.getInputStream) |
|
488 val rc = |
|
489 try { proc.waitFor } |
|
490 finally { |
|
491 proc.getInputStream.close() |
|
492 proc.getErrorStream.close() |
|
493 proc.destroy() |
|
494 Exn.Interrupt.dispose() |
|
495 } |
|
496 (output, rc) |
|
497 } |
|
498 |
|
499 def process_signal(group_pid: String, signal: String = "0"): Boolean = |
|
500 { |
|
501 val bash = |
|
502 if (Platform.is_windows) List(cygwin_root() + "\\bin\\bash.exe") |
|
503 else List("/usr/bin/env", "bash") |
|
504 val (_, rc) = process_output(process(bash ::: List("-c", "kill -" + signal + " -" + group_pid))) |
|
505 rc == 0 |
|
506 } |
|
507 |
|
508 |
|
509 /* GNU bash */ |
329 /* GNU bash */ |
510 |
330 |
511 def bash(script: String, |
331 def bash(script: String, |
512 cwd: JFile = null, |
332 cwd: JFile = null, |
513 env: Map[String, String] = settings(), |
333 env: Map[String, String] = settings(), |