[xbuild] Add new reserved properties $(MSBuildThisFile*).
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / Target.cs
index eb22cb4d749996250089f2cbc5fa95f56e59c869..439c56fd94b14b4887d57ace200ec0d5f5e82e71 100644 (file)
@@ -32,11 +32,12 @@ 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;
                Engine          engine;
                ImportedProject importedProject;
@@ -63,7 +64,7 @@ namespace Microsoft.Build.BuildEngine {
                        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) {
@@ -108,29 +109,73 @@ namespace Microsoft.Build.BuildEngine {
                                throw new ArgumentNullException ("buildTask");
                        buildTasks.Remove (buildTask);
                }
-               
-               // FIXME: log errors instead of throwing exceptions
-               internal bool Build ()
+
+               bool Build ()
+               {
+                       return Build (null);
+               }
+
+               internal bool Build (string built_targets_key)
+               {
+                       bool executeOnErrors;
+                       return Build (built_targets_key, out executeOnErrors);
+               }
+
+               bool Build (string built_targets_key, out bool executeOnErrors)
                {
-                       bool deps;
-                       bool result;
+                       project.PushThisFileProperty (TargetFile);
+                       try {
+                               return BuildActual (built_targets_key, out executeOnErrors);
+                       } finally {
+                               project.PopThisFileProperty ();
+                       }
+               }
 
-                       // log that target is being skipped
-                       if (!ConditionParser.ParseAndEvaluate (Condition, Project))
+               bool BuildActual (string built_targets_key, out bool executeOnErrors)
+               {
+                       bool result = false;
+                       executeOnErrors = false;
+
+                       // built targets are keyed by the particular set of global
+                       // properties. So, a different set could allow a target
+                       // to run again
+                       built_targets_key = project.GetKeyForTarget (Name);
+                       if (project.ParentEngine.BuiltTargetsOutputByName.ContainsKey (built_targets_key)) {
+                               LogTargetSkipped ();
                                return true;
+                       }
+
+                       // Push a null/empty batch, effectively clearing it
+                       project.PushBatch (null, null);
+                       if (!ConditionParser.ParseAndEvaluate (Condition, Project)) {
+                               LogMessage (MessageImportance.Low,
+                                               "Target {0} skipped due to false condition: {1}",
+                                               Name, Condition);
+                               project.PopBatch ();
+                               return true;
+                       }
 
                        try {
                                buildState = BuildState.Started;
-                               deps = BuildDependencies (GetDependencies ());
+                               result = BuildDependencies (GetDependencies (), out executeOnErrors);
 
-                               result = deps ? DoBuild () : false;
+                               if (!result && executeOnErrors)
+                                       ExecuteOnErrors ();
+
+                               if (result)
+                                       // deps built fine, do main build
+                                       result = DoBuild (out executeOnErrors);
 
                                buildState = BuildState.Finished;
-                       // FIXME: log it 
                        } catch (Exception e) {
+                               LogError ("Error building target {0}: {1}", Name, e.ToString ());
                                return false;
+                       } finally {
+                               project.PopBatch ();
                        }
 
+                       project.ParentEngine.BuiltTargetsOutputByName [built_targets_key] = (ITaskItem[]) Outputs.Clone ();
+
                        return result;
                }
 
@@ -143,23 +188,26 @@ namespace Microsoft.Build.BuildEngine {
 
                        if (DependsOnTargets != String.Empty) {
                                deps = new Expression ();
-                               deps.Parse (DependsOnTargets, true);
+                               deps.Parse (DependsOnTargets, ParseOptions.AllowItemsNoMetadataAndSplit);
                                targetNames = (string []) deps.ConvertTo (Project, typeof (string []));
-                               foreach (string name in targetNames) {
-                                       t = project.Targets [name.Trim ()];
+                               foreach (string dep_name in targetNames) {
+                                       t = project.Targets [dep_name.Trim ()];
                                        if (t == null)
-                                               throw new InvalidProjectFileException (String.Format ("Target '{0}' not found.", name.Trim ()));
+                                               throw new InvalidProjectFileException (String.Format (
+                                                               "Target '{0}', a dependency of target '{1}', not found.",
+                                                               dep_name.Trim (), Name));
                                        list.Add (t);
                                }
                        }
                        return list;
                }
 
-               bool BuildDependencies (List <Target> deps)
+               bool BuildDependencies (List <Target> deps, out bool executeOnErrors)
                {
+                       executeOnErrors = false;
                        foreach (Target t in deps) {
                                if (t.BuildState == BuildState.NotStarted)
-                                       if (!t.Build ())
+                                       if (!t.Build (null, out executeOnErrors))
                                                return false;
                                if (t.BuildState == BuildState.Started)
                                        throw new InvalidProjectFileException ("Cycle in target dependencies detected");
@@ -168,28 +216,23 @@ namespace Microsoft.Build.BuildEngine {
                        return true;
                }
                
-               bool DoBuild ()
+               bool DoBuild (out bool executeOnErrors)
                {
-                       bool executeOnErrors = false;
+                       executeOnErrors = false;
                        bool result = true;
+
+                       if (BuildTasks.Count == 0)
+                               // nothing to do
+                               return true;
                
-                       LogTargetStarted ();
-                       
-                       if (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.Message);
+                               LogMessage (MessageImportance.Low, "Error building target {0}: {1}", Name, e.ToString ());
+                               return false;
                        }
 
-                       LogTargetFinished (result);
-                       
                        if (executeOnErrors == true)
                                ExecuteOnErrors ();
                                
@@ -199,9 +242,17 @@ namespace Microsoft.Build.BuildEngine {
                void ExecuteOnErrors ()
                {
                        foreach (XmlElement onError in onErrorElements) {
-                               // FIXME: add condition
                                if (onError.GetAttribute ("ExecuteTargets") == String.Empty)
                                        throw new InvalidProjectFileException ("ExecuteTargets attribute is required in OnError element.");
+
+                               string on_error_condition = onError.GetAttribute ("Condition");
+                               if (!ConditionParser.ParseAndEvaluate (on_error_condition, Project)) {
+                                       LogMessage (MessageImportance.Low,
+                                               "OnError for target {0} skipped due to false condition: {1}",
+                                               Name, on_error_condition);
+                                       continue;
+                               }
+
                                string[] targetsToExecute = onError.GetAttribute ("ExecuteTargets").Split (';');
                                foreach (string t in targetsToExecute)
                                        this.project.Targets [t].Build ();
@@ -211,25 +262,33 @@ namespace Microsoft.Build.BuildEngine {
                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);
+                       bmea = new BuildMessageEventArgs (String.Format (
+                                               "Target {0} skipped, as it has already been built.", Name),
+                                       null, null, MessageImportance.Low);
+
+                       project.ParentEngine.EventSource.FireMessageRaised (this, bmea);
                }
-               
-               void LogTargetStarted ()
+
+               void LogError (string message, params object [] messageArgs)
                {
-                       TargetStartedEventArgs tsea;
-                       string projectFile = project.FullFileName;
-                       tsea = new TargetStartedEventArgs ("Target " + name + " started.", null, name, projectFile, null);
-                       engine.EventSource.FireTargetStarted (this, tsea);
+                       if (message == null)
+                               throw new ArgumentException ("message");
+
+                       BuildErrorEventArgs beea = new BuildErrorEventArgs (
+                               null, null, null, 0, 0, 0, 0, String.Format (message, messageArgs),
+                               null, null);
+                       engine.EventSource.FireErrorRaised (this, beea);
                }
-               
-               void LogTargetFinished (bool succeeded)
+
+               void LogMessage (MessageImportance importance, string message, params object [] messageArgs)
                {
-                       TargetFinishedEventArgs tfea;
-                       string projectFile = project.FullFileName;
-                       tfea = new TargetFinishedEventArgs ("Target " + name + " finished.", null, name, projectFile, null, succeeded);
-                       engine.EventSource.FireTargetFinished (this, tfea);
+                       if (message == null)
+                               throw new ArgumentNullException ("message");
+
+                       BuildMessageEventArgs bmea = new BuildMessageEventArgs (
+                               String.Format (message, messageArgs), null,
+                               null, importance);
+                       engine.EventSource.FireMessageRaised (this, bmea);
                }
        
                public string Condition {
@@ -253,10 +312,39 @@ namespace Microsoft.Build.BuildEngine {
                internal Project Project {
                        get { return project; }
                }
+
+               internal string TargetFile {
+                       get {
+                               if (importedProject != null)
+                                       return importedProject.FullFileName;
+                               return project != null ? project.FullFileName : String.Empty;
+                       }
+               }
+
+               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, ParseOptions.AllowItemsNoMetadataAndSplit);
+
+                               return (ITaskItem []) e.ConvertTo (project, typeof (ITaskItem []));
+                       }
+               }
        }
        
        internal enum BuildState {