1. /*
  2. * Copyright 2001-2004 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. package org.apache.tools.ant.taskdefs.optional.dotnet;
  18. import org.apache.tools.ant.BuildException;
  19. /**
  20. * This task compiles Visual Basic.NET source into executables or modules.
  21. * The task requires vbc.exe on the execute path, unless it or an equivalent
  22. * program is specified in the <tt>executable</tt> parameter
  23. *
  24. * <p>
  25. * All parameters are optional: <vbc/> should suffice to produce a debug
  26. * build of all *.vb files.
  27. *
  28. * <p>
  29. * The task is a directory based task, so attributes like
  30. * <tt>includes="**\/*.vb"</tt> and
  31. * <tt>excludes="broken.vb"</tt> can be used to control
  32. * the files pulled in. By default,
  33. * all *.vb files from the project folder down are included in the command.
  34. * When this happens the destFile -if not specified-
  35. * is taken as the first file in the list, which may be somewhat hard to control.
  36. Specifying the output file with <tt>destfile</tt> is prudent.
  37. </p>
  38. <p>
  39. * Also, dependency checking only works if destfile is set.
  40. *
  41. * As with <csc> nested <tt>src</tt> filesets of source,
  42. * reference filesets, definitions and resources can be provided.
  43. *
  44. * <p>
  45. * Example
  46. * </p>
  47. * <pre><vbc
  48. * optimize="true"
  49. * debug="false"
  50. * warnLevel="4"
  51. * targetType="exe"
  52. * definitions="RELEASE"
  53. * excludes="src/unicode_class.vb"
  54. * mainClass = "MainApp"
  55. * destFile="NetApp.exe"
  56. * optionExplicit="true"
  57. * optionCompare="text"
  58. * references="System.Xml,System.Web.Xml"
  59. * >
  60. * <reference file="${testCSC.dll}" />
  61. * <define name="RELEASE" />
  62. * <define name="DEBUG" if="debug.property"/>
  63. * <define name="def3" unless="def2.property"/>
  64. * </vbc>
  65. </pre>
  66. * @ant.task name="vbc" category="dotnet"
  67. */
  68. public class VisualBasicCompile extends DotnetCompile {
  69. /**
  70. * Compiler option to remove integer checks. Default: false.
  71. */
  72. private boolean removeIntChecks = false;
  73. /**
  74. * Require explicit declaration of variables? Default: false.
  75. */
  76. private boolean optionExplicit = false;
  77. /**
  78. * Enforce strict language semantics? Default: false.
  79. */
  80. private boolean optionStrict = false;
  81. /**
  82. * Whether to compare strings as "text" or "binary". Default: "binary".
  83. */
  84. private String optionCompare;
  85. /**
  86. * Root namespace for all type declarations.
  87. */
  88. private String rootNamespace;
  89. /**
  90. * Declare global imports fornamespaces in referenced metadata files.
  91. */
  92. private String imports;
  93. /**
  94. * Constructor for VisualBasicCompile.
  95. */
  96. public VisualBasicCompile() {
  97. clear();
  98. }
  99. /**
  100. * reset all contents.
  101. */
  102. public void clear() {
  103. super.clear();
  104. imports = null;
  105. rootNamespace = null;
  106. optionCompare = null;
  107. optionExplicit = false;
  108. optionStrict = false;
  109. removeIntChecks = false;
  110. setExecutable("vbc");
  111. }
  112. /**
  113. * get the argument or null for no argument needed
  114. * This is overridden from DotnetCompile.java because VBC uses
  115. * "/win32resource:" rather than "/win32res:"
  116. *
  117. *@return The Win32Res Parameter to CSC
  118. */
  119. protected String getWin32ResParameter() {
  120. if (getWin32Res() != null) {
  121. return "/win32resource:" + getWin32Res().toString();
  122. } else {
  123. return null;
  124. }
  125. }
  126. /**
  127. * Whether to remove integer checks. Default false.
  128. * @param flag on/off flag
  129. */
  130. public void setRemoveIntChecks(boolean flag) {
  131. removeIntChecks = flag;
  132. }
  133. /**
  134. * Get the flag for removing integer checks.
  135. * @return true if flag is turned on
  136. */
  137. public boolean getRemoveIntChecks() {
  138. return removeIntChecks;
  139. }
  140. /**
  141. * Form the option string for removeIntChecks.
  142. * @return The parameter string.
  143. */
  144. public String getRemoveIntChecksParameter() {
  145. return "/removeintchecks" + (removeIntChecks ? "+" : "-");
  146. }
  147. /**
  148. * Whether to require explicit declaration of variables.
  149. * @param flag on/off flag
  150. */
  151. public void setOptionExplicit(boolean flag) {
  152. optionExplicit = flag;
  153. }
  154. /**
  155. * Get the flag for whether to require explicit declaration of variables.
  156. *@return true if flag is turned on
  157. */
  158. public boolean getOptionExplicit() {
  159. return optionExplicit;
  160. }
  161. /**
  162. * Form the option string for optionExplicit..
  163. * @return The parameter string.
  164. */
  165. public String getOptionExplicitParameter() {
  166. return "/optionexplicit" + (optionExplicit ? "+" : "-");
  167. }
  168. /**
  169. * Enforce strict language semantics.
  170. * @param flag on/off flag
  171. */
  172. public void setOptionStrict(boolean flag) {
  173. optionStrict = flag;
  174. }
  175. /**
  176. * Get the flag for whether to enforce strict language semantics.
  177. * @return true if flag is turned on
  178. */
  179. public boolean getOptionStrict() {
  180. return optionStrict;
  181. }
  182. /**
  183. * For the option string for optionStrict.
  184. * @return The parameter string.
  185. */
  186. public String getOptionStrictParameter() {
  187. return "/optionstrict" + (optionStrict ? "+" : "-");
  188. }
  189. /**
  190. * Specifies the root namespace for all type declarations.
  191. * @param rootNamespace a root namespace.
  192. */
  193. public void setRootNamespace(String rootNamespace) {
  194. this.rootNamespace = rootNamespace;
  195. }
  196. /**
  197. * Get the root namespace.
  198. * @return the root namespace.
  199. */
  200. public String getRootNamespace() {
  201. return this.rootNamespace;
  202. }
  203. /**
  204. * Form the option string for rootNamespace.
  205. * @return the root namespace option string.
  206. */
  207. protected String getRootNamespaceParameter() {
  208. if (rootNamespace != null && rootNamespace.length() != 0) {
  209. return "/rootnamespace:" + rootNamespace;
  210. } else {
  211. return null;
  212. }
  213. }
  214. /**
  215. * Declare global imports for namespaces in referenced metadata files.
  216. * @param imports the imports string
  217. */
  218. public void setImports(String imports) {
  219. this.imports = imports;
  220. }
  221. /**
  222. * Get global imports for namespaces in referenced metadata files.
  223. * @return the imports string.
  224. */
  225. public String getImports() {
  226. return this.imports;
  227. }
  228. /**
  229. * Format the option for imports.
  230. * @return the formatted import option.
  231. */
  232. protected String getImportsParameter() {
  233. if (imports != null && imports.length() != 0) {
  234. return "/imports:" + imports;
  235. } else {
  236. return null;
  237. }
  238. }
  239. /**
  240. * Specify binary- or text-style string comparisons. Defaults
  241. * to "binary"
  242. * @param optionCompare the option compare style. "text" | "binary".
  243. */
  244. public void setOptionCompare(String optionCompare) {
  245. if ("text".equalsIgnoreCase(optionCompare)) {
  246. this.optionCompare = "text";
  247. } else {
  248. this.optionCompare = "binary";
  249. }
  250. }
  251. /**
  252. * "binary" or "text" for the string-comparison style.
  253. * @return the option compare style.
  254. */
  255. public String getOptionCompare() {
  256. return this.optionCompare;
  257. }
  258. /**
  259. * Format the option for string comparison style.
  260. * @return The formatted option.
  261. */
  262. protected String getOptionCompareParameter() {
  263. if (optionCompare != null && "text".equalsIgnoreCase(optionCompare)) {
  264. return "/optioncompare:text";
  265. } else {
  266. return "/optioncompare:binary";
  267. }
  268. }
  269. /**
  270. * implement VBC commands
  271. * @param command
  272. */
  273. protected void addCompilerSpecificOptions(NetCommand command) {
  274. command.addArgument(getRemoveIntChecksParameter());
  275. command.addArgument(getImportsParameter());
  276. command.addArgument(getOptionExplicitParameter());
  277. command.addArgument(getOptionStrictParameter());
  278. command.addArgument(getRootNamespaceParameter());
  279. command.addArgument(getOptionCompareParameter());
  280. }
  281. /**
  282. * Get the delimiter that the compiler uses between references.
  283. * For example, c# will return ";"; VB.NET will return ","
  284. * @return The string delimiter for the reference string.
  285. */
  286. public String getReferenceDelimiter() {
  287. return ",";
  288. }
  289. /**
  290. * Get the extension of filenames to compile.
  291. * @return The string extension of files to compile.
  292. */
  293. public String getFileExtension() {
  294. return "vb";
  295. }
  296. /**
  297. * from a resource, get the resource param
  298. * @param resource
  299. * @return a string containing the resource param, or a null string
  300. * to conditionally exclude a resource.
  301. */
  302. protected String createResourceParameter(DotnetResource resource) {
  303. return resource.getVbStyleParameter();
  304. }
  305. /**
  306. * validation code
  307. * @throws BuildException if validation failed
  308. */
  309. protected void validate()
  310. throws BuildException {
  311. super.validate();
  312. if (getDestFile() == null) {
  313. throw new BuildException("DestFile was not specified");
  314. }
  315. }
  316. }