X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Ftool%2FCompiler.java;h=d72d33f9c00acd3f84bceedf717f854613e1677e;hb=2f80ab353242a3fd6d4103097f403b31ca35835a;hp=4ce113c8578eba8c6d7f89c53c58e31ffd13b2f3;hpb=39f5e48cce9a5043f966615072ccc02f254ebfaf;p=org.ibex.tool.git diff --git a/src/org/ibex/tool/Compiler.java b/src/org/ibex/tool/Compiler.java index 4ce113c..d72d33f 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.*; @@ -22,11 +24,13 @@ import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; 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; 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++) { if (args[i].charAt(0) == '-') { if (args[i].length() == 1) { @@ -39,7 +43,20 @@ 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 's': if (i == args.length - 1) { @@ -53,6 +70,10 @@ public class Compiler { break; case 'v': verbose = true; + break; + case 'V': + veryverbose = true; + break; case 'h': printHelp(); return; @@ -61,24 +82,32 @@ 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.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(""); } @@ -93,12 +122,17 @@ 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; public Compiler() { List defs = Collections.EMPTY_LIST; @@ -113,8 +147,15 @@ 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. */ @@ -123,22 +164,28 @@ public class Compiler { /** 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 setVerbose(boolean v) { verbose = v; } + public void setVeryVerbose(boolean v) { veryverbose = v; } - 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++) @@ -162,6 +209,11 @@ public class Compiler { if (!hasErrors) { out.write(w.toString()); out.flush(); } warn = null; + try { + if (jarfile != null) jarfile.close(); + } catch (Exception e) { + e.printStackTrace(); + } } private final FilenameFilter filterSrcs = new FilenameFilter() { @@ -477,11 +529,11 @@ public class Compiler { 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); + 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); @@ -489,7 +541,7 @@ public class Compiler { // 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; } @@ -498,16 +550,16 @@ 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"); + if (veryverbose) System.out.println(" found reader"); return new NameEnvironmentAnswer(s.reader); } 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); } - if (verbose) System.out.println(" found unit"); + if (veryverbose) System.out.println(" found unit"); return new NameEnvironmentAnswer(sources[i].unit); } } @@ -515,11 +567,11 @@ public class Compiler { 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; @@ -548,7 +600,7 @@ public class Compiler { /** 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(); @@ -569,20 +621,45 @@ public class Compiler { } ClassFile[] c = result.getClassFiles(); + String clearing = "\r"; 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(0, clearing.length() - printme.length()); + System.out.print(printme + clearing + "\r"); + for(clearing = ""; clearing.length() < printme.length() + 2; 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(); @@ -591,6 +668,8 @@ public class Compiler { } }; + int percent = 0; + /** Problem creater for compiler. */ private final IProblemFactory problems = new DefaultProblemFactory();