diff --git a/src/Tools/Setup/isabelle/setup/Build_Scala.java b/src/Tools/Setup/isabelle/setup/Build_Scala.java --- a/src/Tools/Setup/isabelle/setup/Build_Scala.java +++ b/src/Tools/Setup/isabelle/setup/Build_Scala.java @@ -1,213 +1,244 @@ /* Title: Tools/Setup/isabelle/setup/Build_Scala.java Author: Makarius Build Isabelle/Scala modules. */ package isabelle.setup; +import java.io.BufferedOutputStream; import java.io.File; import java.io.IOException; +import java.io.OutputStream; import java.math.BigInteger; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Properties; +import java.util.jar.Attributes; +import java.util.jar.JarEntry; +import java.util.jar.JarOutputStream; +import java.util.jar.Manifest; import java.util.stream.Stream; public class Build_Scala { + /** component directory context **/ + public static class Context { private final Path component_dir; private Properties props; public Context(Path dir) throws IOException { component_dir = dir; props = new Properties(); Path path = component_dir.resolve("etc/scala.props"); if (Files.exists(path)) { props.load(Files.newBufferedReader(path)); } } @Override public String toString() { return component_dir.toString(); } public String component_name() { return component_dir.toFile().getName(); } public String name() { return props.getProperty("name", component_name()); } public String description() { return props.getProperty("description", name()); } public String lib_name() { return props.getProperty("lib", "lib") + "/" + name(); } public String jar_name() { return lib_name() + ".jar"; } public String shasum_name() { return lib_name() + ".shasum"; } public String main() { return props.getProperty("main", ""); } public String[] sources() { return props.getProperty("sources", "").split("\\s+"); } public String[] resources() { return props.getProperty("resources", "").split("\\s+"); } public String[] services() { return props.getProperty("services", "").split("\\s+"); } public Path path(String file) { return component_dir.resolve(file); } public boolean exists(String file) { return Files.exists(path(file)); } public String item_name(String s) { int i = s.indexOf(':'); return i > 0 ? s.substring(0, i) : s; } public String item_target(String s) { int i = s.indexOf(':'); return i > 0 ? s.substring(i + 1) : s; } public String shasum(String file) throws IOException, NoSuchAlgorithmException { if (exists(file)) { MessageDigest sha = MessageDigest.getInstance("SHA"); sha.update(Files.readAllBytes(path(file))); String digest = String.format(Locale.ROOT, "%040x", new BigInteger(1, sha.digest())); return digest + " *" + file + "\n"; } else { return ""; } } } + + + /** create jar **/ + + public static void create_jar(Path dir, String main, Path jar) + throws IOException + { + Files.deleteIfExists(jar); + + Manifest manifest = new Manifest(); + Attributes attributes = manifest.getMainAttributes(); + attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0"); + attributes.put(new Attributes.Name("Created-By"), + System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ")"); + if (!main.isEmpty()) { attributes.put(Attributes.Name.MAIN_CLASS, main); } + + try (JarOutputStream out = + new JarOutputStream(new BufferedOutputStream(Files.newOutputStream(jar)), manifest)) + { + for (Path path : Files.walk(dir).sorted().toArray(Path[]::new)) { + boolean is_dir = Files.isDirectory(path); + boolean is_file = Files.isRegularFile(path); + if (is_dir || is_file) { + String name = Environment.slashes(dir.relativize(path).toString()); + JarEntry entry = new JarEntry(is_dir ? name + "/" : name); + entry.setTime(path.toFile().lastModified()); + out.putNextEntry(entry); + if (is_file) { out.write(Files.readAllBytes(path)); } + out.closeEntry(); + } + } + } + } + + + + /** build scala **/ + public static void build_scala(Context context, boolean fresh) throws IOException, InterruptedException, NoSuchAlgorithmException { String jar_name = context.jar_name(); String shasum_name = context.shasum_name(); String[] sources = context.sources(); String[] resources = context.resources(); if (sources.length == 0) { Files.deleteIfExists(context.path(jar_name)); Files.deleteIfExists(context.path(shasum_name)); } else { String shasum_old = context.exists(shasum_name) ? Files.readString(context.path(shasum_name)) : ""; String shasum_sources; { StringBuilder _shasum = new StringBuilder(); for (String s : resources) { _shasum.append(context.shasum(context.item_name(s))); } for (String s : sources) { _shasum.append(context.shasum(s)); } shasum_sources = _shasum.toString(); } if (fresh || !shasum_old.equals(context.shasum(jar_name) + shasum_sources)) { System.out.println("### Building " + context.description() + " ..."); String java_home = Environment.getenv("JAVA_HOME"); String scala_home = Environment.getenv("SCALA_HOME"); String scalac_options = Environment.getenv("ISABELLE_SCALAC_OPTIONS"); String isabelle_class_path = Environment.getenv("ISABELLE_CLASSPATH"); if (java_home.isEmpty()) { throw new RuntimeException("Unknown JAVA_HOME -- Java unavailable"); } if (scala_home.isEmpty()) { throw new RuntimeException("Unknown SCALA_HOME -- Scala unavailable"); } Path build_dir = Files.createTempDirectory("isabelle"); try { /* classpath */ List classpath = new LinkedList(); for (String s : isabelle_class_path.split(":", -1)) { classpath.add(Environment.platform_path(s)); } Map env = new HashMap(Environment.settings()); env.put("CLASSPATH", String.join(File.pathSeparator, classpath)); /* compile sources */ List cmd = new LinkedList(); Environment.Exec_Result res; cmd.add(Environment.platform_path(scala_home + "/bin/scalac")); for (String s : scalac_options.split("\\s+")) { cmd.add(s); } cmd.add("-d"); cmd.add(build_dir.toString()); for (String s : sources) { cmd.add(context.path(s).toString()); } res = Environment.exec_process(cmd, build_dir.toFile(), env, false); if (!res.ok()) throw new RuntimeException(res.err()); /* copy resources */ for (String s : context.resources()) { String name = context.item_name(s); String target = context.item_target(s); Path file_name = Path.of(name).normalize().getFileName(); Path target_path = Path.of(target).normalize(); Path target_dir; Path target_file; { if (target.endsWith("/") || target.endsWith("/.")) { target_dir = build_dir.resolve(target_path); target_file = target_dir.resolve(file_name); } else { target_file = build_dir.resolve(target_path); target_dir = target_file.getParent(); } } Files.createDirectories(target_dir); Files.copy(context.path(name), target_file, StandardCopyOption.COPY_ATTRIBUTES); } - /* jar */ + /* packaging */ - cmd.clear(); - cmd.add(Environment.platform_path(java_home + "/bin/jar")); - cmd.add("-c"); - cmd.add("-f"); - cmd.add(context.path(jar_name).toString()); - if (!context.main().isEmpty()) { - cmd.add("-e"); - cmd.add(context.main()); - } - cmd.add("."); - - res = Environment.exec_process(cmd, build_dir.toFile(), env, false); - if (!res.ok()) throw new RuntimeException(res.err()); - - - /* shasum */ + create_jar(build_dir, context.main(), context.path(jar_name)); String shasum = context.shasum(jar_name) + shasum_sources; Files.writeString(context.path(shasum_name), shasum); } finally { try (Stream walk = Files.walk(build_dir)) { walk.sorted(Comparator.reverseOrder()) .map(Path::toFile) .forEach(File::delete); } } } } } } diff --git a/src/Tools/Setup/isabelle/setup/Environment.java b/src/Tools/Setup/isabelle/setup/Environment.java --- a/src/Tools/Setup/isabelle/setup/Environment.java +++ b/src/Tools/Setup/isabelle/setup/Environment.java @@ -1,382 +1,382 @@ /* Title: Pure/System/isabelle_env.scala Author: Makarius Fundamental Isabelle system environment: quasi-static module with optional init operation. */ package isabelle.setup; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.function.BiFunction; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Environment { /** Support for Cygwin as POSIX emulation on Windows **/ public static Boolean is_windows() { return System.getProperty("os.name", "").startsWith("Windows"); } public static String quote(String s) { return "\"" + s + "\""; } /* system path representations */ - private static String slashes(String s) { return s.replace('\\', '/'); } + public static String slashes(String s) { return s.replace('\\', '/'); } public static String standard_path(String cygwin_root, String platform_path) { if (is_windows()) { String backslashes = platform_path.replace('/', '\\'); Pattern root_pattern = Pattern.compile("(?i)" + Pattern.quote(cygwin_root) + "(?:\\\\+|\\z)(.*)"); Matcher root_matcher = root_pattern.matcher(backslashes); Pattern drive_pattern = Pattern.compile("([a-zA-Z]):\\\\*(.*)"); Matcher drive_matcher = drive_pattern.matcher(backslashes); if (root_matcher.matches()) { String rest = root_matcher.group(1); return "/" + slashes(rest); } else if (drive_matcher.matches()) { String letter = drive_matcher.group(1).toLowerCase(Locale.ROOT); String rest = drive_matcher.group(2); return "/cygdrive/" + letter + (rest.isEmpty() ? "" : "/" + slashes(rest)); } else { return slashes(backslashes); } } else { return platform_path; } } public static String platform_path(String cygwin_root, String standard_path) { if (is_windows()) { StringBuilder result_path = new StringBuilder(); Pattern cygdrive_pattern = Pattern.compile("/cygdrive/([a-zA-Z])($|/.*)"); Matcher cygdrive_matcher = cygdrive_pattern.matcher(standard_path); Pattern named_root_pattern = Pattern.compile("//+([^/]*)(.*)"); Matcher named_root_matcher = named_root_pattern.matcher(standard_path); String rest; if (cygdrive_matcher.matches()) { String drive = cygdrive_matcher.group(1).toUpperCase(Locale.ROOT); rest = cygdrive_matcher.group(2); result_path.append(drive); result_path.append(':'); result_path.append(File.separatorChar); } else if (named_root_matcher.matches()) { String root = named_root_matcher.group(1); rest = named_root_matcher.group(2); result_path.append(File.separatorChar); result_path.append(File.separatorChar); result_path.append(root); } else { if (standard_path.startsWith("/")) { result_path.append(cygwin_root); } rest = standard_path; } for (String p : rest.split("/", -1)) { if (!p.isEmpty()) { int len = result_path.length(); if (len > 0 && result_path.charAt(len - 1) != File.separatorChar) { result_path.append(File.separatorChar); } result_path.append(p); } } return result_path.toString(); } else { return standard_path; } } /* raw process */ public static ProcessBuilder process_builder( List cmd, File cwd, Map env, boolean redirect) { ProcessBuilder builder = new ProcessBuilder(); // fragile on Windows: // see https://docs.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?view=msvc-160 builder.command(cmd); if (cwd != null) builder.directory(cwd); if (env != null) { builder.environment().clear(); builder.environment().putAll(env); } builder.redirectErrorStream(redirect); return builder; } public static class Exec_Result { private final int _rc; private final String _out; private final String _err; Exec_Result(int rc, String out, String err) { _rc = rc; _out = out; _err = err; } public int rc() { return _rc; } public boolean ok() { return _rc == 0; } public String out() { return _out; } public String err() { return _err; } } public static Exec_Result exec_process( List command_line, File cwd, Map env, boolean redirect) throws IOException, InterruptedException { Path out_file = Files.createTempFile(null, null); Path err_file = Files.createTempFile(null, null); Exec_Result res; try { ProcessBuilder builder = process_builder(command_line, cwd, env, redirect); builder.redirectOutput(out_file.toFile()); builder.redirectError(err_file.toFile()); Process proc = builder.start(); proc.getOutputStream().close(); try { proc.waitFor(); } finally { proc.getInputStream().close(); proc.getErrorStream().close(); proc.destroy(); Thread.interrupted(); } int rc = proc.exitValue(); String out = Files.readString(out_file); String err = Files.readString(err_file); res = new Exec_Result(rc, out, err); } finally { Files.deleteIfExists(out_file); Files.deleteIfExists(err_file); } return res; } /* init (e.g. after extraction via 7zip) */ private static String bootstrap_directory( String preference, String variable, String property, String description) { String a = preference; // explicit argument String b = System.getenv(variable); // e.g. inherited from running isabelle tool String c = System.getProperty(property); // e.g. via JVM application boot process String dir; if (a != null && !a.isEmpty()) { dir = a; } else if (b != null && !b.isEmpty()) { dir = b; } else if (c != null && !c.isEmpty()) { dir = c; } else { throw new RuntimeException("Unknown " + description + " directory"); } if ((new File(dir)).isDirectory()) { return dir; } else { throw new RuntimeException("Bad " + description + " directory " + quote(dir)); } } private static void cygwin_exec(String isabelle_root, List cmd) throws IOException, InterruptedException { File cwd = new File(isabelle_root); Map env = new HashMap(System.getenv()); env.put("CYGWIN", "nodosfilewarning"); Exec_Result res = exec_process(cmd, cwd, env, true); if (!res.ok()) throw new RuntimeException(res.out()); } public static void cygwin_link(String content, File target) throws IOException { Path target_path = target.toPath(); Files.writeString(target_path, "!" + content + "\u0000"); Files.setAttribute(target_path, "dos:system", true); } public static void cygwin_init(String isabelle_root, String cygwin_root) throws IOException, InterruptedException { if (is_windows()) { File uninitialized_file = new File(cygwin_root, "isabelle\\uninitialized"); boolean uninitialized = uninitialized_file.isFile() && uninitialized_file.delete(); if (uninitialized) { Path symlinks_path = (new File(cygwin_root + "\\isabelle\\symlinks")).toPath(); String[] symlinks = Files.readAllLines(symlinks_path).toArray(new String[0]); // recover symlinks int i = 0; int m = symlinks.length; int n = (m > 0 && symlinks[m - 1].isEmpty()) ? m - 1 : m; while (i < n) { if (i + 1 < n) { String target = symlinks[i]; String content = symlinks[i + 1]; cygwin_link(content, new File(isabelle_root, target)); i += 2; } else { throw new RuntimeException("Unbalanced symlinks list"); } } cygwin_exec(isabelle_root, List.of(cygwin_root + "\\bin\\dash.exe", "/isabelle/rebaseall")); cygwin_exec(isabelle_root, List.of(cygwin_root + "\\bin\\bash.exe", "/isabelle/postinstall")); } } } /* implicit settings environment */ private static volatile Map _settings = null; public static Map settings() throws IOException, InterruptedException { if (_settings == null) { init("", ""); } // unsynchronized check return _settings; } public static String getenv(String name) throws IOException, InterruptedException { return settings().getOrDefault(name, ""); } public static synchronized void init(String _isabelle_root, String _cygwin_root) throws IOException, InterruptedException { if (_settings == null) { String isabelle_root = bootstrap_directory(_isabelle_root, "ISABELLE_ROOT", "isabelle.root", "Isabelle root"); String cygwin_root = ""; if (is_windows()) { cygwin_root = bootstrap_directory(_cygwin_root, "CYGWIN_ROOT", "cygwin.root", "Cygwin root"); cygwin_init(isabelle_root, cygwin_root); } Map env = new HashMap(System.getenv()); BiFunction env_default = (String a, String b) -> { if (!b.isEmpty()) env.putIfAbsent(a, b); return null; }; String temp_windows = is_windows() ? System.getenv("TEMP") : null; env_default.apply("CYGWIN_ROOT", cygwin_root); env_default.apply("TEMP_WINDOWS", (temp_windows != null && temp_windows.contains("\\")) ? temp_windows : ""); env_default.apply("ISABELLE_JDK_HOME", standard_path(cygwin_root, System.getProperty("java.home", ""))); env_default.apply("HOME", System.getProperty("user.home", "")); env_default.apply("ISABELLE_APP", System.getProperty("isabelle.app", "")); Map settings = new HashMap(); Path settings_file = Files.createTempFile(null, null); try { List cmd = new LinkedList(); if (is_windows()) { cmd.add(cygwin_root + "\\bin\\bash"); cmd.add("-l"); cmd.add(standard_path(cygwin_root, isabelle_root + "\\bin\\isabelle")); } else { cmd.add(isabelle_root + "/bin/isabelle"); } cmd.add("getenv"); cmd.add("-d"); cmd.add(settings_file.toString()); Exec_Result res = exec_process(cmd, null, env, true); if (!res.ok()) throw new RuntimeException(res.out()); for (String s : Files.readString(settings_file).split("\u0000", -1)) { int i = s.indexOf('='); if (i > 0) { settings.put(s.substring(0, i), s.substring(i + 1)); } else if (i < 0 && !s.isEmpty()) { settings.put(s, ""); } } } finally { Files.delete(settings_file); } if (is_windows()) { settings.put("CYGWIN_ROOT", cygwin_root); } settings.put("PATH", settings.get("PATH_JVM")); settings.remove("PATH_JVM"); _settings = Map.copyOf(settings); } } /* Cygwin root (after init) */ public static String cygwin_root() throws IOException, InterruptedException { return getenv("CYGWIN_ROOT"); } public static String standard_path(String platform_path) throws IOException, InterruptedException { return standard_path(cygwin_root(), platform_path); } public static String platform_path(String standard_path) throws IOException, InterruptedException { return platform_path(cygwin_root(), standard_path); } /* kill process (via bash) */ static public boolean kill_process(String group_pid, String signal) throws IOException, InterruptedException { List cmd = new LinkedList(); if (is_windows()) { cmd.add(cygwin_root() + "\\bin\\bash.exe"); } else { cmd.add("/usr/bin/env"); cmd.add("bash"); } cmd.add("-c"); cmd.add("kill -" + signal + " -" + group_pid); return exec_process(cmd, null, null, false).ok(); } }