4e88792a64963aa182faccdd1dc9602a1af1462a
[mono.git] / mcs / class / Microsoft.Build.Utilities / Microsoft.Build.Utilities / ToolTask.cs
1 //
2 // ToolTask.cs: Base class for command line tool tasks. 
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //   Ankit Jain (jankit@novell.com)
7 //
8 // (C) 2005 Marek Sieradzki
9 // Copyright 2009 Novell, Inc (http://www.novell.com)
10 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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.
29
30 #if NET_2_0
31
32 using System;
33 using System.Diagnostics;
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.IO;
37 using System.Resources;
38 using System.Text;
39 using System.Text.RegularExpressions;
40 using Microsoft.Build.Framework;
41 using Mono.XBuild.Utilities;
42
43 using SCS = System.Collections.Specialized;
44
45 namespace Microsoft.Build.Utilities
46 {
47         public abstract class ToolTask : Task
48         {
49                 int                     exitCode;
50                 int                     timeout;
51                 string                  toolPath, toolExe;
52                 Encoding                responseFileEncoding;
53                 MessageImportance       standardErrorLoggingImportance;
54                 MessageImportance       standardOutputLoggingImportance;
55                 StringBuilder toolOutput;
56                 bool typeLoadException;
57
58                 protected ToolTask ()
59                         : this (null, null)
60                 {
61                         this.standardErrorLoggingImportance = MessageImportance.High;
62                         this.standardOutputLoggingImportance = MessageImportance.Normal;
63                 }
64
65                 protected ToolTask (ResourceManager taskResources)
66                         : this (taskResources, null)
67                 {
68                 }
69
70                 protected ToolTask (ResourceManager taskResources,
71                                    string helpKeywordPrefix)
72                 {
73                         this.TaskResources = taskResources;
74                         this.HelpKeywordPrefix = helpKeywordPrefix;
75                         this.toolPath = MonoLocationHelper.GetBinDir ();
76                         this.responseFileEncoding = Encoding.UTF8;
77                         this.timeout = Int32.MaxValue;
78                 }
79
80                 [MonoTODO]
81                 protected virtual bool CallHostObjectToExecute ()
82                 {
83                         return true;
84                 }
85
86                 public override bool Execute ()
87                 {
88                         if (SkipTaskExecution ())
89                                 return true;
90
91                         exitCode = ExecuteTool (GenerateFullPathToTool (), GenerateResponseFileCommands (),
92                                 GenerateCommandLineCommands ());
93
94                         // HandleTaskExecutionErrors is called only if exitCode != 0
95                         return exitCode == 0 || HandleTaskExecutionErrors ();
96                 }
97                 
98                 [MonoTODO]
99                 protected virtual string GetWorkingDirectory ()
100                 {
101                         return null;
102                 }
103                 
104                 protected virtual int ExecuteTool (string pathToTool,
105                                                    string responseFileCommands,
106                                                    string commandLineCommands)
107
108                 {
109                         if (pathToTool == null)
110                                 throw new ArgumentNullException ("pathToTool");
111
112                         string responseFileName;
113                         responseFileName = null;
114                         toolOutput = new StringBuilder ();
115
116                         try {
117                                 string responseFileSwitch = String.Empty;
118                                 if (!String.IsNullOrEmpty (responseFileCommands)) {
119                                         responseFileName = Path.GetTempFileName ();
120                                         File.WriteAllText (responseFileName, responseFileCommands);
121                                         responseFileSwitch = GetResponseFileSwitch (responseFileName);
122                                 }
123
124                                 var pinfo = GetProcessStartInfo (pathToTool, commandLineCommands, responseFileSwitch);
125                                 LogToolCommand (String.Format ("Tool {0} execution started with arguments: {1} {2}",
126                                                 pinfo.FileName, commandLineCommands, responseFileCommands));
127
128                                 var pendingLineFragmentOutput = new StringBuilder ();
129                                 var pendingLineFragmentError = new StringBuilder ();
130                                 var environmentOverride = GetAndLogEnvironmentVariables ();
131                                 try {
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
135                                         // events at all!
136                                         ProcessWrapper pw = ProcessService.StartProcess (pinfo,
137                                                         (_, msg) => ProcessLine (pendingLineFragmentOutput, msg, StandardOutputLoggingImportance),
138                                                         (_, msg) => ProcessLine (pendingLineFragmentError, msg, StandardErrorLoggingImportance),
139                                                         null,
140                                                         environmentOverride);
141
142                                         pw.WaitForOutput (timeout == Int32.MaxValue ? Int32.MaxValue : timeout);
143
144                                         // Process any remaining line
145                                         ProcessLine (pendingLineFragmentOutput, StandardOutputLoggingImportance, true);
146                                         ProcessLine (pendingLineFragmentError, StandardErrorLoggingImportance, true);
147
148                                         exitCode = pw.ExitCode;
149                                         pw.Dispose ();
150                                 } catch (System.ComponentModel.Win32Exception e) {
151                                         Log.LogError ("Error executing tool '{0}': {1}", pathToTool, e.Message);
152                                         return -1;
153                                 }
154
155                                 if (typeLoadException)
156                                         ProcessTypeLoadException ();
157
158                                 pendingLineFragmentOutput.Length = 0;
159                                 pendingLineFragmentError.Length = 0;
160
161                                 Log.LogMessage (MessageImportance.Low, "Tool {0} execution finished.", pathToTool);
162                                 return exitCode;
163                         } finally {
164                                 DeleteTempFile (responseFileName);
165                         }
166                 }
167
168                 void ProcessTypeLoadException ()
169                 {
170                         string output_str = toolOutput.ToString ();
171                         Regex reg  = new Regex (@".*WARNING.*used in (mscorlib|System),.*",
172                                         RegexOptions.Multiline);
173
174                         if (reg.Match (output_str).Success)
175                                 Log.LogError (
176                                         "Error: A referenced assembly may be built with an incompatible " +
177                                         "CLR version. See the compilation output for more details.");
178                         else
179                                 Log.LogError (
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.");
183
184                         Log.LogError (output_str);
185                 }
186
187                 void ProcessLine (StringBuilder outputBuilder, MessageImportance importance, bool isLastLine)
188                 {
189                         if (outputBuilder.Length == 0)
190                                 return;
191
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);
195
196                         ProcessLine (outputBuilder, null, importance);
197                 }
198
199                 void ProcessLine (StringBuilder outputBuilder, string line, MessageImportance importance)
200                 {
201                         // Add to any line fragment from previous call
202                         if (line != null)
203                                 outputBuilder.Append (line);
204
205                         // Don't remove empty lines!
206                         var lines = outputBuilder.ToString ().Split (new string [] {Environment.NewLine}, StringSplitOptions.None);
207
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);
217                                         continue;
218                                 }
219
220                                 toolOutput.AppendLine (singleLine);
221
222                                 // in case of typeLoadException, collect all the output
223                                 // and then handle in ProcessTypeLoadException
224                                 if (!typeLoadException)
225                                         LogEventsFromTextOutput (singleLine, importance);
226                         }
227                 }
228
229                 protected virtual void LogEventsFromTextOutput (string singleLine, MessageImportance importance)
230                 {
231                         if (singleLine.Length == 0) {
232                                 Log.LogMessage (singleLine, importance);
233                                 return;
234                         }
235
236                         if (singleLine.StartsWith ("Unhandled Exception: System.TypeLoadException") ||
237                             singleLine.StartsWith ("Unhandled Exception: System.IO.FileNotFoundException")) {
238                                 typeLoadException = true;
239                         }
240
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"))
246                                 return;
247
248                         Match match = CscErrorRegex.Match (singleLine);
249                         if (!match.Success) {
250                                 Log.LogMessage (importance, singleLine);
251                                 return;
252                         }
253
254                         string filename = match.Result ("${file}") ?? "";
255                         string line = match.Result ("${line}");
256                         int lineNumber = !string.IsNullOrEmpty (line) ? Int32.Parse (line) : 0;
257
258                         string col = match.Result ("${column}");
259                         int columnNumber = 0;
260                         if (!string.IsNullOrEmpty (col))
261                                 columnNumber = col == "255+" ? -1 : Int32.Parse (col);
262
263                         string category = match.Result ("${level}");
264                         string code = match.Result ("${number}");
265                         string text = match.Result ("${message}");
266
267                         if (String.Compare (category, "warning", StringComparison.OrdinalIgnoreCase) == 0) {
268                                 Log.LogWarning (null, code, null, filename, lineNumber, columnNumber, -1,
269                                         -1, text, null);
270                         } else if (String.Compare (category, "error", StringComparison.OrdinalIgnoreCase) == 0) {
271                                 Log.LogError (null, code, null, filename, lineNumber, columnNumber, -1,
272                                         -1, text, null);
273                         } else {
274                                 Log.LogMessage (importance, singleLine);
275                         }
276                 }
277
278                 protected virtual string GenerateCommandLineCommands ()
279                 {
280                         return null;
281                 }
282
283                 protected abstract string GenerateFullPathToTool ();
284
285                 protected virtual string GenerateResponseFileCommands ()
286                 {
287                         return null;
288                 }
289
290                 protected virtual string GetResponseFileSwitch (string responseFilePath)
291                 {
292                         return String.Format ("@{0}", responseFilePath);
293                 }
294
295                 protected virtual ProcessStartInfo GetProcessStartInfo (string pathToTool, string commandLineCommands, string responseFileSwitch)
296                 {
297                         var pinfo = new ProcessStartInfo (pathToTool, String.Format ("{0} {1}", commandLineCommands, responseFileSwitch));
298
299                         pinfo.WorkingDirectory = GetWorkingDirectory () ?? Environment.CurrentDirectory;
300                         pinfo.UseShellExecute = false;
301                         pinfo.RedirectStandardOutput = true;
302                         pinfo.RedirectStandardError = true;
303
304                         return pinfo;
305                 }
306
307                 protected virtual bool HandleTaskExecutionErrors ()
308                 {
309                         if (!Log.HasLoggedErrors && exitCode != 0)
310                                 Log.LogError ("Tool exited with code: {0}. Output: {1}", exitCode,
311                                                 toolOutput != null ? toolOutput.ToString () : String.Empty);
312                         toolOutput = null;
313
314                         return ExitCode == 0 && !Log.HasLoggedErrors;
315                 }
316
317                 protected virtual HostObjectInitializationStatus InitializeHostObject ()
318                 {
319                         return HostObjectInitializationStatus.NoActionReturnSuccess;
320                 }
321
322                 protected virtual void LogToolCommand (string message)
323                 {
324                         Log.LogMessage (MessageImportance.Normal, message);
325                 }
326                 
327                 [MonoTODO]
328                 protected virtual void LogPathToTool (string toolName,
329                                                       string pathToTool)
330                 {
331                 }
332
333                 protected virtual bool SkipTaskExecution()
334                 {
335                         return !ValidateParameters ();
336                 }
337
338                 protected virtual bool ValidateParameters()
339                 {
340                         return true;
341                 }
342
343                 protected void DeleteTempFile (string fileName)
344                 {
345                         if (String.IsNullOrEmpty (fileName))
346                                 return;
347
348                         try {
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);
354                         }
355                 }
356
357                 // If EnvironmentVariables is defined, then merge EnvironmentOverride
358                 // EnvironmentOverride is Obsolete'd in 4.0
359                 //
360                 // Returns the final set of environment variables and logs them
361                 SCS.StringDictionary GetAndLogEnvironmentVariables ()
362                 {
363                         var env_vars = GetEnvironmentVariables ();
364                         if (env_vars == null)
365                                 return env_vars;
366
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);
370
371                         return env_vars;
372                 }
373
374                 SCS.StringDictionary GetEnvironmentVariables ()
375                 {
376                         if (EnvironmentVariables == null || EnvironmentVariables.Length == 0)
377                                 return EnvironmentOverride;
378
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;
384                         }
385
386                         if (EnvironmentOverride != null)
387                                 foreach (DictionaryEntry entry in EnvironmentOverride)
388                                         env_vars [(string)entry.Key] = (string)entry.Value;
389
390                         return env_vars;
391                 }
392
393                 protected virtual StringDictionary EnvironmentOverride
394                 {
395                         get { return null; }
396                 }
397
398                 // Ignore EnvironmentOverride if this is set
399                 public string[] EnvironmentVariables { get; set; }
400
401                 [Output]
402                 public int ExitCode {
403                         get { return exitCode; }
404                 }
405
406                 protected virtual Encoding ResponseFileEncoding
407                 {
408                         get { return responseFileEncoding; }
409                 }
410
411                 protected virtual Encoding StandardErrorEncoding
412                 {
413                         get { return Console.Error.Encoding; }
414                 }
415
416                 protected virtual MessageImportance StandardErrorLoggingImportance {
417                         get { return standardErrorLoggingImportance; }
418                 }
419
420                 protected virtual Encoding StandardOutputEncoding
421                 {
422                         get { return Console.Out.Encoding; }
423                 }
424
425                 protected virtual MessageImportance StandardOutputLoggingImportance {
426                         get { return standardOutputLoggingImportance; }
427                 }
428
429                 protected virtual bool HasLoggedErrors {
430                         get { return Log.HasLoggedErrors; }
431                 }
432
433                 public virtual int Timeout
434                 {
435                         get { return timeout; }
436                         set { timeout = value; }
437                 }
438
439                 public virtual string ToolExe
440                 {
441                         get {
442                                 if (toolExe == null)
443                                         return ToolName;
444                                 else
445                                         return toolExe;
446                         }
447                         set {
448                                 if (!String.IsNullOrEmpty (value))
449                                         toolExe = value;
450                         }
451                 }
452
453                 protected abstract string ToolName
454                 {
455                         get;
456                 }
457
458                 public string ToolPath
459                 {
460                         get { return toolPath; }
461                         set {
462                                 if (!String.IsNullOrEmpty (value))
463                                         toolPath  = value;
464                         }
465                 }
466
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 {
471                         get {
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);
474                                 return errorRegex;
475                         }
476                 }
477         }
478 }
479
480 #endif