2 // Main.cs: Main program file of command line utility.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
6 // Miguel de Icaza (miguel@ximian.com)
7 // Marek Safar (marek.safar@seznam.cz)
9 // (C) 2005 Marek Sieradzki
10 // Copyright 2009 Novell, Inc (http://www.novell.com)
11 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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;
35 using System.Xml.Schema;
37 using System.Collections.Generic;
42 using System.Collections;
44 using System.Reflection;
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;
52 class MonoTODOAttribute : Attribute
56 namespace Mono.XBuild.CommandLine {
57 public class MainClass {
59 Parameters parameters;
63 ProjectCollection project_collection;
64 ProjectRootElement project;
65 ConsoleReportPrinter printer;
68 public static void Main (string[] args)
70 MainClass mc = new MainClass ();
77 string binPath = ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20);
78 defaultSchema = Path.Combine (binPath, "Microsoft.Build.xsd");
79 parameters = new Parameters ();
82 public void Execute ()
85 bool show_stacktrace = false;
88 parameters.ParseArguments (args);
89 show_stacktrace = (parameters.LoggerVerbosity == LoggerVerbosity.Detailed ||
90 parameters.LoggerVerbosity == LoggerVerbosity.Diagnostic);
92 if (!parameters.NoLogo)
93 ErrorUtilities.ShowVersion (false);
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);
100 project_collection.DefaultToolsVersion = parameters.ToolsVersion;
103 foreach (var p in parameters.Properties)
104 project_collection.GlobalProperties.Add (p.Key, p.Value);
106 if (!parameters.NoConsoleLogger) {
107 printer = new ConsoleReportPrinter ();
108 ConsoleLogger cl = new ConsoleLogger (parameters.LoggerVerbosity,
109 printer.Print, printer.SetForeground, printer.ResetColor);
111 cl.Parameters = parameters.ConsoleLoggerParameters;
112 cl.Verbosity = parameters.LoggerVerbosity;
113 project_collection.RegisterLogger (cl);
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)
122 var fl = new FileLogger ();
123 if (fl_params.Length == 0 && i > 0)
124 fl.Parameters = String.Format ("LogFile=msbuild{0}.log", i);
126 fl.Parameters = fl_params;
127 project_collection.RegisterLogger (fl);
131 foreach (LoggerInfo li in parameters.Loggers) {
133 if (li.InfoType == LoadInfoType.AssemblyFilename)
134 assembly = Assembly.LoadFrom (li.Filename);
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);
142 string projectFile = parameters.ProjectFile;
143 if (!File.Exists (projectFile)) {
144 ErrorUtilities.ReportError (0, String.Format ("Project file '{0}' not found.", projectFile));
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));
155 using (var xsdxml = XmlReader.Create (parameters.ValidationSchema))
156 settings.Schemas.Add (XmlSchema.Read (xsdxml, null));
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 ());
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);
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);
179 catch (InvalidProjectFileException ipfe) {
180 ErrorUtilities.ReportError (0, show_stacktrace ? ipfe.ToString () : ipfe.Message);
183 catch (InternalLoggerException ile) {
184 ErrorUtilities.ReportError (0, show_stacktrace ? ile.ToString () : ile.Message);
187 catch (CommandLineException cle) {
188 ErrorUtilities.ReportError(cle.ErrorCode, show_stacktrace ? cle.ToString() : cle.Message);
191 //if (project_collection != null)
192 // project_collection.UnregisterAllLoggers ();
194 Environment.Exit (result ? 0 : 1);
198 void LogWarning (int errorNumber, string message)
200 Console.Error.WriteLine ("Warning {0}: {1}", errorNumber, message);
204 // code from mcs/report.cs
205 class ConsoleReportPrinter
207 string prefix, postfix;
208 bool color_supported;
210 string [] colorPrefixes;
212 public ConsoleReportPrinter ()
217 public ConsoleReportPrinter (TextWriter writer)
219 this.writer = writer;
221 string term = Environment.GetEnvironmentVariable ("TERM");
222 bool xterm_colors = false;
224 color_supported = false;
229 if (Environment.GetEnvironmentVariable ("COLORTERM") != null){
235 case "xterm-256color":
242 if (!(UnixUtils.isatty (1) && UnixUtils.isatty (2)))
245 color_supported = true;
246 PopulateColorPrefixes ();
247 postfix = "\x001b[0m";
250 void PopulateColorPrefixes ()
252 colorPrefixes = new string [16];
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");
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");
271 colorPrefixes [(int)ConsoleColor.White] = GetForeground ("brightwhite");
274 public void SetForeground (ConsoleColor color)
277 prefix = colorPrefixes [(int)color];
280 public void ResetColor ()
282 prefix = "\x001b[0m";
285 static int NameToCode (string s)
310 // maps a color name to its xterm color code
312 static string GetForeground (string s)
316 if (s.StartsWith ("bright")) {
322 return "\x001b[" + highcode + (30 + NameToCode (s)).ToString () + "m";
325 static string GetBackground (string s)
327 return "\x001b[" + (40 + NameToCode (s)).ToString () + "m";
330 string FormatText (string txt)
332 if (prefix != null && color_supported)
333 return prefix + txt + postfix;
338 public void Print (string message)
340 writer.WriteLine (FormatText (message));
346 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
347 extern static int _isatty (int fd);
349 public static bool isatty (int fd)
352 return _isatty (fd) == 1;