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