00384779b0a70c1ce8aa939da0d2da2812bf07ab
[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 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
31 #if NET_2_0
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                 
55                 public static void Main (string[] args)
56                 {
57                         MainClass mc = new MainClass ();
58                         mc.args = args;
59                         mc.Execute ();
60                 }
61                 
62                 public MainClass ()
63                 {
64                         string binPath = ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20);
65                         defaultSchema = Path.Combine (binPath, "Microsoft.Build.xsd");
66                         parameters = new Parameters ();
67                 }
68
69                 public void Execute ()
70                 {
71                         bool result = false;
72                         bool show_stacktrace = false;
73                         
74                         try {
75                                 parameters.ParseArguments (args);
76                                 show_stacktrace = (parameters.LoggerVerbosity == LoggerVerbosity.Detailed ||
77                                         parameters.LoggerVerbosity == LoggerVerbosity.Diagnostic);
78                                 
79                                 if (!parameters.NoLogo)
80                                         ErrorUtilities.ShowVersion (false);
81                                 
82                                 engine  = Engine.GlobalEngine;
83                                 if (!String.IsNullOrEmpty (parameters.ToolsVersion)) {
84                                         if (engine.Toolsets [parameters.ToolsVersion] == null)
85                                                 ErrorUtilities.ReportError (0, new UnknownToolsVersionException (parameters.ToolsVersion).Message);
86
87                                         engine.DefaultToolsVersion = parameters.ToolsVersion;
88                                 }
89                                 
90                                 engine.GlobalProperties = this.parameters.Properties;
91                                 
92                                 if (!parameters.NoConsoleLogger) {
93                                         printer = new ConsoleReportPrinter ();
94                                         ConsoleLogger cl = new ConsoleLogger (parameters.LoggerVerbosity,
95                                                         printer.Print, printer.SetForeground, printer.ResetColor);
96
97                                         cl.Parameters = parameters.ConsoleLoggerParameters;
98                                         cl.Verbosity = parameters.LoggerVerbosity; 
99                                         engine.RegisterLogger (cl);
100                                 }
101                                 
102                                 foreach (LoggerInfo li in parameters.Loggers) {
103                                         Assembly assembly;
104                                         if (li.InfoType == LoadInfoType.AssemblyFilename)
105                                                 assembly = Assembly.LoadFrom (li.Filename);
106                                         else
107                                                 assembly = Assembly.Load (li.AssemblyName);
108                                         ILogger logger = (ILogger)Activator.CreateInstance (assembly.GetType (li.ClassName));
109                                         logger.Parameters = li.Parameters;
110                                         engine.RegisterLogger (logger); 
111                                 }
112                                 
113                                 project = engine.CreateNewProject ();
114                                 
115                                 if (parameters.Validate) {
116                                         if (parameters.ValidationSchema == null)
117                                                 project.SchemaFile = defaultSchema;
118                                         else
119                                                 project.SchemaFile = parameters.ValidationSchema;
120                                 }
121
122                                 string projectFile = parameters.ProjectFile;
123                                 if (!File.Exists (projectFile)) {
124                                         ErrorUtilities.ReportError (0, String.Format ("Project file '{0}' not found.", projectFile));
125                                         return;
126                                 }
127
128                                 result = engine.BuildProjectFile (projectFile, parameters.Targets, null, null, BuildSettings.None, parameters.ToolsVersion);
129                         }
130                         
131                         catch (InvalidProjectFileException ipfe) {
132                                 ErrorUtilities.ReportError (0, show_stacktrace ? ipfe.ToString () : ipfe.Message);
133                         }
134
135                         catch (InternalLoggerException ile) {
136                                 ErrorUtilities.ReportError (0, show_stacktrace ? ile.ToString () : ile.Message);
137                         }
138
139                         catch (CommandLineException cle) {
140                                 ErrorUtilities.ReportError(cle.ErrorCode, show_stacktrace ? cle.ToString() : cle.Message);
141                         }
142
143                         catch (Exception) {
144                                 throw;
145                         }
146                         
147                         finally {
148                                 if (engine != null)
149                                         engine.UnregisterAllLoggers ();
150
151                                 Environment.Exit (result ? 0 : 1);
152                         }
153
154                 }
155         }
156
157         // code from mcs/report.cs
158         class ConsoleReportPrinter
159         {
160                 string prefix, postfix;
161                 bool color_supported;
162                 TextWriter writer;
163                 string [] colorPrefixes;
164
165                 public ConsoleReportPrinter ()
166                         : this (Console.Out)
167                 {
168                 }
169
170                 public ConsoleReportPrinter (TextWriter writer)
171                 {
172                         this.writer = writer;
173
174                         string term = Environment.GetEnvironmentVariable ("TERM");
175                         bool xterm_colors = false;
176
177                         color_supported = false;
178                         switch (term){
179                         case "xterm":
180                         case "rxvt":
181                         case "rxvt-unicode":
182                                 if (Environment.GetEnvironmentVariable ("COLORTERM") != null){
183                                         xterm_colors = true;
184                                 }
185                                 break;
186
187                         case "xterm-color":
188                                 xterm_colors = true;
189                                 break;
190                         }
191                         if (!xterm_colors)
192                                 return;
193
194                         if (!(UnixUtils.isatty (1) && UnixUtils.isatty (2)))
195                                 return;
196
197                         color_supported = true;
198                         PopulateColorPrefixes ();
199                         postfix = "\x001b[0m";
200                 }
201
202                 void PopulateColorPrefixes ()
203                 {
204                         colorPrefixes = new string [16];
205
206                         colorPrefixes [(int)ConsoleColor.Black] = GetForeground ("black");
207                         colorPrefixes [(int)ConsoleColor.DarkBlue] = GetForeground ("blue");
208                         colorPrefixes [(int)ConsoleColor.DarkGreen] = GetForeground ("green");
209                         colorPrefixes [(int)ConsoleColor.DarkCyan] = GetForeground ("cyan");
210                         colorPrefixes [(int)ConsoleColor.DarkRed] = GetForeground ("red");
211                         colorPrefixes [(int)ConsoleColor.DarkMagenta] = GetForeground ("magenta");
212                         colorPrefixes [(int)ConsoleColor.DarkYellow] = GetForeground ("yellow");
213                         colorPrefixes [(int)ConsoleColor.DarkGray] = GetForeground ("grey");
214
215                         colorPrefixes [(int)ConsoleColor.Gray] = GetForeground ("brightgrey");
216                         colorPrefixes [(int)ConsoleColor.Blue] = GetForeground ("brightblue");
217                         colorPrefixes [(int)ConsoleColor.Green] = GetForeground ("brightgreen");
218                         colorPrefixes [(int)ConsoleColor.Cyan] = GetForeground ("brightcyan");
219                         colorPrefixes [(int)ConsoleColor.Red] = GetForeground ("brightred");
220                         colorPrefixes [(int)ConsoleColor.Magenta] = GetForeground ("brightmagenta");
221                         colorPrefixes [(int)ConsoleColor.Yellow] = GetForeground ("brightyellow");
222
223                         colorPrefixes [(int)ConsoleColor.White] = GetForeground ("brightwhite");
224                 }
225
226                 public void SetForeground (ConsoleColor color)
227                 {
228                         if (color_supported)
229                                 prefix = colorPrefixes [(int)color];
230                 }
231
232                 public void ResetColor ()
233                 {
234                         prefix = "\x001b[0m";
235                 }
236
237                 static int NameToCode (string s)
238                 {
239                         switch (s) {
240                         case "black":
241                                 return 0;
242                         case "red":
243                                 return 1;
244                         case "green":
245                                 return 2;
246                         case "yellow":
247                                 return 3;
248                         case "blue":
249                                 return 4;
250                         case "magenta":
251                                 return 5;
252                         case "cyan":
253                                 return 6;
254                         case "grey":
255                         case "white":
256                                 return 7;
257                         }
258                         return 7;
259                 }
260
261                 //
262                 // maps a color name to its xterm color code
263                 //
264                 static string GetForeground (string s)
265                 {
266                         string highcode;
267
268                         if (s.StartsWith ("bright")) {
269                                 highcode = "1;";
270                                 s = s.Substring (6);
271                         } else
272                                 highcode = "";
273
274                         return "\x001b[" + highcode + (30 + NameToCode (s)).ToString () + "m";
275                 }
276
277                 static string GetBackground (string s)
278                 {
279                         return "\x001b[" + (40 + NameToCode (s)).ToString () + "m";
280                 }
281
282                 string FormatText (string txt)
283                 {
284                         if (prefix != null && color_supported)
285                                 return prefix + txt + postfix;
286
287                         return txt;
288                 }
289
290                 public void Print (string message)
291                 {
292                         writer.WriteLine (FormatText (message));
293                 }
294
295         }
296
297         class UnixUtils {
298                 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
299                 extern static int _isatty (int fd);
300
301                 public static bool isatty (int fd)
302                 {
303                         try {
304                                 return _isatty (fd) == 1;
305                         } catch {
306                                 return false;
307                         }
308                 }
309         }
310
311 }
312
313 #endif