Merge pull request #2646 from ludovic-henry/fix-processwatch-dispose
[mono.git] / mcs / tools / xbuild / Main.cs
index 8c8cd6c30951f3654121930067e1566db4fa4f20..8c246124da17708e12ae971ef3e010476f104a92 100644 (file)
@@ -8,6 +8,7 @@
 //
 // (C) 2005 Marek Sieradzki
 // Copyright 2009 Novell, Inc (http://www.novell.com)
+// Copyright 2011 Xamarin Inc (http://www.xamarin.com).
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-#if NET_2_0
 
 using System;
 using System.Collections;
 using System.IO;
 using System.Reflection;
+using System.Text;
 using Microsoft.Build.BuildEngine;
 using Microsoft.Build.Framework;
 using Microsoft.Build.Utilities;
@@ -44,14 +45,15 @@ namespace Mono.XBuild.CommandLine {
                
                Parameters      parameters;
                string[]        args;
-               string          binPath;
                string          defaultSchema;
                
                Engine          engine;
                Project         project;
                ConsoleReportPrinter printer;
-
                
+               // this does nothing but adds strong reference to Microsoft.Build.Tasks*.dll that we need to load consistently.
+               Microsoft.Build.Tasks.Copy dummy;
+
                public static void Main (string[] args)
                {
                        MainClass mc = new MainClass ();
@@ -61,11 +63,11 @@ namespace Mono.XBuild.CommandLine {
                
                public MainClass ()
                {
-                       binPath = ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20);
+                       string binPath = ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20);
                        defaultSchema = Path.Combine (binPath, "Microsoft.Build.xsd");
-                       parameters = new Parameters (binPath);
+                       parameters = new Parameters ();
                }
-               
+
                public void Execute ()
                {
                        bool result = false;
@@ -76,10 +78,16 @@ namespace Mono.XBuild.CommandLine {
                                show_stacktrace = (parameters.LoggerVerbosity == LoggerVerbosity.Detailed ||
                                        parameters.LoggerVerbosity == LoggerVerbosity.Diagnostic);
                                
-                               if (parameters.DisplayVersion)
+                               if (!parameters.NoLogo)
                                        ErrorUtilities.ShowVersion (false);
                                
-                               engine  = new Engine (binPath);
+                               engine  = Engine.GlobalEngine;
+                               if (!String.IsNullOrEmpty (parameters.ToolsVersion)) {
+                                       if (engine.Toolsets [parameters.ToolsVersion] == null)
+                                               ErrorUtilities.ReportError (0, new UnknownToolsVersionException (parameters.ToolsVersion).Message);
+
+                                       engine.DefaultToolsVersion = parameters.ToolsVersion;
+                               }
                                
                                engine.GlobalProperties = this.parameters.Properties;
                                
@@ -92,6 +100,21 @@ namespace Mono.XBuild.CommandLine {
                                        cl.Verbosity = parameters.LoggerVerbosity; 
                                        engine.RegisterLogger (cl);
                                }
+
+                               if (parameters.FileLoggerParameters != null) {
+                                       for (int i = 0; i < parameters.FileLoggerParameters.Length; i ++) {
+                                               string fl_params = parameters.FileLoggerParameters [i];
+                                               if (fl_params == null)
+                                                       continue;
+
+                                               var fl = new FileLogger ();
+                                               if (fl_params.Length == 0 && i > 0)
+                                                       fl.Parameters = String.Format ("LogFile=msbuild{0}.log", i);
+                                               else
+                                                       fl.Parameters = fl_params;
+                                               engine.RegisterLogger (fl);
+                                       }
+                               }
                                
                                foreach (LoggerInfo li in parameters.Loggers) {
                                        Assembly assembly;
@@ -119,14 +142,7 @@ namespace Mono.XBuild.CommandLine {
                                        return;
                                }
 
-                               project.Load (projectFile);
-                               
-                               string oldCurrentDirectory = Environment.CurrentDirectory;
-                               string dir = Path.GetDirectoryName (projectFile);
-                               if (!String.IsNullOrEmpty (dir))
-                                       Directory.SetCurrentDirectory (dir);
-                               result = engine.BuildProject (project, parameters.Targets, null);
-                               Directory.SetCurrentDirectory (oldCurrentDirectory);
+                               result = engine.BuildProjectFile (projectFile, parameters.Targets, null, null, BuildSettings.None, parameters.ToolsVersion);
                        }
                        
                        catch (InvalidProjectFileException ipfe) {
@@ -140,11 +156,6 @@ namespace Mono.XBuild.CommandLine {
                        catch (CommandLineException cle) {
                                ErrorUtilities.ReportError(cle.ErrorCode, show_stacktrace ? cle.ToString() : cle.Message);
                        }
-
-                       catch (Exception) {
-                               throw;
-                       }
-                       
                        finally {
                                if (engine != null)
                                        engine.UnregisterAllLoggers ();
@@ -153,7 +164,6 @@ namespace Mono.XBuild.CommandLine {
                        }
 
                }
-
        }
 
        // code from mcs/report.cs
@@ -187,6 +197,7 @@ namespace Mono.XBuild.CommandLine {
                                break;
 
                        case "xterm-color":
+                       case "xterm-256color":
                                xterm_colors = true;
                                break;
                        }
@@ -312,4 +323,3 @@ namespace Mono.XBuild.CommandLine {
 
 }
 
-#endif