5 import java.util.regex.*;
6 import java.util.logging.*;
7 import java.nio.file.*;
8 import java.nio.file.attribute.*;
12 public class Compiler {
13 private static final Logger log = Logger.getLogger("jagi-fs");
14 private static final Path cwd = Paths.get("").toAbsolutePath();
15 private final Map<Path, File> files = new HashMap<>();
16 private final Map<Path, ClassLoader> libs = new HashMap<>();
17 private final Collection<Path> searchpath = new ArrayList<>();
20 String syspath = System.getenv("PATH");
22 String sep = java.io.File.pathSeparator;
23 int p1 = 0, p2 = syspath.indexOf(sep);
27 el = syspath.substring(p1, p2);
28 p1 = p2 + sep.length();
29 p2 = syspath.indexOf(sep, p1);
31 el =syspath.substring(p1);
34 Path p = Paths.get(el);
35 if(p.getParent() != null)
36 searchpath.add(p.getParent().resolve("share").resolve("java"));
37 } catch(InvalidPathException e) {
42 String proppath = System.getProperty("jagi.search-path");
43 if(proppath != null) {
44 for(String el : proppath.split(":")) {
46 searchpath.add(Paths.get(el));
47 } catch(InvalidPathException e) {
54 public static class ClassOutput {
55 public final String name;
56 public final ByteArrayOutputStream buf = new ByteArrayOutputStream();
58 public ClassOutput(String name) {
63 public static class CompilationException extends RuntimeException {
64 public final Path file;
65 private final List<? extends Object> messages;
67 public CompilationException(Path file, List<? extends Object> messages) {
69 this.messages = messages;
72 public String getMessage() {
73 return(file + ": compilation failed");
76 public String messages() {
77 StringBuilder buf = new StringBuilder();
78 for(Object msg : messages)
79 buf.append(msg.toString() + "\n");
80 return(buf.toString());
83 public void printStackTrace(PrintStream out) {
84 out.print(messages());
85 super.printStackTrace(out);
89 public static class Compilation implements AutoCloseable {
90 public final Path dir, srcdir, outdir, libdir;
91 private final List<Path> infiles = new ArrayList<>();
92 private final List<Path> classpath = new ArrayList<>();
93 private List<String> output = null;
94 private boolean haslib;
96 public Compilation() throws IOException {
97 dir = Files.createTempDirectory("javac");
98 srcdir = dir.resolve("src");
99 outdir = dir.resolve("out");
100 libdir = dir.resolve("lib");
101 Files.createDirectory(srcdir);
102 Files.createDirectory(outdir);
105 public void add(Path p) {
109 public void classpath(Path p) {
113 public void addlib(String clnm, byte[] contents) throws IOException {
115 Files.createDirectory(libdir);
122 int p2 = clnm.indexOf('.', p1);
125 p = p.resolve(clnm.substring(p1, p2));
126 if(!Files.isDirectory(p))
127 Files.createDirectory(p);
130 p = p.resolve(clnm.substring(p1) + ".class");
131 Files.write(p, contents);
134 public void addlib(Map<String, byte[]> classes) throws IOException {
135 for(Map.Entry<String, byte[]> ent : classes.entrySet())
136 addlib(ent.getKey(), ent.getValue());
139 public boolean compile() throws IOException {
140 List<String> args = new ArrayList<>();
143 args.add(outdir.toString());
144 if(!classpath.isEmpty()) {
145 StringBuilder buf = new StringBuilder();
146 for(Path cp : classpath) {
149 buf.append(cp.toString());
152 args.add(buf.toString());
154 for(Path p : infiles)
155 args.add(p.toString());
156 ProcessBuilder cmd = new ProcessBuilder(args);
157 cmd.redirectErrorStream(true);
158 cmd.redirectOutput(ProcessBuilder.Redirect.PIPE);
159 Process proc = cmd.start();
160 List<String> output = new ArrayList<>();
161 BufferedReader fp = new BufferedReader(new InputStreamReader(proc.getInputStream(), Utils.UTF8));
163 String ln = fp.readLine();
170 status = proc.waitFor();
171 } catch(InterruptedException e) {
172 Thread.currentThread().interrupt();
173 throw(new IOException("compilation interrupted"));
175 this.output = output;
179 public List<String> output() {
181 throw(new IllegalStateException());
185 public Collection<ClassOutput> classes() throws IOException {
186 Collection<ClassOutput> ret = new ArrayList<>();
187 for(Path p : (Iterable<Path>)Files.walk(outdir)::iterator) {
188 Path rel = outdir.relativize(p);
189 String fn = rel.getName(rel.getNameCount() - 1).toString();
190 if(!Files.isRegularFile(p) || !fn.endsWith(".class"))
192 StringBuilder clnm = new StringBuilder();
193 for(int i = 0; i < rel.getNameCount() - 1; i++) {
194 clnm.append(rel.getName(i));
197 clnm.append(fn.substring(0, fn.length() - 6));
198 ClassOutput cls = new ClassOutput(clnm.toString());
199 Files.copy(p, cls.buf);
205 private static void remove(Path p) throws IOException {
206 if(Files.isDirectory(p)) {
207 for(Path ent : (Iterable<Path>)Files.list(p)::iterator)
213 public void close() throws IOException {
218 public static class BufferedClassLoader extends ClassLoader {
219 public final Map<String, byte[]> contents;
221 public BufferedClassLoader(ClassLoader parent, Collection<ClassOutput> contents) {
223 this.contents = new HashMap<>();
224 for(ClassOutput clc : contents)
225 this.contents.put(clc.name, clc.buf.toByteArray());
228 public Class<?> findClass(String name) throws ClassNotFoundException {
229 byte[] c = contents.get(name);
231 throw(new ClassNotFoundException(name));
232 return(defineClass(name, c, 0, c.length));
236 public static class LibClassLoader extends ClassLoader {
237 private final ClassLoader[] classpath;
239 public LibClassLoader(ClassLoader parent, Collection<ClassLoader> classpath) {
241 this.classpath = classpath.toArray(new ClassLoader[0]);
244 public Class<?> findClass(String name) throws ClassNotFoundException {
245 for(ClassLoader lib : classpath) {
247 return(lib.loadClass(name));
248 } catch(ClassNotFoundException e) {}
250 throw(new ClassNotFoundException("Could not find " + name + " in any of " + Arrays.asList(classpath).toString()));
254 public ClassLoader libloader(Path p) {
256 ClassLoader ret = libs.get(p);
259 libs.put(p, ret = new URLClassLoader(new URL[] {p.toUri().toURL()}));
260 } catch(MalformedURLException e) {
261 throw(new RuntimeException(e));
268 private Path findlib(String nm) {
270 Path p = Paths.get(nm);
271 if(Files.isRegularFile(p))
273 } catch(InvalidPathException e) {
275 for(Path dir : searchpath) {
276 Path jar = dir.resolve(nm + ".jar");
277 if(Files.isRegularFile(jar))
283 private static final Pattern classpat = Pattern.compile("^((public|abstract)\\s+)*(class|interface)\\s+(\\S+)");
284 private static final Pattern libpat = Pattern.compile("\\$use\\s*:\\s*(\\S+)");
285 private static final Pattern incpat = Pattern.compile("\\$include\\s*:\\s*(\\S+)");
286 public class Module {
287 public final Path file;
288 public final BufferedClassLoader code;
289 public final Collection<Path> classpath = new ArrayList<>();
290 public final Collection<Module> include = new ArrayList<>();
292 public Module(Path file) throws IOException {
294 try(Compilation c = new Compilation()) {
296 for(Path cp : classpath)
299 throw(new CompilationException(file, c.output()));
300 ClassLoader parent = Compiler.class.getClassLoader();
301 if(!classpath.isEmpty() || !include.isEmpty()) {
302 Collection<ClassLoader> libs = new ArrayList<>();
303 for(Path cp : classpath)
304 libs.add(libloader(cp));
305 for(Module mod : include)
307 parent = new LibClassLoader(parent, libs);
309 code = new BufferedClassLoader(parent, c.classes());
313 public void split(Compilation c) throws IOException {
314 StringBuilder head = new StringBuilder();
315 BufferedWriter cur = null;
316 try(BufferedReader fp = Files.newBufferedReader(file)) {
317 for(String ln = fp.readLine(); ln != null; ln = fp.readLine()) {
318 Matcher m = libpat.matcher(ln);
320 Path lib = findlib(m.group(1));
322 throw(new CompilationException(file, Arrays.asList("no such library: " + m.group(1))));
325 m = incpat.matcher(ln);
327 String nm = m.group(1);
330 Path p = file.resolveSibling(nm);
331 if(Files.isRegularFile(p))
335 Path p = cwd.resolve(nm);
336 if(Files.isRegularFile(p))
340 throw(new CompilationException(file, Arrays.asList("no such file to include: " + nm)));
342 c.addlib(f.mod().code.contents);
343 include.add(f.mod());
345 m = classpat.matcher(ln);
347 String clnm = m.group(4);
348 Path sp = c.srcdir.resolve(clnm + ".java");
352 cur = Files.newBufferedWriter(sp);
356 cur.append(ln); cur.append('\n');
358 head.append(ln); head.append('\n');
369 public final Path name;
370 private FileTime mtime = null;
371 private Module mod = null;
373 private File(Path name) {
377 public void update() throws IOException {
379 FileTime mtime = Files.getLastModifiedTime(name);
380 if((this.mtime == null) || (this.mtime.compareTo(mtime) < 0)) {
381 Module pmod = this.mod;
382 this.mod = new Module(name);
384 if(pmod instanceof AutoCloseable) {
386 ((AutoCloseable)pmod).close();
387 } catch(Exception e) {
388 log.log(Level.WARNING, String.format("Error when disposing updated module %s", pmod.file), e);
395 public Module mod() {
397 throw(new RuntimeException("file has not yet been updated"));
402 public File file(Path name) {
403 synchronized(files) {
404 File ret = files.get(name);
406 files.put(name, ret = new File(name));
411 private static Compiler global = null;
412 public static Compiler get() {
414 synchronized(Compiler.class) {
416 global = new Compiler();