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 return ToolLocationHelper.GetPathToDotNetFrameworkFile (ToolExe, TargetDotNetFrameworkVersion.VersionLatest);
160 protected override HostObjectInitializationStatus InitializeHostObject ()
162 throw new NotImplementedException ();
166 protected override bool ValidateParameters ()
171 protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance)
173 singleLine = singleLine.Trim ();
174 if (singleLine.Length == 0)
177 // When IncludeDebugInformation is true, prevents the debug symbols stats from braeking this.
178 if (singleLine.StartsWith ("WROTE SYMFILE") ||
179 singleLine.StartsWith ("OffsetTable") ||
180 singleLine.StartsWith ("Compilation succeeded") ||
181 singleLine.StartsWith ("Compilation failed"))
184 Match match = ErrorRegex.Match (singleLine);
185 if (!match.Success) {
186 Log.LogMessage (messageImportance, singleLine);
190 string filename = match.Result ("${file}") ?? "";
192 string line = match.Result ("${line}");
193 int lineNumber = !string.IsNullOrEmpty (line) ? Int32.Parse (line) : 0;
195 string col = match.Result ("${column}");
196 int columnNumber = 0;
197 if (!string.IsNullOrEmpty (col))
198 columnNumber = col.IndexOf ("+") >= 0 ? -1 : Int32.Parse (col);
200 string category = match.Result ("${level}");
201 string code = match.Result ("${number}");
202 string text = match.Result ("${message}");
204 if (String.Compare (category, "warning", StringComparison.OrdinalIgnoreCase) == 0) {
205 Log.LogWarning (null, code, null, filename, lineNumber, columnNumber, -1,
207 } else if (String.Compare (category, "error", StringComparison.OrdinalIgnoreCase) == 0) {
208 Log.LogError (null, code, null, filename, lineNumber, columnNumber, -1,
211 Log.LogMessage (messageImportance, singleLine);
216 public string BaseAddress {
217 get { return (string) Bag ["BaseAddress"]; }
218 set { Bag ["BaseAddress"] = value; }
222 public string DisabledWarnings {
223 get { return (string) Bag ["DisabledWarnings"]; }
224 set { Bag ["DisabledWarnings"] = value; }
228 public string DocumentationFile {
229 get { return (string) Bag ["DocumentationFile"]; }
230 set { Bag ["DocumentationFile"] = value; }
234 public string ErrorReport {
235 get { return (string) Bag ["ErrorReport"]; }
236 set { Bag ["ErrorReport"] = value; }
240 public bool GenerateDocumentation {
241 get { return GetBoolParameterWithDefault ("GenerateDocumentation", false); }
242 set { Bag ["GenerateDocumentation"] = value; }
246 public ITaskItem [] Imports {
247 get { return (ITaskItem []) Bag ["Imports"]; }
248 set { Bag ["Imports"] = value; }
252 public bool NoStandardLib {
253 get { return GetBoolParameterWithDefault ("NoStandardLib", false); }
254 set { Bag ["NoStandardLib"] = value; }
258 public bool NoWarnings {
259 get { return GetBoolParameterWithDefault ("NoWarnings", false); }
260 set { Bag ["NoWarnings"] = value; }
264 public string OptionCompare {
265 get { return (string) Bag ["OptionCompare"]; }
266 set { Bag ["OptionCompare"] = value; }
270 public bool OptionExplicit {
271 get { return GetBoolParameterWithDefault ("OptionExplicit", false); }
272 set { Bag ["OpExplicit"] = value; }
276 public bool OptionStrict {
277 get { return GetBoolParameterWithDefault ("OptionStrict", false); }
278 set { Bag ["OptionStrict"] = value; }
282 public string OptionStrictType {
283 get { return (string) Bag ["OptionStrictType"]; }
284 set { Bag ["OptionStrictType"] = value; }
288 public bool OptionInfer {
289 get { return GetBoolParameterWithDefault ("OptionInfer", false); }
290 set { Bag ["OptionInfer"] = value; }
294 public string Platform {
295 get { return (string) Bag ["Platfrom"]; }
296 set { Bag ["Platform"] = value; }
300 public bool RemoveIntegerChecks {
301 get { return GetBoolParameterWithDefault ("RemoveIntegerChecks", false); }
302 set { Bag ["RemoveIntegerChecks"] = value; }
306 public string RootNamespace {
307 get { return (string) Bag ["RootNamespace"]; }
308 set { Bag ["RootNamespace"] = value; }
312 public string SdkPath {
313 get { return (string) Bag ["SdkPath"]; }
314 set { Bag ["SdkPath"] = value; }
318 public bool TargetCompactFramework {
319 get { return (bool) Bag ["TargetCompactFramework"]; }
320 set { Bag ["TargetCompactFramework"] = value; }
324 protected override string ToolName {
326 return MSBuildUtils.RunningOnWindows ? "vbnc.bat" : "vbnc";
331 public bool UseHostCompilerIfAvailable {
332 get { return (bool) Bag ["UseHostCompilerIfAvailable"]; }
333 set { Bag ["UseHostCompilerIfAvailable"] = value; }
337 public string VBRuntime {
338 get { return (string) Bag ["VBRuntime"]; }
339 set { Bag ["VBRuntime"] = value; }
343 public string Verbosity {
344 get { return (string) Bag ["Verbosity"]; }
345 set { Bag ["Verbosity"] = value; }
349 public string WarningsAsErrors {
350 get { return (string) Bag ["WarningsAsErrors"]; }
351 set { Bag ["WarningsAsErrors"] = value; }
355 public string WarningsNotAsErrors {
356 get { return (string) Bag ["WarningsNotAsErrors"]; }
357 set { Bag ["WarningsNotAsErrors"] = value; }
360 // from md's VBBindingCompilerServices.cs
361 //matches "/home/path/Default.aspx.vb (40,31) : Error VBNC30205: Expected end of statement."
362 //and "Error : VBNC99999: vbnc crashed nearby this location in the source code."
363 //and "Error : VBNC99999: Unexpected error: Object reference not set to an instance of an object"
364 static Regex errorRegex;
365 static Regex ErrorRegex {
367 if (errorRegex == null)
368 errorRegex = new Regex (@"^\s*((?<file>.*)\s?\((?<line>\d*)(,(?<column>\d*))?\) : )?(?<level>\w+) :? ?(?<number>[^:]*): (?<message>.*)$", RegexOptions.Compiled | RegexOptions.ExplicitCapture);