1 | /*
|
---|
2 | * Copyright 2001-2005 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
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 | // place in the optional ant tasks package
|
---|
25 | // but in its own dotnet group
|
---|
26 | // ====================================================================
|
---|
27 |
|
---|
28 | package org.apache.tools.ant.taskdefs.optional.dotnet;
|
---|
29 |
|
---|
30 | // ====================================================================
|
---|
31 | // imports
|
---|
32 | // ====================================================================
|
---|
33 |
|
---|
34 | import java.io.File;
|
---|
35 |
|
---|
36 | import org.apache.tools.ant.taskdefs.condition.Os;
|
---|
37 |
|
---|
38 | // ====================================================================
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Compiles C# source into executables or modules.
|
---|
42 | *
|
---|
43 | * csc.exe on Windows or mcs on other platforms must be on the execute path, unless another executable
|
---|
44 | * or the full path to that executable is specified in the <tt>executable</tt>
|
---|
45 | * parameter
|
---|
46 | * <p>
|
---|
47 | * All parameters are optional: <csc/> should suffice to produce a debug
|
---|
48 | * build of all *.cs files. However, naming an <tt>destFile</tt>stops the
|
---|
49 | * csc compiler from choosing an output name from random, and
|
---|
50 | * allows the dependency checker to determine if the file is out of date.
|
---|
51 | * <p>
|
---|
52 | * The task is a directory based task, so attributes like <b>includes="*.cs"
|
---|
53 | * </b> and <b>excludes="broken.cs"</b> can be used to control the files pulled
|
---|
54 | * in. By default, all *.cs files from the project folder down are included in
|
---|
55 | * the command. When this happens the output file -if not specified- is taken
|
---|
56 | * as the first file in the list, which may be somewhat hard to control.
|
---|
57 | * Specifying the output file with <tt>destFile</tt> seems prudent. <p>
|
---|
58 | *
|
---|
59 | * <p>
|
---|
60 | * For more complex source trees, nested <tt>src</tt> elemements can be
|
---|
61 | * supplied. When such an element is present, the implicit fileset is ignored.
|
---|
62 | * This makes sense, when you think about it :)
|
---|
63 | *
|
---|
64 | * <p>For historical reasons the pattern
|
---|
65 | * <code>**</code><code>/*.cs</code> is preset as includes list and
|
---|
66 | * you can not override it with an explicit includes attribute. Use
|
---|
67 | * nested <code><src></code> elements instead of the basedir
|
---|
68 | * attribute if you need more control.</p>
|
---|
69 | *
|
---|
70 | * <p>
|
---|
71 | * References to external files can be made through the references attribute,
|
---|
72 | * or (since Ant1.6), via nested <reference> filesets. With the latter,
|
---|
73 | * the timestamps of the references are also used in the dependency
|
---|
74 | * checking algorithm.
|
---|
75 | * <p>
|
---|
76 | *
|
---|
77 | * Example
|
---|
78 | *
|
---|
79 | * <pre><csc
|
---|
80 | * optimize="true"
|
---|
81 | * debug="false"
|
---|
82 | * docFile="documentation.xml"
|
---|
83 | * warnLevel="4"
|
---|
84 | * unsafe="false"
|
---|
85 | * targetType="exe"
|
---|
86 | * incremental="false"
|
---|
87 | * mainClass = "MainApp"
|
---|
88 | * destFile="NetApp.exe"
|
---|
89 | * >
|
---|
90 | * <src dir="src" includes="*.cs" />
|
---|
91 | * <reference file="${testCSC.dll}" />
|
---|
92 | * <define name="RELEASE" />
|
---|
93 | * <define name="DEBUG" if="debug.property"/>
|
---|
94 | * <define name="def3" unless="def3.property"/>
|
---|
95 | * </csc>
|
---|
96 | * </pre>
|
---|
97 | *
|
---|
98 | *
|
---|
99 | * @ant.task name="csc" category="dotnet"
|
---|
100 | * @since Ant 1.3
|
---|
101 | */
|
---|
102 |
|
---|
103 | public class CSharp extends DotnetCompile {
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * defines list: RELEASE;WIN32;NO_SANITY_CHECKS;;SOMETHING_ELSE'
|
---|
107 | */
|
---|
108 | String definitions;
|
---|
109 |
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * output XML documentation flag
|
---|
113 | */
|
---|
114 | private File 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 |
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * constructor inits everything and set up the search pattern
|
---|
145 | */
|
---|
146 |
|
---|
147 | public CSharp() {
|
---|
148 | clear();
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * full cleanup
|
---|
153 | */
|
---|
154 | public void clear() {
|
---|
155 | super.clear();
|
---|
156 | docFile = null;
|
---|
157 | fileAlign = 0;
|
---|
158 | fullpaths = true;
|
---|
159 | incremental = false;
|
---|
160 | unsafe = false;
|
---|
161 | noconfig = false;
|
---|
162 | definitions = null;
|
---|
163 | setExecutable(Os.isFamily("windows") ? "csc" : "mcs");
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * file for generated XML documentation
|
---|
170 | *
|
---|
171 | *@param f output file
|
---|
172 | */
|
---|
173 | public void setDocFile(File f) {
|
---|
174 | docFile = f;
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * get the argument or null for no argument needed
|
---|
180 | *
|
---|
181 | *@return The DocFile Parameter to CSC
|
---|
182 | */
|
---|
183 | protected String getDocFileParameter() {
|
---|
184 | if (docFile != null) {
|
---|
185 | return "/doc:" + docFile.toString();
|
---|
186 | } else {
|
---|
187 | return null;
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Set the file alignment.
|
---|
193 | * Valid values are 0,512, 1024, 2048, 4096, 8192,
|
---|
194 | * and 16384, 0 means 'leave to the compiler'
|
---|
195 | */
|
---|
196 | public void setFileAlign(int fileAlign) {
|
---|
197 | this.fileAlign = fileAlign;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * get the argument or null for no argument needed
|
---|
202 | *
|
---|
203 | *@return The OutputFile Parameter to CSC
|
---|
204 | */
|
---|
205 | protected String getFileAlignParameter() {
|
---|
206 | if (fileAlign != 0) {
|
---|
207 | return "/filealign:" + fileAlign;
|
---|
208 | } else {
|
---|
209 | return null;
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * If true, print the full path of files on errors.
|
---|
216 | *
|
---|
217 | *@param enabled The new fullPaths value
|
---|
218 | */
|
---|
219 | public void setFullPaths(boolean enabled) {
|
---|
220 | fullpaths = enabled;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Gets the fullPathsParameter attribute of the CSharp object
|
---|
226 | *
|
---|
227 | *@return The fullPathsParameter value or null if unset
|
---|
228 | */
|
---|
229 | protected String getFullPathsParameter() {
|
---|
230 | return fullpaths ? "/fullpaths" : null;
|
---|
231 | }
|
---|
232 |
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * set the incremental compilation flag on or off.
|
---|
236 | *
|
---|
237 | *@param incremental on/off flag
|
---|
238 | */
|
---|
239 | public void setIncremental(boolean incremental) {
|
---|
240 | this.incremental = incremental;
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * query the incrementalflag
|
---|
246 | *
|
---|
247 | *@return true if incremental compilation is turned on
|
---|
248 | */
|
---|
249 | public boolean getIncremental() {
|
---|
250 | return incremental;
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * get the incremental build argument
|
---|
256 | *
|
---|
257 | *@return The Incremental Parameter to CSC
|
---|
258 | */
|
---|
259 | protected String getIncrementalParameter() {
|
---|
260 | return "/incremental" + (incremental ? "+" : "-");
|
---|
261 | }
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * The output file. This is identical to the destFile attribute.
|
---|
265 | *
|
---|
266 | *@param params The new outputFile value
|
---|
267 | */
|
---|
268 | public void setOutputFile(File params) {
|
---|
269 | setDestFile(params);
|
---|
270 | }
|
---|
271 |
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * If true, enables the unsafe keyword.
|
---|
275 | *
|
---|
276 | *@param unsafe The new Unsafe value
|
---|
277 | */
|
---|
278 | public void setUnsafe(boolean unsafe) {
|
---|
279 | this.unsafe = unsafe;
|
---|
280 | }
|
---|
281 |
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * query the Unsafe attribute
|
---|
285 | *
|
---|
286 | *@return The Unsafe value
|
---|
287 | */
|
---|
288 | public boolean getUnsafe() {
|
---|
289 | return this.unsafe;
|
---|
290 | }
|
---|
291 |
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * get the argument or null for no argument needed
|
---|
295 | *
|
---|
296 | *@return The Unsafe Parameter to CSC
|
---|
297 | */
|
---|
298 | protected String getUnsafeParameter() {
|
---|
299 | return unsafe ? "/unsafe" : null;
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * A flag that tells the compiler not to read in the compiler
|
---|
305 | * settings files 'csc.rsp' in its bin directory and then the local directory
|
---|
306 | *
|
---|
307 | *@param enabled The new noConfig value
|
---|
308 | */
|
---|
309 | public void setNoConfig(boolean enabled) {
|
---|
310 | noconfig = enabled;
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Gets the noConfigParameter attribute of the CSharp object
|
---|
316 | *
|
---|
317 | *@return The noConfigParameter value
|
---|
318 | */
|
---|
319 | protected String getNoConfigParameter() {
|
---|
320 | return noconfig ? "/noconfig" : null;
|
---|
321 | }
|
---|
322 |
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * Semicolon separated list of defined constants.
|
---|
326 | *
|
---|
327 | *@param params The new definitions value
|
---|
328 | */
|
---|
329 | public void setDefinitions(String params) {
|
---|
330 | definitions = params;
|
---|
331 | }
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * override the superclasses version of this method (which we call)
|
---|
335 | * with a check for a definitions attribute, the contents of which
|
---|
336 | * are appended to the list.
|
---|
337 | *@return The Definitions Parameter to CSC
|
---|
338 | */
|
---|
339 | protected String getDefinitionsParameter() {
|
---|
340 | String predecessors = super.getDefinitionsParameter();
|
---|
341 | if (notEmpty(definitions)) {
|
---|
342 | if (predecessors == null) {
|
---|
343 | predecessors = "/define:";
|
---|
344 | }
|
---|
345 | return predecessors + definitions;
|
---|
346 | } else {
|
---|
347 | return predecessors;
|
---|
348 | }
|
---|
349 | }
|
---|
350 |
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * add Commands unique to C#.
|
---|
354 | * @param command ongoing command
|
---|
355 | */
|
---|
356 | public void addCompilerSpecificOptions(NetCommand command) {
|
---|
357 | command.addArgument(getIncludeDefaultReferencesParameter());
|
---|
358 | command.addArgument(getWarnLevelParameter());
|
---|
359 | command.addArgument(getDocFileParameter());
|
---|
360 | command.addArgument(getFullPathsParameter());
|
---|
361 | command.addArgument(getFileAlignParameter());
|
---|
362 | command.addArgument(getIncrementalParameter());
|
---|
363 | command.addArgument(getNoConfigParameter());
|
---|
364 | command.addArgument(getUnsafeParameter());
|
---|
365 | }
|
---|
366 |
|
---|
367 | // end execute
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Returns the delimiter which C# uses to separate references, i.e., a semi colon.
|
---|
371 | */
|
---|
372 | public String getReferenceDelimiter() {
|
---|
373 | return ";";
|
---|
374 | }
|
---|
375 |
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * This method indicates the filename extension for C# files.
|
---|
379 | * @return the file extension for C#, i.e., "cs" (without the dot).
|
---|
380 | */
|
---|
381 | public String getFileExtension() {
|
---|
382 | return "cs";
|
---|
383 | }
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * from a resource, get the resource param string
|
---|
387 | * @param resource
|
---|
388 | * @return a string containing the resource param, or a null string
|
---|
389 | * to conditionally exclude a resource.
|
---|
390 | */
|
---|
391 | protected String createResourceParameter(DotnetResource resource) {
|
---|
392 | return resource.getCSharpStyleParameter();
|
---|
393 | }
|
---|
394 |
|
---|
395 | }
|
---|
396 |
|
---|