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 if (!File.Exists (pathToTool)) {
115 Log.LogError ("Tool not found at {0}", pathToTool);
119 string output, error, responseFileName;
120 StreamWriter outwr, errwr;
122 outwr = errwr = null;
123 responseFileName = output = error = null;
124 toolOutput = new StringBuilder ();
127 string arguments = commandLineCommands;
128 if (!String.IsNullOrEmpty (responseFileCommands)) {
129 responseFileName = Path.GetTempFileName ();
130 File.WriteAllText (responseFileName, responseFileCommands);
131 arguments = arguments + " " + GetResponseFileSwitch (responseFileName);
134 LogToolCommand (String.Format ("Tool {0} execution started with arguments: {1} {2}",
135 pathToTool, commandLineCommands, responseFileCommands));
137 output = Path.GetTempFileName ();
138 error = Path.GetTempFileName ();
139 outwr = new StreamWriter (output);
140 errwr = new StreamWriter (error);
142 ProcessStartInfo pinfo = new ProcessStartInfo (pathToTool, arguments);
143 pinfo.WorkingDirectory = GetWorkingDirectory () ?? Environment.CurrentDirectory;
145 pinfo.UseShellExecute = false;
146 pinfo.RedirectStandardOutput = true;
147 pinfo.RedirectStandardError = true;
150 ProcessWrapper pw = ProcessService.StartProcess (pinfo, outwr, errwr, null, environmentOverride);
151 pw.WaitForOutput (timeout == Int32.MaxValue ? -1 : timeout);
152 exitCode = pw.ExitCode;
156 } catch (System.ComponentModel.Win32Exception e) {
157 Log.LogError ("Error executing tool '{0}': {1}", pathToTool, e.Message);
161 ProcessOutputFile (output, StandardOutputLoggingImportance);
162 ProcessOutputFile (error, StandardErrorLoggingImportance);
164 Log.LogMessage (MessageImportance.Low, "Tool {0} execution finished.", pathToTool);
167 DeleteTempFile (responseFileName);
173 DeleteTempFile (output);
174 DeleteTempFile (error);
178 void ProcessOutputFile (string filename, MessageImportance importance)
180 using (StreamReader sr = File.OpenText (filename)) {
182 while ((line = sr.ReadLine ()) != null) {
183 if (typeLoadException) {
184 toolOutput.Append (sr.ReadToEnd ());
185 string output_str = toolOutput.ToString ();
186 Regex reg = new Regex (@".*WARNING.*used in (mscorlib|System),.*",
187 RegexOptions.Multiline);
189 if (reg.Match (output_str).Success)
191 "Error: A referenced assembly may be built with an incompatible " +
192 "CLR version. See the compilation output for more details.");
195 "Error: A dependency of a referenced assembly may be missing, or " +
196 "you may be referencing an assembly created with a newer CLR " +
197 "version. See the compilation output for more details.");
199 Log.LogError (output_str);
202 toolOutput.AppendLine (line);
203 LogEventsFromTextOutput (line, importance);
208 protected virtual void LogEventsFromTextOutput (string singleLine, MessageImportance importance)
210 singleLine = singleLine.Trim ();
211 if (singleLine.Length == 0)
214 if (singleLine.StartsWith ("Unhandled Exception: System.TypeLoadException") ||
215 singleLine.StartsWith ("Unhandled Exception: System.IO.FileNotFoundException")) {
216 typeLoadException = true;
219 // When IncludeDebugInformation is true, prevents the debug symbols stats from braeking this.
220 if (singleLine.StartsWith ("WROTE SYMFILE") ||
221 singleLine.StartsWith ("OffsetTable") ||
222 singleLine.StartsWith ("Compilation succeeded") ||
223 singleLine.StartsWith ("Compilation failed"))
226 Match match = CscErrorRegex.Match (singleLine);
230 string filename = match.Result ("${file}") ?? "";
231 string line = match.Result ("${line}");
232 int lineNumber = !string.IsNullOrEmpty (line) ? Int32.Parse (line) : 0;
234 string col = match.Result ("${column}");
235 int columnNumber = 0;
236 if (!string.IsNullOrEmpty (col))
237 columnNumber = col == "255+" ? -1 : Int32.Parse (col);
239 string category = match.Result ("${level}");
240 string code = match.Result ("${number}");
241 string text = match.Result ("${message}");
243 if (String.Compare (category, "warning", StringComparison.OrdinalIgnoreCase) == 0) {
244 Log.LogWarning (null, code, null, filename, lineNumber, columnNumber, -1,
246 } else if (String.Compare (category, "error", StringComparison.OrdinalIgnoreCase) == 0) {
247 Log.LogError (null, code, null, filename, lineNumber, columnNumber, -1,
250 Log.LogMessage (importance, singleLine);
254 protected virtual string GenerateCommandLineCommands ()
259 protected abstract string GenerateFullPathToTool ();
261 protected virtual string GenerateResponseFileCommands ()
266 protected virtual string GetResponseFileSwitch (string responseFilePath)
268 return String.Format ("@{0}", responseFilePath);
271 protected virtual bool HandleTaskExecutionErrors ()
273 if (!Log.HasLoggedErrors && exitCode != 0)
274 Log.LogError ("Tool exited with code: {0}. Output: {1}", exitCode,
275 toolOutput != null ? toolOutput.ToString () : String.Empty);
278 return ExitCode == 0 && !Log.HasLoggedErrors;
281 protected virtual HostObjectInitializationStatus InitializeHostObject ()
283 return HostObjectInitializationStatus.NoActionReturnSuccess;
286 protected virtual void LogToolCommand (string message)
288 Log.LogMessage (MessageImportance.Normal, message);
292 protected virtual void LogPathToTool (string toolName,
297 protected virtual bool SkipTaskExecution()
299 return !ValidateParameters ();
302 protected virtual bool ValidateParameters()
307 protected void DeleteTempFile (string fileName)
309 if (String.IsNullOrEmpty (fileName))
313 File.Delete (fileName);
314 } catch (IOException ioe) {
315 Log.LogWarning ("Unable to delete temporary file '{0}' : {1}", ioe.Message);
316 } catch (UnauthorizedAccessException uae) {
317 Log.LogWarning ("Unable to delete temporary file '{0}' : {1}", uae.Message);
321 protected virtual StringDictionary EnvironmentOverride
323 get { return environmentOverride; }
327 public int ExitCode {
328 get { return exitCode; }
331 protected virtual Encoding ResponseFileEncoding
333 get { return responseFileEncoding; }
336 protected virtual Encoding StandardErrorEncoding
338 get { return Console.Error.Encoding; }
341 protected virtual MessageImportance StandardErrorLoggingImportance {
342 get { return standardErrorLoggingImportance; }
345 protected virtual Encoding StandardOutputEncoding
347 get { return Console.Out.Encoding; }
350 protected virtual MessageImportance StandardOutputLoggingImportance {
351 get { return standardOutputLoggingImportance; }
354 protected virtual bool HasLoggedErrors {
355 get { return Log.HasLoggedErrors; }
358 public virtual int Timeout
360 get { return timeout; }
361 set { timeout = value; }
364 public virtual string ToolExe
373 if (!String.IsNullOrEmpty (value))
378 protected abstract string ToolName
383 public string ToolPath
385 get { return toolPath; }
387 if (!String.IsNullOrEmpty (value))
392 // Snatched from our codedom code, with some changes to make it compatible with csc
393 // (the line+column group is optional is csc)
394 static Regex errorRegex;
395 static Regex CscErrorRegex {
397 if (errorRegex == null)
398 errorRegex = new Regex (@"^(\s*(?<file>[^\(]+)(\((?<line>\d*)(,(?<column>\d*[\+]*))?\))?:\s+)*(?<level>\w+)\s+(?<number>.*\d):\s*(?<message>.*)", RegexOptions.Compiled | RegexOptions.ExplicitCapture);