Merge pull request #3293 from esdrubal/mono-sym-fixes
[mono.git] / mcs / tools / xbuild / 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
32
33 using System;
34 using System.Collections;
35 using System.IO;
36 using System.Reflection;
37 using System.Text;
38 using Microsoft.Build.BuildEngine;
39 using Microsoft.Build.Framework;
40 using Microsoft.Build.Utilities;
41 using Mono.XBuild.Framework;
42
43 namespace Mono.XBuild.CommandLine {
44         public class MainClass {
45                 
46                 Parameters      parameters;
47                 string[]        args;
48                 string          defaultSchema;
49                 
50                 Engine          engine;
51                 Project         project;
52                 ConsoleReportPrinter printer;
53                 
54                 // this does nothing but adds strong reference to Microsoft.Build.Tasks*.dll that we need to load consistently.
55                 Microsoft.Build.Tasks.Copy dummy;
56
57                 public static void Main (string[] args)
58                 {
59                         MainClass mc = new MainClass ();
60                         mc.args = args;
61                         mc.Execute ();
62                 }
63                 
64                 public MainClass ()
65                 {
66                         string binPath = ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20);
67                         defaultSchema = Path.Combine (binPath, "Microsoft.Build.xsd");
68                         parameters = new Parameters ();
69                 }
70
71                 public void Execute ()
72                 {
73                         bool result = false;
74                         bool show_stacktrace = false;
75                         
76                         try {
77                                 parameters.ParseArguments (args);
78                                 show_stacktrace = (parameters.LoggerVerbosity == LoggerVerbosity.Detailed ||
79                                         parameters.LoggerVerbosity == LoggerVerbosity.Diagnostic);
80                                 
81                                 if (!parameters.NoLogo)
82                                         ErrorUtilities.ShowVersion (false);
83                                 
84                                 engine  = Engine.GlobalEngine;
85                                 if (!String.IsNullOrEmpty (parameters.ToolsVersion)) {
86                                         if (engine.Toolsets [parameters.ToolsVersion] == null)
87                                                 ErrorUtilities.ReportError (0, new UnknownToolsVersionException (parameters.ToolsVersion).Message);
88
89                                         engine.DefaultToolsVersion = parameters.ToolsVersion;
90                                 }
91                                 
92                                 engine.GlobalProperties = this.parameters.Properties;
93                                 
94                                 if (!parameters.NoConsoleLogger) {
95                                         printer = new ConsoleReportPrinter ();
96                                         ConsoleLogger cl = new ConsoleLogger (parameters.LoggerVerbosity,
97                                                         printer.Print, printer.SetForeground, printer.ResetColor);
98
99                                         cl.Parameters = parameters.ConsoleLoggerParameters;
100                                         cl.Verbosity = parameters.LoggerVerbosity; 
101                                         engine.RegisterLogger (cl);
102                                 }
103
104                                 if (parameters.FileLoggerParameters != null) {
105                                         for (int i = 0; i < parameters.FileLoggerParameters.Length; i ++) {
106                                                 string fl_params = parameters.FileLoggerParameters [i];
107                                                 if (fl_params == null)
108                                                         continue;
109
110                                                 var fl = new FileLogger ();
111                                                 if (fl_params.Length == 0 && i > 0)
112                                                         fl.Parameters = String.Format ("LogFile=msbuild{0}.log", i);
113                                                 else
114                                                         fl.Parameters = fl_params;
115                                                 engine.RegisterLogger (fl);
116                                         }
117                                 }
118                                 
119                                 foreach (LoggerInfo li in parameters.Loggers) {
120                                         Assembly assembly;
121                                         if (li.InfoType == LoadInfoType.AssemblyFilename)
122                                                 assembly = Assembly.LoadFrom (li.Filename);
123                                         else
124                                                 assembly = Assembly.Load (li.AssemblyName);
125                                         ILogger logger = (ILogger)Activator.CreateInstance (assembly.GetType (li.ClassName));
126                                         logger.Parameters = li.Parameters;
127                                         engine.RegisterLogger (logger); 
128                                 }
129                                 
130                                 project = engine.CreateNewProject ();
131                                 
132                                 if (parameters.Validate) {
133                                         if (parameters.ValidationSchema == null)
134                                                 project.SchemaFile = defaultSchema;
135                                         else
136                                                 project.SchemaFile = parameters.ValidationSchema;
137                                 }
138
139                                 string projectFile = parameters.ProjectFile;
140                                 if (!File.Exists (projectFile)) {
141                                         ErrorUtilities.ReportError (0, String.Format ("Project file '{0}' not found.", projectFile));
142                                         return;
143                                 }
144
145                                 result = engine.BuildProjectFile (projectFile, parameters.Targets, null, null, BuildSettings.None, parameters.ToolsVersion);
146                         }
147                         
148                         catch (InvalidProjectFileException ipfe) {
149                                 ErrorUtilities.ReportError (0, show_stacktrace ? ipfe.ToString () : ipfe.Message);
150                         }
151
152                         catch (InternalLoggerException ile) {
153                                 ErrorUtilities.ReportError (0, show_stacktrace ? ile.ToString () : ile.Message);
154                         }
155
156                         catch (CommandLineException cle) {
157                                 ErrorUtilities.ReportError(cle.ErrorCode, show_stacktrace ? cle.ToString() : cle.Message);
158                         }
159                         finally {
160                                 if (engine != null)
161                                         engine.UnregisterAllLoggers ();
162
163                                 Environment.Exit (result ? 0 : 1);
164                         }
165
166                 }
167         }
168
169         // code from mcs/report.cs
170         class ConsoleReportPrinter
171         {
172                 string prefix, postfix;
173                 bool color_supported;
174                 TextWriter writer;
175                 string [] colorPrefixes;
176
177                 public ConsoleReportPrinter ()
178                         : this (Console.Out)
179                 {
180                 }
181
182                 public ConsoleReportPrinter (TextWriter writer)
183                 {
184                         this.writer = writer;
185
186                         string term = Environment.GetEnvironmentVariable ("TERM");
187                         bool xterm_colors = false;
188
189                         color_supported = false;
190                         switch (term){
191                         case "xterm":
192                         case "rxvt":
193                         case "rxvt-unicode":
194                                 if (Environment.GetEnvironmentVariable ("COLORTERM") != null){
195                                         xterm_colors = true;
196                                 }
197                                 break;
198
199                         case "xterm-color":
200                         case "xterm-256color":
201                                 xterm_colors = true;
202                                 break;
203                         }
204                         if (!xterm_colors)
205                                 return;
206
207                         if (!(UnixUtils.isatty (1) && UnixUtils.isatty (2)))
208                                 return;
209
210                         color_supported = true;
211                         PopulateColorPrefixes ();
212                         postfix = "\x001b[0m";
213                 }
214
215                 void PopulateColorPrefixes ()
216                 {
217                         colorPrefixes = new string [16];
218
219                         colorPrefixes [(int)ConsoleColor.Black] = GetForeground ("black");
220                         colorPrefixes [(int)ConsoleColor.DarkBlue] = GetForeground ("blue");
221                         colorPrefixes [(int)ConsoleColor.DarkGreen] = GetForeground ("green");
222                         colorPrefixes [(int)ConsoleColor.DarkCyan] = GetForeground ("cyan");
223                         colorPrefixes [(int)ConsoleColor.DarkRed] = GetForeground ("red");
224                         colorPrefixes [(int)ConsoleColor.DarkMagenta] = GetForeground ("magenta");
225                         colorPrefixes [(int)ConsoleColor.DarkYellow] = GetForeground ("yellow");
226                         colorPrefixes [(int)ConsoleColor.DarkGray] = GetForeground ("grey");
227
228                         colorPrefixes [(int)ConsoleColor.Gray] = GetForeground ("brightgrey");
229                         colorPrefixes [(int)ConsoleColor.Blue] = GetForeground ("brightblue");
230                         colorPrefixes [(int)ConsoleColor.Green] = GetForeground ("brightgreen");
231                         colorPrefixes [(int)ConsoleColor.Cyan] = GetForeground ("brightcyan");
232                         colorPrefixes [(int)ConsoleColor.Red] = GetForeground ("brightred");
233                         colorPrefixes [(int)ConsoleColor.Magenta] = GetForeground ("brightmagenta");
234                         colorPrefixes [(int)ConsoleColor.Yellow] = GetForeground ("brightyellow");
235
236                         colorPrefixes [(int)ConsoleColor.White] = GetForeground ("brightwhite");
237                 }
238
239                 public void SetForeground (ConsoleColor color)
240                 {
241                         if (color_supported)
242                                 prefix = colorPrefixes [(int)color];
243                 }
244
245                 public void ResetColor ()
246                 {
247                         prefix = "\x001b[0m";
248                 }
249
250                 static int NameToCode (string s)
251                 {
252                         switch (s) {
253                         case "black":
254                                 return 0;
255                         case "red":
256                                 return 1;
257                         case "green":
258                                 return 2;
259                         case "yellow":
260                                 return 3;
261                         case "blue":
262                                 return 4;
263                         case "magenta":
264                                 return 5;
265                         case "cyan":
266                                 return 6;
267                         case "grey":
268                         case "white":
269                                 return 7;
270                         }
271                         return 7;
272                 }
273
274                 //
275                 // maps a color name to its xterm color code
276                 //
277                 static string GetForeground (string s)
278                 {
279                         string highcode;
280
281                         if (s.StartsWith ("bright")) {
282                                 highcode = "1;";
283                                 s = s.Substring (6);
284                         } else
285                                 highcode = "";
286
287                         return "\x001b[" + highcode + (30 + NameToCode (s)).ToString () + "m";
288                 }
289
290                 static string GetBackground (string s)
291                 {
292                         return "\x001b[" + (40 + NameToCode (s)).ToString () + "m";
293                 }
294
295                 string FormatText (string txt)
296                 {
297                         if (prefix != null && color_supported)
298                                 return prefix + txt + postfix;
299
300                         return txt;
301                 }
302
303                 public void Print (string message)
304                 {
305                         writer.WriteLine (FormatText (message));
306                 }
307
308         }
309
310         class UnixUtils {
311                 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
312                 extern static int _isatty (int fd);
313
314                 public static bool isatty (int fd)
315                 {
316                         try {
317                                 return _isatty (fd) == 1;
318                         } catch {
319                                 return false;
320                         }
321                 }
322         }
323
324 }
325