47b5ffddffd0e5213883adcdeb7ff3040db9727c
[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 System.Linq;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
37 using Mono.XBuild.Utilities;
38
39 namespace Microsoft.Build.BuildEngine {
40         public class Engine {
41                 
42                 string                  binPath;
43                 bool                    buildEnabled;
44                 Dictionary<string, TaskDatabase> defaultTasksTableByToolsVersion;
45                 const string            defaultTasksProjectName = "Microsoft.Common.tasks";
46                 EventSource             eventSource;
47                 bool                    buildStarted;
48                 //ToolsetDefinitionLocations toolsetLocations;
49                 BuildPropertyGroup      global_properties;
50                 //IDictionary           importedProjects;
51                 List <ILogger>          loggers;
52                 //bool                  onlyLogCriticalEvents;
53                 Dictionary <string, Project>    projects;
54                 string defaultToolsVersion;
55
56                 // the key here represents the project+target+global_properties set
57                 Dictionary <string, ITaskItem[]> builtTargetsOutputByName;
58                 Stack<Project> currentlyBuildingProjectsStack;
59
60                 static Engine           globalEngine;
61                 static Version          version;
62
63                 static Engine ()
64                 {
65                         version = new Version ("0.1");
66                 }
67                 
68                 public Engine ()
69                         : this (ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20))
70                 {
71                 }
72
73                 public Engine (ToolsetDefinitionLocations locations)
74                         : this (ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20))
75                 {
76                         //toolsetLocations = locations;
77                 }
78                 
79                 public Engine (BuildPropertyGroup globalProperties)
80                         : this (ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20))
81                 {
82                         this.global_properties = globalProperties;
83                 }
84
85                 public Engine (BuildPropertyGroup globalProperties, ToolsetDefinitionLocations locations)
86                         : this (ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20))
87                 {
88                         this.global_properties = globalProperties;
89                         //toolsetLocations = locations;
90                 }
91
92                 // engine should be invoked with path where binary files are
93                 // to find microsoft.build.tasks
94                 public Engine (string binPath)
95                 {
96                         this.binPath = binPath;
97                         this.buildEnabled = true;
98                         this.projects = new Dictionary <string, Project> ();
99                         this.eventSource = new EventSource ();
100                         this.loggers = new List <ILogger> ();
101                         this.buildStarted = false;
102                         this.global_properties = new BuildPropertyGroup ();
103                         this.builtTargetsOutputByName = new Dictionary<string, ITaskItem[]> ();
104                         this.currentlyBuildingProjectsStack = new Stack<Project> ();
105                         this.Toolsets = new ToolsetCollection ();
106                         LoadDefaultToolsets ();
107                         defaultTasksTableByToolsVersion = new Dictionary<string, TaskDatabase> ();
108                 }
109
110                 //FIXME: should be loaded from config file
111                 void LoadDefaultToolsets ()
112                 {
113                         Toolsets.Add (new Toolset ("2.0",
114                                                 ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20)));
115                         Toolsets.Add (new Toolset ("3.0",
116                                                 ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version30)));
117                         Toolsets.Add (new Toolset ("3.5",
118                                                 ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version35)));
119 #if NET_4_0
120                         Toolsets.Add (new Toolset ("4.0",
121                                                 ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version40)));
122 #endif
123                 }
124                 
125                 [MonoTODO]
126                 public bool BuildProject (Project project)
127                 {
128                         if (project == null)
129                                 throw new ArgumentException ("project");
130                         builtTargetsOutputByName.Clear ();
131                         return project.Build ();
132                 }
133                 
134                 [MonoTODO]
135                 public bool BuildProject (Project project, string targetName)
136                 {
137                         if (project == null)
138                                 throw new ArgumentException ("project");
139                         if (targetName == null)
140                                 return false;
141
142                         return BuildProject (project, new string[] { targetName}, null, BuildSettings.None);
143                 }
144                 
145                 [MonoTODO]
146                 public bool BuildProject (Project project, string[] targetNames)
147                 {
148                         return BuildProject (project, targetNames, null, BuildSettings.None);
149                 }
150
151                 [MonoTODO]
152                 public bool BuildProject (Project project,
153                                           string[] targetNames,
154                                           IDictionary targetOutputs)
155                 {
156                         return BuildProject (project, targetNames, targetOutputs, BuildSettings.None);
157                 }
158                 
159                 public bool BuildProject (Project project,
160                                           string[] targetNames,
161                                           IDictionary targetOutputs,
162                                           BuildSettings buildFlags)
163                 {
164                         if (project == null)
165                                 throw new ArgumentException ("project");
166                         if (targetNames == null)
167                                 return false;
168
169                         if ((buildFlags & BuildSettings.DoNotResetPreviouslyBuiltTargets) != BuildSettings.DoNotResetPreviouslyBuiltTargets)
170                                 builtTargetsOutputByName.Clear ();
171
172                         if (defaultToolsVersion != null)
173                                 // it has been explicitly set, xbuild does this..
174                                 project.ToolsVersion = defaultToolsVersion;
175                         return project.Build (targetNames, targetOutputs, buildFlags);
176                 }
177
178                 [MonoTODO]
179                 public bool BuildProjectFile (string projectFile)
180                 {
181                         return BuildProjectFile (projectFile, new string [0]);
182                 }
183                 
184                 [MonoTODO]
185                 public bool BuildProjectFile (string projectFile,
186                                               string targetName)
187                 {
188                         return BuildProjectFile (projectFile,
189                                                  targetName == null ? new string [0] : new string [] {targetName});
190                 }
191                 
192                 [MonoTODO]
193                 public bool BuildProjectFile (string projectFile,
194                                               string[] targetNames)
195                 {
196                         return BuildProjectFile (projectFile, targetNames, null);
197                 }
198                 
199                 [MonoTODO]
200                 public bool BuildProjectFile (string projectFile,
201                                               string[] targetNames,
202                                               BuildPropertyGroup globalProperties)
203                 {
204                         return BuildProjectFile (projectFile, targetNames, globalProperties, null, BuildSettings.None);
205                 }
206                 
207                 [MonoTODO]
208                 public bool BuildProjectFile (string projectFile,
209                                               string[] targetNames,
210                                               BuildPropertyGroup globalProperties,
211                                               IDictionary targetOutputs)
212                 {
213                         return BuildProjectFile (projectFile, targetNames, globalProperties, targetOutputs, BuildSettings.None);
214                 }
215                 
216                 public bool BuildProjectFile (string projectFile,
217                                               string[] targetNames,
218                                               BuildPropertyGroup globalProperties,
219                                               IDictionary targetOutputs,
220                                               BuildSettings buildFlags)
221                 {
222                         return BuildProjectFile (projectFile, targetNames, globalProperties, targetOutputs, buildFlags, null);
223                 }
224                         
225                 //FIXME: add a test for null @toolsVersion
226                 public bool BuildProjectFile (string projectFile,
227                                               string[] targetNames,
228                                               BuildPropertyGroup globalProperties,
229                                               IDictionary targetOutputs,
230                                               BuildSettings buildFlags, string toolsVersion)
231                 {
232                         bool result = false;
233                         try {
234                                 StartEngineBuild ();
235                                 result = BuildProjectFileInternal (projectFile, targetNames, globalProperties, targetOutputs, buildFlags, toolsVersion);
236                                 return result;
237                         } catch (InvalidProjectFileException ie) {
238                                 this.LogErrorWithFilename (projectFile, ie.Message);
239                                 this.LogMessage (MessageImportance.Low, String.Format ("{0}: {1}", projectFile, ie.ToString ()));
240                                 return false;
241                         } catch (Exception e) {
242                                 this.LogErrorWithFilename (projectFile, e.Message);
243                                 this.LogMessage (MessageImportance.Low, String.Format ("{0}: {1}", projectFile, e.ToString ()));
244                                 return false;
245                         } finally {
246                                 EndEngineBuild (result);
247                         }
248                 }
249
250                 bool BuildProjectFileInternal (string projectFile,
251                                               string[] targetNames,
252                                               BuildPropertyGroup globalProperties,
253                                               IDictionary targetOutputs,
254                                               BuildSettings buildFlags, string toolsVersion)
255                 {
256
257                         if ((buildFlags & BuildSettings.DoNotResetPreviouslyBuiltTargets) != BuildSettings.DoNotResetPreviouslyBuiltTargets)
258                                 builtTargetsOutputByName.Clear ();
259
260                         Project project;
261
262                         bool newProject = false;
263                         if (!projects.TryGetValue (projectFile, out project)) {
264                                 project = CreateNewProject ();
265                                 newProject = true;
266                         }
267
268                         BuildPropertyGroup engine_old_grp = null;
269                         BuildPropertyGroup project_old_grp = null;
270                         if (globalProperties != null) {
271                                 engine_old_grp = GlobalProperties.Clone (true);
272                                 project_old_grp = project.GlobalProperties.Clone (true);
273
274                                 // Override project's global properties with the
275                                 // ones explicitlcur_y specified here
276                                 foreach (BuildProperty bp in globalProperties)
277                                         project.GlobalProperties.AddProperty (bp);
278
279                                 if (!newProject)
280                                         project.NeedToReevaluate ();
281                         }
282
283                         if (newProject)
284                                 project.Load (projectFile);
285
286                         try {
287                                 string oldProjectToolsVersion = project.ToolsVersion;
288                                 if (String.IsNullOrEmpty (toolsVersion) && defaultToolsVersion != null)
289                                         // no tv specified, let the project inherit it from the
290                                         // engine. 'defaultToolsVersion' will be effective only
291                                         // it has been overridden. Otherwise, the project's own
292                                         // tv will be used.
293                                         project.ToolsVersion = defaultToolsVersion;
294                                 else
295                                         project.ToolsVersion = toolsVersion;
296
297                                 try {
298                                         return project.Build (targetNames, targetOutputs, buildFlags);
299                                 } finally {
300                                         project.ToolsVersion = oldProjectToolsVersion;
301                                 }
302                         } finally {
303                                 if (globalProperties != null) {
304                                         GlobalProperties = engine_old_grp;
305                                         project.GlobalProperties = project_old_grp;
306                                 }
307                         }
308                 }
309
310                 void CheckBinPath ()
311                 {
312                         if (BinPath == null) {
313                                 throw new InvalidOperationException ("Before a project can be instantiated, " +
314                                         "Engine.BinPath must be set to the location on disk where MSBuild " + 
315                                         "is installed. This is used to evaluate $(MSBuildBinPath).");
316                         }
317                 }
318
319                 public Project CreateNewProject ()
320                 {
321                         return new Project (this);
322                 }
323
324                 public Project GetLoadedProject (string projectFullFileName)
325                 {
326                         if (projectFullFileName == null)
327                                 throw new ArgumentNullException ("projectFullFileName");
328                         
329                         Project project;
330                         projects.TryGetValue (projectFullFileName, out project);
331
332                         return project;
333                 }
334
335                 internal void RemoveLoadedProject (Project p)
336                 {
337                         if (!String.IsNullOrEmpty (p.FullFileName)) {
338                                 ClearBuiltTargetsForProject (p);
339                                 projects.Remove (p.FullFileName);
340                         }
341                 }
342
343                 internal void AddLoadedProject (Project p)
344                 {
345                         if (p.FullFileName != String.Empty)
346                                 projects.Add (p.FullFileName, p);
347                 }
348         
349                 public void UnloadProject (Project project)
350                 {
351                         if (project == null)
352                                 throw new ArgumentNullException ("project");
353
354                         if (project.ParentEngine != this)
355                                 throw new InvalidOperationException ("The \"Project\" object specified does not belong to the correct \"Engine\" object.");
356                         
357                         project.CheckUnloaded ();
358                         
359                         RemoveLoadedProject (project);
360                         
361                         project.Unload ();
362                 }
363
364                 public void UnloadAllProjects ()
365                 {
366                         IList<Project> values = new List<Project> (projects.Values);
367                         foreach (Project p in values)
368                                 UnloadProject (p);
369                 }
370
371                 [MonoTODO]
372                 public void RegisterLogger (ILogger logger)
373                 {
374                         if (logger == null)
375                                 throw new ArgumentNullException ("logger");
376                         
377                         logger.Initialize (eventSource);
378                         loggers.Add (logger);
379                 }
380                 
381                 [MonoTODO]
382                 public void UnregisterAllLoggers ()
383                 {
384                         // FIXME: check if build succeeded
385                         // FIXME: it shouldn't be here
386                         if (buildStarted)
387                                 LogBuildFinished (true);
388                         foreach (ILogger i in loggers) {
389                                 i.Shutdown ();
390                         }
391                         loggers.Clear ();
392                 }
393
394                 void StartEngineBuild ()
395                 {
396                         if (!buildStarted) {
397                                 LogBuildStarted ();
398                                 buildStarted = true;
399                         }
400                 }
401
402                 void EndEngineBuild (bool succeeded)
403                 {
404                         if (buildStarted && currentlyBuildingProjectsStack.Count == 0) {
405                                 LogBuildFinished (succeeded);
406                                 buildStarted = false;
407                         }
408                 }
409
410                 internal void StartProjectBuild (Project project, string [] target_names)
411                 {
412                         StartEngineBuild ();
413
414                         if (currentlyBuildingProjectsStack.Count == 0 ||
415                                 String.Compare (currentlyBuildingProjectsStack.Peek ().FullFileName, project.FullFileName) != 0)
416                                         LogProjectStarted (project, target_names);
417
418                         currentlyBuildingProjectsStack.Push (project);
419                 }
420
421                 internal void EndProjectBuild (Project project, bool succeeded)
422                 {
423                         if (!buildStarted)
424                                 throw new Exception ("build isnt started currently");
425
426                         Project top_project = currentlyBuildingProjectsStack.Pop ();
427
428                         if (String.Compare (project.FullFileName, top_project.FullFileName) != 0)
429                                 throw new Exception (String.Format (
430                                                         "INTERNAL ERROR: Project finishing is not the same as the one on top " +
431                                                         "of the stack. Project: {0} Top of stack: {1}",
432                                                         project.FullFileName, top_project.FullFileName));
433
434                         if (currentlyBuildingProjectsStack.Count == 0 ||
435                                 String.Compare (top_project.FullFileName, currentlyBuildingProjectsStack.Peek ().FullFileName) != 0)
436                                 LogProjectFinished (top_project, succeeded);
437
438                         EndEngineBuild (succeeded);
439                 }
440
441                 internal void ClearBuiltTargetsForProject (Project project)
442                 {
443                         string project_key = project.GetKeyForTarget (String.Empty, false);
444                         var to_remove_keys = BuiltTargetsOutputByName.Keys.Where (key => key.StartsWith (project_key)).ToList ();
445                         foreach (string to_remove_key in to_remove_keys)
446                                 BuiltTargetsOutputByName.Remove (to_remove_key);
447                 }
448
449                 void LogProjectStarted (Project project, string [] target_names)
450                 {
451                         string targets;
452                         if (target_names == null || target_names.Length == 0)
453                                 targets = String.Empty;
454                         else
455                                 targets = String.Join (";", target_names);
456
457                         ProjectStartedEventArgs psea = new ProjectStartedEventArgs ("Project started.", null, project.FullFileName, targets,
458                                         project.EvaluatedPropertiesAsDictionaryEntries, project.EvaluatedItemsByNameAsDictionaryEntries);
459
460                         eventSource.FireProjectStarted (this, psea);
461                 }
462
463                 void LogProjectFinished (Project project, bool succeeded)
464                 {
465                         ProjectFinishedEventArgs pfea;
466                         pfea = new ProjectFinishedEventArgs ("Project started.", null, project.FullFileName, succeeded);
467                         eventSource.FireProjectFinished (this, pfea);
468                 }
469
470                 void LogBuildStarted ()
471                 {
472                         BuildStartedEventArgs bsea;
473                         bsea = new BuildStartedEventArgs ("Build started.", null);
474                         eventSource.FireBuildStarted (this, bsea);
475                 }
476                 
477                 void LogBuildFinished (bool succeeded)
478                 {
479                         BuildFinishedEventArgs bfea;
480                         bfea = new BuildFinishedEventArgs ("Build finished.", null, succeeded);
481                         eventSource.FireBuildFinished (this, bfea);
482                 }
483
484                 internal TaskDatabase GetDefaultTasks (string toolsVersion)
485                 {
486                         TaskDatabase db;
487                         if (defaultTasksTableByToolsVersion.TryGetValue (toolsVersion, out db))
488                                 return db;
489
490                         var toolset = Toolsets [toolsVersion];
491                         if (toolset == null)
492                                 throw new UnknownToolsVersionException (toolsVersion);
493
494                         string toolsPath = toolset.ToolsPath;
495                         string tasksFile = Path.Combine (toolsPath, defaultTasksProjectName);
496                         this.LogMessage (MessageImportance.Low, "Loading default tasks for ToolsVersion: {0} from {1}", toolsVersion, tasksFile);
497
498                         // set a empty taskdb here, because the project loading the tasks
499                         // file will try to get the default task db
500                         defaultTasksTableByToolsVersion [toolsVersion] = new TaskDatabase ();
501
502                         db = defaultTasksTableByToolsVersion [toolsVersion] = RegisterDefaultTasks (tasksFile);
503
504                         return db;
505                 }
506                 
507                 TaskDatabase RegisterDefaultTasks (string tasksFile)
508                 {
509                         Project defaultTasksProject = CreateNewProject ();
510                         TaskDatabase db;
511                         
512                         if (File.Exists (tasksFile)) {
513                                 defaultTasksProject.Load (tasksFile);
514                                 db = defaultTasksProject.TaskDatabase;
515                         } else {
516                                 this.LogWarning ("Default tasks file {0} not found, ignoring.", tasksFile);
517                                 db = new TaskDatabase ();
518                         }
519
520                         return db;
521                 }
522
523                 public string BinPath {
524                         get { return binPath; }
525                         set { binPath = value; }
526                 }
527
528                 public bool BuildEnabled {
529                         get { return buildEnabled; }
530                         set { buildEnabled = value; }
531                 }
532
533                 public static Version Version {
534                         get { return version; }
535                 }
536
537                 public static Engine GlobalEngine {
538                         get {
539                                 if (globalEngine == null)
540                                         globalEngine = new Engine ();
541                                 return globalEngine;
542                         }
543                 }
544
545                 public BuildPropertyGroup GlobalProperties {
546                         get { return global_properties; }
547                         set { global_properties = value; }
548                 }
549                 
550                 public ToolsetCollection Toolsets {
551                         get; private set;
552                 }
553
554                 public string DefaultToolsVersion {
555                         get {
556                                 // This is used as the fall back version if the
557                                 // project can't find a version to use
558                                 // Hard-coded to 2.0, so it allows even vs2005 projects
559                                 // to build correctly, as they won't have a ToolsVersion
560                                 // set!
561                                 return String.IsNullOrEmpty (defaultToolsVersion)
562                                                 ? "2.0"
563                                                 : defaultToolsVersion;
564                         }
565                         set {
566                                 if (Toolsets [value] == null)
567                                         throw new UnknownToolsVersionException (value);
568                                 defaultToolsVersion = value;
569                         }
570                 }
571                 
572                 public bool IsBuilding {
573                         get { return buildStarted; }
574                 }
575                 
576                 public bool OnlyLogCriticalEvents {
577                         get { return eventSource.OnlyLogCriticalEvents; }
578                         set { eventSource.OnlyLogCriticalEvents = value; }
579                 }
580                 
581                 internal EventSource EventSource {
582                         get { return eventSource; }
583                 }
584                 
585                 internal Dictionary<string, ITaskItem[]> BuiltTargetsOutputByName {
586                         get { return builtTargetsOutputByName; }
587                 }
588         }
589 }
590
591 #endif