73906
|
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.setup;
|
|
9 |
|
|
10 |
import java.io.File;
|
|
11 |
import java.io.IOException;
|
|
12 |
import java.nio.file.Files;
|
|
13 |
import java.nio.file.Path;
|
|
14 |
import java.util.HashMap;
|
|
15 |
import java.util.LinkedList;
|
|
16 |
import java.util.List;
|
|
17 |
import java.util.Locale;
|
|
18 |
import java.util.Map;
|
|
19 |
import java.util.function.BiFunction;
|
|
20 |
import java.util.regex.Matcher;
|
|
21 |
import java.util.regex.Pattern;
|
|
22 |
|
|
23 |
|
|
24 |
public class Environment
|
|
25 |
{
|
|
26 |
/** Support for Cygwin as POSIX emulation on Windows **/
|
|
27 |
|
|
28 |
public static Boolean is_windows()
|
|
29 |
{
|
|
30 |
return System.getProperty("os.name", "").startsWith("Windows");
|
|
31 |
}
|
|
32 |
|
|
33 |
public static String quote(String s)
|
|
34 |
{
|
|
35 |
return "\"" + s + "\"";
|
|
36 |
}
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
/* system path representations */
|
|
41 |
|
|
42 |
private static String slashes(String s) { return s.replace('\\', '/'); }
|
|
43 |
|
|
44 |
public static String standard_path(String cygwin_root, String platform_path)
|
|
45 |
{
|
|
46 |
if (is_windows()) {
|
|
47 |
String backslashes = platform_path.replace('/', '\\');
|
|
48 |
|
|
49 |
Pattern root_pattern =
|
|
50 |
Pattern.compile("(?i)" + Pattern.quote(cygwin_root) + "(?:\\\\+|\\z)(.*)");
|
|
51 |
Matcher root_matcher = root_pattern.matcher(backslashes);
|
|
52 |
|
|
53 |
Pattern drive_pattern = Pattern.compile("([a-zA-Z]):\\\\*(.*)");
|
|
54 |
Matcher drive_matcher = drive_pattern.matcher(backslashes);
|
|
55 |
|
|
56 |
if (root_matcher.matches()) {
|
|
57 |
String rest = root_matcher.group(1);
|
|
58 |
return "/" + slashes(rest);
|
|
59 |
}
|
|
60 |
else if (drive_matcher.matches()) {
|
|
61 |
String letter = drive_matcher.group(1).toLowerCase(Locale.ROOT);
|
|
62 |
String rest = drive_matcher.group(2);
|
|
63 |
return "/cygdrive/" + letter + (rest.isEmpty() ? "" : "/" + slashes(rest));
|
|
64 |
}
|
|
65 |
else { return slashes(backslashes); }
|
|
66 |
}
|
|
67 |
else { return platform_path; }
|
|
68 |
}
|
|
69 |
|
|
70 |
public static String platform_path(String cygwin_root, String standard_path)
|
|
71 |
{
|
|
72 |
if (is_windows()) {
|
|
73 |
StringBuilder result_path = new StringBuilder();
|
|
74 |
|
|
75 |
Pattern cygdrive_pattern = Pattern.compile("/cygdrive/([a-zA-Z])($|/.*)");
|
|
76 |
Matcher cygdrive_matcher = cygdrive_pattern.matcher(standard_path);
|
|
77 |
|
|
78 |
Pattern named_root_pattern = Pattern.compile("//+([^/]*)(.*)");
|
|
79 |
Matcher named_root_matcher = named_root_pattern.matcher(standard_path);
|
|
80 |
|
|
81 |
String rest;
|
|
82 |
if (cygdrive_matcher.matches()) {
|
|
83 |
String drive = cygdrive_matcher.group(1).toUpperCase(Locale.ROOT);
|
|
84 |
rest = cygdrive_matcher.group(2);
|
|
85 |
result_path.append(drive);
|
|
86 |
result_path.append(':');
|
|
87 |
result_path.append(File.separatorChar);
|
|
88 |
}
|
|
89 |
else if (named_root_matcher.matches()) {
|
|
90 |
String root = named_root_matcher.group(1);
|
|
91 |
rest = named_root_matcher.group(2);
|
|
92 |
result_path.append(File.separatorChar);
|
|
93 |
result_path.append(File.separatorChar);
|
|
94 |
result_path.append(root);
|
|
95 |
}
|
|
96 |
else {
|
|
97 |
if (standard_path.startsWith("/")) { result_path.append(cygwin_root); }
|
|
98 |
rest = standard_path;
|
|
99 |
}
|
|
100 |
|
|
101 |
for (String p : rest.split("/", -1)) {
|
|
102 |
if (!p.isEmpty()) {
|
|
103 |
int len = result_path.length();
|
|
104 |
if (len > 0 && result_path.charAt(len - 1) != File.separatorChar) {
|
|
105 |
result_path.append(File.separatorChar);
|
|
106 |
}
|
|
107 |
result_path.append(p);
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
return result_path.toString();
|
|
112 |
}
|
|
113 |
else { return standard_path; }
|
|
114 |
}
|
|
115 |
|
|
116 |
|
|
117 |
/* raw process */
|
|
118 |
|
|
119 |
public static ProcessBuilder process_builder(
|
|
120 |
List<String> cmd, File cwd, Map<String,String> env, boolean redirect)
|
|
121 |
{
|
|
122 |
ProcessBuilder builder = new ProcessBuilder();
|
|
123 |
|
|
124 |
// fragile on Windows:
|
|
125 |
// see https://docs.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?view=msvc-160
|
|
126 |
builder.command(cmd);
|
|
127 |
|
|
128 |
if (cwd != null) builder.directory(cwd);
|
|
129 |
if (env != null) {
|
|
130 |
builder.environment().clear();
|
|
131 |
builder.environment().putAll(env);
|
|
132 |
}
|
|
133 |
builder.redirectErrorStream(redirect);
|
|
134 |
|
|
135 |
return builder;
|
|
136 |
}
|
|
137 |
|
|
138 |
public static class Exec_Result
|
|
139 |
{
|
|
140 |
private final int _rc;
|
|
141 |
private final String _out;
|
|
142 |
private final String _err;
|
|
143 |
|
|
144 |
Exec_Result(int rc, String out, String err)
|
|
145 |
{
|
|
146 |
_rc = rc;
|
|
147 |
_out = out;
|
|
148 |
_err = err;
|
|
149 |
}
|
|
150 |
|
|
151 |
public int rc() { return _rc; }
|
|
152 |
public boolean ok() { return _rc == 0; }
|
|
153 |
public String out() { return _out; }
|
|
154 |
public String err() { return _err; }
|
|
155 |
}
|
|
156 |
|
|
157 |
public static Exec_Result exec_process(
|
|
158 |
List<String> command_line,
|
|
159 |
File cwd,
|
|
160 |
Map<String,String> env,
|
|
161 |
boolean redirect) throws IOException, InterruptedException
|
|
162 |
{
|
|
163 |
Path out_file = Files.createTempFile(null, null);
|
|
164 |
Path err_file = Files.createTempFile(null, null);
|
|
165 |
Exec_Result res;
|
|
166 |
try {
|
|
167 |
ProcessBuilder builder = process_builder(command_line, cwd, env, redirect);
|
|
168 |
builder.redirectOutput(out_file.toFile());
|
|
169 |
builder.redirectError(err_file.toFile());
|
|
170 |
|
|
171 |
Process proc = builder.start();
|
|
172 |
proc.getOutputStream().close();
|
|
173 |
try { proc.waitFor(); }
|
|
174 |
finally {
|
|
175 |
proc.getInputStream().close();
|
|
176 |
proc.getErrorStream().close();
|
|
177 |
proc.destroy();
|
|
178 |
Thread.interrupted();
|
|
179 |
}
|
|
180 |
|
|
181 |
int rc = proc.exitValue();
|
|
182 |
String out = Files.readString(out_file);
|
|
183 |
String err = Files.readString(err_file);
|
|
184 |
res = new Exec_Result(rc, out, err);
|
|
185 |
}
|
|
186 |
finally {
|
|
187 |
Files.deleteIfExists(out_file);
|
|
188 |
Files.deleteIfExists(err_file);
|
|
189 |
}
|
|
190 |
return res;
|
|
191 |
}
|
|
192 |
|
|
193 |
|
|
194 |
/* init (e.g. after extraction via 7zip) */
|
|
195 |
|
|
196 |
private static String bootstrap_directory(
|
|
197 |
String preference, String variable, String property, String description)
|
|
198 |
{
|
|
199 |
String a = preference; // explicit argument
|
|
200 |
String b = System.getenv(variable); // e.g. inherited from running isabelle tool
|
|
201 |
String c = System.getProperty(property); // e.g. via JVM application boot process
|
|
202 |
String dir;
|
|
203 |
|
|
204 |
if (a != null && !a.isEmpty()) { dir = a; }
|
|
205 |
else if (b != null && !b.isEmpty()) { dir = b; }
|
|
206 |
else if (c != null && !c.isEmpty()) { dir = c; }
|
|
207 |
else { throw new RuntimeException("Unknown " + description + " directory"); }
|
|
208 |
|
|
209 |
if ((new File(dir)).isDirectory()) { return dir; }
|
|
210 |
else { throw new RuntimeException("Bad " + description + " directory " + quote(dir)); }
|
|
211 |
}
|
|
212 |
|
|
213 |
private static void cygwin_exec(String isabelle_root, List<String> cmd)
|
|
214 |
throws IOException, InterruptedException
|
|
215 |
{
|
|
216 |
File cwd = new File(isabelle_root);
|
|
217 |
Map<String,String> env = new HashMap<String,String>(System.getenv());
|
|
218 |
env.put("CYGWIN", "nodosfilewarning");
|
|
219 |
Exec_Result res = exec_process(cmd, cwd, env, true);
|
|
220 |
if (!res.ok()) throw new RuntimeException(res.out());
|
|
221 |
}
|
|
222 |
|
|
223 |
public static void cygwin_link(String content, File target) throws IOException
|
|
224 |
{
|
|
225 |
Path target_path = target.toPath();
|
|
226 |
Files.writeString(target_path, "!<symlink>" + content + "\u0000");
|
|
227 |
Files.setAttribute(target_path, "dos:system", true);
|
|
228 |
}
|
|
229 |
|
|
230 |
public static void cygwin_init(String isabelle_root, String cygwin_root)
|
|
231 |
throws IOException, InterruptedException
|
|
232 |
{
|
|
233 |
if (is_windows()) {
|
|
234 |
File uninitialized_file = new File(cygwin_root, "isabelle\\uninitialized");
|
|
235 |
boolean uninitialized = uninitialized_file.isFile() && uninitialized_file.delete();
|
|
236 |
|
|
237 |
if (uninitialized) {
|
|
238 |
Path symlinks_path = (new File(cygwin_root + "\\isabelle\\symlinks")).toPath();
|
|
239 |
String[] symlinks = Files.readAllLines(symlinks_path).toArray(new String[0]);
|
|
240 |
|
|
241 |
// recover symlinks
|
|
242 |
int i = 0;
|
|
243 |
int m = symlinks.length;
|
|
244 |
int n = (m > 0 && symlinks[m - 1].isEmpty()) ? m - 1 : m;
|
|
245 |
while (i < n) {
|
|
246 |
if (i + 1 < n) {
|
|
247 |
String target = symlinks[i];
|
|
248 |
String content = symlinks[i + 1];
|
|
249 |
cygwin_link(content, new File(isabelle_root, target));
|
|
250 |
i += 2;
|
|
251 |
} else { throw new RuntimeException("Unbalanced symlinks list"); }
|
|
252 |
}
|
|
253 |
|
|
254 |
cygwin_exec(isabelle_root,
|
|
255 |
List.of(cygwin_root + "\\bin\\dash.exe", "/isabelle/rebaseall"));
|
|
256 |
cygwin_exec(isabelle_root,
|
|
257 |
List.of(cygwin_root + "\\bin\\bash.exe", "/isabelle/postinstall"));
|
|
258 |
}
|
|
259 |
}
|
|
260 |
}
|
|
261 |
|
|
262 |
|
|
263 |
/* implicit settings environment */
|
|
264 |
|
|
265 |
private static volatile Map<String,String> _settings = null;
|
|
266 |
|
|
267 |
public static Map<String,String> settings()
|
|
268 |
throws IOException, InterruptedException
|
|
269 |
{
|
|
270 |
if (_settings == null) { init("", ""); } // unsynchronized check
|
|
271 |
return _settings;
|
|
272 |
}
|
|
273 |
|
|
274 |
public static synchronized void init(String _isabelle_root, String _cygwin_root)
|
|
275 |
throws IOException, InterruptedException
|
|
276 |
{
|
|
277 |
if (_settings == null) {
|
|
278 |
String isabelle_root =
|
|
279 |
bootstrap_directory(_isabelle_root, "ISABELLE_ROOT", "isabelle.root", "Isabelle root");
|
|
280 |
|
|
281 |
String cygwin_root = "";
|
|
282 |
if (is_windows()) {
|
|
283 |
cygwin_root = bootstrap_directory(_cygwin_root, "CYGWIN_ROOT", "cygwin.root", "Cygwin root");
|
|
284 |
cygwin_init(isabelle_root, cygwin_root);
|
|
285 |
}
|
|
286 |
|
|
287 |
Map<String,String> env = new HashMap<String,String>(System.getenv());
|
|
288 |
|
|
289 |
BiFunction<String,String,Void> env_default =
|
|
290 |
(String a, String b) -> { if (!b.isEmpty()) env.putIfAbsent(a, b); return null; };
|
|
291 |
|
|
292 |
String temp_windows = is_windows() ? System.getenv("TEMP") : null;
|
|
293 |
|
|
294 |
env_default.apply("CYGWIN_ROOT", cygwin_root);
|
|
295 |
env_default.apply("TEMP_WINDOWS",
|
|
296 |
(temp_windows != null && temp_windows.contains("\\")) ? temp_windows : "");
|
|
297 |
env_default.apply("ISABELLE_JDK_HOME",
|
|
298 |
standard_path(cygwin_root, System.getProperty("java.home", "")));
|
|
299 |
env_default.apply("HOME", System.getProperty("user.home", ""));
|
|
300 |
env_default.apply("ISABELLE_APP", System.getProperty("isabelle.app", ""));
|
|
301 |
|
|
302 |
Map<String,String> settings = new HashMap<String,String>();
|
|
303 |
Path settings_file = Files.createTempFile(null, null);
|
|
304 |
try {
|
|
305 |
List<String> cmd = new LinkedList<String>();
|
|
306 |
if (is_windows()) {
|
|
307 |
cmd.add(cygwin_root + "\\bin\\bash");
|
|
308 |
cmd.add("-l");
|
|
309 |
cmd.add(standard_path(cygwin_root, isabelle_root + "\\bin\\isabelle"));
|
|
310 |
} else {
|
|
311 |
cmd.add(isabelle_root + "/bin/isabelle");
|
|
312 |
}
|
|
313 |
cmd.add("getenv");
|
|
314 |
cmd.add("-d");
|
|
315 |
cmd.add(settings_file.toString());
|
|
316 |
|
|
317 |
Exec_Result res = exec_process(cmd, null, env, true);
|
|
318 |
if (!res.ok()) throw new RuntimeException(res.out());
|
|
319 |
|
|
320 |
for (String s : Files.readString(settings_file).split("\u0000", -1)) {
|
|
321 |
int i = s.indexOf('=');
|
|
322 |
if (i > 0) { settings.put(s.substring(0, i), s.substring(i + 1)); }
|
|
323 |
else if (i < 0 && !s.isEmpty()) { settings.put(s, ""); }
|
|
324 |
}
|
|
325 |
}
|
|
326 |
finally { Files.delete(settings_file); }
|
|
327 |
|
|
328 |
if (is_windows()) { settings.put("CYGWIN_ROOT", cygwin_root); }
|
|
329 |
|
|
330 |
settings.put("PATH", settings.get("PATH_JVM"));
|
|
331 |
settings.remove("PATH_JVM");
|
|
332 |
|
|
333 |
_settings = Map.copyOf(settings);
|
|
334 |
}
|
|
335 |
}
|
|
336 |
}
|