accept multiple file source locations and add verbose switch
[org.ibex.tool.git] / src / java / org / ibex / tool / Compiler.java
index 34b504e..b70859f 100644 (file)
@@ -20,13 +20,12 @@ import org.eclipse.jdt.internal.compiler.impl.*;
 import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
 
 public class Compiler {
-    private static final boolean DEBUG = false;
-
     // Static Entry Point /////////////////////////////////////////////////////
 
     public static void main(String[] args) {
-        String source = null, target = null;
-        String srcdir = null, blddir = null;
+        boolean verbose = false;
+        List srcdir = new ArrayList();
+        String source = null, target = null, blddir = null;
 
         for (int i=0; i < args.length; i++) {
             if (args[i].charAt(0) == '-') {
@@ -52,26 +51,34 @@ public class Compiler {
                             System.out.println("Missing parameter: "+args[i]); return; }
                         target = args[++i];
                         break;
+                    case 'v':
+                        verbose = true;
                     case 'h':
                         printHelp(); return;
 
                 }
-            } else srcdir = args[i];
+            } else srcdir.add(args[i]);
         }
 
         Compiler c = new Compiler();
         if (blddir != null) c.setBuildDir(blddir);
-        if (srcdir != null) c.setSourceDir(srcdir);
         if (source != null) c.setSource(source);
         if (target != null) c.setTarget(target);
+
+        String[] s = new String[srcdir.size()];
+        srcdir.toArray(s);
+        c.setSourceDirs(s);
+
+        c.setVerbose(verbose);
         c.compile();
     }
     private static void printHelp() {
-        System.out.println("Usage java -cp ... org.ibex.tool.Compiler <options> <source directory>");
+        System.out.println("Usage java -cp ... org.ibex.tool.Compiler [options] <source dir> ...");
         System.out.println("Options:");
         System.out.println("  -d <directory>   Location for generated class files.");
         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("  -h or --help     Print this message.");
         System.out.println("");
     }
@@ -87,10 +94,11 @@ public class Compiler {
     private Source[] sources;
 
     private File builddir = new File(".");
-    private String sourcedir = null;
+    private String[] sourcedirs = new String[0];
 
-    private boolean hasErrors;
     private PrintWriter warn;
+    private boolean hasErrors;
+    private boolean verbose = false;
 
     public Compiler() {
         List defs = Collections.EMPTY_LIST;
@@ -107,7 +115,7 @@ public class Compiler {
 
 
     public void setBuildDir(String dir) { builddir = new File(dir == null ? "." : dir); }
-    public void setSourceDir(String dir) { sourcedir = 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); }
@@ -117,10 +125,20 @@ public class Compiler {
 
     public void setBuilddir(File f) { builddir = f; }
 
+    public void setVerbose(boolean v) { verbose = v; }
+
     public void compile() {
         List s = new ArrayList();
-        filterSources(s, new File(sourcedir), new char[0][]);
-        if (DEBUG) System.out.println("working with "+s.size() +" sources");
+        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]);
+                continue;
+            }
+            filterSources(s, src, new char[0][], sourcedirs[i]);
+        }
+
+        if (verbose) 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++)
@@ -179,7 +197,7 @@ public class Compiler {
 
         return bytes;
     }
-    private void filterSources(List s, File dir, char[][] pack) {
+    private void filterSources(List s, File dir, char[][] pack, String srcdir) {
         List bclasses = new ArrayList();
         File bdir = new File(builddir, str(pack, File.separatorChar));
 
@@ -187,7 +205,7 @@ public class Compiler {
         File[] ja = dir.listFiles(filterSrcs);
         for (int i=0; i < ja.length; i++) {
             char[] n = name(classname(ja[i].getName()));
-            Source src = new Source(ja[i], n, pack);
+            Source src = new Source(ja[i], n, pack, srcdir);
 
             // skip the file if the build version is newer than the source
             String bclass = new String(n);
@@ -211,7 +229,8 @@ public class Compiler {
             };
             File[] bfile = bdir.listFiles(f);
             for (int j=0; j < bfile.length; j++) {
-                Source src = new Source(bfile[j], name(classname(bfile[j].getName())), pack);
+                Source src = new Source(bfile[j],
+                    name(classname(bfile[j].getName())), pack, srcdir);
                 src.compiled = bytes(bfile[j]);
                 s.add(src);
             }
@@ -223,7 +242,7 @@ public class Compiler {
             char[][] newpack = new char[pack.length + 1][];
             for (int j=0; j < pack.length; j++) newpack[j] = pack[j];
             newpack[pack.length] = d[i].getName().toCharArray();
-            filterSources(s, d[i], newpack);
+            filterSources(s, d[i], newpack, srcdir);
         }
     }
 
@@ -271,9 +290,9 @@ public class Compiler {
             }
         };
 
-        private Source(File o, char[] n, char[][] p) {
+        private Source(File o, char[] n, char[][] p, String srcdir) {
             orig = o; this.n = n; this.p = p;
-            String file = sourcedir;
+            String file = srcdir;
             file += File.separatorChar +  str(p, File.separatorChar);
             file += File.separatorChar + new String(n) + ".java";
             fileName = file.toCharArray();
@@ -444,11 +463,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 (DEBUG) System.out.println("findType: "+ str(p, '.') + "."+new String(n));
+            if (verbose) System.out.print("findType: "+ str(p, '.') + "."+new String(n));
 
             try {
                 Class c = Class.forName(str(p, '.') + '.' + new String(n));
-                if (DEBUG) System.out.println("  found in classloader: "+ c);
+                if (verbose) 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);
@@ -456,7 +475,7 @@ public class Compiler {
 
             // cut out searches for java.* packages in sources list
             if (p.length > 0 && eq(p[0], "java".toCharArray())) {
-                if (DEBUG) System.out.println("  not found, unknown java.*");
+                if (verbose) System.out.println("  not found, unknown java.*");
                 return null;
             }
 
@@ -465,28 +484,28 @@ public class Compiler {
                     Source s = sources[i];
                     if (eq(n, s.n) && eq(p, s.p)) {
                         if (s.reader != null) {
-                            if (DEBUG) System.out.println("  found reader");
+                            if (verbose) System.out.println("  found reader");
                             return new NameEnvironmentAnswer(s.reader);
                         }
                         if (s.compiled != null) {
-                            if (DEBUG) System.out.println("  found compiled, new reader");
+                            if (verbose) System.out.println("  found compiled, new reader");
                             s.reader = new ClassFileReader(s.compiled,
                                 s.orig.getName().toCharArray(), true);
                             return new NameEnvironmentAnswer(s.reader);
                         }
-                        if (DEBUG) System.out.println("  found unit");
+                        if (verbose) System.out.println("  found unit");
                         return new NameEnvironmentAnswer(sources[i].unit);
                     }
                 }
             } catch (ClassFormatException e) {
                 System.out.println("Unexpected ClassFormatException"); // FIXME
-                e.printStackTrace();
+                e.printStackTrace(); return null;
             }
-            if (DEBUG) System.out.println("  not found");
+            if (verbose) System.out.println("  not found");
             return null;
         }
         public boolean isPackage(char[][] parent, char[] name) {
-            if (DEBUG) System.out.println("isPackage: "+ str(parent, '.') + "."+new String(name));
+            if (verbose) 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;
@@ -515,7 +534,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 (DEBUG) System.out.println("got result: "+result);
+            if (verbose) System.out.println("got result: "+result);
             if (result.hasProblems()) {
                 boolean err = false;
                 IProblem[] p = result.getProblems();
@@ -542,12 +561,10 @@ public class Compiler {
                     File path = builddir;
                     char[][] name = c[i].getCompoundName();
                     path = new File(builddir, str(pack(name), '/'));
-                    if (DEBUG) System.out.println("DEBUG: creating path "+path+", out of builddir="+builddir);
                     if (!path.exists()) path.mkdirs();
 
                     // write new class file
                     path = new File(path, new String(name(name)) + ".class");
-                    if (DEBUG) System.out.println("DEBUG: writing file "+path);
                     OutputStream o = new BufferedOutputStream(
                         new FileOutputStream(path));
                     o.write(c[i].getBytes());