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
51 string toolPath, toolExe;
52 Encoding responseFileEncoding;
53 MessageImportance standardErrorLoggingImportance;
54 MessageImportance standardOutputLoggingImportance;
55 StringBuilder toolOutput;
56 bool typeLoadException;
61 this.standardErrorLoggingImportance = MessageImportance.High;
62 this.standardOutputLoggingImportance = MessageImportance.Normal;
65 protected ToolTask (ResourceManager taskResources)
66 : this (taskResources, null)
70 protected ToolTask (ResourceManager taskResources,
71 string helpKeywordPrefix)
73 this.TaskResources = taskResources;
74 this.HelpKeywordPrefix = helpKeywordPrefix;
75 this.toolPath = MonoLocationHelper.GetBinDir ();
76 this.responseFileEncoding = Encoding.UTF8;
77 this.timeout = Int32.MaxValue;
81 protected virtual bool CallHostObjectToExecute ()
86 public override bool Execute ()
88 if (SkipTaskExecution ())
91 exitCode = ExecuteTool (GenerateFullPathToTool (), GenerateResponseFileCommands (),
92 GenerateCommandLineCommands ());
94 // HandleTaskExecutionErrors is called only if exitCode != 0
95 return exitCode == 0 || HandleTaskExecutionErrors ();
99 protected virtual string GetWorkingDirectory ()
104 protected virtual int ExecuteTool (string pathToTool,
105 string responseFileCommands,
106 string commandLineCommands)
109 if (pathToTool == null)
110 throw new ArgumentNullException ("pathToTool");
112 string responseFileName;
113 responseFileName = null;
114 toolOutput = new StringBuilder ();
117 string responseFileSwitch = String.Empty;
118 if (!String.IsNullOrEmpty (responseFileCommands)) {
119 responseFileName = Path.GetTempFileName ();
120 File.WriteAllText (responseFileName, responseFileCommands);
121 responseFileSwitch = GetResponseFileSwitch (responseFileName);
124 var pinfo = GetProcessStartInfo (pathToTool, commandLineCommands, responseFileSwitch);
125 LogToolCommand (String.Format ("Tool {0} execution started with arguments: {1} {2}",
126 pinfo.FileName, commandLineCommands, responseFileCommands));
128 var pendingLineFragmentOutput = new StringBuilder ();
129 var pendingLineFragmentError = new StringBuilder ();
130 var environmentOverride = GetAndLogEnvironmentVariables ();
132 // When StartProcess returns, the process has already .Start()'ed
133 // If we subscribe to the events after that, then for processes that
134 // finish executing before we can subscribe, we won't get the output/err
136 ProcessWrapper pw = ProcessService.StartProcess (pinfo,
137 (_, msg) => ProcessLine (pendingLineFragmentOutput, msg, StandardOutputLoggingImportance),
138 (_, msg) => ProcessLine (pendingLineFragmentError, msg, StandardErrorLoggingImportance),
140 environmentOverride);
142 pw.WaitForOutput (timeout == Int32.MaxValue ? Int32.MaxValue : timeout);
144 // Process any remaining line
145 ProcessLine (pendingLineFragmentOutput, StandardOutputLoggingImportance, true);
146 ProcessLine (pendingLineFragmentError, StandardErrorLoggingImportance, true);
148 exitCode = pw.ExitCode;
150 } catch (System.ComponentModel.Win32Exception e) {
151 Log.LogError ("Error executing tool '{0}': {1}", pathToTool, e.Message);
155 if (typeLoadException)
156 ProcessTypeLoadException ();
158 pendingLineFragmentOutput.Length = 0;
159 pendingLineFragmentError.Length = 0;
161 Log.LogMessage (MessageImportance.Low, "Tool {0} execution finished.", pathToTool);
164 DeleteTempFile (responseFileName);
168 void ProcessTypeLoadException ()
170 string output_str = toolOutput.ToString ();
171 Regex reg = new Regex (@".*WARNING.*used in (mscorlib|System),.*",
172 RegexOptions.Multiline);
174 if (reg.Match (output_str).Success)
176 "Error: A referenced assembly may be built with an incompatible " +
177 "CLR version. See the compilation output for more details.");
180 "Error: A dependency of a referenced assembly may be missing, or " +
181 "you may be referencing an assembly created with a newer CLR " +
182 "version. See the compilation output for more details.");
184 Log.LogError (output_str);
187 void ProcessLine (StringBuilder outputBuilder, MessageImportance importance, bool isLastLine)
189 if (outputBuilder.Length == 0)
192 if (isLastLine && !outputBuilder.ToString ().EndsWith (Environment.NewLine))
193 // last line, but w/o an trailing newline, so add that
194 outputBuilder.Append (Environment.NewLine);
196 ProcessLine (outputBuilder, null, importance);
199 void ProcessLine (StringBuilder outputBuilder, string line, MessageImportance importance)
201 // Add to any line fragment from previous call
203 outputBuilder.Append (line);
205 // Don't remove empty lines!
206 var lines = outputBuilder.ToString ().Split (new string [] {Environment.NewLine}, StringSplitOptions.None);
208 // Clear the builder. If any incomplete line is found,
209 // then it will get added back
210 outputBuilder.Length = 0;
211 for (int i = 0; i < lines.Length; i ++) {
212 string singleLine = lines [i];
213 if (i == lines.Length - 1 && !singleLine.EndsWith (Environment.NewLine)) {
214 // Last line doesn't end in newline, could be part of
215 // a bigger line. Save for later processing
216 outputBuilder.Append (singleLine);
220 toolOutput.AppendLine (singleLine);
222 // in case of typeLoadException, collect all the output
223 // and then handle in ProcessTypeLoadException
224 if (!typeLoadException)
225 LogEventsFromTextOutput (singleLine, importance);
229 protected virtual void LogEventsFromTextOutput (string singleLine, MessageImportance importance)
231 if (singleLine.Length == 0) {
232 Log.LogMessage (singleLine, importance);
236 if (singleLine.StartsWith ("Unhandled Exception: System.TypeLoadException") ||
237 singleLine.StartsWith ("Unhandled Exception: System.IO.FileNotFoundException")) {
238 typeLoadException = true;
241 // When IncludeDebugInformation is true, prevents the debug symbols stats from braeking this.
242 if (singleLine.StartsWith ("WROTE SYMFILE") ||
243 singleLine.StartsWith ("OffsetTable") ||
244 singleLine.StartsWith ("Compilation succeeded") ||
245 singleLine.StartsWith ("Compilation failed"))
248 Match match = CscErrorRegex.Match (singleLine);
249 if (!match.Success) {
250 Log.LogMessage (importance, singleLine);
254 string filename = match.Result ("${file}") ?? "";
255 string line = match.Result ("${line}");
256 int lineNumber = !string.IsNullOrEmpty (line) ? Int32.Parse (line) : 0;
258 string col = match.Result ("${column}");
259 int columnNumber = 0;
260 if (!string.IsNullOrEmpty (col))
261 columnNumber = col.IndexOf ("+") >= 0 ? -1 : Int32.Parse (col);
263 string category = match.Result ("${level}");
264 string code = match.Result ("${number}");
265 string text = match.Result ("${message}");
267 if (String.Compare (category, "warning", StringComparison.OrdinalIgnoreCase) == 0) {
268 Log.LogWarning (null, code, null, filename, lineNumber, columnNumber, -1,
270 } else if (String.Compare (category, "error", StringComparison.OrdinalIgnoreCase) == 0) {
271 Log.LogError (null, code, null, filename, lineNumber, columnNumber, -1,
274 Log.LogMessage (importance, singleLine);
278 protected virtual string GenerateCommandLineCommands ()
283 protected abstract string GenerateFullPathToTool ();
285 protected virtual string GenerateResponseFileCommands ()
290 protected virtual string GetResponseFileSwitch (string responseFilePath)
292 return String.Format ("@{0}", responseFilePath);
295 protected virtual ProcessStartInfo GetProcessStartInfo (string pathToTool, string commandLineCommands, string responseFileSwitch)
297 var pinfo = new ProcessStartInfo (pathToTool, String.Format ("{0} {1}", commandLineCommands, responseFileSwitch));
299 pinfo.WorkingDirectory = GetWorkingDirectory () ?? Environment.CurrentDirectory;
300 pinfo.UseShellExecute = false;
301 pinfo.RedirectStandardOutput = true;
302 pinfo.RedirectStandardError = true;
307 protected virtual bool HandleTaskExecutionErrors ()
309 if (!Log.HasLoggedErrors && exitCode != 0)
310 Log.LogError ("Tool exited with code: {0}. Output: {1}", exitCode,
311 toolOutput != null ? toolOutput.ToString () : String.Empty);
314 return ExitCode == 0 && !Log.HasLoggedErrors;
317 protected virtual HostObjectInitializationStatus InitializeHostObject ()
319 return HostObjectInitializationStatus.NoActionReturnSuccess;
322 protected virtual void LogToolCommand (string message)
324 Log.LogMessage (MessageImportance.Normal, message);
328 protected virtual void LogPathToTool (string toolName,
333 protected virtual bool SkipTaskExecution()
335 return !ValidateParameters ();
338 protected virtual bool ValidateParameters()
343 protected void DeleteTempFile (string fileName)
345 if (String.IsNullOrEmpty (fileName))
349 File.Delete (fileName);
350 } catch (IOException ioe) {
351 Log.LogWarning ("Unable to delete temporary file '{0}' : {1}", ioe.Message);
352 } catch (UnauthorizedAccessException uae) {
353 Log.LogWarning ("Unable to delete temporary file '{0}' : {1}", uae.Message);
357 // If EnvironmentVariables is defined, then merge EnvironmentOverride
358 // EnvironmentOverride is Obsolete'd in 4.0
360 // Returns the final set of environment variables and logs them
361 SCS.StringDictionary GetAndLogEnvironmentVariables ()
363 var env_vars = GetEnvironmentVariables ();
364 if (env_vars == null)
367 Log.LogMessage (MessageImportance.Low, "Environment variables being passed to the tool:");
368 foreach (DictionaryEntry entry in env_vars)
369 Log.LogMessage (MessageImportance.Low, "\t{0}={1}", (string)entry.Key, (string)entry.Value);
374 SCS.StringDictionary GetEnvironmentVariables ()
376 if (EnvironmentVariables == null || EnvironmentVariables.Length == 0)
377 return EnvironmentOverride;
379 var env_vars = new SCS.ProcessStringDictionary ();
380 foreach (string pair in EnvironmentVariables) {
381 string [] key_value = pair.Split ('=');
382 if (!String.IsNullOrEmpty (key_value [0]))
383 env_vars [key_value [0]] = key_value.Length > 1 ? key_value [1] : String.Empty;
386 if (EnvironmentOverride != null)
387 foreach (DictionaryEntry entry in EnvironmentOverride)
388 env_vars [(string)entry.Key] = (string)entry.Value;
393 protected virtual StringDictionary EnvironmentOverride
398 // Ignore EnvironmentOverride if this is set
399 public string[] EnvironmentVariables { get; set; }
402 public int ExitCode {
403 get { return exitCode; }
406 protected virtual Encoding ResponseFileEncoding
408 get { return responseFileEncoding; }
411 protected virtual Encoding StandardErrorEncoding
413 get { return Console.Error.Encoding; }
416 protected virtual MessageImportance StandardErrorLoggingImportance {
417 get { return standardErrorLoggingImportance; }
420 protected virtual Encoding StandardOutputEncoding
422 get { return Console.Out.Encoding; }
425 protected virtual MessageImportance StandardOutputLoggingImportance {
426 get { return standardOutputLoggingImportance; }
429 protected virtual bool HasLoggedErrors {
430 get { return Log.HasLoggedErrors; }
433 public virtual int Timeout
435 get { return timeout; }
436 set { timeout = value; }
439 public virtual string ToolExe
448 if (!String.IsNullOrEmpty (value))
453 protected abstract string ToolName
458 public string ToolPath
460 get { return toolPath; }
462 if (!String.IsNullOrEmpty (value))
467 // Snatched from our codedom code, with some changes to make it compatible with csc
468 // (the line+column group is optional is csc)
469 static Regex errorRegex;
470 static Regex CscErrorRegex {
472 if (errorRegex == null)
473 errorRegex = new Regex (@"^(\s*(?<file>[^\(]+)(\((?<line>\d*)(,(?<column>\d*[\+]*))?\))?:\s+)*(?<level>\w+)\s+(?<number>.*\d):\s*(?<message>.*)", RegexOptions.Compiled | RegexOptions.ExplicitCapture);