5 // Leszek Ciesielski <skolima@gmail.com>
6 // Marek Sieradzki <marek.sieradzki@gmail.com>
8 // Copyright (C) 2006 Forcom (http://www.forcom.com.pl/)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Text.RegularExpressions;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
37 using Mono.XBuild.Utilities;
39 namespace Microsoft.Build.Tasks {
41 public class Vbc : ManagedCompiler {
48 protected internal override void AddResponseFileCommands (
49 CommandLineBuilderExtension commandLine )
51 base.AddResponseFileCommands (commandLine);
53 commandLine.AppendSwitchIfNotNull ("/libpath:", AdditionalLibPaths, ",");
55 commandLine.AppendSwitchIfNotNull ("/baseaddress:", BaseAddress);
57 if (DefineConstants != null)
58 commandLine.AppendSwitchUnquotedIfNotNull ("/define:",
59 String.Format ("\"{0}\"", EscapeDoubleQuotes (DefineConstants)));
63 commandLine.AppendSwitchIfNotNull ("/doc:", DocumentationFile);
67 // GenerateDocumentation
70 foreach (ITaskItem item in Imports)
71 commandLine.AppendSwitchIfNotNull ("/imports:", item.ItemSpec);
73 commandLine.AppendSwitchIfNotNull ("/main:", MainEntryPoint);
76 if (Bag ["NoStandardLib"] != null && NoStandardLib)
77 commandLine.AppendSwitch ("/nostdlib");
80 commandLine.AppendSwitch ("/nowarn");
82 commandLine.AppendSwitchIfNotNull ("/optioncompare:", OptionCompare);
84 if (Bag ["OptionExplicit"] != null)
86 commandLine.AppendSwitch ("/optionexplicit+");
88 commandLine.AppendSwitch ("/optionexplicit-");
90 if (Bag ["OptionStrict"] != null)
92 commandLine.AppendSwitch ("/optionstrict+");
94 commandLine.AppendSwitch ("/optionstrict-");
96 if (Bag ["OptionInfer"] != null)
98 commandLine.AppendSwitch ("/optioninfer+");
100 commandLine.AppendSwitch ("/optioninfer-");
106 if (References != null)
107 foreach (ITaskItem item in References)
108 commandLine.AppendSwitchIfNotNull ("/reference:", item.ItemSpec);
110 if (Bag ["RemoveIntegerChecks"] != null)
111 if (RemoveIntegerChecks)
112 commandLine.AppendSwitch ("/removeintchecks+");
114 commandLine.AppendSwitch ("/removeintchecks-");
116 if (ResponseFiles != null)
117 foreach (ITaskItem item in ResponseFiles)
118 commandLine.AppendFileNameIfNotNull (String.Format ("@{0}", item.ItemSpec));
120 commandLine.AppendSwitchIfNotNull ("/rootnamespace:", RootNamespace);
122 commandLine.AppendSwitchIfNotNull ("/sdkpath:", SdkPath);
124 // TargetCompactFramework
126 if (String.Compare (VBRuntime, "Embed", StringComparison.OrdinalIgnoreCase) == 0)
127 commandLine.AppendSwitch ("/vbruntime*");
133 // WarningsNotAsErrors
137 string EscapeDoubleQuotes (string text)
142 return text.Replace ("\"", "\\\"");
146 protected override bool CallHostObjectToExecute ()
148 throw new NotImplementedException ();
152 protected override string GenerateFullPathToTool ()
154 if (!string.IsNullOrEmpty (ToolPath))
155 return Path.Combine (ToolPath, ToolExe);
156 var possibleToolPath = ToolLocationHelper.GetPathToDotNetFrameworkFile (ToolExe, TargetDotNetFrameworkVersion.VersionLatest);
157 if (!string.IsNullOrEmpty(possibleToolPath))
158 return possibleToolPath;
159 return ToolLocationHelper.GetPathToDotNetFrameworkBinFile(ToolExe);
163 protected override HostObjectInitializationStatus InitializeHostObject ()
165 throw new NotImplementedException ();
169 protected override bool ValidateParameters ()
174 protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance)
176 singleLine = singleLine.Trim ();
177 if (singleLine.Length == 0)
180 // When IncludeDebugInformation is true, prevents the debug symbols stats from braeking this.
181 if (singleLine.StartsWith ("WROTE SYMFILE") ||
182 singleLine.StartsWith ("OffsetTable") ||
183 singleLine.StartsWith ("Compilation succeeded") ||
184 singleLine.StartsWith ("Compilation failed"))
187 Match match = ErrorRegex.Match (singleLine);
188 if (!match.Success) {
189 Log.LogMessage (messageImportance, singleLine);
193 string filename = match.Result ("${file}") ?? "";
195 string line = match.Result ("${line}");
196 int lineNumber = !string.IsNullOrEmpty (line) ? Int32.Parse (line) : 0;
198 string col = match.Result ("${column}");
199 int columnNumber = 0;
200 if (!string.IsNullOrEmpty (col))
201 columnNumber = col.IndexOf ("+") >= 0 ? -1 : Int32.Parse (col);
203 string category = match.Result ("${level}");
204 string code = match.Result ("${number}");
205 string text = match.Result ("${message}");
207 if (String.Compare (category, "warning", StringComparison.OrdinalIgnoreCase) == 0) {
208 Log.LogWarning (null, code, null, filename, lineNumber, columnNumber, -1,
210 } else if (String.Compare (category, "error", StringComparison.OrdinalIgnoreCase) == 0) {
211 Log.LogError (null, code, null, filename, lineNumber, columnNumber, -1,
214 Log.LogMessage (messageImportance, singleLine);
219 public string BaseAddress {
220 get { return (string) Bag ["BaseAddress"]; }
221 set { Bag ["BaseAddress"] = value; }
225 public string DisabledWarnings {
226 get { return (string) Bag ["DisabledWarnings"]; }
227 set { Bag ["DisabledWarnings"] = value; }
231 public string DocumentationFile {
232 get { return (string) Bag ["DocumentationFile"]; }
233 set { Bag ["DocumentationFile"] = value; }
237 public string ErrorReport {
238 get { return (string) Bag ["ErrorReport"]; }
239 set { Bag ["ErrorReport"] = value; }
243 public bool GenerateDocumentation {
244 get { return GetBoolParameterWithDefault ("GenerateDocumentation", false); }
245 set { Bag ["GenerateDocumentation"] = value; }
249 public ITaskItem [] Imports {
250 get { return (ITaskItem []) Bag ["Imports"]; }
251 set { Bag ["Imports"] = value; }
255 public bool NoStandardLib {
256 get { return GetBoolParameterWithDefault ("NoStandardLib", false); }
257 set { Bag ["NoStandardLib"] = value; }
261 public bool NoWarnings {
262 get { return GetBoolParameterWithDefault ("NoWarnings", false); }
263 set { Bag ["NoWarnings"] = value; }
267 public string OptionCompare {
268 get { return (string) Bag ["OptionCompare"]; }
269 set { Bag ["OptionCompare"] = value; }
273 public bool OptionExplicit {
274 get { return GetBoolParameterWithDefault ("OptionExplicit", false); }
275 set { Bag ["OpExplicit"] = value; }
279 public bool OptionStrict {
280 get { return GetBoolParameterWithDefault ("OptionStrict", false); }
281 set { Bag ["OptionStrict"] = value; }
285 public string OptionStrictType {
286 get { return (string) Bag ["OptionStrictType"]; }
287 set { Bag ["OptionStrictType"] = value; }
291 public bool OptionInfer {
292 get { return GetBoolParameterWithDefault ("OptionInfer", false); }
293 set { Bag ["OptionInfer"] = value; }
297 public string Platform {
298 get { return (string) Bag ["Platfrom"]; }
299 set { Bag ["Platform"] = value; }
303 public bool RemoveIntegerChecks {
304 get { return GetBoolParameterWithDefault ("RemoveIntegerChecks", false); }
305 set { Bag ["RemoveIntegerChecks"] = value; }
309 public string RootNamespace {
310 get { return (string) Bag ["RootNamespace"]; }
311 set { Bag ["RootNamespace"] = value; }
315 public string SdkPath {
316 get { return (string) Bag ["SdkPath"]; }
317 set { Bag ["SdkPath"] = value; }
321 public bool TargetCompactFramework {
322 get { return (bool) Bag ["TargetCompactFramework"]; }
323 set { Bag ["TargetCompactFramework"] = value; }
327 protected override string ToolName {
329 return MSBuildUtils.RunningOnWindows ? "vbnc.bat" : "vbnc";
334 public bool UseHostCompilerIfAvailable {
335 get { return (bool) Bag ["UseHostCompilerIfAvailable"]; }
336 set { Bag ["UseHostCompilerIfAvailable"] = value; }
340 public string VBRuntime {
341 get { return (string) Bag ["VBRuntime"]; }
342 set { Bag ["VBRuntime"] = value; }
346 public string Verbosity {
347 get { return (string) Bag ["Verbosity"]; }
348 set { Bag ["Verbosity"] = value; }
352 public string WarningsAsErrors {
353 get { return (string) Bag ["WarningsAsErrors"]; }
354 set { Bag ["WarningsAsErrors"] = value; }
358 public string WarningsNotAsErrors {
359 get { return (string) Bag ["WarningsNotAsErrors"]; }
360 set { Bag ["WarningsNotAsErrors"] = value; }
363 // from md's VBBindingCompilerServices.cs
364 //matches "/home/path/Default.aspx.vb (40,31) : Error VBNC30205: Expected end of statement."
365 //and "Error : VBNC99999: vbnc crashed nearby this location in the source code."
366 //and "Error : VBNC99999: Unexpected error: Object reference not set to an instance of an object"
367 static Regex errorRegex;
368 static Regex ErrorRegex {
370 if (errorRegex == null)
371 errorRegex = new Regex (@"^\s*((?<file>.*)\s?\((?<line>\d*)(,(?<column>\d*))?\) : )?(?<level>\w+) :? ?(?<number>[^:]*): (?<message>.*)$", RegexOptions.Compiled | RegexOptions.ExplicitCapture);