2006-05-22 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 bool Build ()
85                 {
86                         bool result;
87                 
88                         buildState = BuildState.Started;
89                         if (dependsOnTargets == null) {
90                                 ;
91                         } else if (dependsOnTargets.Value == "") {
92                                 ;
93                         } else {
94                                 OldExpression dependencies = new OldExpression (Project);
95                                 dependencies.ParseSource (dependsOnTargets.Value);
96                                 
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];
101                                         if (t == null)
102                                                 throw new InvalidProjectFileException (String.Format ("Target {0} not found.", trimmed));
103                                         if (t.BuildState == BuildState.NotStarted) {
104                                                 t.Build ();
105                                         }
106                                         if (t.BuildState == BuildState.Started)
107                                                 throw new InvalidProjectFileException ("Cycle in target dependencies detected.");
108                                 }
109                         }
110                         
111                         result = RealBuild ();
112                         buildState = BuildState.Finished;
113                         
114                         return result;
115                 }
116                 
117                 private bool RealBuild ()
118                 {
119                         bool executeOnErrors = false;
120                         bool result = true;
121                 
122                         LogTargetStarted ();
123                         
124                         if (this.batchingImpl.BuildNeeded ()) {
125                                 foreach (BuildTask bt in buildTasks) {
126                                         if (this.batchingImpl.BatchBuildTask (bt) == false && bt.ContinueOnError == false) {
127                                                 executeOnErrors = true;
128                                                 result = false;
129                                                 break;
130                                         }
131                                 }
132                         } else {
133                                 LogTargetSkipped ();
134                         }
135
136                         LogTargetFinished (result);
137                         
138                         if (executeOnErrors == true)
139                                 ExecuteOnErrors ();
140                                 
141                         return result;
142                 }
143                 
144                 private void ExecuteOnErrors ()
145                 {
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 ();
153                         }
154                 }
155                 
156                 private void LogTargetSkipped ()
157                 {
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);
162                 }
163                 
164                 private void LogTargetStarted ()
165                 {
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);
170                 }
171                 
172                 private void LogTargetFinished (bool succeeded)
173                 {
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);
178                 }
179         
180                 [MonoTODO]
181                 public BuildTask AddNewTask (string taskName)
182                 {
183                         throw new NotImplementedException ();
184                 }
185
186                 public IEnumerator GetEnumerator ()
187                 {
188                         foreach (BuildTask bt in buildTasks) {
189                                 yield return bt;
190                         }
191                 }
192
193                 public void RemoveTask (BuildTask buildTask)
194                 {
195                         //taskElements.Remove (taskElement);
196                         buildTasks.Remove (buildTask);
197                 }
198
199                 public string Condition {
200                         get { return condition.Value; }
201                         set { condition.Value = value; }
202                 }
203
204                 public string DependsOnTargets {
205                         get {
206                                 if (dependsOnTargets == null)
207                                         return null;
208                                 else
209                                         return dependsOnTargets.Value;
210                         }
211                         set {
212                                 if (dependsOnTargets != null)
213                                         dependsOnTargets.Value = value;
214                         }
215                 }
216
217                 public bool IsImported {
218                         get { return isImported; }
219                         internal set { isImported = value; }
220                 }
221
222                 public string Name {
223                         get { return name; }
224                 }
225                 
226                 internal Project Project {
227                         get { return project; }
228                 }
229                 
230                 internal BuildState BuildState {
231                         get { return buildState; }
232                 }
233         }
234         
235         internal enum BuildState {
236                 NotStarted,
237                 Started,
238                 Finished,
239                 Skipped
240         }
241 }
242
243 #endif