2006-05-03 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / Target.cs
1 //
2 // Target.cs: Represents a target.
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.Xml;
33 using Microsoft.Build.Framework;
34
35 namespace Microsoft.Build.BuildEngine {
36         public class Target : IEnumerable {
37         
38                 BatchingImpl    batchingImpl;
39                 BuildState      buildState;
40                 XmlAttribute    condition;
41                 XmlAttribute    dependsOnTargets;
42                 Engine          engine;
43                 bool            isImported;
44                 string          name;
45                 Project         project;
46                 XmlElement      targetElement;
47                 ArrayList       onErrorElements;
48                 
49                 ArrayList       buildTasks;
50                 
51                 internal Target (XmlElement targetElement, Project project)
52                 {
53                         if (project == null)
54                                 throw new ArgumentNullException ("project");
55                         if (targetElement == null)
56                                 throw new ArgumentNullException ("targetElement");
57
58                         this.targetElement = targetElement;
59                         this.name = targetElement.GetAttribute ("Name");
60                         this.condition = targetElement.GetAttributeNode ("Condition");
61                         this.dependsOnTargets = targetElement.GetAttributeNode ("DependsOnTargets");
62
63                         this.project = project;
64                         this.engine = project.ParentEngine;
65                         this.isImported = false;;
66
67                         this.onErrorElements  = new ArrayList ();
68                         this.buildState = BuildState.NotStarted;
69                         this.buildTasks = new ArrayList ();
70                         this.batchingImpl = new BatchingImpl (project, this.targetElement);
71
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);
77                                                 continue;
78                                         }
79                                         buildTasks.Add (new BuildTask (xe, this));
80                                 }
81                         }
82                 }
83                 
84                 internal void Build ()
85                 {
86                         buildState = BuildState.Started;
87                         if (dependsOnTargets == null) {
88                                 ;
89                         } else if (dependsOnTargets.Value == "") {
90                                 ;
91                         } else {
92                                 OldExpression dependencies = new OldExpression (Project);
93                                 dependencies.ParseSource (dependsOnTargets.Value);
94                                 
95                                 string[] targetsToBuildFirst = (string[]) dependencies.ConvertTo (typeof (string[]));
96                                 foreach (string target in targetsToBuildFirst) {
97                                         string trimmed = target.Trim ();
98                                         Target t = (Target) project.Targets [trimmed];
99                                         if (t == null)
100                                                 throw new InvalidProjectFileException (String.Format ("Target {0} not found.", trimmed));
101                                         if (t.BuildState == BuildState.NotStarted) {
102                                                 t.Build ();
103                                         }
104                                         if (t.BuildState == BuildState.Started)
105                                                 throw new InvalidProjectFileException ("Cycle in target dependencies detected.");
106                                 }
107                         }
108                         RealBuild ();
109                         buildState = BuildState.Finished;
110                 }
111                 
112                 private void RealBuild ()
113                 {
114                         bool executeOnErrors = false;
115                         bool result = true;
116                 
117                         LogTargetStarted ();
118                         
119                         if (this.batchingImpl.BuildNeeded ()) {
120                                 foreach (BuildTask bt in buildTasks) {
121                                         if (this.batchingImpl.BatchBuildTask (bt) == false && bt.ContinueOnError == false) {
122                                                 executeOnErrors = true;
123                                                 result = false;
124                                                 break;
125                                         }
126                                 }
127                         } else {
128                                 LogTargetSkipped ();
129                         }
130
131                         LogTargetFinished (result);
132                         
133                         if (executeOnErrors == true)
134                                 ExecuteOnErrors ();
135                 }
136                 
137                 private void ExecuteOnErrors ()
138                 {
139                         foreach (XmlElement onError in onErrorElements) {
140                                 // FIXME: add condition
141                                 if (onError.GetAttribute ("ExecuteTargets") == String.Empty)
142                                         throw new InvalidProjectFileException ("ExecuteTargets attribute is required in OnError element.");
143                                 string[] targetsToExecute = onError.GetAttribute ("ExecuteTargets").Split (';');
144                                 foreach (string t in targetsToExecute)
145                                         this.project.Targets [t].Build ();
146                         }
147                 }
148                 
149                 private void LogTargetSkipped ()
150                 {
151                         BuildMessageEventArgs bmea;
152                         bmea = new BuildMessageEventArgs (String.Format ("Skipping target \"{0}\" because its outputs are up-to-date.",
153                                 name), null, "MSBuild", MessageImportance.Normal);
154                         engine.EventSource.FireMessageRaised (this, bmea);
155                 }
156                 
157                 private void LogTargetStarted ()
158                 {
159                         TargetStartedEventArgs tsea;
160                         string projectFile = project.FullFileName;
161                         tsea = new TargetStartedEventArgs ("Target " + name + " started.", null, name, projectFile, null);
162                         engine.EventSource.FireTargetStarted (this, tsea);
163                 }
164                 
165                 private void LogTargetFinished (bool succeeded)
166                 {
167                         TargetFinishedEventArgs tfea;
168                         string projectFile = project.FullFileName;
169                         tfea = new TargetFinishedEventArgs ("Target " + name + " finished.", null, name, projectFile, null, succeeded);
170                         engine.EventSource.FireTargetFinished (this, tfea);
171                 }
172         
173                 [MonoTODO]
174                 public BuildTask AddNewTask (string taskName)
175                 {
176                         throw new NotImplementedException ();
177                 }
178
179                 public IEnumerator GetEnumerator ()
180                 {
181                         foreach (BuildTask bt in buildTasks) {
182                                 yield return bt;
183                         }
184                 }
185
186                 public void RemoveTask (BuildTask buildTask)
187                 {
188                         //taskElements.Remove (taskElement);
189                         buildTasks.Remove (buildTask);
190                 }
191
192                 public string Condition {
193                         get { return condition.Value; }
194                         set { condition.Value = value; }
195                 }
196
197                 public string DependsOnTargets {
198                         get {
199                                 if (dependsOnTargets == null)
200                                         return null;
201                                 else
202                                         return dependsOnTargets.Value;
203                         }
204                         set {
205                                 if (dependsOnTargets != null)
206                                         dependsOnTargets.Value = value;
207                         }
208                 }
209
210                 public bool IsImported {
211                         get { return isImported; }
212                         internal set { isImported = value; }
213                 }
214
215                 public string Name {
216                         get { return name; }
217                 }
218                 
219                 internal Project Project {
220                         get { return project; }
221                 }
222                 
223                 internal BuildState BuildState {
224                         get { return buildState; }
225                 }
226         }
227         
228         internal enum BuildState {
229                 NotStarted,
230                 Started,
231                 Finished,
232                 Skipped
233         }
234 }
235
236 #endif