be more specific about compiler options
[org.ibex.tool.git] / src / org / ibex / tool / Compiler.java
index b3970ec..382cf74 100644 (file)
@@ -21,11 +21,13 @@ 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) throws IOException {
-        boolean verbose = false;
+        boolean verbose = false, veryverbose = false, warnings = true;
         List srcdir = new ArrayList();
         String source = null, target = null, blddir = null, mainclass = null;
        boolean buildjar = false;
@@ -43,13 +45,13 @@ public class Compiler {
                     case 'd':
                         if (i == args.length - 1) {
                             System.out.println("Missing parameter: "+args[i]); return; }
-                       if (blddir != null) { System.err.println("cannot specify both -d and -j"); 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.err.println("cannot specify both -d and -j"); return; }
+                       if (blddir != null) { System.out.println("cannot specify both -d and -j"); return; }
                         blddir = args[++i];
                        buildjar = true;
                         break;
@@ -58,6 +60,11 @@ public class Compiler {
                             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) {
                             System.out.println("Missing parameter: "+args[i]); return; }
@@ -71,6 +78,9 @@ public class Compiler {
                     case 'v':
                         verbose = true;
                        break;
+                    case 'V':
+                        veryverbose = true;
+                       break;
                     case 'h':
                         printHelp(); return;
 
@@ -92,6 +102,8 @@ public class Compiler {
         c.setSourceDirs(s);
 
         c.setVerbose(verbose);
+        c.setWarnings(warnings);
+        c.setVeryVerbose(veryverbose);
         c.compile();
     }
     private static void printHelp() {
@@ -103,6 +115,7 @@ public class Compiler {
         System.out.println("  -s <release>     Compile with specified source compatibility.");
         System.out.println("  -t <release>     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("");
     }
@@ -127,6 +140,8 @@ public class Compiler {
     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;
@@ -153,12 +168,28 @@ public class Compiler {
     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 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() throws IOException {
         List s = new ArrayList();
@@ -172,13 +203,13 @@ public class Compiler {
         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++)
@@ -200,13 +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() {
@@ -522,11 +554,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);
@@ -534,7 +566,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;
             }
 
@@ -543,16 +575,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);
                     }
                 }
@@ -560,11 +592,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;
@@ -586,14 +618,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();
@@ -617,6 +649,19 @@ public class Compiler {
             for (int i=0; i < c.length; i++) {
                 try {
                    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"));
@@ -624,7 +669,7 @@ public class Compiler {
                        } catch (java.util.zip.ZipException ze) {
                            // HACK: horrendous hack
                            if (ze.getMessage().indexOf("duplicate entry") != -1) {
-                               System.err.println("compiler emitted class twice: " + new String(name(name)));
+                               //System.out.println("compiler emitted class twice: " + new String(name(name)));
                            } else {
                                throw ze;
                            }
@@ -647,6 +692,9 @@ public class Compiler {
         }
     };
 
+    int percent = 0;
+    String clearing = "";
+
     /** Problem creater for compiler. */
     private final IProblemFactory problems = new DefaultProblemFactory();