Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 549   Methods: 31
NCLOC: 177   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
Ilasm.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 /*
 55   
  *  build notes
 56   
  *  -The reference CD to listen to while editing this file is
 57   
  *  nap: Underworld  - Everything, Everything
 58   
  */
 59   
 
 60   
 package org.apache.tools.ant.taskdefs.optional.dotnet;
 61   
 
 62   
 
 63   
 import java.io.File;
 64   
 import java.util.Vector;
 65   
 
 66   
 import org.apache.tools.ant.BuildException;
 67   
 import org.apache.tools.ant.DirectoryScanner;
 68   
 import org.apache.tools.ant.Project;
 69   
 import org.apache.tools.ant.types.EnumeratedAttribute;
 70   
 import org.apache.tools.ant.types.FileSet;
 71   
 import org.apache.tools.ant.taskdefs.MatchingTask;
 72   
 
 73   
 /**
 74   
  *  Assembles .NET Intermediate Language files. The task will only work
 75   
  *  on win2K until other platforms support csc.exe or an equivalent. ilasm.exe
 76   
  *  must be on the execute path too. <p>
 77   
  *
 78   
  *  <p>
 79   
  *
 80   
  *  All parameters are optional: &lt;il/&gt; should suffice to produce a debug
 81   
  *  build of all *.il files. The option set is roughly compatible with the
 82   
  *  CSharp class; even though the command line options are only vaguely
 83   
  *  equivalent. [The low level commands take things like /OUT=file, csc wants
 84   
  *  /out:file ... /verbose is used some places; /quiet here in ildasm... etc.]
 85   
  *  It would be nice if someone made all the command line tools consistent (and
 86   
  *  not as brittle as the java cmdline tools) <p>
 87   
  *
 88   
  *  The task is a directory based task, so attributes like <b>includes="*.il"
 89   
  *  </b> and <b>excludes="broken.il"</b> can be used to control the files pulled
 90   
  *  in. You can also use nested &lt;src&gt filesets to refer to source.
 91   
  *
 92   
  * @author     Steve Loughran steve_l@iseran.com
 93   
  * @version    0.6
 94   
  * @ant.task    name="ilasm" category="dotnet"
 95   
  */
 96   
 
 97   
 public class Ilasm
 98   
          extends DotnetBaseMatchingTask {
 99   
 
 100   
     /**
 101   
      *  Name of the executable. The .exe suffix is deliberately not included in
 102   
      *  anticipation of the unix version
 103   
      */
 104   
     protected static final String exe_name = "ilasm";
 105   
 
 106   
     /**
 107   
      *  what is the file extension we search on?
 108   
      */
 109   
     protected static final String file_ext = "il";
 110   
 
 111   
     /**
 112   
      *  and now derive the search pattern from the extension
 113   
      */
 114   
     protected static final String file_pattern = "**/*." + file_ext;
 115   
 
 116   
     /**
 117   
      *  title of task for external presentation
 118   
      */
 119   
     protected static final String exe_title = "ilasm";
 120   
 
 121   
     /**
 122   
      *  type of target. Should be one of exe|library|module|winexe|(null)
 123   
      *  default is exe; the actual value (if not null) is fed to the command
 124   
      *  line. <br>
 125   
      *  See /target
 126   
      */
 127   
     protected String targetType;
 128   
 
 129   
     /**
 130   
      *  verbose flag
 131   
      */
 132   
     protected boolean verbose;
 133   
 
 134   
     /**
 135   
      *  listing flag
 136   
      */
 137   
 
 138   
     protected boolean listing;
 139   
 
 140   
     /**
 141   
      *  resource file (.res format) to include in the app.
 142   
      */
 143   
     protected File resourceFile;
 144   
 
 145   
     /**
 146   
      *  flag to control action on execution trouble
 147   
      */
 148   
     protected boolean failOnError;
 149   
 
 150   
     /**
 151   
      *  debug flag. Controls generation of debug information.
 152   
      */
 153   
     protected boolean debug;
 154   
 
 155   
     /**
 156   
      *  file containing private key
 157   
      */
 158   
 
 159   
     private File keyfile;
 160   
 
 161   
     /**
 162   
      *  any extra command options?
 163   
      */
 164   
     protected String extraOptions;
 165   
     /**
 166   
      * filesets of references
 167   
      */
 168   
     protected Vector referenceFilesets =new Vector();
 169   
 
 170   
 
 171   
     /**
 172   
      *  constructor inits everything and set up the search pattern
 173   
      */
 174  0
     public Ilasm() {
 175  0
         Clear();
 176  0
         setIncludes(file_pattern);
 177   
     }
 178   
 
 179   
 
 180   
     /**
 181   
      *  reset all contents.
 182   
      */
 183  0
     public void Clear() {
 184  0
         targetType = null;
 185  0
         srcDir = null;
 186  0
         listing = false;
 187  0
         verbose = false;
 188  0
         debug = true;
 189  0
         outputFile = null;
 190  0
         failOnError = true;
 191  0
         resourceFile = null;
 192  0
         extraOptions = null;
 193   
     }
 194   
 
 195   
 
 196   
 
 197   
     /**
 198   
      * Sets the type of target, either "exe" or "library".
 199   
      *
 200   
      *@param  targetType          one of exe|library|
 201   
      *@exception  BuildException  if target is not one of
 202   
      *      exe|library
 203   
      */
 204  0
     public void setTargetType(String targetType)
 205   
              throws BuildException {
 206  0
         this.targetType = targetType.toLowerCase();
 207  0
         if (!targetType.equals("exe") && !targetType.equals("library")) {
 208  0
             throw new BuildException("targetType " + targetType + " is not a valid type");
 209   
         }
 210   
     }
 211   
 
 212   
 
 213   
     /**
 214   
      *  accessor method for target type
 215   
      *
 216   
      *@return    the current target option
 217   
      */
 218  0
     public String getTargetType() {
 219  0
         return targetType;
 220   
     }
 221   
 
 222   
 
 223   
     /**
 224   
      *  g get the target type or null for no argument needed
 225   
      *
 226   
      *@return    The TargetTypeParameter value
 227   
      */
 228   
 
 229  0
     protected String getTargetTypeParameter() {
 230  0
         if (!notEmpty(targetType)) {
 231  0
             return null;
 232   
         }
 233  0
         if (targetType.equals("exe")) {
 234  0
             return "/exe";
 235  0
         } else if (targetType.equals("library")) {
 236  0
             return "/dll";
 237   
         } else {
 238  0
             return null;
 239   
         }
 240   
     }
 241   
 
 242   
 
 243   
     /**
 244   
      * Sets the Owner attribute.
 245   
      *
 246   
      * @param  s  The new Owner value
 247   
      * @ant.attribute ignore="true"
 248   
      */
 249  0
     public void setOwner(String s) {
 250  0
         log("This option is not supported by ILASM as of Beta-2, and will be ignored", Project.MSG_WARN);
 251   
     }
 252   
 
 253   
 
 254   
     /**
 255   
      *  test for a string containing something useful
 256   
      *
 257   
      *@param  s       any string
 258   
      *@return         true if the argument is not null or empty
 259   
      */
 260  0
     protected boolean notEmpty(String s) {
 261  0
         return s != null && s.length() != 0;
 262   
     }
 263   
 
 264   
 
 265   
     /**
 266   
      *  If true, enable verbose ILASM output.
 267   
      *
 268   
      *@param  b  flag set to true for verbose on
 269   
      */
 270  0
     public void setVerbose(boolean b) {
 271  0
         verbose = b;
 272   
     }
 273   
 
 274   
 
 275   
     /**
 276   
      *  turn the verbose flag into a parameter for ILASM
 277   
      *
 278   
      *@return    null or the appropriate command line string
 279   
      */
 280  0
     protected String getVerboseParameter() {
 281  0
         return verbose ? null : "/quiet";
 282   
     }
 283   
 
 284   
 
 285   
     /**
 286   
      * If true, produce a listing (off by default).
 287   
      *
 288   
      *@param  b  flag set to true for listing on
 289   
      */
 290  0
     public void setListing(boolean b) {
 291  0
         listing = b;
 292   
     }
 293   
 
 294   
 
 295   
     /**
 296   
      *  turn the listing flag into a parameter for ILASM
 297   
      *
 298   
      *@return    the appropriate string from the state of the listing flag
 299   
      */
 300  0
     protected String getListingParameter() {
 301  0
         return listing ? "/listing" : "/nolisting";
 302   
     }
 303   
 
 304   
 
 305   
     /**
 306   
      * Set the output file; identical to setDestFile
 307   
      * @see DotnetBaseMatchingTask.setDestFile
 308   
      *@param  params  The new outputFile value
 309   
      */
 310  0
     public void setOutputFile(File params) {
 311  0
         outputFile = params;
 312   
     }
 313   
 
 314   
 
 315   
     /**
 316   
      *  get the output file
 317   
      *
 318   
      *@return    the argument string or null for no argument
 319   
      */
 320  0
     protected String getOutputFileParameter() {
 321  0
         if (outputFile == null) {
 322  0
             return null;
 323   
         }
 324  0
         return "/output=" + outputFile.toString();
 325   
     }
 326   
 
 327   
 
 328   
     /**
 329   
      * name of resource file to include.
 330   
      *
 331   
      * @param  fileName  path to the file. Can be relative, absolute, whatever.
 332   
      */
 333  0
     public void setResourceFile(File fileName) {
 334  0
         resourceFile = fileName;
 335   
     }
 336   
 
 337   
 
 338   
     /**
 339   
      *  Gets the resourceFileParameter attribute of the Ilasm task
 340   
      *
 341   
      *@return    The resourceFileParameter value
 342   
      */
 343  0
     protected String getResourceFileParameter() {
 344  0
         if (resourceFile != null) {
 345  0
             return "/resource=" + resourceFile.toString();
 346   
         } else {
 347  0
             return null;
 348   
         }
 349   
     }
 350   
 
 351   
 
 352   
     /**
 353   
      * If true, fails if ilasm tool fails.
 354   
      *
 355   
      *@param  b  The new failOnError value
 356   
      */
 357  0
     public void setFailOnError(boolean b) {
 358  0
         failOnError = b;
 359   
     }
 360   
 
 361   
 
 362   
     /**
 363   
      *  query fail on error flag
 364   
      *
 365   
      *@return    The failFailOnError value
 366   
      */
 367  0
     public boolean getFailOnError() {
 368  0
         return failOnError;
 369   
     }
 370   
 
 371   
 
 372   
     /**
 373   
      *  set the debug flag on or off.
 374   
      *
 375   
      *@param  f  on/off flag
 376   
      */
 377  0
     public void setDebug(boolean f) {
 378  0
         debug = f;
 379   
     }
 380   
 
 381   
 
 382   
     /**
 383   
      *  query the debug flag
 384   
      *
 385   
      *@return    true if debug is turned on
 386   
      */
 387  0
     public boolean getDebug() {
 388  0
         return debug;
 389   
     }
 390   
 
 391   
 
 392   
     /**
 393   
      *  get the argument or null for no argument needed
 394   
      *
 395   
      *@return    The debugParameter value
 396   
      */
 397  0
     protected String getDebugParameter() {
 398  0
         return debug ? "/debug" : null;
 399   
     }
 400   
 
 401   
 
 402   
     /**
 403   
      * the name of a file containing a private key.
 404   
      *
 405   
      *@param  keyfile  The new keyfile value
 406   
      */
 407  0
     public void setKeyfile(File keyfile) {
 408  0
         this.keyfile = keyfile;
 409   
     }
 410   
 
 411   
 
 412   
     /**
 413   
      *  get the argument or null for no argument needed
 414   
      *
 415   
      *@return    The keyfileParameter value
 416   
      */
 417  0
     protected String getKeyfileParameter() {
 418  0
         if (keyfile != null) {
 419  0
             return "/keyfile:" + keyfile.toString();
 420   
         } else {
 421  0
             return null;
 422   
         }
 423   
     }
 424   
 
 425   
 
 426   
     /**
 427   
      * Any extra options which are not explicitly
 428   
      * supported by this task.
 429   
      *
 430   
      *@param  extraOptions  The new ExtraOptions value
 431   
      */
 432  0
     public void setExtraOptions(String extraOptions) {
 433  0
         this.extraOptions = extraOptions;
 434   
     }
 435   
 
 436   
 
 437   
     /**
 438   
      *  Gets the ExtraOptions attribute
 439   
      *
 440   
      *@return    The ExtraOptions value
 441   
      */
 442  0
     public String getExtraOptions() {
 443  0
         return this.extraOptions;
 444   
     }
 445   
 
 446   
 
 447   
     /**
 448   
      *  get any extra options or null for no argument needed
 449   
      *
 450   
      *@return    The ExtraOptions Parameter to CSC
 451   
      */
 452  0
     protected String getExtraOptionsParameter() {
 453  0
         if (extraOptions != null && extraOptions.length() != 0) {
 454  0
             return extraOptions;
 455   
         } else {
 456  0
             return null;
 457   
         }
 458   
     }
 459   
 
 460   
     /**
 461   
      * set the target type to one of exe|library
 462   
      * @param targetType
 463   
      */
 464  0
     public void setTargetType(TargetTypes targetType) {
 465  0
         this.targetType = targetType.getValue();
 466   
     }
 467   
 
 468   
     /**
 469   
      *  This is the execution entry point. Build a list of files and call ilasm
 470   
      *  on each of them.
 471   
      *
 472   
      *@throws  BuildException  if the assembly failed and FailOnError is true
 473   
      */
 474  0
     public void execute()
 475   
              throws BuildException {
 476  0
         if (srcDir == null) {
 477  0
             srcDir = getProject().resolveFile(".");
 478   
         }
 479   
 
 480  0
         NetCommand command = buildIlasmCommand();
 481   
 
 482  0
         addFilesAndExecute(command, false);
 483   
 
 484   
     }
 485   
     // end execute
 486   
 
 487   
 
 488   
     /**
 489   
      * build up our ilasm command
 490   
      * @return
 491   
      */
 492  0
     private NetCommand buildIlasmCommand() {
 493  0
         NetCommand command = new NetCommand(this, exe_title, exe_name);
 494  0
         command.setFailOnError(getFailOnError());
 495   
         //fill in args
 496  0
         command.addArgument(getDebugParameter());
 497  0
         command.addArgument(getTargetTypeParameter());
 498  0
         command.addArgument(getListingParameter());
 499  0
         command.addArgument(getOutputFileParameter());
 500  0
         command.addArgument(getResourceFileParameter());
 501  0
         command.addArgument(getVerboseParameter());
 502  0
         command.addArgument(getKeyfileParameter());
 503  0
         command.addArgument(getExtraOptionsParameter());
 504   
 
 505   
         /*
 506   
          *  space for more argumentativeness
 507   
          *  command.addArgument();
 508   
          *  command.addArgument();
 509   
          */
 510  0
         return command;
 511   
     }
 512   
 
 513   
     /**
 514   
      * add a new reference fileset to the compilation
 515   
      * @param reference
 516   
      */
 517  0
     public void addReference(FileSet reference) {
 518  0
         referenceFilesets.add(reference);
 519   
     }
 520   
 
 521   
     /**
 522   
      * test for a file being managed or not
 523   
      * @return true if we think this is a managed executable, and thus OK
 524   
      * for linking
 525   
      * @todo look at the PE header of the exe and see if it is managed or not.
 526   
      */
 527  0
     protected static boolean isFileManagedBinary(File file) {
 528  0
         String filename= file.toString().toLowerCase();
 529  0
         return filename.endsWith(".exe") || filename.endsWith(".dll")
 530   
                 || filename.endsWith(".netmodule");
 531   
     }
 532   
 
 533   
 
 534   
     /**
 535   
      * Target types to build.
 536   
      * valid build types are exe|library|module|winexe
 537   
      */
 538   
     public static class TargetTypes extends EnumeratedAttribute {
 539  0
         public String[] getValues() {
 540  0
             return new String[]{
 541   
                 "exe",
 542   
                 "library",
 543   
             };
 544   
         }
 545   
     }
 546   
 
 547   
 }
 548   
 
 549