From 0f66c7daed3f1707588bc34248d2c1e2193502b3 Mon Sep 17 00:00:00 2001 From: adam Date: Mon, 27 Dec 2004 03:50:50 +0000 Subject: [PATCH] added support for -j and -m darcs-hash:20041227035050-5007d-73cda0dda469d261ee9bfa148fe5d4071e0ca122.gz --- src/org/ibex/tool/Compiler.java | 96 +++++++++++++++++++++++++++++++-------- 1 file changed, 76 insertions(+), 20 deletions(-) diff --git a/src/org/ibex/tool/Compiler.java b/src/org/ibex/tool/Compiler.java index 126837d..b3970ec 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,10 +24,11 @@ import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; public class Compiler { // Static Entry Point ///////////////////////////////////////////////////// - public static void main(String[] args) { + public static void main(String[] args) throws IOException { boolean verbose = 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++) { @@ -40,8 +43,21 @@ 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; } 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; } + 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) { System.out.println("Missing parameter: "+args[i]); return; } @@ -63,9 +79,13 @@ 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); @@ -77,7 +97,9 @@ public class Compiler { 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."); @@ -95,7 +117,11 @@ 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; @@ -115,8 +141,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. */ @@ -127,8 +160,15 @@ public class Compiler { public void setVerbose(boolean v) { verbose = 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()) { @@ -162,6 +202,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() { @@ -571,18 +616,29 @@ 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 (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.err.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(); -- 1.7.10.4