001: package nice.tools.ant;
002:
003: import org.apache.tools.ant.*;
004: import org.apache.tools.ant.types.*;
005: import java.io.File;
006: import java.util.Vector;
007: import bossa.modules.Compilation;
008:
009: /**
010: <h2><a name="java">Nicec</a></h2>
011: <h3>Description</h3>
012: <p>Runs the Nice compiler.</p>
013: All arguments to the Nice compiler Task has to be placed as attributes in the nicec xml-element.
014: <h3>Parameters</h3>
015: <table border="1" cellpadding="2" cellspacing="0">
016: <tr>
017: <td valign="top"><b>Attribute</b></td>
018: <td valign="top"><b>Description</b></td>
019: <td align="center" valign="top"><b>Required</b></td>
020: </tr>
021: <tr>
022: <td valign="top">package</td>
023: <td valign="top">The Nice package to compile.</td>
024: <td align="center" valign="top">Yes</td>
025: </tr>
026: <tr>
027: <td valign="top">jar</td>
028: <td valign="top">Compile the Nice sources to archive.</td>
029: <td align="center" valign="top">No</td>
030: </tr>
031: <tr>
032: <td valign="top">sourcepath</td>
033: <td valign="top">Search path for source packages. Is a list of directories and .jar archives.</td>
034: <td align="center" valign="top">No</td>
035: </tr>
036: <tr>
037: <td valign="top">destination</td>
038: <td valign="top">Destination directory for compiled packages.</td>
039: <td align="center" valign="top">No</td>
040: </tr>
041: <tr>
042: <td valign="top">classpath</td>
043: <td valign="top">Search path for compiled packages and libraries.</td>
044: <td align="center" valign="top">No</td>
045: </tr>
046: <tr>
047: <td valign="top">output</td>
048: <td valign="top">Generate native executable.</td>
049: <td align="center" valign="top">No</td>
050: </tr>
051: <tr>
052: <td valign="top">compile</td>
053: <td valign="top">Compile packages but do not link them.</td>
054: <td align="center" valign="top">No</td>
055: </tr>
056: <tr>
057: <td valign="top">recompile</td>
058: <td valign="top">Force recompilation of package.</td>
059: <td align="center" valign="top">No</td>
060: </tr>
061: <tr>
062: <td valign="top">recompile_all</td>
063: <td valign="top">Force recompilation of all dependant packages.</td>
064: <td align="center" valign="top">No</td>
065: </tr>
066: <tr>
067: <td valign="top">exclude_runtime</td>
068: <td valign="top">Avoid inclusion of the runtime in the archive.</td>
069: <td align="center" valign="top">No</td>
070: </tr>
071: <tr>
072: <td valign="top">runtime</td>
073: <td valign="top">Location of nice.jar.</td>
074: <td align="center" valign="top">Yes</td>
075: </tr>
076: <tr>
077: <td valign="top">native_compiler</td>
078: <td valign="top">Location of the native compiler binary (gcj).</td>
079: <td align="center" valign="top">No</td>
080: </tr>
081: <tr>
082: <td valign="top">editor</td>
083: <td valign="top">Tell nicec that it is called by an editor.</td>
084: <td align="center" valign="top">No</td>
085: </tr>
086: </table>
087:
088: <h4>classpath</h4>
089: <p><code>Nicec</code>'s <i>classpath</i> attribute is a PATH like structure and can also be set via a nested
090: <i>classpath</i> element. This is very reasonable if you want to make your build script's pathes platform
091: independent. </p>
092: <h5>Example</h5>
093: <pre>
094: <nicec package="test" >
095: <classpath>
096: <pathelement location="\test.jar"/>
097: <pathelement path="${java.class.path}"/>
098: </classpath>
099: </java>
100: </pre>
101: <p>It is possible to use the <i>classpath</i> attribute together with the
102: <i>classpath<i> nested tag. In this case the result is a concatenated path.</p>
103: <p>It is highly recommended to use the nested version!<p>
104:
105:
106: <h3>Examples</h3>
107: <pre>
108: <taskdef name="nicec" classname="nice.tools.ant.Nicec"/>
109: <target name="nice-compiler">
110: <nicec package="test" runtime="../share/java/nice.jar"/>
111: </target>
112: </pre>
113:
114:
115: * @author Alex Greif <a href="mailto:alex.greif@web.de">alex.greif@web.de</a>
116: */
117:
118: public class Nicec extends Task {
119:
120: private static final String ERROR_MSG = "Compilation failed with errors.";
121: private static final String BUG_MSG = "Compilation failed because of a bug in the compiler.";
122: private static final String WARNING_MSG = "Compilation successful despite warnings.";
123: private static final String OK_MSG = "Compilation successful.";
124:
125: /** Search path for source packages
126: PATH is a list of directories and .jar archives
127: */
128: private String sourcepath;
129:
130: public void setSourcepath(String sourcepath) {
131: this .sourcepath = sourcepath;
132: }
133:
134: /** Destination directory for compiled packages
135: */
136: private File destination;
137:
138: public void setDestination(File destination) {
139: this .destination = destination;
140: }
141:
142: /** Search path for compiled packages and libraries
143: */
144: private String classpath = "";
145:
146: public void setClasspath(String classpath) {
147: this .classpath = classpath;
148: }
149:
150: /** Compile to archive
151: You can then run the program with 'java -jar FILE'
152: */
153: private String jar;
154:
155: public void setJar(String jar) {
156: this .jar = jar;
157: }
158:
159: /** Generate native executable
160: */
161: private String output;
162:
163: public void setOutput(String output) {
164: this .output = output;
165: }
166:
167: /** Force recompilation of package
168: */
169: private boolean recompile;
170:
171: public void setRecompile(boolean recompile) {
172: this .recompile = recompile;
173: }
174:
175: /** Force recompilation of all dependant packages
176: */
177: private boolean recompile_all;
178:
179: public void setRecompile_all(boolean recompile_all) {
180: this .recompile_all = recompile_all;
181: }
182:
183: /** Compile packages but do not link them
184: */
185: private boolean compile;
186:
187: public void setCompile(boolean compile) {
188: this .compile = compile;
189: }
190:
191: /** Avoid inclusion of the runtime in the archive
192: */
193: private boolean exclude_runtime;
194:
195: public void setExclude_runtime(boolean exclude_runtime) {
196: this .exclude_runtime = exclude_runtime;
197: }
198:
199: /** Location of nice.jar
200: */
201: private String runtime = null;
202:
203: public void setRuntime(String runtime) {
204: this .runtime = runtime;
205: }
206:
207: /** Location of the native compiler binary (gcj)
208: */
209: private String native_compiler;
210:
211: public void setNative_compiler(String native_compiler) {
212: this .native_compiler = native_compiler;
213: }
214:
215: /** Tell nicec that it is called by an editor.
216: */
217: private boolean editor;
218:
219: public void setEditor(boolean editor) {
220: this .editor = editor;
221: }
222:
223: /** The package to compile
224: */
225: private String pack;
226:
227: public void setPackage(String pack) {
228: this .pack = pack;
229: }
230:
231: private Path nestedClasspath = null;
232:
233: /**
234: * Creates a nested classpath element
235: */
236: public Path createClasspath() {
237: nestedClasspath = new Path(project);
238: return nestedClasspath.createPath();
239: }
240:
241: /** Executes the ant Nice compiler.
242: */
243: public void execute() throws BuildException {
244: log("runtime: " + runtime, Project.MSG_VERBOSE);
245: log("Base dir: " + project.getBaseDir(), Project.MSG_VERBOSE);
246:
247: String oldUserDir = System.getProperty("user.dir");
248: try {
249: System.setProperty("user.dir", project.getBaseDir()
250: .getAbsolutePath());
251:
252: NicecListener listener = new NicecListener(this );
253: Compilation compilation = bossa.modules.fun
254: .createCompilation(listener,
255: new bossa.parser.JavaccParser(false));
256: if (sourcepath != null)
257: compilation.sourcePath = sourcepath;
258: if (destination != null)
259: compilation.destinationDir = destination
260: .getAbsolutePath();
261: compilation.packagePath = classpath
262: + (nestedClasspath != null ? File.pathSeparator
263: + nestedClasspath : "");
264: compilation.output = jar;
265: compilation.recompileCommandLine = recompile;
266: compilation.recompileAll = recompile_all;
267: compilation.skipLink = compile;
268: compilation.excludeRuntime = exclude_runtime;
269: compilation.runtimeFile = runtime;
270: nice.tools.compiler.fun.compile(compilation, pack, output,
271: native_compiler, editor);
272: int retval = listener.statusCode;
273:
274: switch (retval) {
275: case nice.tools.compiler.fun.ERROR:
276: throw new BuildException(ERROR_MSG, location);
277: case nice.tools.compiler.fun.BUG:
278: throw new BuildException(BUG_MSG, location);
279: case nice.tools.compiler.fun.WARNING:
280: log(WARNING_MSG, Project.MSG_WARN);
281: break;
282: case nice.tools.compiler.fun.OK:
283: log(OK_MSG, Project.MSG_INFO);
284: break;
285: }
286: } finally {
287: System.setProperty("user.dir", oldUserDir);
288: }
289: }
290:
291: /** Only for test usage.
292: */
293: public static void main(String[] args) {
294: Nicec nicec = new Nicec();
295: nicec.setRuntime("../share/java/nice.jar");
296: nicec.setPackage("test");
297: nicec.execute();
298: }
299:
300: }
301:
302: // Setting for Emacs
303: // Local variables:
304: // tab-width:2
305: // indent-tabs-mode:t
306: // End:
|