KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
20  * build notes
21  * -The reference CD to listen to while editing this file is
22  * nap: Underworld - Everything, Everything
23  */

24 // ====================================================================
25
// place in the optional ant tasks package
26
// but in its own dotnet group
27
// ====================================================================
28

29 package org.apache.tools.ant.taskdefs.optional.dotnet;
30
31 // ====================================================================
32
// imports
33
// ====================================================================
34

35 import java.io.File JavaDoc;
36
37 // ====================================================================
38

39 /**
40  * Compiles C# source into executables or modules.
41  *
42  * csc.exe on Windows or mcs on other platforms must be on the execute
43  * path, unless another executable or the full path to that executable
44  * is specified in the <tt>executable</tt> parameter
45  * <p>
46  * All parameters are optional: &lt;csc/&gt; should suffice to produce a debug
47  * build of all *.cs files. However, naming an <tt>destFile</tt>stops the
48  * csc compiler from choosing an output name from random, and
49  * allows the dependency checker to determine if the file is out of date.
50  * <p>
51  * The task is a directory based task, so attributes like <b>includes="*.cs"
52  * </b> and <b>excludes="broken.cs"</b> can be used to control the files pulled
53  * in. By default, all *.cs files from the project folder down are included in
54  * the command. When this happens the output file -if not specified- is taken
55  * as the first file in the list, which may be somewhat hard to control.
56  * Specifying the output file with <tt>destFile</tt> seems prudent. <p>
57  *
58  * <p>
59  * For more complex source trees, nested <tt>src</tt> elemements can be
60  * supplied. When such an element is present, the implicit fileset is ignored.
61  * This makes sense, when you think about it :)
62  *
63  * <p>For historical reasons the pattern
64  * <code>**</code><code>/*.cs</code> is preset as includes list and
65  * you can not override it with an explicit includes attribute. Use
66  * nested <code>&lt;src&gt;</code> elements instead of the basedir
67  * attribute if you need more control.</p>
68  *
69  * <p>
70  * References to external files can be made through the references attribute,
71  * or (since Ant1.6), via nested &lt;reference&gt; filesets. With the latter,
72  * the timestamps of the references are also used in the dependency
73  * checking algorithm.
74  * <p>
75  *
76  * Example
77  *
78  * <pre>&lt;csc
79  * optimize=&quot;true&quot;
80  * debug=&quot;false&quot;
81  * docFile=&quot;documentation.xml&quot;
82  * warnLevel=&quot;4&quot;
83  * unsafe=&quot;false&quot;
84  * targetType=&quot;exe&quot;
85  * incremental=&quot;false&quot;
86  * mainClass = &quot;MainApp&quot;
87  * destFile=&quot;NetApp.exe&quot;
88  * &gt;
89  * &lt;src dir="src" includes="*.cs" /&gt;
90  * &lt;reference file="${testCSC.dll}" /&gt;
91  * &lt;define name="RELEASE" /&gt;
92  * &lt;define name="DEBUG" if="debug.property"/&gt;
93  * &lt;define name="def3" unless="def3.property"/&gt;
94  * &lt;/csc&gt;
95  * </pre>
96  *
97  *
98  * @ant.task name="csc" category="dotnet"
99  * @since Ant 1.3
100  */

101
102 public class CSharp extends DotnetCompile {
103
104     // CheckStyle:VisibilityModifier OFF - bc
105
/**
106      * defines list: RELEASE;WIN32;NO_SANITY_CHECKS;;SOMETHING_ELSE'
107      */

108     String JavaDoc definitions;
109
110
111     /**
112      * output XML documentation flag
113      */

114     private File JavaDoc docFile;
115
116     /**
117      * file alignment; 0 means let the compiler decide
118      */

119     private int fileAlign = 0;
120
121     /**
122      * use full paths to things
123      */

124     private boolean fullpaths = false;
125
126     /**
127      * incremental build flag
128      */

129     private boolean incremental;
130
131     /**
132      * enable unsafe code flag. Clearly set to false by default
133      */

134     protected boolean unsafe;
135
136     /**
137      * A flag that tells the compiler not to read in the compiler
138      * settings files 'csc.rsp' in its bin directory and then the local directory
139      */

140     private boolean noconfig = false;
141     // CheckStyle:VisibilityModifier ON
142

143
144     /**
145      * constructor inits everything and set up the search pattern
146      */

147
148     public CSharp() {
149         clear();
150     }
151
152     /**
153      * full cleanup
154      */

155     public void clear() {
156         super.clear();
157         docFile = null;
158         fileAlign = 0;
159         fullpaths = true;
160         incremental = false;
161         unsafe = false;
162         noconfig = false;
163         definitions = null;
164         setExecutable(isWindows ? "csc" : "mcs");
165     }
166
167
168
169     /**
170      * file for generated XML documentation
171      *
172      *@param f output file
173      */

174     public void setDocFile(File JavaDoc f) {
175         docFile = f;
176     }
177
178
179     /**
180      * get the argument or null for no argument needed
181      *
182      *@return The DocFile Parameter to CSC
183      */

184     protected String JavaDoc getDocFileParameter() {
185         if (docFile != null) {
186             return "/doc:" + docFile.toString();
187         } else {
188             return null;
189         }
190     }
191
192     /**
193      * Set the file alignment.
194      * Valid values are 0,512, 1024, 2048, 4096, 8192,
195      * and 16384, 0 means 'leave to the compiler'
196      * @param fileAlign the value to use.
197      */

198     public void setFileAlign(int fileAlign) {
199         this.fileAlign = fileAlign;
200     }
201
202     /**
203      * get the argument or null for no argument needed
204      *
205      *@return The OutputFile Parameter to CSC
206      */

207     protected String JavaDoc getFileAlignParameter() {
208         if (fileAlign != 0 && !"mcs".equals(getExecutable())) {
209             return "/filealign:" + fileAlign;
210         } else {
211             return null;
212         }
213     }
214
215
216     /**
217      * If true, print the full path of files on errors.
218      *
219      *@param enabled The new fullPaths value
220      */

221     public void setFullPaths(boolean enabled) {
222         fullpaths = enabled;
223     }
224
225
226     /**
227      * Gets the fullPathsParameter attribute of the CSharp object
228      *
229      *@return The fullPathsParameter value or null if unset
230      */

231     protected String JavaDoc getFullPathsParameter() {
232         return fullpaths ? "/fullpaths" : null;
233     }
234
235
236     /**
237      * set the incremental compilation flag on or off.
238      *
239      *@param incremental on/off flag
240      */

241     public void setIncremental(boolean incremental) {
242         this.incremental = incremental;
243     }
244
245
246     /**
247      * query the incrementalflag
248      *
249      *@return true if incremental compilation is turned on
250      */

251     public boolean getIncremental() {
252         return incremental;
253     }
254
255
256     /**
257      * get the incremental build argument
258      *
259      *@return The Incremental Parameter to CSC
260      */

261     protected String JavaDoc getIncrementalParameter() {
262         return "/incremental" + (incremental ? "+" : "-");
263     }
264
265     /**
266      * The output file. This is identical to the destFile attribute.
267      *
268      *@param params The new outputFile value
269      */

270     public void setOutputFile(File JavaDoc params) {
271         setDestFile(params);
272     }
273
274
275     /**
276      * If true, enables the unsafe keyword.
277      *
278      *@param unsafe The new Unsafe value
279      */

280     public void setUnsafe(boolean unsafe) {
281         this.unsafe = unsafe;
282     }
283
284
285     /**
286      * query the Unsafe attribute
287      *
288      *@return The Unsafe value
289      */

290     public boolean getUnsafe() {
291         return this.unsafe;
292     }
293
294
295     /**
296      * get the argument or null for no argument needed
297      *
298      *@return The Unsafe Parameter to CSC
299      */

300     protected String JavaDoc getUnsafeParameter() {
301         return unsafe ? "/unsafe" : null;
302     }
303
304
305     /**
306      * A flag that tells the compiler not to read in the compiler
307      * settings files 'csc.rsp' in its bin directory and then the local directory
308      *
309      *@param enabled The new noConfig value
310      */

311     public void setNoConfig(boolean enabled) {
312         noconfig = enabled;
313     }
314
315
316     /**
317      * Gets the noConfigParameter attribute of the CSharp object
318      *
319      *@return The noConfigParameter value
320      */

321     protected String JavaDoc getNoConfigParameter() {
322         return noconfig ? "/noconfig" : null;
323     }
324
325
326     /**
327      * Semicolon separated list of defined constants.
328      *
329      *@param params The new definitions value
330      */

331     public void setDefinitions(String JavaDoc params) {
332         definitions = params;
333     }
334
335     /**
336      * override the superclasses version of this method (which we call)
337      * with a check for a definitions attribute, the contents of which
338      * are appended to the list.
339      *@return The Definitions Parameter to CSC
340      */

341     protected String JavaDoc getDefinitionsParameter() {
342         String JavaDoc predecessors = super.getDefinitionsParameter();
343         if (notEmpty(definitions)) {
344             if (predecessors == null) {
345                 predecessors = "/define:";
346             }
347             return predecessors + definitions;
348         } else {
349             return predecessors;
350         }
351     }
352
353
354     /**
355      * add Commands unique to C#.
356      * @param command ongoing command
357      */

358     public void addCompilerSpecificOptions(NetCommand command) {
359         command.addArgument(getIncludeDefaultReferencesParameter());
360         command.addArgument(getWarnLevelParameter());
361         command.addArgument(getDocFileParameter());
362         command.addArgument(getFullPathsParameter());
363         command.addArgument(getFileAlignParameter());
364         command.addArgument(getIncrementalParameter());
365         command.addArgument(getNoConfigParameter());
366         command.addArgument(getUnsafeParameter());
367     }
368
369     // end execute
370

371     /**
372      * Returns the delimiter which C# uses to separate references, i.e., a semi colon.
373      * @return the delimiter.
374      */

375     public String JavaDoc getReferenceDelimiter() {
376         return ";";
377     }
378
379
380     /**
381      * This method indicates the filename extension for C# files.
382      * @return the file extension for C#, i.e., "cs" (without the dot).
383      */

384     public String JavaDoc getFileExtension() {
385         return "cs";
386     }
387
388     /**
389      * Build a C# style parameter.
390      * @param command the command.
391      * @param resource the resource.
392      */

393     protected void createResourceParameter(
394         NetCommand command, DotnetResource resource) {
395         resource.getParameters(getProject(), command, true);
396     }
397
398 }
399
400
Popular Tags