New test.
[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 System.Collections.Generic;
33 using System.IO;
34 using Microsoft.Build.Framework;
35 using Mono.XBuild.Utilities;
36
37 namespace Microsoft.Build.BuildEngine {
38         public class Engine {
39                 
40                 string                  binPath;
41                 bool                    buildEnabled;
42                 TaskDatabase            defaultTasks;
43                 bool                    defaultTasksRegistered;
44                 const string            defaultTasksProjectName = "Microsoft.Common.tasks";
45                 EventSource             eventSource;
46                 bool                    buildStarted;
47                 BuildPropertyGroup      globalProperties;
48                 //IDictionary           importedProjects;
49                 List <ILogger>          loggers;
50                 bool                    onlyLogCriticalEvents;
51                 Dictionary <string, Project>    projects;
52
53                 static Engine           globalEngine;
54                 static Version          version;
55
56                 static Engine ()
57                 {
58                         version = new Version ("0.1");
59                 }
60                 
61                 public Engine ()
62                         : this (null)
63                 {
64                 }
65
66                 // engine should be invoked with path where binary files are
67                 // to find microsoft.build.tasks
68                 public Engine (string binPath)
69                 {
70                         this.binPath = binPath;
71                         this.buildEnabled = true;
72                         this.projects = new Dictionary <string, Project> ();
73                         this.eventSource = new EventSource ();
74                         this.loggers = new List <ILogger> ();
75                         this.buildStarted = false;
76                         this.globalProperties = new BuildPropertyGroup ();
77                         
78                         RegisterDefaultTasks ();
79                 }
80                 
81                 [MonoTODO]
82                 public bool BuildProject (Project project)
83                 {
84                         return project.Build ();
85                 }
86                 
87                 [MonoTODO]
88                 public bool BuildProject (Project project, string targetName)
89                 {
90                         return BuildProject (project, new string[] { targetName}, new Hashtable (), BuildSettings.None);
91                 }
92                 
93                 [MonoTODO]
94                 public bool BuildProject (Project project, string[] targetNames)
95                 {
96                         return BuildProject (project, targetNames, new Hashtable (), BuildSettings.None);
97                 }
98
99                 [MonoTODO]
100                 public bool BuildProject (Project project,
101                                           string[] targetNames,
102                                           IDictionary targetOutputs)
103                 {
104                         return BuildProject (project, targetNames, targetOutputs, BuildSettings.None);
105                 }
106                 
107                 [MonoTODO ("use buildFlags")]
108                 public bool BuildProject (Project project,
109                                           string[] targetNames,
110                                           IDictionary targetOutputs,
111                                           BuildSettings buildFlags)
112                 {
113                         bool result;
114                         
115                         LogProjectStarted (project, targetNames);
116                                 
117                         result =  project.Build (targetNames, targetOutputs);
118                         
119                         LogProjectFinished (project, result);
120                         
121                         return result;
122                 }
123
124                 [MonoTODO]
125                 public bool BuildProjectFile (string projectFile)
126                 {
127                         throw new NotImplementedException ();
128                 }
129                 
130                 [MonoTODO]
131                 public bool BuildProjectFile (string projectFile,
132                                               string targetName)
133                 {
134                         throw new NotImplementedException ();
135                 }
136                 
137                 [MonoTODO]
138                 public bool BuildProjectFile (string projectFile,
139                                               string[] targetNames)
140                 {
141                         throw new NotImplementedException ();
142                 }
143                 
144                 [MonoTODO]
145                 public bool BuildProjectFile (string projectFile,
146                                               string[] targetNames,
147                                               BuildPropertyGroup globalProperties)
148                 {
149                         return BuildProjectFile (projectFile, targetNames, globalProperties, new Hashtable (), BuildSettings.None);
150                 }
151                 
152                 [MonoTODO]
153                 public bool BuildProjectFile (string projectFile,
154                                               string[] targetNames,
155                                               BuildPropertyGroup globalProperties,
156                                               IDictionary targetOutputs)
157                 {
158                         return BuildProjectFile (projectFile, targetNames, globalProperties, targetOutputs, BuildSettings.None);
159                 }
160                 
161                 [MonoTODO ("use buildFlags")]
162                 public bool BuildProjectFile (string projectFile,
163                                               string[] targetNames,
164                                               BuildPropertyGroup globalProperties,
165                                               IDictionary targetOutputs,
166                                               BuildSettings buildFlags)
167                 {
168                         bool result;
169                         Project project;
170                         
171                         if (projects.ContainsKey (projectFile)) {
172                                 project = (Project) projects [projectFile];
173                                 LogProjectStarted (project, targetNames);
174                                 result = project.Build (targetNames, targetOutputs);
175                         }
176                         else
177                                 return false;
178                         
179                         LogProjectFinished (project, result);
180                         
181                         return result;
182                 }
183
184                 private void CheckBinPath ()
185                 {
186                         if (BinPath == null) {
187                                 throw new InvalidOperationException ("Before a project can be instantiated, " +
188                                         "Engine.BinPath must be set to the location on disk where MSBuild " + 
189                                         "is installed. This is used to evaluate $(MSBuildBinPath).");
190                         }
191                 }
192
193                 public Project CreateNewProject ()
194                 {
195                         if (defaultTasksRegistered == true)
196                                 CheckBinPath ();
197                         // FIXME: I don't really know if it should be here
198                         LogBuildStarted ();
199                         return new Project (this);
200                 }
201
202                 public Project GetLoadedProject (string projectFullFileName)
203                 {
204                         if (projectFullFileName == null)
205                                 throw new ArgumentNullException ("projectFullFileName");
206                         
207                         return (Project) projects [projectFullFileName];
208                 }
209
210                 internal void RemoveLoadedProject (Project p)
211                 {
212                         if (p.FullFileName != String.Empty)
213                                 projects.Remove (p.FullFileName);
214                 }
215
216                 internal void AddLoadedProject (Project p)
217                 {
218                         if (p.FullFileName != String.Empty)
219                                 projects.Add (p.FullFileName, p);
220                 }
221         
222                 public void UnloadProject (Project project)
223                 {
224                         if (project.ParentEngine != this)
225                                 throw new InvalidOperationException ("This project is not loaded in this engine");
226                         
227                         project.CheckUnloaded ();
228                         
229                         if (project.FullFileName != String.Empty)
230                                 projects.Remove (project.FullFileName);
231                         
232                         project.Unload ();
233                 }
234
235                 public void UnloadAllProjects ()
236                 {
237                         foreach (KeyValuePair <string, Project> e in projects)
238                                 UnloadProject ((Project) e.Value);
239                 }
240
241                 [MonoTODO]
242                 public void RegisterLogger (ILogger logger)
243                 {
244                         if (logger == null)
245                                 throw new ArgumentNullException ("logger");
246                         
247                         logger.Initialize (eventSource);
248                         loggers.Add (logger);
249                 }
250                 
251                 [MonoTODO]
252                 public void UnregisterAllLoggers ()
253                 {
254                         // FIXME: check if build succeeded
255                         LogBuildFinished (true);
256                         foreach (ILogger i in loggers) {
257                                 i.Shutdown ();
258                         }
259                         loggers.Clear ();
260                 }
261                 
262                 private void LogProjectStarted (Project project, string[] targetNames)
263                 {
264                         ProjectStartedEventArgs psea;
265                         if (targetNames.Length == 0) {
266                                 if (project.DefaultTargets != String.Empty)
267                                         psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName,
268                                                 project.DefaultTargets, null, null);
269                                 else
270                                         psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName, "default", null, null);
271                         } else
272                         psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName, String.Join (";",
273                                 targetNames), null, null);
274                         eventSource.FireProjectStarted (this, psea);
275                 }
276                 
277                 private void LogProjectFinished (Project project, bool succeeded)
278                 {
279                         ProjectFinishedEventArgs pfea;
280                         pfea = new ProjectFinishedEventArgs ("Project started.", null, project.FullFileName, succeeded);
281                         eventSource.FireProjectFinished (this, pfea);
282                 }
283                 
284                 private void LogBuildStarted ()
285                 {
286                         BuildStartedEventArgs bsea;
287                         bsea = new BuildStartedEventArgs ("Build started.", null);
288                         eventSource.FireBuildStarted (this, bsea);
289                 }
290                 
291                 private void LogBuildFinished (bool succeeded)
292                 {
293                         BuildFinishedEventArgs bfea;
294                         bfea = new BuildFinishedEventArgs ("Build finished.", null, succeeded);
295                         eventSource.FireBuildFinished (this, bfea);
296                 }
297                 
298                 private void RegisterDefaultTasks ()
299                 {
300                         this.defaultTasksRegistered = false;
301                         
302                         Project defaultTasksProject = CreateNewProject ();
303                         
304                         if (binPath != null) {
305                                 if (File.Exists (Path.Combine (binPath, defaultTasksProjectName)) == true) {
306                                         defaultTasksProject.Load (Path.Combine (binPath, defaultTasksProjectName));
307                                         defaultTasks = defaultTasksProject.TaskDatabase;
308                                 } else {
309                                         defaultTasks = new TaskDatabase ();
310                                 }
311                         } else {
312                                 defaultTasks = new TaskDatabase ();
313                         }
314                         
315                         this.defaultTasksRegistered = true;
316                 }
317
318                 public string BinPath {
319                         get { return binPath; }
320                         set { binPath = value; }
321                 }
322
323                 public bool BuildEnabled {
324                         get { return buildEnabled; }
325                         set { buildEnabled = value; }
326                 }
327
328                 public static Version Version {
329                         get { return version; }
330                 }
331
332                 public static Engine GlobalEngine {
333                         get {
334                                 if (globalEngine == null)
335                                         globalEngine = new Engine ();
336                                 return globalEngine;
337                         }
338                 }
339
340                 public BuildPropertyGroup GlobalProperties {
341                         get { return globalProperties; }
342                         set { globalProperties = value; }
343                 }
344
345                 public bool OnlyLogCriticalEvents {
346                         get { return eventSource.OnlyLogCriticalEvents; }
347                         set { eventSource.OnlyLogCriticalEvents = value; }
348                 }
349                 
350                 internal EventSource EventSource {
351                         get { return eventSource; }
352                 }
353                 
354                 internal bool DefaultTasksRegistered {
355                         get { return defaultTasksRegistered; }
356                 }
357                 
358                 internal TaskDatabase DefaultTasks {
359                         get { return defaultTasks; }
360                 }
361         }
362 }
363
364 #endif