2006-03-21 Crestez Leonard <cdleonard@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / Engine.cs
1 //
2 // Engine.cs: Main engine of XBuild.
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 Microsoft.Build.Framework;
33
34 namespace Microsoft.Build.BuildEngine {
35         public class Engine {
36                 
37                 string                  binPath;
38                 bool                    buildEnabled;
39                 BuildPropertyGroup      environmentProperties;
40                 EventSource             eventSource;
41                 bool                    buildStarted;
42                 BuildPropertyGroup      globalProperties;
43                 IDictionary             importedProjects;
44                 IList                   loggers;
45                 bool                    onlyLogCriticalEvents;
46                 IDictionary             projects;
47                 BuildPropertyGroup      reservedProperties;
48
49                 // FIXME: GlobalEngine static property uses this but what about GlobalEngineAccessor?
50                 static Engine           globalEngine;
51                 static Version          version;
52
53                 static Engine ()
54                 {
55                         version = new Version("0.1");
56                 }
57                 
58                 public Engine ()
59                         : this (null)
60                 {
61                 }
62
63                 // engine should be invoked with path where binary files are
64                 // to find microsoft.build.tasks
65                 public Engine (string binPath)
66                 {
67                         this.binPath = binPath;
68                         this.projects = new Hashtable ();
69                         this.eventSource = new EventSource ();
70                         this.loggers = new ArrayList ();
71                         this.buildStarted = false;
72                         this.LoadEnvironmentProperties ();
73                         this.reservedProperties = new BuildPropertyGroup ();
74                         this.reservedProperties.AddProperty (new BuildProperty ("MSBuildBinPath", binPath, PropertyType.Reserved));
75                 }
76                 
77                 [MonoTODO]
78                 public bool BuildProject (Project project)
79                 {
80                         return project.Build ();
81                 }
82                 
83                 [MonoTODO]
84                 public bool BuildProject (Project project, string targetName)
85                 {
86                         return BuildProject (project, new string[] { targetName}, new Hashtable (), BuildSettings.None);
87                 }
88                 
89                 [MonoTODO]
90                 public bool BuildProject (Project project, string[] targetNames)
91                 {
92                         return BuildProject (project, targetNames, new Hashtable (), BuildSettings.None);
93                 }
94
95                 [MonoTODO]
96                 public bool BuildProject (Project project,
97                                           string[] targetNames,
98                                           IDictionary targetOutputs)
99                 {
100                         return BuildProject (project, targetNames, targetOutputs, BuildSettings.None);
101                 }
102                 
103                 [MonoTODO ("use buildFlags")]
104                 public bool BuildProject (Project project,
105                                           string[] targetNames,
106                                           IDictionary targetOutputs,
107                                           BuildSettings buildFlags)
108                 {
109                         bool result;
110                         
111                         LogProjectStarted (project, targetNames);
112                                 
113                         result =  project.Build (targetNames, targetOutputs);
114                         
115                         LogProjectFinished (project, result);
116                         
117                         return result;
118                 }
119
120                 [MonoTODO]
121                 public bool BuildProjectFile (string projectFile)
122                 {
123                         throw new NotImplementedException ();
124                 }
125                 
126                 [MonoTODO]
127                 public bool BuildProjectFile (string projectFile,
128                                               string targetName)
129                 {
130                         throw new NotImplementedException ();
131                 }
132                 
133                 [MonoTODO]
134                 public bool BuildProjectFile (string projectFile,
135                                               string[] targetNames)
136                 {
137                         throw new NotImplementedException ();
138                 }
139                 
140                 [MonoTODO]
141                 public bool BuildProjectFile (string projectFile,
142                                               string[] targetNames,
143                                               BuildPropertyGroup globalProperties)
144                 {
145                         return BuildProjectFile (projectFile, targetNames, globalProperties, new Hashtable (), BuildSettings.None);
146                 }
147                 
148                 [MonoTODO]
149                 public bool BuildProjectFile (string projectFile,
150                                               string[] targetNames,
151                                               BuildPropertyGroup globalProperties,
152                                               IDictionary targetOutputs)
153                 {
154                         return BuildProjectFile (projectFile, targetNames, globalProperties, targetOutputs, BuildSettings.None);
155                 }
156                 
157                 [MonoTODO ("use buildFlags")]
158                 public bool BuildProjectFile (string projectFile,
159                                               string[] targetNames,
160                                               BuildPropertyGroup globalProperties,
161                                               IDictionary targetOutputs,
162                                               BuildSettings buildFlags)
163                 {
164                         bool result;
165                         Project project;
166                         
167                         if (projects.Contains (projectFile)) {
168                                 project = (Project) projects [projectFile];
169                                 LogProjectStarted (project, targetNames);
170                                 result = project.Build (targetNames, targetOutputs);
171                         }
172                         else
173                                 return false;
174                         
175                         LogProjectFinished (project, result);
176                         
177                         return result;
178                 }
179
180                 public Project CreateNewProject ()
181                 {
182                         if (buildStarted == false) {
183                                 LogBuildStarted ();
184                                 buildStarted = true;
185                         }
186                         Project p = new Project (this);
187                         p.EnvironmentProperties = this.environmentProperties;
188                         p.ReservedProperties = this.reservedProperties;
189                         if (globalProperties != null) {
190                                 BuildPropertyGroup bpg = new BuildPropertyGroup ();
191                                 foreach (BuildProperty bp in globalProperties)
192                                         bpg.AddProperty (bp.Clone (false));
193                                 p.GlobalProperties = bpg;
194                         }
195                         return p;
196                 }
197
198                 public Project GetLoadedProject (string projectFullFileName)
199                 {
200                         return (Project) projects [projectFullFileName];
201                 }
202
203                 public void RegisterLogger (ILogger logger)
204                 {
205                         if (logger == null)
206                                 throw new ArgumentNullException ("logger");
207                         logger.Initialize (eventSource);
208                         loggers.Add (logger);
209                 }
210                 
211                 [MonoTODO]
212                 public void UnloadAllProjects ()
213                 {
214                 }
215                 
216                 [MonoTODO]
217                 public void UnloadProject (Project project)
218                 {
219                 }
220
221                 public void UnregisterAllLoggers ()
222                 {
223                         // FIXME: check if build succeeded
224                         LogBuildFinished (true);
225                         foreach (ILogger i in loggers) {
226                                 i.Shutdown ();
227                         }
228                         loggers.Clear ();
229                 }
230                 
231                 private void LoadEnvironmentProperties ()
232                 {
233                         environmentProperties = new BuildPropertyGroup ();
234                         IDictionary environment = Environment.GetEnvironmentVariables ();
235                         foreach (DictionaryEntry de in environment) {
236                                 BuildProperty bp;
237                                 bp = new BuildProperty ((string) de.Key, (string) de.Value, PropertyType.Environment);
238                                 environmentProperties.AddProperty (bp);
239                         }
240                 }
241                 
242                 private void LogProjectStarted (Project project, string[] targetNames)
243                 {
244                         ProjectStartedEventArgs psea;
245                         if (targetNames.Length == 0) {
246                                 if (project.DefaultTargets != String.Empty)
247                                         psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName,
248                                                 project.DefaultTargets, null, null);
249                                 else
250                                         psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName, "default", null, null);
251                         } else
252                         psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName, String.Join (";",
253                                 targetNames), null, null);
254                         eventSource.FireProjectStarted (this, psea);
255                 }
256                 
257                 private void LogProjectFinished (Project project, bool succeeded)
258                 {
259                         ProjectFinishedEventArgs pfea;
260                         pfea = new ProjectFinishedEventArgs ("Project started.", null, project.FullFileName, succeeded);
261                         eventSource.FireProjectFinished (this, pfea);
262                 }
263                 
264                 private void LogBuildStarted ()
265                 {
266                         BuildStartedEventArgs bsea;
267                         bsea = new BuildStartedEventArgs ("Build started.", null);
268                         eventSource.FireBuildStarted (this, bsea);
269                 }
270                 
271                 private void LogBuildFinished (bool succeeded)
272                 {
273                         BuildFinishedEventArgs bfea;
274                         bfea = new BuildFinishedEventArgs ("Build finished.", null, succeeded);
275                         eventSource.FireBuildFinished (this, bfea);
276                 }
277
278                 public string BinPath {
279                         get { return binPath; }
280                         set { binPath = value; }
281                 }
282
283                 public bool BuildEnabled {
284                         get { return buildEnabled; }
285                         set { buildEnabled = value; }
286                 }
287
288                 public static Version Version {
289                         get { return version; }
290                 }
291
292                 public static Engine GlobalEngine {
293                         get { return globalEngine; }
294                 }
295
296                 public BuildPropertyGroup GlobalProperties {
297                         get { return globalProperties; }
298                         set { globalProperties = value; }
299                 }
300
301                 public bool OnlyLogCriticalEvents {
302                         get { return eventSource.OnlyLogCriticalEvents; }
303                         set { eventSource.OnlyLogCriticalEvents = value; }
304                 }
305                 
306                 internal EventSource EventSource {
307                         get { return eventSource; }
308                 }
309         }
310 }
311
312 #endif