author | wenzelm |
Mon, 28 Jun 2021 14:11:53 +0200 | |
changeset 73891 | 6c9044f04756 |
parent 73890 | 8f6b2eb15240 |
child 73893 | eb7655fcb090 |
permissions | -rw-r--r-- |
73890 | 1 |
/* Title: Pure/System/isabelle_env.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Fundamental Isabelle system environment: quasi-static module with |
|
5 |
optional init operation. |
|
6 |
*/ |
|
7 |
||
8 |
package isabelle |
|
9 |
||
10 |
||
11 |
import java.io.{File => JFile} |
|
12 |
||
13 |
import scala.jdk.CollectionConverters._ |
|
14 |
||
15 |
||
16 |
||
17 |
object Isabelle_Env |
|
18 |
{ |
|
19 |
/** bootstrap information **/ |
|
20 |
||
21 |
def jdk_home(): String = |
|
22 |
{ |
|
23 |
val java_home = System.getProperty("java.home", "") |
|
24 |
val home = new JFile(java_home) |
|
25 |
val parent = home.getParent |
|
26 |
if (home.getName == "jre" && parent != null && |
|
27 |
(new JFile(new JFile(parent, "bin"), "javac")).exists) parent |
|
28 |
else java_home |
|
29 |
} |
|
30 |
||
31 |
def bootstrap_directory( |
|
32 |
preference: String, envar: String, property: String, description: String): String = |
|
33 |
{ |
|
34 |
val value = |
|
35 |
proper_string(preference) orElse // explicit argument |
|
36 |
proper_string(System.getenv(envar)) orElse // e.g. inherited from running isabelle tool |
|
37 |
proper_string(System.getProperty(property)) getOrElse // e.g. via JVM application boot process |
|
38 |
error("Unknown " + description + " directory") |
|
39 |
||
40 |
if ((new JFile(value)).isDirectory) value |
|
41 |
else error("Bad " + description + " directory " + quote(value)) |
|
42 |
} |
|
43 |
||
44 |
||
45 |
||
46 |
/** raw process **/ |
|
47 |
||
48 |
/* raw process */ |
|
49 |
||
50 |
def process(command_line: List[String], |
|
51 |
cwd: JFile = null, |
|
52 |
env: Map[String, String] = settings(), |
|
53 |
redirect: Boolean = false): Process = |
|
54 |
{ |
|
55 |
val proc = new ProcessBuilder |
|
56 |
||
57 |
// fragile on Windows: |
|
58 |
// see https://docs.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?view=msvc-160 |
|
59 |
proc.command(command_line.asJava) |
|
60 |
||
61 |
if (cwd != null) proc.directory(cwd) |
|
62 |
if (env != null) { |
|
63 |
proc.environment.clear() |
|
64 |
for ((x, y) <- env) proc.environment.put(x, y) |
|
65 |
} |
|
66 |
proc.redirectErrorStream(redirect) |
|
67 |
proc.start |
|
68 |
} |
|
69 |
||
70 |
def process_output(proc: Process): (String, Int) = |
|
71 |
{ |
|
72 |
proc.getOutputStream.close() |
|
73 |
||
74 |
val output = File.read_stream(proc.getInputStream) |
|
75 |
val rc = |
|
76 |
try { proc.waitFor } |
|
77 |
finally { |
|
78 |
proc.getInputStream.close() |
|
79 |
proc.getErrorStream.close() |
|
80 |
proc.destroy() |
|
81 |
Exn.Interrupt.dispose() |
|
82 |
} |
|
83 |
(output, rc) |
|
84 |
} |
|
85 |
||
86 |
||
87 |
||
88 |
/** implicit settings environment **/ |
|
89 |
||
90 |
@volatile private var _settings: Option[Map[String, String]] = None |
|
91 |
||
92 |
def settings(): Map[String, String] = |
|
93 |
{ |
|
73891
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
94 |
if (_settings.isEmpty) init("", "") // unsynchronized check |
73890 | 95 |
_settings.get |
96 |
} |
|
97 |
||
73891
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
98 |
def init(isabelle_root: String, cygwin_root: String): Unit = synchronized |
73890 | 99 |
{ |
73891
6c9044f04756
clarified modules (again): services require full Isabelle/Scala environment;
wenzelm
parents:
73890
diff
changeset
|
100 |
if (_settings.isEmpty) { |
73890 | 101 |
val isabelle_root1 = |
102 |
bootstrap_directory(isabelle_root, "ISABELLE_ROOT", "isabelle.root", "Isabelle root") |
|
103 |
||
104 |
val cygwin_root1 = |
|
105 |
if (Platform.is_windows) |
|
106 |
bootstrap_directory(cygwin_root, "CYGWIN_ROOT", "cygwin.root", "Cygwin root") |
|
107 |
else "" |
|
108 |
||
109 |
if (Platform.is_windows) Cygwin.init(isabelle_root1, cygwin_root1) |
|
110 |
||
111 |
def set_cygwin_root(): Unit = |
|
112 |
{ |
|
113 |
if (Platform.is_windows) |
|
114 |
_settings = Some(_settings.getOrElse(Map.empty) + ("CYGWIN_ROOT" -> cygwin_root1)) |
|
115 |
} |
|
116 |
||
117 |
set_cygwin_root() |
|
118 |
||
119 |
def default(env: Map[String, String], entry: (String, String)): Map[String, String] = |
|
120 |
if (env.isDefinedAt(entry._1) || entry._2 == "") env |
|
121 |
else env + entry |
|
122 |
||
123 |
val env = |
|
124 |
{ |
|
125 |
val temp_windows = |
|
126 |
{ |
|
127 |
val temp = if (Platform.is_windows) System.getenv("TEMP") else null |
|
128 |
if (temp != null && temp.contains('\\')) temp else "" |
|
129 |
} |
|
130 |
val user_home = System.getProperty("user.home", "") |
|
131 |
val isabelle_app = System.getProperty("isabelle.app", "") |
|
132 |
||
133 |
default( |
|
134 |
default( |
|
135 |
default(sys.env + ("ISABELLE_JDK_HOME" -> File.standard_path(jdk_home())), |
|
136 |
"TEMP_WINDOWS" -> temp_windows), |
|
137 |
"HOME" -> user_home), |
|
138 |
"ISABELLE_APP" -> isabelle_app) |
|
139 |
} |
|
140 |
||
141 |
val settings = |
|
142 |
{ |
|
143 |
val dump = JFile.createTempFile("settings", null) |
|
144 |
dump.deleteOnExit |
|
145 |
try { |
|
146 |
val cmd1 = |
|
147 |
if (Platform.is_windows) |
|
148 |
List(cygwin_root1 + "\\bin\\bash", "-l", |
|
149 |
File.standard_path(isabelle_root1 + "\\bin\\isabelle")) |
|
150 |
else |
|
151 |
List(isabelle_root1 + "/bin/isabelle") |
|
152 |
val cmd = cmd1 ::: List("getenv", "-d", dump.toString) |
|
153 |
||
154 |
val (output, rc) = process_output(process(cmd, env = env, redirect = true)) |
|
155 |
if (rc != 0) error(output) |
|
156 |
||
157 |
val entries = |
|
158 |
space_explode('\u0000', File.read(dump)).flatMap( |
|
159 |
{ |
|
160 |
case Properties.Eq(a, b) => Some(a -> b) |
|
161 |
case s => if (s.isEmpty || s.startsWith("=")) None else Some(s -> "") |
|
162 |
}).toMap |
|
163 |
entries + ("PATH" -> entries("PATH_JVM")) - "PATH_JVM" |
|
164 |
} |
|
165 |
finally { dump.delete } |
|
166 |
} |
|
167 |
_settings = Some(settings) |
|
168 |
set_cygwin_root() |
|
169 |
} |
|
170 |
} |
|
171 |
} |