KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > dotnet > Ilasm


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 /*
19  * build notes
20  * -The reference CD to listen to while editing this file is
21  * nap: Underworld - Everything, Everything
22  */

23
24 package org.apache.tools.ant.taskdefs.optional.dotnet;
25
26
27 import java.io.File JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 import org.apache.tools.ant.BuildException;
31 import org.apache.tools.ant.Project;
32 import org.apache.tools.ant.taskdefs.condition.Os;
33 import org.apache.tools.ant.types.EnumeratedAttribute;
34 import org.apache.tools.ant.types.FileSet;
35
36 /**
37  * Assembles .NET Intermediate Language files.
38  * ilasm.exe must be on the execute path, unless another executable
39  * or the full path to that executable is specified in the <tt>executable</tt>
40  * parameter
41  * <p>
42  *
43  * <p>
44  *
45  * All parameters are optional: &lt;il/&gt; should suffice to produce a debug
46  * build of all *.il files. The option set is roughly compatible with the
47  * CSharp class; even though the command line options are only vaguely
48  * equivalent. [The low level commands take things like /OUT=file, csc wants
49  * /out:file ... /verbose is used some places; /quiet here in ildasm... etc.]
50  * It would be nice if someone made all the command line tools consistent (and
51  * not as brittle as the java cmdline tools) <p>
52  * <p>
53  * The task is a directory based task, so attributes like <b>includes="*.il"
54  * </b> and <b>excludes="broken.il"</b> can be used to control the files pulled
55  * in. You can also use nested &lt;src&gt filesets to refer to source.
56  * <p>
57  *
58  * @ant.task name="ilasm" category="dotnet"
59  */

60
61 public class Ilasm
62          extends DotnetBaseMatchingTask {
63
64     // CheckStyle:VisibilityModifier OFF - bc
65
// CheckStyle:ConstantNameCheck OFF - bc
66
/**
67      * Name of the executable. The .exe suffix is deliberately not included in
68      * anticipation of the unix version
69      */

70     protected static final String JavaDoc exe_name = "ilasm";
71
72     /**
73      * what is the file extension we search on?
74      */

75     protected static final String JavaDoc file_ext = "il";
76
77     /**
78      * and now derive the search pattern from the extension
79      */

80     protected static final String JavaDoc file_pattern = "**/*." + file_ext;
81
82     /**
83      * title of task for external presentation
84      */

85     protected static final String JavaDoc exe_title = "ilasm";
86
87     /**
88      * type of target. Should be one of exe|library|module|winexe|(null)
89      * default is exe; the actual value (if not null) is fed to the command
90      * line. <br>
91      * See /target
92      */

93     protected String JavaDoc targetType;
94
95     /**
96      * verbose flag
97      */

98     protected boolean verbose;
99
100     /**
101      * listing flag
102      */

103
104     protected boolean listing;
105
106     /**
107      * resource file (.res format) to include in the app.
108      */

109     protected File JavaDoc resourceFile;
110
111     /**
112      * flag to control action on execution trouble
113      */

114     protected boolean failOnError;
115
116     /**
117      * debug flag. Controls generation of debug information.
118      */

119     protected boolean debug;
120
121     /**
122      * file containing private key
123      */

124
125     private File JavaDoc keyfile;
126
127     /**
128      * any extra command options?
129      */

130     protected String JavaDoc extraOptions;
131
132     /**
133      * filesets of references
134      */

135     protected Vector JavaDoc referenceFilesets = new Vector JavaDoc();
136
137     // CheckStyle:ConstantNameCheck ON
138
// CheckStyle:VisibilityModifier ON
139

140     /**
141      * @since Ant 1.7
142      */

143     private boolean isMono = !Os.isFamily("windows");
144
145     /**
146      * constructor inits everything and set up the search pattern
147      */

148     public Ilasm() {
149         Clear();
150         setIncludes(file_pattern);
151     }
152
153     // CheckStyle:MethodNameCheck OFF - bc
154
/**
155      * reset all contents.
156      */

157     public void Clear() {
158         targetType = null;
159         srcDir = null;
160         listing = false;
161         verbose = false;
162         debug = true;
163         outputFile = null;
164         failOnError = true;
165         resourceFile = null;
166         extraOptions = null;
167     }
168     // CheckStyle:MethodNameCheck ON
169

170
171
172     /**
173      * Sets the type of target, either "exe" or "library".
174      *
175      *@param targetType one of exe|library|
176      *@exception BuildException if target is not one of
177      * exe|library
178      */

179     public void setTargetType(String JavaDoc targetType)
180              throws BuildException {
181         this.targetType = targetType.toLowerCase();
182         if (!targetType.equals("exe") && !targetType.equals("library")) {
183             throw new BuildException("targetType " + targetType + " is not a valid type");
184         }
185     }
186
187
188     /**
189      * accessor method for target type
190      *
191      *@return the current target option
192      */

193     public String JavaDoc getTargetType() {
194         return targetType;
195     }
196
197
198     /**
199      * g get the target type or null for no argument needed
200      *
201      *@return The TargetTypeParameter value
202      */

203
204     protected String JavaDoc getTargetTypeParameter() {
205         if (!notEmpty(targetType)) {
206             return null;
207         }
208         if (targetType.equals("exe")) {
209             return "/exe";
210         } else if (targetType.equals("library")) {
211             return "/dll";
212         } else {
213             return null;
214         }
215     }
216
217
218     /**
219      * Sets the Owner attribute.
220      *
221      * @param s The new Owner value
222      * @ant.attribute ignore="true"
223      */

224     public void setOwner(String JavaDoc s) {
225         log("This option is not supported by ILASM as of Beta-2, "
226             + "and will be ignored", Project.MSG_WARN);
227     }
228
229
230     /**
231      * test for a string containing something useful
232      *
233      *@param s any string
234      *@return true if the argument is not null or empty
235      */

236     protected boolean notEmpty(String JavaDoc s) {
237         return s != null && s.length() != 0;
238     }
239
240
241     /**
242      * If true, enable verbose ILASM output.
243      *
244      *@param b flag set to true for verbose on
245      */

246     public void setVerbose(boolean b) {
247         verbose = b;
248     }
249
250
251     /**
252      * turn the verbose flag into a parameter for ILASM
253      *
254      *@return null or the appropriate command line string
255      */

256     protected String JavaDoc getVerboseParameter() {
257         return verbose ? null : "/quiet";
258     }
259
260
261     /**
262      * If true, produce a listing (off by default).
263      *
264      *@param b flag set to true for listing on
265      */

266     public void setListing(boolean b) {
267         listing = b;
268     }
269
270
271     /**
272      * turn the listing flag into a parameter for ILASM
273      *
274      *@return the appropriate string from the state of the listing flag
275      */

276     protected String JavaDoc getListingParameter() {
277         if (!isMono) {
278             return listing ? "/listing" : "/nolisting";
279         }
280         return null;
281     }
282
283
284     /**
285      * Set the output file; identical to setDestFile
286      * @see DotnetBaseMatchingTask#setDestFile
287      *@param params The new outputFile value
288      */

289     public void setOutputFile(File JavaDoc params) {
290         outputFile = params;
291     }
292
293
294     /**
295      * get the output file
296      *
297      *@return the argument string or null for no argument
298      */

299     protected String JavaDoc getOutputFileParameter() {
300         if (outputFile == null) {
301             return null;
302         }
303         return "/output=" + outputFile.toString();
304     }
305
306
307     /**
308      * name of resource file to include.
309      *
310      * @param fileName path to the file. Can be relative, absolute, whatever.
311      */

312     public void setResourceFile(File JavaDoc fileName) {
313         resourceFile = fileName;
314     }
315
316
317     /**
318      * Gets the resourceFileParameter attribute of the Ilasm task
319      *
320      *@return The resourceFileParameter value
321      */

322     protected String JavaDoc getResourceFileParameter() {
323         if (resourceFile != null) {
324             return "/resource=" + resourceFile.toString();
325         } else {
326             return null;
327         }
328     }
329
330
331     /**
332      * If true, fails if ilasm tool fails.
333      *
334      *@param b The new failOnError value
335      */

336     public void setFailOnError(boolean b) {
337         failOnError = b;
338     }
339
340
341     /**
342      * query fail on error flag
343      *
344      *@return The failFailOnError value
345      */

346     public boolean getFailOnError() {
347         return failOnError;
348     }
349
350
351     /**
352      * set the debug flag on or off.
353      *
354      *@param f on/off flag
355      */

356     public void setDebug(boolean f) {
357         debug = f;
358     }
359
360
361     /**
362      * query the debug flag
363      *
364      *@return true if debug is turned on
365      */

366     public boolean getDebug() {
367         return debug;
368     }
369
370
371     /**
372      * get the argument or null for no argument needed
373      *
374      *@return The debugParameter value
375      */

376     protected String JavaDoc getDebugParameter() {
377         return debug ? "/debug" : null;
378     }
379
380
381     /**
382      * the name of a file containing a private key.
383      *
384      *@param keyfile The new keyfile value
385      */

386     public void setKeyfile(File JavaDoc keyfile) {
387         this.keyfile = keyfile;
388     }
389
390
391     /**
392      * get the argument or null for no argument needed
393      *
394      *@return The keyfileParameter value
395      */

396     protected String JavaDoc getKeyfileParameter() {
397         if (keyfile != null) {
398             return "/keyfile:" + keyfile.toString();
399         } else {
400             return null;
401         }
402     }
403
404
405     /**
406      * Any extra options which are not explicitly
407      * supported by this task.
408      *
409      *@param extraOptions The new ExtraOptions value
410      */

411     public void setExtraOptions(String JavaDoc extraOptions) {
412         this.extraOptions = extraOptions;
413     }
414
415
416     /**
417      * Gets the ExtraOptions attribute
418      *
419      *@return The ExtraOptions value
420      */

421     public String JavaDoc getExtraOptions() {
422         return this.extraOptions;
423     }
424
425
426     /**
427      * get any extra options or null for no argument needed
428      *
429      *@return The ExtraOptions Parameter to CSC
430      */

431     protected String JavaDoc getExtraOptionsParameter() {
432         if (extraOptions != null && extraOptions.length() != 0) {
433             return extraOptions;
434         } else {
435             return null;
436         }
437     }
438
439     /**
440      * set the target type to one of exe|library
441      * @param targetType the enumerated value.
442      */

443     public void setTargetType(TargetTypes targetType) {
444         this.targetType = targetType.getValue();
445     }
446
447     /**
448      * Explicitly override the Mono auto-detection.
449      *
450      * <p>Defaults to false on Windows and true on any other platform.</p>
451      * @param b a <code>boolean</code> value.
452      * @since Ant 1.7
453      */

454     public void setMono(boolean b) {
455         isMono = b;
456     }
457
458     /**
459      * This is the execution entry point. Build a list of files and call ilasm
460      * on each of them.
461      *
462      *@throws BuildException if the assembly failed and FailOnError is true
463      */

464     public void execute()
465              throws BuildException {
466         log("This task is deprecated and will be removed in a future version\n"
467             + "of Ant. It is now part of the .NET Antlib:\n"
468             + "http://ant.apache.org/antlibs/dotnet/index.html",
469             Project.MSG_WARN);
470         NetCommand command = buildIlasmCommand();
471
472         addFilesAndExecute(command, false);
473
474     }
475     // end execute
476

477
478     /**
479      * build up our ilasm command
480      * @return
481      */

482     private NetCommand buildIlasmCommand() {
483         NetCommand command = new NetCommand(this, exe_title, exe_name);
484         command.setFailOnError(getFailOnError());
485         //fill in args
486
command.addArgument(getDebugParameter());
487         command.addArgument(getTargetTypeParameter());
488         command.addArgument(getListingParameter());
489         command.addArgument(getOutputFileParameter());
490         command.addArgument(getResourceFileParameter());
491         command.addArgument(getVerboseParameter());
492         command.addArgument(getKeyfileParameter());
493         command.addArgument(getExtraOptionsParameter());
494
495         /*
496          * space for more argumentativeness
497          * command.addArgument();
498          * command.addArgument();
499          */

500         return command;
501     }
502
503     /**
504      * add a new reference fileset to the compilation
505      * @param reference the fileset to use.
506      */

507     public void addReference(FileSet reference) {
508         referenceFilesets.add(reference);
509     }
510
511     /**
512      * test for a file being managed or not
513      * @param file the file to test.
514      * @return true if we think this is a managed executable, and thus OK
515      * for linking
516      * @todo look at the PE header of the exe and see if it is managed or not.
517      */

518     protected static boolean isFileManagedBinary(File JavaDoc file) {
519         String JavaDoc filename = file.toString().toLowerCase();
520         return filename.endsWith(".exe") || filename.endsWith(".dll")
521                 || filename.endsWith(".netmodule");
522     }
523
524
525     /**
526      * Target types to build.
527      * valid build types are exe|library|module|winexe
528      */

529     public static class TargetTypes extends EnumeratedAttribute {
530         /** {@inheritDoc}. */
531         public String JavaDoc[] getValues() {
532             return new String JavaDoc[]{
533                 "exe",
534                 "library",
535             };
536         }
537     }
538
539 }
540
541
Popular Tags