1 18 19 24 25 28 package org.apache.tools.ant.taskdefs.optional.dotnet; 29 30 32 import java.io.File ; 33 import java.util.Vector ; 34 import java.util.Enumeration ; 35 import java.util.Hashtable ; 36 37 import org.apache.tools.ant.BuildException; 38 import org.apache.tools.ant.Project; 39 import org.apache.tools.ant.types.Commandline; 40 import org.apache.tools.ant.types.Path; 41 import org.apache.tools.ant.types.FileSet; 42 import org.apache.tools.ant.types.EnumeratedAttribute; 43 44 45 69 70 public abstract class DotnetCompile 71 extends DotnetBaseMatchingTask { 72 73 76 private String references; 77 78 81 private boolean includeDefaultReferences = true; 82 83 86 private File win32icon; 87 88 91 private File win32res; 92 93 96 private boolean failOnError; 97 98 103 private Path referenceFiles; 104 105 108 private boolean optimize; 109 110 114 protected Vector definitionList = new Vector (); 115 116 119 protected Vector resources = new Vector (); 120 121 124 125 protected String executable; 126 127 protected static final String REFERENCE_OPTION = "/reference:"; 128 129 132 protected boolean debug; 133 134 137 private int warnLevel; 138 139 142 protected String mainClass; 143 144 147 protected String extraOptions; 148 149 155 protected String targetType; 156 157 160 161 protected boolean utf8output = false; 162 163 166 protected String additionalModules; 167 170 protected Vector referenceFilesets = new Vector (); 171 172 175 private boolean useResponseFile = false; 176 private static final int AUTOMATIC_RESPONSE_FILE_THRESHOLD = 64; 177 178 180 183 184 public DotnetCompile() { 185 clear(); 186 setIncludes(getFilePattern()); 187 } 188 189 192 public void clear() { 193 targetType = null; 194 win32icon = null; 195 srcDir = null; 196 mainClass = null; 197 warnLevel = 3; 198 optimize = false; 199 debug = true; 200 references = null; 201 failOnError = true; 202 additionalModules = null; 203 includeDefaultReferences = true; 204 extraOptions = null; 205 } 206 207 208 213 public void setReferences(String s) { 214 references = s; 215 } 216 217 218 223 protected String getReferencesParameter() { 224 if (notEmpty(references)) { 226 if (isWindows) { 227 return '\"' + REFERENCE_OPTION + references + '\"'; 228 } else { 229 return REFERENCE_OPTION + references; 230 } 231 } else { 232 return null; 233 } 234 } 235 236 242 public void setReferenceFiles(Path path) { 243 if (referenceFiles == null) { 245 referenceFiles = new Path(this.getProject()); 246 } 247 referenceFiles.append(path); 248 } 249 250 254 public void addReference(FileSet reference) { 255 referenceFilesets.add(reference); 256 } 257 258 259 260 265 protected String getReferenceFilesParameter() { 266 if (references == null) { 268 return null; 269 } 270 274 if (references.length() == 0) { 276 return null; 277 } 278 279 StringBuffer s = new StringBuffer (REFERENCE_OPTION); 280 if (isWindows) { 281 s.append('\"'); 282 } 283 s.append(references); 284 if (isWindows) { 285 s.append('\"'); 286 } 287 return s.toString(); 288 } 289 290 291 300 public void setIncludeDefaultReferences(boolean f) { 301 includeDefaultReferences = f; 302 } 303 304 305 310 public boolean getIncludeDefaultReferences() { 311 return includeDefaultReferences; 312 } 313 314 315 320 protected String getIncludeDefaultReferencesParameter() { 321 return "/nostdlib" + (includeDefaultReferences ? "-" : "+"); 322 } 323 324 325 326 331 public void setOptimize(boolean f) { 332 optimize = f; 333 } 334 335 336 341 public boolean getOptimize() { 342 return optimize; 343 } 344 345 346 351 protected String getOptimizeParameter() { 352 return "/optimize" + (optimize ? "+" : "-"); 353 } 354 355 356 361 public void setDebug(boolean f) { 362 debug = f; 363 } 364 365 366 371 public boolean getDebug() { 372 return debug; 373 } 374 375 376 381 protected String getDebugParameter() { 382 return "/debug" + (debug ? "+" : "-"); 383 } 384 385 386 393 public void setWarnLevel(int warnLevel) { 394 this.warnLevel = warnLevel; 395 } 396 397 398 403 public int getWarnLevel() { 404 return warnLevel; 405 } 406 407 408 413 protected String getWarnLevelParameter() { 414 return "/warn:" + warnLevel; 415 } 416 417 418 423 public void setMainClass(String mainClass) { 424 this.mainClass = mainClass; 425 } 426 427 428 433 public String getMainClass() { 434 return this.mainClass; 435 } 436 437 438 443 protected String getMainClassParameter() { 444 if (mainClass != null && mainClass.length() != 0) { 445 return "/main:" + mainClass; 446 } else { 447 return null; 448 } 449 } 450 451 452 458 public void setExtraOptions(String extraOptions) { 459 this.extraOptions = extraOptions; 460 } 461 462 463 468 public String getExtraOptions() { 469 return this.extraOptions; 470 } 471 472 473 478 protected String getExtraOptionsParameter() { 479 if (extraOptions != null && extraOptions.length() != 0) { 480 return extraOptions; 481 } else { 482 return null; 483 } 484 } 485 486 492 protected String [] getExtraOptionsParameters() { 493 String extra = getExtraOptionsParameter(); 494 return extra == null ? null : Commandline.translateCommandline(extra); 495 } 496 497 502 public void setDestDir(File dirName) { 503 log("DestDir currently unused", Project.MSG_WARN); 504 } 505 506 507 511 public void setTargetType(TargetTypes targetType) { 512 this.targetType = targetType.getValue(); 513 } 514 521 public void setTargetType(String ttype) 522 throws BuildException { 523 ttype = ttype.toLowerCase(); 524 if (ttype.equals("exe") || ttype.equals("library") 525 || ttype.equals("module") || ttype.equals("winexe")) { 526 targetType = ttype; 527 } else { 528 throw new BuildException("targetType " + ttype 529 + " is not one of 'exe', 'module', 'winexe' or 'library'"); 530 } 531 } 532 533 534 539 public String getTargetType() { 540 return targetType; 541 } 542 543 544 549 protected String getTargetTypeParameter() { 550 if (notEmpty(targetType)) { 551 return "/target:" + targetType; 552 } else { 553 return null; 554 } 555 } 556 557 558 563 public void setWin32Icon(File fileName) { 564 win32icon = fileName; 565 } 566 567 568 573 protected String getWin32IconParameter() { 574 if (win32icon != null) { 575 return "/win32icon:" + win32icon.toString(); 576 } else { 577 return null; 578 } 579 } 580 581 582 588 public void setWin32Res(File fileName) { 589 win32res = fileName; 590 } 591 592 596 public File getWin32Res() { 597 return win32res; 598 } 599 600 601 606 protected String getWin32ResParameter() { 607 if (win32res != null) { 608 return "/win32res:" + win32res.toString(); 609 } else { 610 return null; 611 } 612 } 613 614 615 620 public void setUtf8Output(boolean enabled) { 621 utf8output = enabled; 622 } 623 624 625 630 protected String getUtf8OutputParameter() { 631 return utf8output ? "/utf8output" : null; 632 } 633 634 635 639 public void addDefine(DotnetDefine define) { 640 definitionList.addElement(define); 641 } 642 643 644 649 protected String getDefinitionsParameter() throws BuildException { 650 StringBuffer defines = new StringBuffer (); 651 Enumeration defEnum = definitionList.elements(); 652 boolean firstDefinition = true; 653 while (defEnum.hasMoreElements()) { 654 DotnetDefine define = (DotnetDefine) defEnum.nextElement(); 656 if (define.isSet(this)) { 657 if (!firstDefinition) { 659 defines.append(getDefinitionsDelimiter()); 660 } 661 defines.append(define.getValue(this)); 662 firstDefinition = false; 663 } 664 } 665 if (defines.length() == 0) { 666 return null; 667 } else { 668 return "/d:" + defines; 669 } 670 } 671 672 673 678 public void setAdditionalModules(String params) { 679 additionalModules = params; 680 } 681 682 683 688 protected String getAdditionalModulesParameter() { 689 if (notEmpty(additionalModules)) { 690 return "/addmodule:" + additionalModules; 691 } else { 692 return null; 693 } 694 } 695 696 697 702 protected String getDestFileParameter() { 703 if (outputFile != null) { 704 return "/out:" + outputFile.toString(); 705 } else { 706 return null; 707 } 708 } 709 710 711 716 public void setFailOnError(boolean b) { 717 failOnError = b; 718 } 719 720 721 726 public boolean getFailOnError() { 727 return failOnError; 728 } 729 730 734 public void addResource(DotnetResource resource) { 735 resources.add(resource); 736 } 737 738 742 protected String getExecutable() { 743 return executable; 744 } 745 746 754 public void setExecutable(String executable) { 755 this.executable = executable; 756 } 757 758 764 protected boolean notEmpty(String s) { 765 return s != null && s.length() != 0; 766 } 767 768 772 protected void validate() 773 throws BuildException { 774 if (outputFile != null && outputFile.isDirectory()) { 775 throw new BuildException("destFile cannot be a directory"); 776 } 777 if (getExecutable() == null) { 778 throw new BuildException("There is no executable defined for this task"); 779 } 780 } 781 782 786 public String getFilePattern() { 787 return "**/*." + getFileExtension(); 788 } 789 790 794 public boolean isUseResponseFile() { 795 return useResponseFile; 796 } 797 798 806 public void setUseResponseFile(boolean useResponseFile) { 807 this.useResponseFile = useResponseFile; 808 } 809 810 815 public void execute() 816 throws BuildException { 817 log("This task is deprecated and will be removed in a future version\n" 818 + "of Ant. It is now part of the .NET Antlib:\n" 819 + "http://ant.apache.org/antlibs/dotnet/index.html", 820 Project.MSG_WARN); 821 822 validate(); 823 NetCommand command = createNetCommand(); 824 command.setAutomaticResponseFileThreshold(AUTOMATIC_RESPONSE_FILE_THRESHOLD); 826 command.setUseResponseFile(useResponseFile); 827 fillInSharedParameters(command); 829 addResources(command); 830 addCompilerSpecificOptions(command); 831 int referencesOutOfDate 832 = addReferenceFilesets(command, getOutputFileTimestamp()); 833 boolean forceBuild = referencesOutOfDate > 0; 835 addFilesAndExecute(command, forceBuild); 836 837 } 838 839 844 public abstract String getReferenceDelimiter(); 845 846 850 public abstract String getFileExtension(); 851 852 853 857 protected void fillInSharedParameters(NetCommand command) { 858 command.setFailOnError(getFailOnError()); 859 command.addArgument("/nologo"); 861 command.addArgument(getAdditionalModulesParameter()); 862 command.addArgument(getDebugParameter()); 863 command.addArgument(getDefinitionsParameter()); 864 command.addArguments(getExtraOptionsParameters()); 865 command.addArgument(getMainClassParameter()); 866 command.addArgument(getOptimizeParameter()); 867 command.addArgument(getDestFileParameter()); 868 command.addArgument(getReferencesParameter()); 869 command.addArgument(getTargetTypeParameter()); 870 command.addArgument(getUtf8OutputParameter()); 871 command.addArgument(getWin32IconParameter()); 872 command.addArgument(getWin32ResParameter()); 873 } 874 875 880 protected void addResources(NetCommand command) { 881 Enumeration e = resources.elements(); 882 while (e.hasMoreElements()) { 883 DotnetResource resource = (DotnetResource) e.nextElement(); 884 createResourceParameter(command, resource); 885 } 886 } 887 888 893 protected abstract void createResourceParameter(NetCommand command, DotnetResource resource); 894 895 896 902 903 protected int addReferenceFilesets(NetCommand command, long outputTimestamp) { 904 int filesOutOfDate = 0; 905 Hashtable filesToBuild = new Hashtable (); 906 for (int i = 0; i < referenceFilesets.size(); i++) { 907 FileSet fs = (FileSet) referenceFilesets.elementAt(i); 908 filesOutOfDate += command.scanOneFileset( 909 fs.getDirectoryScanner(getProject()), 910 filesToBuild, 911 outputTimestamp); 912 } 913 if (filesToBuild.size() == 0) { 915 return 0; 916 } 917 Enumeration files = filesToBuild.elements(); 919 while (files.hasMoreElements()) { 920 File file = (File ) files.nextElement(); 921 if (isFileManagedBinary(file)) { 922 if (isWindows) { 923 command.addArgument( 924 '"' + REFERENCE_OPTION + file.toString() + '"'); 925 } else { 926 command.addArgument(REFERENCE_OPTION + file.toString()); 927 } 928 } else { 929 log("ignoring " + file + " as it is not a managed executable", 930 Project.MSG_VERBOSE); 931 } 932 933 } 934 935 return filesOutOfDate; 936 } 937 938 942 protected NetCommand createNetCommand() { 943 NetCommand command = new NetCommand(this, getTaskName(), getExecutable()); 944 return command; 945 } 946 947 951 protected abstract void addCompilerSpecificOptions(NetCommand command); 952 953 957 public String getDefinitionsDelimiter() { 958 return ";"; 959 } 960 961 962 969 protected static boolean isFileManagedBinary(File file) { 970 String filename = file.toString().toLowerCase(); 971 return filename.endsWith(".exe") || filename.endsWith(".dll") 972 || filename.endsWith(".netmodule"); 973 } 974 975 979 public static class TargetTypes extends EnumeratedAttribute { 980 981 public String [] getValues() { 982 return new String [] { 983 "exe", 984 "library", 985 "module", 986 "winexe" 987 }; 988 } 989 } 990 991 992 } 993 994 995 | Popular Tags |