2 // ToolTask.cs: Base class for command line tool tasks.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
6 // Ankit Jain (jankit@novell.com)
8 // (C) 2005 Marek Sieradzki
9 // Copyright 2009 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Diagnostics;
34 using System.Collections;
35 using System.Collections.Specialized;
37 using System.Resources;
39 using System.Text.RegularExpressions;
40 using Microsoft.Build.Framework;
41 using Mono.XBuild.Utilities;
43 using SCS = System.Collections.Specialized;
45 namespace Microsoft.Build.Utilities
47 public abstract class ToolTask : Task
49 SCS.ProcessStringDictionary environmentOverride;
52 string toolPath, toolExe;
53 Encoding responseFileEncoding;
54 MessageImportance standardErrorLoggingImportance;
55 MessageImportance standardOutputLoggingImportance;
56 StringBuilder toolOutput;
57 bool typeLoadException;
62 this.standardErrorLoggingImportance = MessageImportance.High;
63 this.standardOutputLoggingImportance = MessageImportance.Normal;
66 protected ToolTask (ResourceManager taskResources)
67 : this (taskResources, null)
71 protected ToolTask (ResourceManager taskResources,
72 string helpKeywordPrefix)
74 this.TaskResources = taskResources;
75 this.HelpKeywordPrefix = helpKeywordPrefix;
76 this.toolPath = MonoLocationHelper.GetBinDir ();
77 this.responseFileEncoding = Encoding.UTF8;
78 this.timeout = Int32.MaxValue;
79 this.environmentOverride = new SCS.ProcessStringDictionary ();
83 protected virtual bool CallHostObjectToExecute ()
88 public override bool Execute ()
90 if (SkipTaskExecution ())
93 exitCode = ExecuteTool (GenerateFullPathToTool (), GenerateResponseFileCommands (),
94 GenerateCommandLineCommands ());
96 // HandleTaskExecutionErrors is called only if exitCode != 0
97 return exitCode == 0 || HandleTaskExecutionErrors ();
101 protected virtual string GetWorkingDirectory ()
106 protected virtual int ExecuteTool (string pathToTool,
107 string responseFileCommands,
108 string commandLineCommands)
111 if (pathToTool == null)
112 throw new ArgumentNullException ("pathToTool");
114 string output, error, responseFileName;
115 StreamWriter outwr, errwr;
117 outwr = errwr = null;
118 responseFileName = output = error = null;
119 toolOutput = new StringBuilder ();
122 string arguments = commandLineCommands;
123 if (!String.IsNullOrEmpty (responseFileCommands)) {
124 responseFileName = Path.GetTempFileName ();
125 File.WriteAllText (responseFileName, responseFileCommands);
126 arguments = arguments + " " + GetResponseFileSwitch (responseFileName);
129 LogToolCommand (String.Format ("Tool {0} execution started with arguments: {1} {2}",
130 pathToTool, commandLineCommands, responseFileCommands));
132 output = Path.GetTempFileName ();
133 error = Path.GetTempFileName ();
134 outwr = new StreamWriter (output);
135 errwr = new StreamWriter (error);
137 ProcessStartInfo pinfo = new ProcessStartInfo (pathToTool, arguments);
138 pinfo.WorkingDirectory = GetWorkingDirectory () ?? Environment.CurrentDirectory;
140 pinfo.UseShellExecute = false;
141 pinfo.RedirectStandardOutput = true;
142 pinfo.RedirectStandardError = true;
145 ProcessWrapper pw = ProcessService.StartProcess (pinfo, outwr, errwr, null, environmentOverride);
146 pw.WaitForOutput (timeout == Int32.MaxValue ? -1 : timeout);
147 exitCode = pw.ExitCode;
151 } catch (System.ComponentModel.Win32Exception e) {
152 Log.LogError ("Error executing tool '{0}': {1}", pathToTool, e.Message);
156 ProcessOutputFile (output, StandardOutputLoggingImportance);
157 ProcessOutputFile (error, StandardErrorLoggingImportance);
159 Log.LogMessage (MessageImportance.Low, "Tool {0} execution finished.", pathToTool);
162 DeleteTempFile (responseFileName);
168 DeleteTempFile (output);
169 DeleteTempFile (error);
173 void ProcessOutputFile (string filename, MessageImportance importance)
175 using (StreamReader sr = File.OpenText (filename)) {
177 while ((line = sr.ReadLine ()) != null) {
178 if (typeLoadException) {
179 toolOutput.Append (sr.ReadToEnd ());
180 string output_str = toolOutput.ToString ();
181 Regex reg = new Regex (@".*WARNING.*used in (mscorlib|System),.*",
182 RegexOptions.Multiline);
184 if (reg.Match (output_str).Success)
186 "Error: A referenced assembly may be built with an incompatible " +
187 "CLR version. See the compilation output for more details.");
190 "Error: A dependency of a referenced assembly may be missing, or " +
191 "you may be referencing an assembly created with a newer CLR " +
192 "version. See the compilation output for more details.");
194 Log.LogError (output_str);
197 toolOutput.AppendLine (line);
198 LogEventsFromTextOutput (line, importance);
203 protected virtual void LogEventsFromTextOutput (string singleLine, MessageImportance importance)
205 singleLine = singleLine.Trim ();
206 if (singleLine.Length == 0)
209 if (singleLine.StartsWith ("Unhandled Exception: System.TypeLoadException") ||
210 singleLine.StartsWith ("Unhandled Exception: System.IO.FileNotFoundException")) {
211 typeLoadException = true;
214 // When IncludeDebugInformation is true, prevents the debug symbols stats from braeking this.
215 if (singleLine.StartsWith ("WROTE SYMFILE") ||
216 singleLine.StartsWith ("OffsetTable") ||
217 singleLine.StartsWith ("Compilation succeeded") ||
218 singleLine.StartsWith ("Compilation failed"))
221 Match match = CscErrorRegex.Match (singleLine);
222 if (!match.Success) {
223 Log.LogMessage (importance, singleLine);
227 string filename = match.Result ("${file}") ?? "";
228 string line = match.Result ("${line}");
229 int lineNumber = !string.IsNullOrEmpty (line) ? Int32.Parse (line) : 0;
231 string col = match.Result ("${column}");
232 int columnNumber = 0;
233 if (!string.IsNullOrEmpty (col))
234 columnNumber = col == "255+" ? -1 : Int32.Parse (col);
236 string category = match.Result ("${level}");
237 string code = match.Result ("${number}");
238 string text = match.Result ("${message}");
240 if (String.Compare (category, "warning", StringComparison.OrdinalIgnoreCase) == 0) {
241 Log.LogWarning (null, code, null, filename, lineNumber, columnNumber, -1,
243 } else if (String.Compare (category, "error", StringComparison.OrdinalIgnoreCase) == 0) {
244 Log.LogError (null, code, null, filename, lineNumber, columnNumber, -1,
247 Log.LogMessage (importance, singleLine);
251 protected virtual string GenerateCommandLineCommands ()
256 protected abstract string GenerateFullPathToTool ();
258 protected virtual string GenerateResponseFileCommands ()
263 protected virtual string GetResponseFileSwitch (string responseFilePath)
265 return String.Format ("@{0}", responseFilePath);
268 protected virtual bool HandleTaskExecutionErrors ()
270 if (!Log.HasLoggedErrors && exitCode != 0)
271 Log.LogError ("Tool exited with code: {0}. Output: {1}", exitCode,
272 toolOutput != null ? toolOutput.ToString () : String.Empty);
275 return ExitCode == 0 && !Log.HasLoggedErrors;
278 protected virtual HostObjectInitializationStatus InitializeHostObject ()
280 return HostObjectInitializationStatus.NoActionReturnSuccess;
283 protected virtual void LogToolCommand (string message)
285 Log.LogMessage (MessageImportance.Normal, message);
289 protected virtual void LogPathToTool (string toolName,
294 protected virtual bool SkipTaskExecution()
296 return !ValidateParameters ();
299 protected virtual bool ValidateParameters()
304 protected void DeleteTempFile (string fileName)
306 if (String.IsNullOrEmpty (fileName))
310 File.Delete (fileName);
311 } catch (IOException ioe) {
312 Log.LogWarning ("Unable to delete temporary file '{0}' : {1}", ioe.Message);
313 } catch (UnauthorizedAccessException uae) {
314 Log.LogWarning ("Unable to delete temporary file '{0}' : {1}", uae.Message);
318 protected virtual StringDictionary EnvironmentOverride
320 get { return environmentOverride; }
324 public int ExitCode {
325 get { return exitCode; }
328 protected virtual Encoding ResponseFileEncoding
330 get { return responseFileEncoding; }
333 protected virtual Encoding StandardErrorEncoding
335 get { return Console.Error.Encoding; }
338 protected virtual MessageImportance StandardErrorLoggingImportance {
339 get { return standardErrorLoggingImportance; }
342 protected virtual Encoding StandardOutputEncoding
344 get { return Console.Out.Encoding; }
347 protected virtual MessageImportance StandardOutputLoggingImportance {
348 get { return standardOutputLoggingImportance; }
351 protected virtual bool HasLoggedErrors {
352 get { return Log.HasLoggedErrors; }
355 public virtual int Timeout
357 get { return timeout; }
358 set { timeout = value; }
361 public virtual string ToolExe
370 if (!String.IsNullOrEmpty (value))
375 protected abstract string ToolName
380 public string ToolPath
382 get { return toolPath; }
384 if (!String.IsNullOrEmpty (value))
389 // Snatched from our codedom code, with some changes to make it compatible with csc
390 // (the line+column group is optional is csc)
391 static Regex errorRegex;
392 static Regex CscErrorRegex {
394 if (errorRegex == null)
395 errorRegex = new Regex (@"^(\s*(?<file>[^\(]+)(\((?<line>\d*)(,(?<column>\d*[\+]*))?\))?:\s+)*(?<level>\w+)\s+(?<number>.*\d):\s*(?<message>.*)", RegexOptions.Compiled | RegexOptions.ExplicitCapture);