* Makefile ($(build_lib)): Make CYCLIC_DEP_FILES depend on this.
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / Target.cs
index 2d3457b078c81b32cabd05268650d139ab6391d7..b039112044481ba408352640fecd09baf3127f28 100644 (file)
@@ -32,23 +32,22 @@ using System.Collections;
 using System.Collections.Generic;
 using System.Xml;
 using Microsoft.Build.Framework;
+using Microsoft.Build.Utilities;
 
 namespace Microsoft.Build.BuildEngine {
        public class Target : IEnumerable {
        
-               BatchingImpl    batchingImpl;
+               TargetBatchingImpl batchingImpl;
                BuildState      buildState;
-               XmlAttribute    condition;
-               XmlAttribute    dependsOnTargets;
                Engine          engine;
-               bool            isImported;
+               ImportedProject importedProject;
                string          name;
                Project         project;
                XmlElement      targetElement;
                List <XmlElement>       onErrorElements;
                List <BuildTask>        buildTasks;
                
-               internal Target (XmlElement targetElement, Project project)
+               internal Target (XmlElement targetElement, Project project, ImportedProject importedProject)
                {
                        if (project == null)
                                throw new ArgumentNullException ("project");
@@ -57,92 +56,143 @@ namespace Microsoft.Build.BuildEngine {
 
                        this.targetElement = targetElement;
                        this.name = targetElement.GetAttribute ("Name");
-                       this.condition = targetElement.GetAttributeNode ("Condition");
-                       this.dependsOnTargets = targetElement.GetAttributeNode ("DependsOnTargets");
 
                        this.project = project;
                        this.engine = project.ParentEngine;
-                       this.isImported = false;;
+                       this.importedProject = importedProject;
 
                        this.onErrorElements  = new List <XmlElement> ();
                        this.buildState = BuildState.NotStarted;
                        this.buildTasks = new List <BuildTask> ();
-                       this.batchingImpl = new BatchingImpl (project, this.targetElement);
+                       this.batchingImpl = new TargetBatchingImpl (project, this.targetElement);
 
+                       bool onErrorFound = false;
                        foreach (XmlNode xn in targetElement.ChildNodes) {
                                if (xn is XmlElement) {
                                        XmlElement xe = (XmlElement) xn;
                                        if (xe.Name == "OnError") {
                                                onErrorElements.Add (xe);
-                                               continue;
-                                       }
-                                       buildTasks.Add (new BuildTask (xe, this));
+                                               onErrorFound = true;
+                                       } else if (onErrorFound)
+                                               throw new InvalidProjectFileException (
+                                                       "The element <OnError> must be last under element <Target>. Found element <Error> instead.");
+                                       else
+                                               buildTasks.Add (new BuildTask (xe, this));
                                }
                        }
                }
                
+               [MonoTODO]
+               public BuildTask AddNewTask (string taskName)
+               {
+                       if (taskName == null)
+                               throw new ArgumentNullException ("taskName");
+               
+                       XmlElement task = project.XmlDocument.CreateElement (taskName, Project.XmlNamespace);
+                       targetElement.AppendChild (task);
+                       BuildTask bt = new BuildTask (task, this);
+                       buildTasks.Add (bt);
+                       
+                       return bt;
+               }
+
+               public IEnumerator GetEnumerator ()
+               {
+                       foreach (BuildTask bt in buildTasks)
+                               yield return bt;
+               }
+
+               // FIXME: shouldn't we remove it from XML?
+               public void RemoveTask (BuildTask buildTask)
+               {
+                       if (buildTask == null)
+                               throw new ArgumentNullException ("buildTask");
+                       buildTasks.Remove (buildTask);
+               }
+               
+               // FIXME: log errors instead of throwing exceptions
                internal bool Build ()
                {
+                       bool deps;
                        bool result;
-               
-                       buildState = BuildState.Started;
-                       if (dependsOnTargets == null) {
-                               ;
-                       } else if (dependsOnTargets.Value == String.Empty) {
-                               ;
-                       } else {
-                               OldExpression dependencies = new OldExpression (Project);
-                               dependencies.ParseSource (dependsOnTargets.Value);
-                               
-                               string[] targetsToBuildFirst = (string[]) dependencies.ConvertTo (typeof (string[]));
-                               foreach (string target in targetsToBuildFirst) {
-                                       string trimmed = target.Trim ();
-                                       Target t = (Target) project.Targets [trimmed];
+
+                       // log that target is being skipped
+                       if (!ConditionParser.ParseAndEvaluate (Condition, Project))
+                               return true;
+
+                       try {
+                               buildState = BuildState.Started;
+                               deps = BuildDependencies (GetDependencies ());
+
+                               result = deps ? DoBuild () : false;
+
+                               buildState = BuildState.Finished;
+                       // FIXME: log it 
+                       } catch (Exception e) {
+                               LogError ("Error building target {0}: {1}", Name, e.ToString ());
+                               return false;
+                       }
+
+                       return result;
+               }
+
+               List <Target> GetDependencies ()
+               {
+                       List <Target> list = new List <Target> ();
+                       Target t;
+                       string [] targetNames;
+                       Expression deps;
+
+                       if (DependsOnTargets != String.Empty) {
+                               deps = new Expression ();
+                               deps.Parse (DependsOnTargets, true);
+                               targetNames = (string []) deps.ConvertTo (Project, typeof (string []));
+                               foreach (string name in targetNames) {
+                                       t = project.Targets [name.Trim ()];
                                        if (t == null)
-                                               throw new InvalidProjectFileException (String.Format ("Target {0} not found.", trimmed));
-                                       if (t.BuildState == BuildState.NotStarted) {
-                                               t.Build ();
-                                       }
-                                       if (t.BuildState == BuildState.Started)
-                                               throw new InvalidProjectFileException ("Cycle in target dependencies detected.");
+                                               throw new InvalidProjectFileException (String.Format ("Target '{0}' not found.", name.Trim ()));
+                                       list.Add (t);
                                }
                        }
-                       
-                       result = RealBuild ();
-                       buildState = BuildState.Finished;
-                       
-                       return result;
+                       return list;
+               }
+
+               bool BuildDependencies (List <Target> deps)
+               {
+                       foreach (Target t in deps) {
+                               if (t.BuildState == BuildState.NotStarted)
+                                       if (!t.Build ())
+                                               return false;
+                               if (t.BuildState == BuildState.Started)
+                                       throw new InvalidProjectFileException ("Cycle in target dependencies detected");
+                       }
+
+                       return true;
                }
                
-               private bool RealBuild ()
+               bool DoBuild ()
                {
-                       bool executeOnErrors = false;
+                       bool executeOnErrors;
                        bool result = true;
+
+                       if (BuildTasks.Count == 0)
+                               // nothing to do
+                               return true;
                
-                       LogTargetStarted ();
-                       
-                       if (this.batchingImpl.BuildNeeded ()) {
-                               foreach (BuildTask bt in buildTasks) {
-                                       result = batchingImpl.BatchBuildTask (bt);
-                               
-                                       if (!result && !bt.ContinueOnError) {
-                                               executeOnErrors = true;
-                                               break;
-                                       }
-                               }
-                       } else {
-                               LogTargetSkipped ();
+                       try {
+                               result = batchingImpl.Build (this, out executeOnErrors);
+                       } catch (Exception e) {
+                               LogError ("Error building target {0}: {1}", Name, e.ToString ());
+                               throw;
                        }
 
-                       LogTargetFinished (result);
-                       
                        if (executeOnErrors == true)
                                ExecuteOnErrors ();
                                
                        return result;
                }
                
-               private void ExecuteOnErrors ()
+               void ExecuteOnErrors ()
                {
                        foreach (XmlElement onError in onErrorElements) {
                                // FIXME: add condition
@@ -153,70 +203,30 @@ namespace Microsoft.Build.BuildEngine {
                                        this.project.Targets [t].Build ();
                        }
                }
-               
-               private void LogTargetSkipped ()
-               {
-                       BuildMessageEventArgs bmea;
-                       bmea = new BuildMessageEventArgs (String.Format ("Skipping target \"{0}\" because its outputs are up-to-date.",
-                               name), null, "MSBuild", MessageImportance.Normal);
-                       engine.EventSource.FireMessageRaised (this, bmea);
-               }
-               
-               private void LogTargetStarted ()
-               {
-                       TargetStartedEventArgs tsea;
-                       string projectFile = project.FullFileName;
-                       tsea = new TargetStartedEventArgs ("Target " + name + " started.", null, name, projectFile, null);
-                       engine.EventSource.FireTargetStarted (this, tsea);
-               }
-               
-               private void LogTargetFinished (bool succeeded)
-               {
-                       TargetFinishedEventArgs tfea;
-                       string projectFile = project.FullFileName;
-                       tfea = new TargetFinishedEventArgs ("Target " + name + " finished.", null, name, projectFile, null, succeeded);
-                       engine.EventSource.FireTargetFinished (this, tfea);
-               }
-       
-               [MonoTODO]
-               public BuildTask AddNewTask (string taskName)
-               {
-                       throw new NotImplementedException ();
-               }
 
-               public IEnumerator GetEnumerator ()
+               void LogError (string message, params object [] messageArgs)
                {
-                       foreach (BuildTask bt in buildTasks) {
-                               yield return bt;
-                       }
-               }
+                       if (message == null)
+                               throw new ArgumentException ("message");
 
-               public void RemoveTask (BuildTask buildTask)
-               {
-                       buildTasks.Remove (buildTask);
+                       BuildErrorEventArgs beea = new BuildErrorEventArgs (
+                               null, null, null, 0, 0, 0, 0, String.Format (message, messageArgs),
+                               null, null);
+                       engine.EventSource.FireErrorRaised (this, beea);
                }
-
+       
                public string Condition {
-                       get { return condition.Value; }
-                       set { condition.Value = value; }
+                       get { return targetElement.GetAttribute ("Condition"); }
+                       set { targetElement.SetAttribute ("Condition", value); }
                }
 
                public string DependsOnTargets {
-                       get {
-                               if (dependsOnTargets == null)
-                                       return null;
-                               else
-                                       return dependsOnTargets.Value;
-                       }
-                       set {
-                               if (dependsOnTargets != null)
-                                       dependsOnTargets.Value = value;
-                       }
+                       get { return targetElement.GetAttribute ("DependsOnTargets"); }
+                       set { targetElement.SetAttribute ("DependsOnTargets", value); }
                }
 
                public bool IsImported {
-                       get { return isImported; }
-                       internal set { isImported = value; }
+                       get { return importedProject != null; }
                }
 
                public string Name {
@@ -226,10 +236,31 @@ namespace Microsoft.Build.BuildEngine {
                internal Project Project {
                        get { return project; }
                }
+
+               internal List<BuildTask> BuildTasks {
+                       get { return buildTasks; }
+               }
+
+               internal Engine Engine {
+                       get { return engine; }
+               }
                
                internal BuildState BuildState {
                        get { return buildState; }
                }
+
+               internal ITaskItem [] Outputs {
+                       get {
+                               string outputs = targetElement.GetAttribute ("Outputs");
+                               if (outputs == String.Empty)
+                                       return new ITaskItem [0];
+
+                               Expression e = new Expression ();
+                               e.Parse (outputs, true);
+
+                               return (ITaskItem []) e.ConvertTo (project, typeof (ITaskItem []));
+                       }
+               }
        }
        
        internal enum BuildState {