[MSBuild] Fix minor assembly resolution issue
[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                 
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                                 if (parameters.FileLoggerParameters != null) {
103                                         for (int i = 0; i < parameters.FileLoggerParameters.Length; i ++) {
104                                                 string fl_params = parameters.FileLoggerParameters [i];
105                                                 if (fl_params == null)
106                                                         continue;
107
108                                                 var fl = new FileLogger ();
109                                                 if (fl_params.Length == 0 && i > 0)
110                                                         fl.Parameters = String.Format ("LogFile=msbuild{0}.log", i);
111                                                 else
112                                                         fl.Parameters = fl_params;
113                                                 engine.RegisterLogger (fl);
114                                         }
115                                 }
116                                 
117                                 foreach (LoggerInfo li in parameters.Loggers) {
118                                         Assembly assembly;
119                                         if (li.InfoType == LoadInfoType.AssemblyFilename)
120                                                 assembly = Assembly.LoadFrom (li.Filename);
121                                         else
122                                                 assembly = Assembly.Load (li.AssemblyName);
123                                         ILogger logger = (ILogger)Activator.CreateInstance (assembly.GetType (li.ClassName));
124                                         logger.Parameters = li.Parameters;
125                                         engine.RegisterLogger (logger); 
126                                 }
127                                 
128                                 project = engine.CreateNewProject ();
129                                 
130                                 if (parameters.Validate) {
131                                         if (parameters.ValidationSchema == null)
132                                                 project.SchemaFile = defaultSchema;
133                                         else
134                                                 project.SchemaFile = parameters.ValidationSchema;
135                                 }
136
137                                 string projectFile = parameters.ProjectFile;
138                                 if (!File.Exists (projectFile)) {
139                                         ErrorUtilities.ReportError (0, String.Format ("Project file '{0}' not found.", projectFile));
140                                         return;
141                                 }
142
143                                 result = engine.BuildProjectFile (projectFile, parameters.Targets, null, null, BuildSettings.None, parameters.ToolsVersion);
144                         }
145                         
146                         catch (InvalidProjectFileException ipfe) {
147                                 ErrorUtilities.ReportError (0, show_stacktrace ? ipfe.ToString () : ipfe.Message);
148                         }
149
150                         catch (InternalLoggerException ile) {
151                                 ErrorUtilities.ReportError (0, show_stacktrace ? ile.ToString () : ile.Message);
152                         }
153
154                         catch (CommandLineException cle) {
155                                 ErrorUtilities.ReportError(cle.ErrorCode, show_stacktrace ? cle.ToString() : cle.Message);
156                         }
157                         finally {
158                                 if (engine != null)
159                                         engine.UnregisterAllLoggers ();
160
161                                 Environment.Exit (result ? 0 : 1);
162                         }
163
164                 }
165         }
166
167         // code from mcs/report.cs
168         class ConsoleReportPrinter
169         {
170                 string prefix, postfix;
171                 bool color_supported;
172                 TextWriter writer;
173                 string [] colorPrefixes;
174
175                 public ConsoleReportPrinter ()
176                         : this (Console.Out)
177                 {
178                 }
179
180                 public ConsoleReportPrinter (TextWriter writer)
181                 {
182                         this.writer = writer;
183
184                         string term = Environment.GetEnvironmentVariable ("TERM");
185                         bool xterm_colors = false;
186
187                         color_supported = false;
188                         switch (term){
189                         case "xterm":
190                         case "rxvt":
191                         case "rxvt-unicode":
192                                 if (Environment.GetEnvironmentVariable ("COLORTERM") != null){
193                                         xterm_colors = true;
194                                 }
195                                 break;
196
197                         case "xterm-color":
198                         case "xterm-256color":
199                                 xterm_colors = true;
200                                 break;
201                         }
202                         if (!xterm_colors)
203                                 return;
204
205                         if (!(UnixUtils.isatty (1) && UnixUtils.isatty (2)))
206                                 return;
207
208                         color_supported = true;
209                         PopulateColorPrefixes ();
210                         postfix = "\x001b[0m";
211                 }
212
213                 void PopulateColorPrefixes ()
214                 {
215                         colorPrefixes = new string [16];
216
217                         colorPrefixes [(int)ConsoleColor.Black] = GetForeground ("black");
218                         colorPrefixes [(int)ConsoleColor.DarkBlue] = GetForeground ("blue");
219                         colorPrefixes [(int)ConsoleColor.DarkGreen] = GetForeground ("green");
220                         colorPrefixes [(int)ConsoleColor.DarkCyan] = GetForeground ("cyan");
221                         colorPrefixes [(int)ConsoleColor.DarkRed] = GetForeground ("red");
222                         colorPrefixes [(int)ConsoleColor.DarkMagenta] = GetForeground ("magenta");
223                         colorPrefixes [(int)ConsoleColor.DarkYellow] = GetForeground ("yellow");
224                         colorPrefixes [(int)ConsoleColor.DarkGray] = GetForeground ("grey");
225
226                         colorPrefixes [(int)ConsoleColor.Gray] = GetForeground ("brightgrey");
227                         colorPrefixes [(int)ConsoleColor.Blue] = GetForeground ("brightblue");
228                         colorPrefixes [(int)ConsoleColor.Green] = GetForeground ("brightgreen");
229                         colorPrefixes [(int)ConsoleColor.Cyan] = GetForeground ("brightcyan");
230                         colorPrefixes [(int)ConsoleColor.Red] = GetForeground ("brightred");
231                         colorPrefixes [(int)ConsoleColor.Magenta] = GetForeground ("brightmagenta");
232                         colorPrefixes [(int)ConsoleColor.Yellow] = GetForeground ("brightyellow");
233
234                         colorPrefixes [(int)ConsoleColor.White] = GetForeground ("brightwhite");
235                 }
236
237                 public void SetForeground (ConsoleColor color)
238                 {
239                         if (color_supported)
240                                 prefix = colorPrefixes [(int)color];
241                 }
242
243                 public void ResetColor ()
244                 {
245                         prefix = "\x001b[0m";
246                 }
247
248                 static int NameToCode (string s)
249                 {
250                         switch (s) {
251                         case "black":
252                                 return 0;
253                         case "red":
254                                 return 1;
255                         case "green":
256                                 return 2;
257                         case "yellow":
258                                 return 3;
259                         case "blue":
260                                 return 4;
261                         case "magenta":
262                                 return 5;
263                         case "cyan":
264                                 return 6;
265                         case "grey":
266                         case "white":
267                                 return 7;
268                         }
269                         return 7;
270                 }
271
272                 //
273                 // maps a color name to its xterm color code
274                 //
275                 static string GetForeground (string s)
276                 {
277                         string highcode;
278
279                         if (s.StartsWith ("bright")) {
280                                 highcode = "1;";
281                                 s = s.Substring (6);
282                         } else
283                                 highcode = "";
284
285                         return "\x001b[" + highcode + (30 + NameToCode (s)).ToString () + "m";
286                 }
287
288                 static string GetBackground (string s)
289                 {
290                         return "\x001b[" + (40 + NameToCode (s)).ToString () + "m";
291                 }
292
293                 string FormatText (string txt)
294                 {
295                         if (prefix != null && color_supported)
296                                 return prefix + txt + postfix;
297
298                         return txt;
299                 }
300
301                 public void Print (string message)
302                 {
303                         writer.WriteLine (FormatText (message));
304                 }
305
306         }
307
308         class UnixUtils {
309                 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
310                 extern static int _isatty (int fd);
311
312                 public static bool isatty (int fd)
313                 {
314                         try {
315                                 return _isatty (fd) == 1;
316                         } catch {
317                                 return false;
318                         }
319                 }
320         }
321
322 }
323