In .:
[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 //
7 // (C) 2005 Marek Sieradzki
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 #if NET_2_0
29
30 using System;
31 using System.Collections;
32 using System.IO;
33 using System.Reflection;
34 using Microsoft.Build.BuildEngine;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
37 using Mono.XBuild.Framework;
38 using Mono.XBuild.Utilities;
39
40 namespace Mono.XBuild.CommandLine {
41         public class MainClass {
42                 
43                 Parameters      parameters;
44                 string[]        args;
45                 string          binPath;
46                 string          defaultSchema;
47                 
48                 Engine          engine;
49                 Project         project;
50                 
51                 public static void Main (string[] args)
52                 {
53                         MainClass mc = new MainClass ();
54                         mc.args = args;
55                         mc.Execute ();
56                 }
57                 
58                 public MainClass ()
59                 {
60                         binPath = MonoLocationHelper.GetXBuildDir ();
61                         defaultSchema = Path.Combine (binPath, "Microsoft.Build.xsd");
62                         parameters = new Parameters (binPath);
63                 }
64                 
65                 public void Execute ()
66                 {
67                         try {
68                                 parameters.ParseArguments (args);
69                                 
70                                 if (parameters.DisplayVersion == true)
71                                         Display (version);
72                                 
73                                 engine  = new Engine (binPath);
74                                 
75                                 engine.GlobalProperties = this.parameters.Properties;
76                                 
77                                 if (parameters.NoConsoleLogger == false ) {
78                                         ConsoleLogger cl = new ConsoleLogger ();
79                                         cl.Parameters = parameters.ConsoleLoggerParameters;
80                                         cl.Verbosity = parameters.LoggerVerbosity; 
81                                         engine.RegisterLogger (cl);
82                                 }
83                                 
84                                 foreach (LoggerInfo li in parameters.Loggers) {
85                                         Assembly assembly;
86                                         if (li.InfoType == LoadInfoType.AssemblyFilename)
87                                                 assembly = Assembly.LoadFrom (li.Filename);
88                                         else
89                                                 assembly = Assembly.Load (li.AssemblyName);
90                                         ILogger logger = (ILogger)Activator.CreateInstance (assembly.GetType (li.ClassName));
91                                         logger.Parameters = li.Parameters;
92                                         engine.RegisterLogger (logger); 
93                                 }
94                                 
95                                 project = engine.CreateNewProject ();
96                                 
97                                 if (parameters.Validate == true) {
98                                         if (parameters.ValidationSchema == null)
99                                                 project.SchemaFile = defaultSchema;
100                                         else
101                                                 project.SchemaFile = parameters.ValidationSchema;
102                                 }
103
104                                 project.Load (parameters.ProjectFile);
105                                 
106                                 engine.BuildProject (project, parameters.Targets, new Hashtable ());
107                         }
108                         catch (CommandLineException cex) {
109                                 switch (cex.ErrorCode) {
110                                 case 1:
111                                         ReportErrorFromException (cex);
112                                         break;
113                                 case 2:
114                                         ReportErrorFromException (cex);
115                                         break;
116                                 case 3:
117                                         if (parameters.NoLogo)
118                                                 ReportErrorFromException (cex);
119                                         else {
120                                                 Display (version);
121                                                 Display (usage);
122                                         }
123                                         break;
124                                 case 4:
125                                         ReportErrorFromException (cex);
126                                         break;
127                                 case 5:
128                                         Version ();
129                                         break;
130                                 case 6:
131                                         Usage ();
132                                         break;
133                                 case 7:
134                                         ReportErrorFromException (cex);
135                                         break;
136                                 default:
137                                         throw;
138                                 }
139                         }
140                         catch (InvalidProjectFileException ipfe ) {
141                                 ReportError (0008, ipfe.Message);
142                         }
143                         catch (Exception ex) {
144                                 ReportError (0, String.Format ("{0}\n{1}",ex.Message, ex.StackTrace));
145                         }
146                         finally {
147                                 if (engine != null)
148                                         engine.UnregisterAllLoggers ();
149                         }
150                 }
151                 
152                 private void Display (string[] array) {
153                         foreach (string s in array)
154                                 Console.WriteLine (s);
155                 }
156                 
157                 private void Version ()
158                 {
159                         Display (version);
160                         Environment.Exit (0);
161                 }
162                 
163                 private void Usage ()
164                 {
165                         Display (version);
166                         Display (usage);
167                         Environment.Exit (0);
168                 }
169                 
170                 private void ReportErrorFromException (CommandLineException cex)
171                 {
172                         ReportError (cex.ErrorCode, cex.Message);
173                 }
174                 
175                 private void ReportError (int errorNum, string msg) {
176                         Console.WriteLine (String.Format ("MSBUILD: error MSBUILD{0:0000}: {1}", errorNum, msg));
177                         Environment.Exit (1);
178                 }
179
180                 private void ReportWarning (int errorNum, string msg) {
181                         Console.WriteLine (String.Format ("MSBUILD: warning MSBUILD{0:0000}: {1}", errorNum, msg));
182                 }
183
184                 private void ReportInvalidArgument (string option, string value) {
185                         ReportError (1012, String.Format ("'{0}' is not a valid setting for option '{1}'", value, option));
186                 }
187
188                 private void ReportMissingArgument (string option) {
189                         ReportError (1003, String.Format ("Compiler option '{0}' must be followed by an argument", option));
190                 }
191
192                 private void ReportNotImplemented (string option) {
193                         ReportError (0, String.Format ("Compiler option '{0}' is not implemented", option));
194                 }
195
196                 private void ReportMissingFileSpec (string option) {
197                         ReportError (1008, String.Format ("Missing file specification for '{0}' command-line option", option));
198                 }
199
200                 private void ReportMissingText (string option) {
201                         ReportError (1010, String.Format ("Missing ':<text>' for '{0}' option", option));
202                 }
203
204                 string[] usage = {
205                         "",
206                         "Syntax:              xbuild.exe [options] [project file]",
207                         "",
208                         "Description:         Builds the specified targets in the project file. If",
209                         "                     a project file is not specified, MSBuild searches the",
210                         "                     current working directory for a file that has a file",
211                         "                     extension that ends in \"proj\" and uses that file.",
212                         "",
213                         "Switches:",
214                         "",
215                         "  /help              Display this usage message. (Short form: /? or /h)",
216                         "",
217                         "  /nologo            Do not display the startup banner and copyright message.",
218                         "",
219                         "  /version           Display version information only. (Short form: /ver)",
220                         "",
221                         "  @<file>            Insert command-line settings from a text file. To specify",
222                         "                     multiple response files, specify each response file",
223                         "                     separately.",
224                         "",
225                         "  /noautoresponse    Do not auto-include the MSBuild.rsp file. (Short form:",
226                         "                     /noautorsp)",
227                         "",
228                         "  /target:<targets>  Build these targets in this project. Use a semicolon or a",
229                         "                     comma to separate multiple targets, or specify each",
230                         "                     target separately. (Short form: /t)",
231                         "                     Example:",
232                         "                       /target:Resources;Compile",
233                         "",
234                         "  /property:<n>=<v>  Set or override these project-level properties. <n> is",
235                         "                     the property name, and <v> is the property value. Use a",
236                         "                     semicolon or a comma to separate multiple properties, or",
237                         "                     specify each property separately. (Short form: /p)",
238                         "                     Example:",
239                         @"                       /property:WarningLevel=2;OutDir=bin\Debug\",
240                         "",
241                         "  /logger:<logger>   Use this logger to log events from MSBuild. To specify",
242                         "                     multiple loggers, specify each logger separately.",
243                         "                     The <logger> syntax is:",
244                         "                        [<logger class>,]<logger assembly>[;<logger parameters>]",
245                         "                     The <logger class> syntax is:",
246                         "                        [<partial or full namespace>.]<logger class name>",
247                         "                     The <logger assembly> syntax is:",
248                         "                        {<assembly name>[,<strong name>] | <assembly file>}",
249                         "                     The <logger parameters> are optional, and are passed",
250                         "                     to the logger exactly as you typed them. (Short form: /l)",
251                         "                     Examples:",
252                         "                       /logger:XMLLogger,MyLogger,Version=1.0.2,Culture=neutral",
253                         @"                       /logger:XMLLogger,C:\Loggers\MyLogger.dll;OutputAsHTML",
254                         "",
255                         "  /verbosity:<level> Display this amount of information in the event log.",
256                         "                     The available verbosity levels are: q[uiet], m[inimal],",
257                         "                     n[ormal], d[etailed], and diag[nostic]. (Short form: /v)",
258                         "                     Example:",
259                         "                       /verbosity:quiet",
260                         "",
261                         "  /consoleloggerparameters:<parameters>",
262                         "                     Parameters to console logger. (Short form: /clp)",
263                         "                     The available parameters are:",
264                         "                        PerformanceSummary--show time spent in tasks, targets",
265                         "                            and projects.",
266                         "                        NoSummary--don't show error and warning summary at the",
267                         "                            end.",
268                         "                     Example:",
269                         "                        /consoleloggerparameters:PerformanceSummary;NoSummary",
270                         "",
271                         "  /noconsolelogger   Disable the default console logger and do not log events",
272                         "                     to the console. (Short form: /noconlog)",
273                         "",
274                         "  /validate          Validate the project against the default schema. (Short",
275                         "                     form: /val)",
276                         "",
277                         "  /validate:<schema> Validate the project against the specified schema. (Short",
278                         "                     form: /val)",
279                         "                     Example:",
280                         "                       /validate:MyExtendedBuildSchema.xsd",
281                         "",
282                         "Examples:",
283                         "",
284                         "        MSBuild MyApp.sln /t:Rebuild /p:Configuration=Release",
285                         "        MSBuild MyApp.csproj /t:Clean /p:Configuration=Debug",
286                 };
287                 
288                 string[] version = {
289                         "XBuild Engine Version 0.1",
290                         String.Format ("Mono, Version {0}", Consts.MonoVersion),
291                         "Copyright (C) Marek Sieradzki 2005. All rights reserved.",
292                 };
293         }
294 }
295
296 #endif