Merge pull request #820 from brendanzagaeski/master
[mono.git] / mcs / tools / msbuild / Main.cs
1 //
2 // Main.cs: Main program file of command line utility.
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //   Miguel de Icaza (miguel@ximian.com)
7 //   Marek Safar (marek.safar@seznam.cz)
8 //
9 // (C) 2005 Marek Sieradzki
10 // Copyright 2009 Novell, Inc (http://www.novell.com)
11 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using Microsoft.Build.Utilities;
32 using Microsoft.Build.Exceptions;
33 using Microsoft.Build.Construction;
34 using System.Xml;
35 using System.Xml.Schema;
36 using System.Linq;
37 using System.Collections.Generic;
38
39 #if NET_2_0
40
41 using System;
42 using System.Collections;
43 using System.IO;
44 using System.Reflection;
45 using System.Text;
46 using Microsoft.Build.Evaluation;
47 using Microsoft.Build.Execution;
48 using Microsoft.Build.Framework;
49 using Microsoft.Build.Logging;
50 using Mono.XBuild.Framework;
51
52 class MonoTODOAttribute : Attribute
53 {
54 }
55
56 namespace Mono.XBuild.CommandLine {
57         public class MainClass {
58                 
59                 Parameters      parameters;
60                 string[]        args;
61                 string          defaultSchema;
62                 
63                 ProjectCollection               project_collection;
64                 ProjectRootElement              project;
65                 ConsoleReportPrinter printer;
66
67                 
68                 public static void Main (string[] args)
69                 {
70                         MainClass mc = new MainClass ();
71                         mc.args = args;
72                         mc.Execute ();
73                 }
74                 
75                 public MainClass ()
76                 {
77                         string binPath = ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20);
78                         defaultSchema = Path.Combine (binPath, "Microsoft.Build.xsd");
79                         parameters = new Parameters ();
80                 }
81
82                 public void Execute ()
83                 {
84                         bool result = false;
85                         bool show_stacktrace = false;
86                         
87                         try {
88                                 parameters.ParseArguments (args);
89                                 show_stacktrace = (parameters.LoggerVerbosity == LoggerVerbosity.Detailed ||
90                                         parameters.LoggerVerbosity == LoggerVerbosity.Diagnostic);
91                                 
92                                 if (!parameters.NoLogo)
93                                         ErrorUtilities.ShowVersion (false);
94                                 
95                                 project_collection  = new ProjectCollection ();
96                                 if (!String.IsNullOrEmpty (parameters.ToolsVersion)) {
97                                         if (project_collection.GetToolset (parameters.ToolsVersion) == null)
98                                                 ErrorUtilities.ReportError (0, new InvalidToolsetDefinitionException ("Toolset " + parameters.ToolsVersion + " was not found").Message);
99
100                                         project_collection.DefaultToolsVersion = parameters.ToolsVersion;
101                                 }
102                                 
103                                 foreach (var p in parameters.Properties)
104                                         project_collection.GlobalProperties.Add (p.Key, p.Value);
105                                 
106                                 if (!parameters.NoConsoleLogger) {
107                                         printer = new ConsoleReportPrinter ();
108                                         ConsoleLogger cl = new ConsoleLogger (parameters.LoggerVerbosity,
109                                                         printer.Print, printer.SetForeground, printer.ResetColor);
110
111                                         cl.Parameters = parameters.ConsoleLoggerParameters;
112                                         cl.Verbosity = parameters.LoggerVerbosity; 
113                                         project_collection.RegisterLogger (cl);
114                                 }
115
116                                 if (parameters.FileLoggerParameters != null) {
117                                         for (int i = 0; i < parameters.FileLoggerParameters.Length; i ++) {
118                                                 string fl_params = parameters.FileLoggerParameters [i];
119                                                 if (fl_params == null)
120                                                         continue;
121
122                                                 var fl = new FileLogger ();
123                                                 if (fl_params.Length == 0 && i > 0)
124                                                         fl.Parameters = String.Format ("LogFile=msbuild{0}.log", i);
125                                                 else
126                                                         fl.Parameters = fl_params;
127                                                 project_collection.RegisterLogger (fl);
128                                         }
129                                 }
130                                 
131                                 foreach (LoggerInfo li in parameters.Loggers) {
132                                         Assembly assembly;
133                                         if (li.InfoType == LoadInfoType.AssemblyFilename)
134                                                 assembly = Assembly.LoadFrom (li.Filename);
135                                         else
136                                                 assembly = Assembly.Load (li.AssemblyName);
137                                         ILogger logger = (ILogger)Activator.CreateInstance (assembly.GetType (li.ClassName));
138                                         logger.Parameters = li.Parameters;
139                                         project_collection.RegisterLogger (logger); 
140                                 }
141                                 
142                                 string projectFile = parameters.ProjectFile;
143                                 if (!File.Exists (projectFile)) {
144                                         ErrorUtilities.ReportError (0, String.Format ("Project file '{0}' not found.", projectFile));
145                                         return;
146                                 }
147
148                                 XmlReaderSettings settings = new XmlReaderSettings ();
149                                 if (parameters.Validate) {
150                                         settings.ValidationType = ValidationType.Schema;
151                                         if (parameters.ValidationSchema == null)
152                                                 using (var xsdxml = XmlReader.Create (defaultSchema))
153                                                         settings.Schemas.Add (XmlSchema.Read (xsdxml, null));
154                                         else
155                                                 using (var xsdxml = XmlReader.Create (parameters.ValidationSchema))
156                                                         settings.Schemas.Add (XmlSchema.Read (xsdxml, null));
157                                 }
158
159                                 var projectInstances = new List<ProjectInstance> ();
160                                 if (string.Equals (Path.GetExtension (projectFile), ".sln", StringComparison.OrdinalIgnoreCase)) {
161                                         var parser = new SolutionParser ();
162                                         var root = ProjectRootElement.Create ();
163                                         parser.ParseSolution (projectFile, project_collection, root, LogWarning);
164                                         foreach (var p in project_collection.LoadedProjects)
165                                                 projectInstances.Add (p.CreateProjectInstance ());
166                                 } else {
167                                         project = ProjectRootElement.Create (XmlReader.Create (projectFile, settings), project_collection);
168                                         var pi = new ProjectInstance (project, parameters.Properties, parameters.ToolsVersion, project_collection);
169                                         projectInstances.Add (pi);
170                                 }
171                                 foreach (var projectInstance in projectInstances) {
172                                         var targets = parameters.Targets.Length == 0 ? projectInstance.DefaultTargets.ToArray () : parameters.Targets;
173                                         result = projectInstance.Build (targets, parameters.Loggers.Count > 0 ? parameters.Loggers : project_collection.Loggers);
174                                         if (!result)
175                                                 break;
176                                 }
177                         }
178                         
179                         catch (InvalidProjectFileException ipfe) {
180                                 ErrorUtilities.ReportError (0, show_stacktrace ? ipfe.ToString () : ipfe.Message);
181                         }
182
183                         catch (InternalLoggerException ile) {
184                                 ErrorUtilities.ReportError (0, show_stacktrace ? ile.ToString () : ile.Message);
185                         }
186
187                         catch (CommandLineException cle) {
188                                 ErrorUtilities.ReportError(cle.ErrorCode, show_stacktrace ? cle.ToString() : cle.Message);
189                         }
190                         finally {
191                                 //if (project_collection != null)
192                                 //      project_collection.UnregisterAllLoggers ();
193
194                                 Environment.Exit (result ? 0 : 1);
195                         }
196                 }
197
198                 void LogWarning (int errorNumber, string message)
199                 {
200                         Console.Error.WriteLine ("Warning {0}: {1}", errorNumber, message);
201                 }
202         }
203
204         // code from mcs/report.cs
205         class ConsoleReportPrinter
206         {
207                 string prefix, postfix;
208                 bool color_supported;
209                 TextWriter writer;
210                 string [] colorPrefixes;
211
212                 public ConsoleReportPrinter ()
213                         : this (Console.Out)
214                 {
215                 }
216
217                 public ConsoleReportPrinter (TextWriter writer)
218                 {
219                         this.writer = writer;
220
221                         string term = Environment.GetEnvironmentVariable ("TERM");
222                         bool xterm_colors = false;
223
224                         color_supported = false;
225                         switch (term){
226                         case "xterm":
227                         case "rxvt":
228                         case "rxvt-unicode":
229                                 if (Environment.GetEnvironmentVariable ("COLORTERM") != null){
230                                         xterm_colors = true;
231                                 }
232                                 break;
233
234                         case "xterm-color":
235                         case "xterm-256color":
236                                 xterm_colors = true;
237                                 break;
238                         }
239                         if (!xterm_colors)
240                                 return;
241
242                         if (!(UnixUtils.isatty (1) && UnixUtils.isatty (2)))
243                                 return;
244
245                         color_supported = true;
246                         PopulateColorPrefixes ();
247                         postfix = "\x001b[0m";
248                 }
249
250                 void PopulateColorPrefixes ()
251                 {
252                         colorPrefixes = new string [16];
253
254                         colorPrefixes [(int)ConsoleColor.Black] = GetForeground ("black");
255                         colorPrefixes [(int)ConsoleColor.DarkBlue] = GetForeground ("blue");
256                         colorPrefixes [(int)ConsoleColor.DarkGreen] = GetForeground ("green");
257                         colorPrefixes [(int)ConsoleColor.DarkCyan] = GetForeground ("cyan");
258                         colorPrefixes [(int)ConsoleColor.DarkRed] = GetForeground ("red");
259                         colorPrefixes [(int)ConsoleColor.DarkMagenta] = GetForeground ("magenta");
260                         colorPrefixes [(int)ConsoleColor.DarkYellow] = GetForeground ("yellow");
261                         colorPrefixes [(int)ConsoleColor.DarkGray] = GetForeground ("grey");
262
263                         colorPrefixes [(int)ConsoleColor.Gray] = GetForeground ("brightgrey");
264                         colorPrefixes [(int)ConsoleColor.Blue] = GetForeground ("brightblue");
265                         colorPrefixes [(int)ConsoleColor.Green] = GetForeground ("brightgreen");
266                         colorPrefixes [(int)ConsoleColor.Cyan] = GetForeground ("brightcyan");
267                         colorPrefixes [(int)ConsoleColor.Red] = GetForeground ("brightred");
268                         colorPrefixes [(int)ConsoleColor.Magenta] = GetForeground ("brightmagenta");
269                         colorPrefixes [(int)ConsoleColor.Yellow] = GetForeground ("brightyellow");
270
271                         colorPrefixes [(int)ConsoleColor.White] = GetForeground ("brightwhite");
272                 }
273
274                 public void SetForeground (ConsoleColor color)
275                 {
276                         if (color_supported)
277                                 prefix = colorPrefixes [(int)color];
278                 }
279
280                 public void ResetColor ()
281                 {
282                         prefix = "\x001b[0m";
283                 }
284
285                 static int NameToCode (string s)
286                 {
287                         switch (s) {
288                         case "black":
289                                 return 0;
290                         case "red":
291                                 return 1;
292                         case "green":
293                                 return 2;
294                         case "yellow":
295                                 return 3;
296                         case "blue":
297                                 return 4;
298                         case "magenta":
299                                 return 5;
300                         case "cyan":
301                                 return 6;
302                         case "grey":
303                         case "white":
304                                 return 7;
305                         }
306                         return 7;
307                 }
308
309                 //
310                 // maps a color name to its xterm color code
311                 //
312                 static string GetForeground (string s)
313                 {
314                         string highcode;
315
316                         if (s.StartsWith ("bright")) {
317                                 highcode = "1;";
318                                 s = s.Substring (6);
319                         } else
320                                 highcode = "";
321
322                         return "\x001b[" + highcode + (30 + NameToCode (s)).ToString () + "m";
323                 }
324
325                 static string GetBackground (string s)
326                 {
327                         return "\x001b[" + (40 + NameToCode (s)).ToString () + "m";
328                 }
329
330                 string FormatText (string txt)
331                 {
332                         if (prefix != null && color_supported)
333                                 return prefix + txt + postfix;
334
335                         return txt;
336                 }
337
338                 public void Print (string message)
339                 {
340                         writer.WriteLine (FormatText (message));
341                 }
342
343         }
344
345         class UnixUtils {
346                 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
347                 extern static int _isatty (int fd);
348
349                 public static bool isatty (int fd)
350                 {
351                         try {
352                                 return _isatty (fd) == 1;
353                         } catch {
354                                 return false;
355                         }
356                 }
357         }
358
359 }
360
361 #endif