2 // Target.cs: Represents a target.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
33 using Microsoft.Build.Framework;
35 namespace Microsoft.Build.BuildEngine {
36 public class Target : IEnumerable {
38 BatchingImpl batchingImpl;
39 BuildState buildState;
40 XmlAttribute condition;
41 XmlAttribute dependsOnTargets;
46 XmlElement targetElement;
47 ArrayList onErrorElements;
51 internal Target (XmlElement targetElement, Project project)
54 throw new ArgumentNullException ("project");
55 if (targetElement == null)
56 throw new ArgumentNullException ("targetElement");
58 this.targetElement = targetElement;
59 this.name = targetElement.GetAttribute ("Name");
60 this.condition = targetElement.GetAttributeNode ("Condition");
61 this.dependsOnTargets = targetElement.GetAttributeNode ("DependsOnTargets");
63 this.project = project;
64 this.engine = project.ParentEngine;
65 this.isImported = false;;
67 this.onErrorElements = new ArrayList ();
68 this.buildState = BuildState.NotStarted;
69 this.buildTasks = new ArrayList ();
70 this.batchingImpl = new BatchingImpl (project, this.targetElement);
72 foreach (XmlNode xn in targetElement.ChildNodes) {
73 if (xn is XmlElement) {
74 XmlElement xe = (XmlElement) xn;
75 if (xe.Name == "OnError") {
76 onErrorElements.Add (xe);
79 buildTasks.Add (new BuildTask (xe, this));
84 internal bool Build ()
88 buildState = BuildState.Started;
89 if (dependsOnTargets == null) {
91 } else if (dependsOnTargets.Value == "") {
94 OldExpression dependencies = new OldExpression (Project);
95 dependencies.ParseSource (dependsOnTargets.Value);
97 string[] targetsToBuildFirst = (string[]) dependencies.ConvertTo (typeof (string[]));
98 foreach (string target in targetsToBuildFirst) {
99 string trimmed = target.Trim ();
100 Target t = (Target) project.Targets [trimmed];
102 throw new InvalidProjectFileException (String.Format ("Target {0} not found.", trimmed));
103 if (t.BuildState == BuildState.NotStarted) {
106 if (t.BuildState == BuildState.Started)
107 throw new InvalidProjectFileException ("Cycle in target dependencies detected.");
111 result = RealBuild ();
112 buildState = BuildState.Finished;
117 private bool RealBuild ()
119 bool executeOnErrors = false;
124 if (this.batchingImpl.BuildNeeded ()) {
125 foreach (BuildTask bt in buildTasks) {
126 if (this.batchingImpl.BatchBuildTask (bt) == false && bt.ContinueOnError == false) {
127 executeOnErrors = true;
136 LogTargetFinished (result);
138 if (executeOnErrors == true)
144 private void ExecuteOnErrors ()
146 foreach (XmlElement onError in onErrorElements) {
147 // FIXME: add condition
148 if (onError.GetAttribute ("ExecuteTargets") == String.Empty)
149 throw new InvalidProjectFileException ("ExecuteTargets attribute is required in OnError element.");
150 string[] targetsToExecute = onError.GetAttribute ("ExecuteTargets").Split (';');
151 foreach (string t in targetsToExecute)
152 this.project.Targets [t].Build ();
156 private void LogTargetSkipped ()
158 BuildMessageEventArgs bmea;
159 bmea = new BuildMessageEventArgs (String.Format ("Skipping target \"{0}\" because its outputs are up-to-date.",
160 name), null, "MSBuild", MessageImportance.Normal);
161 engine.EventSource.FireMessageRaised (this, bmea);
164 private void LogTargetStarted ()
166 TargetStartedEventArgs tsea;
167 string projectFile = project.FullFileName;
168 tsea = new TargetStartedEventArgs ("Target " + name + " started.", null, name, projectFile, null);
169 engine.EventSource.FireTargetStarted (this, tsea);
172 private void LogTargetFinished (bool succeeded)
174 TargetFinishedEventArgs tfea;
175 string projectFile = project.FullFileName;
176 tfea = new TargetFinishedEventArgs ("Target " + name + " finished.", null, name, projectFile, null, succeeded);
177 engine.EventSource.FireTargetFinished (this, tfea);
181 public BuildTask AddNewTask (string taskName)
183 throw new NotImplementedException ();
186 public IEnumerator GetEnumerator ()
188 foreach (BuildTask bt in buildTasks) {
193 public void RemoveTask (BuildTask buildTask)
195 //taskElements.Remove (taskElement);
196 buildTasks.Remove (buildTask);
199 public string Condition {
200 get { return condition.Value; }
201 set { condition.Value = value; }
204 public string DependsOnTargets {
206 if (dependsOnTargets == null)
209 return dependsOnTargets.Value;
212 if (dependsOnTargets != null)
213 dependsOnTargets.Value = value;
217 public bool IsImported {
218 get { return isImported; }
219 internal set { isImported = value; }
226 internal Project Project {
227 get { return project; }
230 internal BuildState BuildState {
231 get { return buildState; }
235 internal enum BuildState {