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 (project_collection);
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 project.FullPath = projectFile;
169 var pi = new ProjectInstance (project, parameters.Properties, parameters.ToolsVersion, project_collection);
170 projectInstances.Add (pi);
172 foreach (var projectInstance in projectInstances) {
173 var targets = parameters.Targets.Length == 0 ? projectInstance.DefaultTargets.ToArray () : parameters.Targets;
174 result = projectInstance.Build (targets, parameters.Loggers.Count > 0 ? parameters.Loggers : project_collection.Loggers);
180 catch (InvalidProjectFileException ipfe) {
181 ErrorUtilities.ReportError (0, show_stacktrace ? ipfe.ToString () : ipfe.Message);
184 catch (InternalLoggerException ile) {
185 ErrorUtilities.ReportError (0, show_stacktrace ? ile.ToString () : ile.Message);
188 catch (CommandLineException cle) {
189 ErrorUtilities.ReportError(cle.ErrorCode, show_stacktrace ? cle.ToString() : cle.Message);
192 //if (project_collection != null)
193 // project_collection.UnregisterAllLoggers ();
195 Environment.Exit (result ? 0 : 1);
199 void LogWarning (int errorNumber, string message)
201 Console.Error.WriteLine ("Warning {0}: {1}", errorNumber, message);
205 // code from mcs/report.cs
206 class ConsoleReportPrinter
208 string prefix, postfix;
209 bool color_supported;
211 string [] colorPrefixes;
213 public ConsoleReportPrinter ()
218 public ConsoleReportPrinter (TextWriter writer)
220 this.writer = writer;
222 string term = Environment.GetEnvironmentVariable ("TERM");
223 bool xterm_colors = false;
225 color_supported = false;
230 if (Environment.GetEnvironmentVariable ("COLORTERM") != null){
236 case "xterm-256color":
243 if (!(UnixUtils.isatty (1) && UnixUtils.isatty (2)))
246 color_supported = true;
247 PopulateColorPrefixes ();
248 postfix = "\x001b[0m";
251 void PopulateColorPrefixes ()
253 colorPrefixes = new string [16];
255 colorPrefixes [(int)ConsoleColor.Black] = GetForeground ("black");
256 colorPrefixes [(int)ConsoleColor.DarkBlue] = GetForeground ("blue");
257 colorPrefixes [(int)ConsoleColor.DarkGreen] = GetForeground ("green");
258 colorPrefixes [(int)ConsoleColor.DarkCyan] = GetForeground ("cyan");
259 colorPrefixes [(int)ConsoleColor.DarkRed] = GetForeground ("red");
260 colorPrefixes [(int)ConsoleColor.DarkMagenta] = GetForeground ("magenta");
261 colorPrefixes [(int)ConsoleColor.DarkYellow] = GetForeground ("yellow");
262 colorPrefixes [(int)ConsoleColor.DarkGray] = GetForeground ("grey");
264 colorPrefixes [(int)ConsoleColor.Gray] = GetForeground ("brightgrey");
265 colorPrefixes [(int)ConsoleColor.Blue] = GetForeground ("brightblue");
266 colorPrefixes [(int)ConsoleColor.Green] = GetForeground ("brightgreen");
267 colorPrefixes [(int)ConsoleColor.Cyan] = GetForeground ("brightcyan");
268 colorPrefixes [(int)ConsoleColor.Red] = GetForeground ("brightred");
269 colorPrefixes [(int)ConsoleColor.Magenta] = GetForeground ("brightmagenta");
270 colorPrefixes [(int)ConsoleColor.Yellow] = GetForeground ("brightyellow");
272 colorPrefixes [(int)ConsoleColor.White] = GetForeground ("brightwhite");
275 public void SetForeground (ConsoleColor color)
278 prefix = colorPrefixes [(int)color];
281 public void ResetColor ()
283 prefix = "\x001b[0m";
286 static int NameToCode (string s)
311 // maps a color name to its xterm color code
313 static string GetForeground (string s)
317 if (s.StartsWith ("bright")) {
323 return "\x001b[" + highcode + (30 + NameToCode (s)).ToString () + "m";
326 static string GetBackground (string s)
328 return "\x001b[" + (40 + NameToCode (s)).ToString () + "m";
331 string FormatText (string txt)
333 if (prefix != null && color_supported)
334 return prefix + txt + postfix;
339 public void Print (string message)
341 writer.WriteLine (FormatText (message));
347 [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
348 extern static int _isatty (int fd);
350 public static bool isatty (int fd)
353 return _isatty (fd) == 1;