X-Git-Url: http://git.megacz.com/?p=org.ibex.tool.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Ftool%2FCompiler.java;h=5dad538f3935f5aecbe7944a431e7088bfa7eba2;hp=532fb846f23d58b605bf124b71590e3ac6369f34;hb=61d00b0566029b7f0eaa7cc4d2932ac764e5e1f9;hpb=972548f2d156478714aab6c3cef9ed71628e025b diff --git a/src/org/ibex/tool/Compiler.java b/src/org/ibex/tool/Compiler.java index 532fb84..5dad538 100644 --- a/src/org/ibex/tool/Compiler.java +++ b/src/org/ibex/tool/Compiler.java @@ -1,7 +1,9 @@ package org.ibex.tool; -import java.util.*; import java.io.*; +import java.util.*; +import java.util.zip.*; +import java.util.jar.*; import java.lang.reflect.*; @@ -19,13 +21,16 @@ import org.eclipse.jdt.internal.compiler.env.*; import org.eclipse.jdt.internal.compiler.impl.*; import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; +// FEATURE: add build dependencies like make (grab some Java dependency-checker code off the net?) + public class Compiler { // Static Entry Point ///////////////////////////////////////////////////// - public static void main(String[] args) { - boolean verbose = false; + public static void main(String[] args) throws IOException { + boolean verbose = false, veryverbose = false, warnings = true; List srcdir = new ArrayList(); - String source = null, target = null, blddir = null; + String source = null, target = null, blddir = null, mainclass = null; + boolean buildjar = false; if (args.length == 0) args = new String[] { "--help" }; for (int i=0; i < args.length; i++) { @@ -40,7 +45,25 @@ public class Compiler { case 'd': if (i == args.length - 1) { System.out.println("Missing parameter: "+args[i]); return; } + if (blddir != null) { System.out.println("cannot specify both -d and -j"); return; } + blddir = args[++i]; + break; + case 'j': + if (i == args.length - 1) { + System.out.println("Missing parameter: "+args[i]); return; } + if (blddir != null) { System.out.println("cannot specify both -d and -j"); return; } blddir = args[++i]; + buildjar = true; + break; + case 'm': + if (i == args.length - 1) { + System.out.println("Missing parameter: "+args[i]); return; } + mainclass = args[++i]; + break; + case 'w': + if (i == args.length - 1) { + System.out.println("Missing parameter: "+args[i]); return; } + warnings = false; break; case 's': if (i == args.length - 1) { @@ -54,6 +77,10 @@ public class Compiler { break; case 'v': verbose = true; + break; + case 'V': + veryverbose = true; + break; case 'h': printHelp(); return; @@ -62,24 +89,33 @@ public class Compiler { } Compiler c = new Compiler(); - if (blddir != null) c.setBuildDir(blddir); + if (blddir != null) { + if (buildjar) c.setBuildJar(blddir); + else c.setBuildDir(blddir); + } if (source != null) c.setSource(source); if (target != null) c.setTarget(target); + if (mainclass != null) c.setMainClass(mainclass); String[] s = new String[srcdir.size()]; srcdir.toArray(s); c.setSourceDirs(s); c.setVerbose(verbose); + c.setWarnings(warnings); + c.setVeryVerbose(veryverbose); c.compile(); } private static void printHelp() { System.out.println("Usage java -cp ... org.ibex.tool.Compiler [options] ..."); System.out.println("Options:"); - System.out.println(" -d Location for generated class files."); + System.out.println(" -d Directory to put generated class files in"); + System.out.println(" -j Jar file to put generated class files in"); + System.out.println(" -m Main-Class for jar file."); System.out.println(" -s Compile with specified source compatibility."); System.out.println(" -t Compile with specified class file compatibility."); System.out.println(" -v Verbose output."); + System.out.println(" -V Very Verbose output."); System.out.println(" -h or --help Print this message."); System.out.println(""); } @@ -94,12 +130,18 @@ public class Compiler { private Source[] sources; - private File builddir = new File("."); + private File builddir = null; + private boolean buildzip = false; + private ZipOutputStream jarfile = null; + private String mainclass = null; + private String[] sourcedirs = new String[0]; private PrintWriter warn; private boolean hasErrors; private boolean verbose = false; + private boolean veryverbose = false; + private boolean warnings = true; public Compiler() { List defs = Collections.EMPTY_LIST; @@ -114,32 +156,60 @@ public class Compiler { preprocessor = new Preprocessor(null, null, defs); } + public void setMainClass(String mainclass) { this.mainclass = mainclass; } + public void setBuildJar(String dir) throws IOException { + builddir = new File(dir == null ? "." : dir); + buildzip = true; + } + public void setBuildDir(String dir) throws IOException { + builddir = new File(dir == null ? "." : dir); + } - public void setBuildDir(String dir) { builddir = new File(dir == null ? "." : dir); } public void setSourceDirs(String[] dir) { sourcedirs = dir; } /** Pass CompilerOptions.VERSION_1_*. A String of form "1.1", ".2", etc. */ - public void setSource(String v) { settings.put(CompilerOptions.OPTION_Source, v); } + public void setSource(String v) { + if (v.equals("1.1")) settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_1); + else if (v.equals("1.2")) settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_2); + else if (v.equals("1.3")) settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_3); + else if (v.equals("1.4")) settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_4); + else if (v.equals("1.5")) settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_5); + else throw new RuntimeException("I have no idea what Java " + v + " is. Ask David Crawshaw."); + } /** Pass CompilerOptions.VERSION_1_*. A String of form "1.1", ".2", etc. */ - public void setTarget(String v) { settings.put(CompilerOptions.OPTION_TargetPlatform, v); } - - public void setBuilddir(File f) { builddir = f; } + public void setTarget(String v) { + if (v.equals("1.1")) settings.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_1); + else if (v.equals("1.2")) settings.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_2); + else if (v.equals("1.3")) settings.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_3); + else if (v.equals("1.4")) settings.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_4); + else if (v.equals("1.5")) settings.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_5); + else throw new RuntimeException("I have no idea what Java " + v + " is. Ask David Crawshaw."); + } public void setVerbose(boolean v) { verbose = v; } + public void setVeryVerbose(boolean v) { veryverbose = v; } + public void setWarnings(boolean w) { warnings = w; } - public void compile() { + public void compile() throws IOException { List s = new ArrayList(); + + if (buildzip) { + Manifest m = new Manifest(); + m.getMainAttributes().putValue("Manifest-Version", "1.0"); + if (mainclass != null) m.getMainAttributes().putValue("Main-Class", mainclass); + jarfile = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(builddir)), m); + } for (int i=0; i < sourcedirs.length; i++) { File src = new File(sourcedirs[i]); if (!src.exists()) { - if (verbose) System.err.println("source directory not found: "+sourcedirs[i]); + if (veryverbose) System.out.println("source directory not found: "+sourcedirs[i]); continue; } filterSources(s, src, new char[0][], sourcedirs[i]); } - if (verbose) System.out.println("working with "+s.size() +" sources"); + if (veryverbose) System.out.println("working with "+s.size() +" sources"); sources = new Source[s.size()]; s.toArray(sources); ICompilationUnit[] units = new ICompilationUnit[s.size()]; int u = 0; for (int i=0; i < sources.length; i++) @@ -161,8 +231,14 @@ public class Compiler { env, policy, settings, results, problems); jdt.compile(units); - if (!hasErrors) { out.write(w.toString()); out.flush(); } + if (warnings && !hasErrors) { out.write(w.toString()); out.flush(); } warn = null; + try { + if (jarfile != null) jarfile.close(); + } catch (Exception e) { + e.printStackTrace(); + } + if (verbose) System.out.print(clearing + " \r"); } private final FilenameFilter filterSrcs = new FilenameFilter() { @@ -171,33 +247,7 @@ public class Compiler { private final FileFilter filterDirs = new FileFilter() { public boolean accept(File path) { return path.isDirectory(); } }; - private byte[] bytes(File bfile) { - byte[] bytes = new byte[2 * 1024]; - int pos = 0, in = 0; - try { - InputStream bin = new FileInputStream(bfile); - while ((in = bin.read(bytes, pos, bytes.length - pos)) != -1) { - pos += in; - if (pos == bytes.length) { - byte[] newbytes= new byte[pos * 2]; - System.arraycopy(bytes, 0, newbytes, 0, pos); - bytes = newbytes; - } - } - bin.close(); - } catch (IOException e) { - System.out.println("Error reading class file"); // FIXME - e.printStackTrace(); return null; - } - if (pos != bytes.length) { - byte[] newbytes= new byte[pos * 2]; - System.arraycopy(bytes, 0, newbytes, 0, pos); - bytes = newbytes; - } - - return bytes; - } private void filterSources(List s, File dir, char[][] pack, String srcdir) { List bclasses = new ArrayList(); File bdir = new File(builddir, str(pack, File.separatorChar)); @@ -309,188 +359,35 @@ public class Compiler { } } - // ClassLoader Wrappers /////////////////////////////////////////////////// - - final static class LoadedNestedType implements IBinaryNestedType { - private Class c; - LoadedNestedType(Class c) { this.c = c; } - public char[] getName() { return name(c); } - public char[] getEnclosingTypeName() { return name(c.getDeclaringClass()); } - public int getModifiers() { return c.getModifiers(); } - } - - final static class LoadedField implements IBinaryField { - private Field f; - private Constant c; - - LoadedField(Field f) { - this.f = f; - int m = f.getModifiers(); - - c = Constant.NotAConstant; - if (Modifier.isFinal(m) && Modifier.isStatic(m)) { - try { - Class type = f.getType(); - if (type == Boolean.TYPE) { - c = new BooleanConstant(f.getBoolean(null)); - } else if (type == Byte.TYPE) { - c = new ByteConstant(f.getByte(null)); - } else if (type == Character.TYPE) { - c = new CharConstant(f.getChar(null)); - } else if (type == Double.TYPE) { - c = new DoubleConstant(f.getDouble(null)); - } else if (type == Float.TYPE) { - c = new FloatConstant(f.getFloat(null)); - } else if (type == Integer.TYPE) { - c = new IntConstant(f.getInt(null)); - } else if (type == Long.TYPE) { - c = new LongConstant(f.getLong(null)); - } else if (type == Short.TYPE) { - c = new ShortConstant(f.getShort(null)); - } else if (type == String.class) { - c = new StringConstant((String)f.get(null)); - } - } catch (IllegalAccessException e) {} - } - } - public Constant getConstant() { return c; } - public char[] getTypeName() { return typeName(f.getType()); } - public char[] getName() { return f.getName().toCharArray(); } - public int getModifiers() { return f.getModifiers(); } - } - - final static class LoadedConstructor implements IBinaryMethod { - private Constructor c; - private char[][] ex; - private char[] desc; - - LoadedConstructor(Constructor c) { - this.c = c; - - Class[] exs = c.getExceptionTypes(); - ex = new char[exs.length][]; - for (int i=0; i < ex.length; i++) ex[i] = name(exs[i]); - - desc = descriptor(c.getParameterTypes(), null); - } - public int getModifiers() { return c.getModifiers(); } - public char[] getSelector() { return c.getName().toCharArray(); } - public boolean isConstructor() { return true; } - public boolean isClinit() { return false; } - public char[][] getArgumentNames() { return null; } - public char[][] getExceptionTypeNames() { return ex; } - public char[] getMethodDescriptor() { return desc; } - } - - final static class LoadedMethod implements IBinaryMethod { - private Method m; - private char[][] ex; - private char[] desc; - - LoadedMethod(Method m) { - this.m = m; - - Class[] exs = m.getExceptionTypes(); - ex = new char[exs.length][]; - for (int i=0; i < ex.length; i++) ex[i] = name(exs[i]); - - desc = descriptor(m.getParameterTypes(), m.getReturnType()); - } - public int getModifiers() { return m.getModifiers(); } - public char[] getSelector() { return m.getName().toCharArray(); } - public boolean isConstructor() { return false; } - public boolean isClinit() { return false; } - public char[][] getArgumentNames() { return null; } // FEATURE: does this do anything cool? - public char[][] getExceptionTypeNames() { return ex; } - public char[] getMethodDescriptor() { return desc; } - } - - final static class LoadedClass implements IBinaryType { - private Class c; - private IBinaryField[] f; - private char[][] inf; - private IBinaryNestedType[] nested; - private IBinaryMethod[] meth = null; - - LoadedClass(Class c) { - this.c = c; - - Field[] fields = c.getDeclaredFields(); - List flist = new ArrayList(fields.length); - for (int i=0; i < fields.length; i++) - if (!Modifier.isPrivate(fields[i].getModifiers())) - flist.add(new LoadedField(fields[i])); - f = new IBinaryField[flist.size()]; - flist.toArray(f); - - Class[] interfaces = c.getInterfaces(); - inf = new char[interfaces.length][]; - for (int i=0; i < inf.length; i++) inf[i] = name(interfaces[i]); - - Class[] classes = c.getClasses(); - nested = new IBinaryNestedType[classes.length]; - for (int i=0; i < nested.length; i++) - nested[i] = new LoadedNestedType(classes[i]); - - Constructor[] constructors = c.getDeclaredConstructors(); - Method[] methods = c.getDeclaredMethods(); - if (methods.length + constructors.length > 0) { - List m = new ArrayList(methods.length + constructors.length); - for (int j=0; j < methods.length; j++) - if (!Modifier.isPrivate(methods[j].getModifiers())) - m.add(new LoadedMethod(methods[j])); - for (int j=0; j < constructors.length; j++) - if (!Modifier.isPrivate(constructors[j].getModifiers())) - m.add(new LoadedConstructor(constructors[j])); - meth = new IBinaryMethod[m.size()]; m.toArray(meth); - } - } - - public char[] getName() { return name(c); } - public char[] getEnclosingTypeName() { return name(c.getDeclaringClass()); } - public char[] getSuperclassName() { return name(c.getSuperclass()); } - public IBinaryField[] getFields() { return f; } - public char[][] getInterfaceNames() { return inf; } - public IBinaryNestedType[] getMemberTypes() { return nested; } - public IBinaryMethod[] getMethods() { return meth; } - public int getModifiers() { return c.getModifiers(); } - - public boolean isBinaryType() { return true; } - public boolean isClass() { return true; } - public boolean isInterface() { return c.isInterface(); } - public boolean isAnonymous() { return false; } - public boolean isLocal() { return false; } // FIXME - public boolean isMember() { return false; } // FIXME - public char[] sourceFileName() { return null; } - public char[] getFileName() { return null; } - - public boolean equals(Object o) { - return o == this || super.equals(o) || - (o != null && o instanceof LoadedClass && - c.equals(((LoadedClass)o).c)); - } - } - - // Compiler Parameters //////////////////////////////////////////////////// + // FEATURE: may be able to use this to block access to APIs generated for stack objects + final AccessRestriction access = null; + /** Used by compiler to resolve classes. */ final INameEnvironment env = new INameEnvironment() { public NameEnvironmentAnswer findType(char[][] c) { return findType(name(c), pack(c)); } public NameEnvironmentAnswer findType(char[] n, char[][] p) { - if (verbose) System.out.print("findType: "+ str(p, '.') + "."+new String(n)); + if (veryverbose) System.out.print("findType: "+ str(p, '.') + "."+new String(n)); try { - Class c = Class.forName(str(p, '.') + '.' + new String(n)); - if (verbose) System.out.println(" found in classloader: "+ c); + String classname = new String(n); + Class c = Class.forName(str(p, '.') + '.' + classname); + if (veryverbose) System.out.println(" found in classloader: "+ c); IBinaryType b = (IBinaryType)loaded.get(c); - if (b == null) loaded.put(c, b = new LoadedClass(c)); - return new NameEnvironmentAnswer(b); - } catch (ClassNotFoundException e) {} + if (b == null) loaded.put(c, b = new ClassFileReader( + bytes(c), (str(p, '/') + '/' + classname).toCharArray())); + return new NameEnvironmentAnswer(b, access); + } catch (ClassNotFoundException e) { + } catch (ClassFormatException e) { + e.printStackTrace(); + throw new Error("ClassFormatException reading system class: " + + str(p, '.')+new String(n)); + } // cut out searches for java.* packages in sources list if (p.length > 0 && eq(p[0], "java".toCharArray())) { - if (verbose) System.out.println(" not found, unknown java.*"); + if (veryverbose) System.out.println(" not found, unknown java.*"); return null; } @@ -499,28 +396,28 @@ public class Compiler { Source s = sources[i]; if (eq(n, s.n) && eq(p, s.p)) { if (s.reader != null) { - if (verbose) System.out.println(" found reader"); - return new NameEnvironmentAnswer(s.reader); + if (veryverbose) System.out.println(" found reader"); + return new NameEnvironmentAnswer(s.reader, access); } if (s.compiled != null) { - if (verbose) System.out.println(" found compiled, new reader"); + if (veryverbose) System.out.println(" found compiled, new reader"); s.reader = new ClassFileReader(s.compiled, - s.orig.getName().toCharArray(), true); - return new NameEnvironmentAnswer(s.reader); + s.orig.getName().toCharArray()); + return new NameEnvironmentAnswer(s.reader, access); } - if (verbose) System.out.println(" found unit"); - return new NameEnvironmentAnswer(sources[i].unit); + if (veryverbose) System.out.println(" found unit"); + return new NameEnvironmentAnswer(sources[i].unit, access); } } } catch (ClassFormatException e) { System.out.println("Unexpected ClassFormatException"); // FIXME e.printStackTrace(); return null; } - if (verbose) System.out.println(" not found"); + if (veryverbose) System.out.println(" not found"); return null; } public boolean isPackage(char[][] parent, char[] name) { - if (verbose) System.out.println("isPackage: "+ str(parent, '.') + "."+new String(name)); + if (veryverbose) System.out.println("isPackage: "+ str(parent, '.') + "."+new String(name)); String parentName = str(parent, '/'); for (int i=0; i < sources.length; i++) { if (eq(name, sources[i].n) && eq(parent, sources[i].p)) return false; @@ -542,14 +439,14 @@ public class Compiler { { settings.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.GENERATE); settings.put(CompilerOptions.OPTION_SourceFileAttribute, CompilerOptions.GENERATE); - settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_3); + settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_5); settings.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_1); }; /** Used by compiler for processing compiled classes and their errors. */ private final ICompilerRequestor results = new ICompilerRequestor() { public void acceptResult(CompilationResult result) { - if (verbose) System.out.println("got result: "+result); + if (veryverbose) System.out.println("got result: "+result); if (result.hasProblems()) { boolean err = false; IProblem[] p = result.getProblems(); @@ -572,18 +469,42 @@ public class Compiler { ClassFile[] c = result.getClassFiles(); for (int i=0; i < c.length; i++) { try { - // build package path to new class file - File path = builddir; - char[][] name = c[i].getCompoundName(); - path = new File(builddir, str(pack(name), '/')); - if (!path.exists()) path.mkdirs(); - - // write new class file - path = new File(path, new String(name(name)) + ".class"); - OutputStream o = new BufferedOutputStream( - new FileOutputStream(path)); - o.write(c[i].getBytes()); - o.close(); + char[][] name = c[i].getCompoundName(); + if (verbose) { + String pct = ""; + /*+ percent; + percent++; + while (pct.length() < 2) pct = " " + pct; + pct = "(" + pct + "%) "; + */ + String printme = " writing: " + pct + str(pack(name),'.') + "." + new String(name(name)); + if (clearing.length() < printme.length()) clearing = ""; + else clearing = clearing.substring(printme.length()); + System.out.print(printme + clearing + "\r"); + for(clearing = ""; clearing.length() < printme.length() + 5; clearing += " "); + } + if (buildzip) { + try { + jarfile.putNextEntry(new ZipEntry(str(pack(name), '/') + "/" + new String(name(name)) + ".class")); + jarfile.write(c[i].getBytes()); + } catch (java.util.zip.ZipException ze) { + // HACK: horrendous hack + if (ze.getMessage().indexOf("duplicate entry") != -1) { + //System.out.println("compiler emitted class twice: " + new String(name(name))); + } else { + throw ze; + } + } + } else { + // build package path to new class file + File path = new File(builddir, str(pack(name), '/')); + if (!path.exists()) path.mkdirs(); + // write new class file + path = new File(path, new String(name(name)) + ".class"); + OutputStream o = new BufferedOutputStream(new FileOutputStream(path)); + o.write(c[i].getBytes()); + o.close(); + } } catch (IOException e) { System.out.println("IOException writing class"); // FIXME e.printStackTrace(); @@ -592,6 +513,9 @@ public class Compiler { } }; + int percent = 0; + String clearing = ""; + /** Problem creater for compiler. */ private final IProblemFactory problems = new DefaultProblemFactory(); @@ -600,7 +524,7 @@ public class Compiler { /** Convert source file path into class name block. * eg. in: java/lang/String.java -> { {java} {lang} {String} } */ - private char[][] classname(String name) { + private static char[][] classname(String name) { String delim = name.indexOf('/') == -1 ? "." : "/"; name = name.trim(); if (name.endsWith(".java")) name = name.substring(0, name.length() - 5); @@ -698,4 +622,37 @@ public class Compiler { return sb.toString().toCharArray(); } + private static byte[] bytes(Class c) { + return bytes(c.getResourceAsStream('/' + c.getName().replace('.', '/') + ".class")); + } + private static byte[] bytes(File bfile) { + try { return bytes(new FileInputStream(bfile)); } + catch (FileNotFoundException e) { throw new Error("FileNotFoundException reading bytes: "+e); } + } + private static byte[] bytes(InputStream bin) { + byte[] bytes = new byte[2 * 1024]; + int pos = 0, in = 0; + try { + while ((in = bin.read(bytes, pos, bytes.length - pos)) != -1) { + pos += in; + if (pos == bytes.length) { + byte[] newbytes= new byte[pos * 2]; + System.arraycopy(bytes, 0, newbytes, 0, pos); + bytes = newbytes; + } + } + bin.close(); + } catch (IOException e) { + e.printStackTrace(); + throw new Error("IOException reading class file: "+e); + } + + if (pos != bytes.length) { + byte[] newbytes= new byte[pos * 2]; + System.arraycopy(bytes, 0, newbytes, 0, pos); + bytes = newbytes; + } + + return bytes; + } }