Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / Project.cs
1 //
2 // Project.cs: Project class
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //   Ankit Jain (jankit@novell.com)
7 //
8 // (C) 2005 Marek Sieradzki
9 // Copyright 2011 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 using System;
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Collections.Specialized;
34 using System.IO;
35 using System.Linq;
36 using System.Reflection;
37 using System.Text;
38 using System.Xml;
39 using System.Xml.Schema;
40 using Microsoft.Build.Framework;
41 using Microsoft.Build.Utilities;
42 using Mono.XBuild.Framework;
43 using Mono.XBuild.CommandLine;
44
45 namespace Microsoft.Build.BuildEngine {
46         public class Project {
47         
48                 bool                            buildEnabled;
49                 Dictionary <string, List <string>>      conditionedProperties;
50                 string[]                        defaultTargets;
51                 Encoding                        encoding;
52                 BuildItemGroup                  evaluatedItems;
53                 BuildItemGroup                  evaluatedItemsIgnoringCondition;
54                 Dictionary <string, BuildItemGroup>     evaluatedItemsByName;
55                 Dictionary <string, BuildItemGroup>     evaluatedItemsByNameIgnoringCondition;
56                 BuildPropertyGroup              evaluatedProperties;
57                 string                          firstTargetName;
58                 string                          fullFileName;
59                 BuildPropertyGroup              globalProperties;
60                 GroupingCollection              groupingCollection;
61                 bool                            isDirty;
62                 bool                            isValidated;
63                 BuildItemGroupCollection        itemGroups;
64                 ImportCollection                imports;
65                 List<string>                    initialTargets;
66                 Dictionary <string, BuildItemGroup> last_item_group_containing;
67                 bool                            needToReevaluate;
68                 Engine                          parentEngine;
69                 BuildPropertyGroupCollection    propertyGroups;
70                 string                          schemaFile;
71                 TaskDatabase                    taskDatabase;
72                 TargetCollection                targets;
73                 DateTime                        timeOfLastDirty;
74                 UsingTaskCollection             usingTasks;
75                 XmlDocument                     xmlDocument;
76                 bool                            unloaded;
77                 bool                            initialTargetsBuilt;
78                 bool                            building;
79                 BuildSettings                   current_settings;
80                 Stack<Batch>                    batches;
81
82                 // This is used to keep track of "current" file,
83                 // which is then used to set the reserved properties
84                 // $(MSBuildThisFile*)
85                 Stack<string> this_file_property_stack;
86                 ProjectLoadSettings             project_load_settings;
87
88
89                 static string extensions_path;
90                 static XmlNamespaceManager      manager;
91                 static string ns = "http://schemas.microsoft.com/developer/msbuild/2003";
92
93                 public Project ()
94                         : this (Engine.GlobalEngine)
95                 {
96                 }
97
98                 public Project (Engine engine) : this (engine, null)
99                 {
100                 }
101                 
102                 public Project (Engine engine, string toolsVersion)
103                 {
104                         parentEngine  = engine;
105                         ToolsVersion = toolsVersion;
106
107                         buildEnabled = ParentEngine.BuildEnabled;
108                         xmlDocument = new XmlDocument ();
109                         xmlDocument.PreserveWhitespace = false;
110                         xmlDocument.AppendChild (xmlDocument.CreateElement ("Project", XmlNamespace));
111                         xmlDocument.DocumentElement.SetAttribute ("xmlns", ns);
112                         
113                         fullFileName = String.Empty;
114                         timeOfLastDirty = DateTime.Now;
115                         current_settings = BuildSettings.None;
116                         project_load_settings = ProjectLoadSettings.None;
117
118                         encoding = null;
119
120                         initialTargets = new List<string> ();
121                         defaultTargets = new string [0];
122                         batches = new Stack<Batch> ();
123                         this_file_property_stack = new Stack<string> ();
124
125                         globalProperties = new BuildPropertyGroup (null, this, null, false);
126                         foreach (BuildProperty bp in parentEngine.GlobalProperties)
127                                 GlobalProperties.AddProperty (bp.Clone (true));
128                         
129                         ProcessXml ();
130
131                 }
132
133                 [MonoTODO ("Not tested")]
134                 public void AddNewImport (string importLocation,
135                                           string importCondition)
136                 {
137                         if (importLocation == null)
138                                 throw new ArgumentNullException ("importLocation");
139
140                         XmlElement importElement = xmlDocument.CreateElement ("Import", XmlNamespace);
141                         xmlDocument.DocumentElement.AppendChild (importElement);
142                         importElement.SetAttribute ("Project", importLocation);
143                         if (!String.IsNullOrEmpty (importCondition))
144                                 importElement.SetAttribute ("Condition", importCondition);
145
146                         AddImport (importElement, null, false);
147                         MarkProjectAsDirty ();
148                         NeedToReevaluate ();
149                 }
150
151                 public BuildItem AddNewItem (string itemName,
152                                              string itemInclude)
153                 {
154                         return AddNewItem (itemName, itemInclude, false);
155                 }
156                 
157                 [MonoTODO ("Adds item not in the same place as MS")]
158                 public BuildItem AddNewItem (string itemName,
159                                              string itemInclude,
160                                              bool treatItemIncludeAsLiteral)
161                 {
162                         BuildItemGroup big;
163
164                         if (itemGroups.Count == 0)
165                                 big = AddNewItemGroup ();
166                         else {
167                                 if (last_item_group_containing.ContainsKey (itemName)) {
168                                         big = last_item_group_containing [itemName];
169                                 } else {
170                                         // FIXME: not tested
171                                         BuildItemGroup [] groups = new BuildItemGroup [itemGroups.Count];
172                                         itemGroups.CopyTo (groups, 0);
173                                         big = groups [0];
174                                 }
175                         }
176
177                         BuildItem item = big.AddNewItem (itemName, itemInclude, treatItemIncludeAsLiteral);
178                                 
179                         MarkProjectAsDirty ();
180                         NeedToReevaluate ();
181
182                         return item;
183                 }
184
185                 [MonoTODO ("Not tested")]
186                 public BuildItemGroup AddNewItemGroup ()
187                 {
188                         XmlElement element = xmlDocument.CreateElement ("ItemGroup", XmlNamespace);
189                         xmlDocument.DocumentElement.AppendChild (element);
190
191                         BuildItemGroup big = new BuildItemGroup (element, this, null, false);
192                         itemGroups.Add (big);
193                         MarkProjectAsDirty ();
194                         NeedToReevaluate ();
195
196                         return big;
197                 }
198
199                 [MonoTODO ("Ignores insertAtEndOfProject")]
200                 public BuildPropertyGroup AddNewPropertyGroup (bool insertAtEndOfProject)
201                 {
202                         XmlElement element = xmlDocument.CreateElement ("PropertyGroup", XmlNamespace);
203                         xmlDocument.DocumentElement.AppendChild (element);
204
205                         BuildPropertyGroup bpg = new BuildPropertyGroup (element, this, null, false);
206                         propertyGroups.Add (bpg);
207                         MarkProjectAsDirty ();
208                         NeedToReevaluate ();
209
210                         return bpg;
211                 }
212                 
213                 [MonoTODO ("Not tested, isn't added to TaskDatabase (no reevaluation)")]
214                 public void AddNewUsingTaskFromAssemblyFile (string taskName,
215                                                              string assemblyFile)
216                 {
217                         if (taskName == null)
218                                 throw new ArgumentNullException ("taskName");
219                         if (assemblyFile == null)
220                                 throw new ArgumentNullException ("assemblyFile");
221
222                         XmlElement element = xmlDocument.CreateElement ("UsingTask", XmlNamespace);
223                         xmlDocument.DocumentElement.AppendChild (element);
224                         element.SetAttribute ("TaskName", taskName);
225                         element.SetAttribute ("AssemblyFile", assemblyFile);
226
227                         UsingTask ut = new UsingTask (element, this, null);
228                         usingTasks.Add (ut);
229                         MarkProjectAsDirty ();
230                 }
231                 
232                 [MonoTODO ("Not tested, isn't added to TaskDatabase (no reevaluation)")]
233                 public void AddNewUsingTaskFromAssemblyName (string taskName,
234                                                              string assemblyName)
235                 {
236                         if (taskName == null)
237                                 throw new ArgumentNullException ("taskName");
238                         if (assemblyName == null)
239                                 throw new ArgumentNullException ("assemblyName");
240
241                         XmlElement element = xmlDocument.CreateElement ("UsingTask", XmlNamespace);
242                         xmlDocument.DocumentElement.AppendChild (element);
243                         element.SetAttribute ("TaskName", taskName);
244                         element.SetAttribute ("AssemblyName", assemblyName);
245
246                         UsingTask ut = new UsingTask (element, this, null);
247                         usingTasks.Add (ut);
248                         MarkProjectAsDirty ();
249                 }
250                 
251                 [MonoTODO ("Not tested")]
252                 public bool Build ()
253                 {
254                         return Build (new string [0]);
255                 }
256                 
257                 [MonoTODO ("Not tested")]
258                 public bool Build (string targetName)
259                 {
260                         if (targetName == null)
261                                 return Build ((string[]) null);
262                         else
263                                 return Build (new string [1] { targetName });
264                 }
265                 
266                 [MonoTODO ("Not tested")]
267                 public bool Build (string [] targetNames)
268                 {
269                         return Build (targetNames, null);
270                 }
271                 
272                 [MonoTODO ("Not tested")]
273                 public bool Build (string [] targetNames,
274                                    IDictionary targetOutputs)
275                 {
276                         return Build (targetNames, targetOutputs, BuildSettings.None);
277                 }
278
279                 [MonoTODO ("Not tested")]
280                 public bool Build (string [] targetNames,
281                                    IDictionary targetOutputs,
282                                    BuildSettings buildFlags)
283                 
284                 {
285                         bool result = false;
286                         ParentEngine.StartProjectBuild (this, targetNames);
287
288                         // Invoking this to emit a warning in case of unsupported
289                         // ToolsVersion
290                         GetToolsVersionToUse (true);
291
292                         string current_directory = Environment.CurrentDirectory;
293                         try {
294                                 current_settings = buildFlags;
295                                 if (!String.IsNullOrEmpty (fullFileName))
296                                         Directory.SetCurrentDirectory (Path.GetDirectoryName (fullFileName));
297                                 building = true;
298                                 result = BuildInternal (targetNames, targetOutputs, buildFlags);
299                         } catch (InvalidProjectFileException ie) {
300                                 ParentEngine.LogErrorWithFilename (fullFileName, ie.Message);
301                                 ParentEngine.LogMessage (MessageImportance.Low, String.Format ("{0}: {1}", fullFileName, ie.ToString ()));
302                         } catch (Exception e) {
303                                 ParentEngine.LogErrorWithFilename (fullFileName, e.Message);
304                                 ParentEngine.LogMessage (MessageImportance.Low, String.Format ("{0}: {1}", fullFileName, e.ToString ()));
305                                 throw;
306                         } finally {
307                                 ParentEngine.EndProjectBuild (this, result);
308                                 current_settings = BuildSettings.None;
309                                 Directory.SetCurrentDirectory (current_directory);
310                                 building = false;
311                         }
312
313                         return result;
314                 }
315
316                 bool BuildInternal (string [] targetNames,
317                                    IDictionary targetOutputs,
318                                    BuildSettings buildFlags)
319                 {
320                         CheckUnloaded ();
321                         if (buildFlags == BuildSettings.None) {
322                                 needToReevaluate = false;
323                                 Reevaluate ();
324                         }
325
326 #if NET_4_0
327                         ProcessBeforeAndAfterTargets ();
328 #endif
329
330                         if (targetNames == null || targetNames.Length == 0) {
331                                 if (defaultTargets != null && defaultTargets.Length != 0) {
332                                         targetNames = defaultTargets;
333                                 } else if (firstTargetName != null) {
334                                         targetNames = new string [1] { firstTargetName};
335                                 } else {
336                                         if (targets == null || targets.Count == 0) {
337                                                 LogError (fullFileName, "No target found in the project");
338                                                 return false;
339                                         }
340
341                                         return false;
342                                 }
343                         }
344
345                         if (!initialTargetsBuilt) {
346                                 foreach (string target in initialTargets) {
347                                         if (!BuildTarget (target.Trim (), targetOutputs))
348                                                 return false;
349                                 }
350                                 initialTargetsBuilt = true;
351                         }
352
353                         foreach (string target in targetNames) {
354                                 if (target == null)
355                                         throw new ArgumentNullException ("Target name cannot be null");
356
357                                 if (!BuildTarget (target.Trim (), targetOutputs))
358                                         return false;
359                         }
360                                 
361                         return true;
362                 }
363
364                 bool BuildTarget (string target_name, IDictionary targetOutputs)
365                 {
366                         if (target_name == null)
367                                 throw new ArgumentException ("targetNames cannot contain null strings");
368
369                         if (!targets.Exists (target_name)) {
370                                 LogError (fullFileName, "Target named '{0}' not found in the project.", target_name);
371                                 return false;
372                         }
373
374                         string key = GetKeyForTarget (target_name);
375                         if (!targets [target_name].Build (key))
376                                 return false;
377
378                         ITaskItem[] outputs;
379                         if (ParentEngine.BuiltTargetsOutputByName.TryGetValue (key, out outputs)) {
380                                 if (targetOutputs != null)
381                                         targetOutputs.Add (target_name, outputs);
382                         }
383                         return true;
384                 }
385
386                 internal string GetKeyForTarget (string target_name)
387                 {
388                         return GetKeyForTarget (target_name, true);
389                 }
390
391                 internal string GetKeyForTarget (string target_name, bool include_global_properties)
392                 {
393                         // target name is case insensitive
394                         return fullFileName + ":" + target_name.ToLowerInvariant () +
395                                         (include_global_properties ? (":" + GlobalPropertiesToString (GlobalProperties))
396                                                                    : String.Empty);
397                 }
398
399                 string GlobalPropertiesToString (BuildPropertyGroup bgp)
400                 {
401                         StringBuilder sb = new StringBuilder ();
402                         foreach (BuildProperty bp in bgp)
403                                 sb.AppendFormat (" {0}:{1}", bp.Name, bp.FinalValue);
404                         return sb.ToString ();
405                 }
406
407 #if NET_4_0
408                 void ProcessBeforeAndAfterTargets ()
409                 {
410                         var beforeTable = Targets.AsIEnumerable ()
411                                                 .SelectMany (target => GetTargetNamesFromString (target.BeforeTargets),
412                                                                 (target, before_target) => new {before_target, name = target.Name})
413                                                 .ToLookup (x => x.before_target, x => x.name)
414                                                 .ToDictionary (x => x.Key, x => x.Distinct ().ToList ());
415
416                         foreach (var pair in beforeTable) {
417                                 if (targets.Exists (pair.Key))
418                                         targets [pair.Key].BeforeThisTargets = pair.Value;
419                                 else
420                                         LogWarning (FullFileName, "Target '{0}', not found in the project", pair.Key);
421                         }
422
423                         var afterTable = Targets.AsIEnumerable ()
424                                                 .SelectMany (target => GetTargetNamesFromString (target.AfterTargets),
425                                                                 (target, after_target) => new {after_target, name = target.Name})
426                                                 .ToLookup (x => x.after_target, x => x.name)
427                                                 .ToDictionary (x => x.Key, x => x.Distinct ().ToList ());
428
429                         foreach (var pair in afterTable) {
430                                 if (targets.Exists (pair.Key))
431                                         targets [pair.Key].AfterThisTargets = pair.Value;
432                                 else
433                                         LogWarning (FullFileName, "Target '{0}', not found in the project", pair.Key);
434                         }
435                 }
436
437                 string[] GetTargetNamesFromString (string targets)
438                 {
439                         Expression expr = new Expression ();
440                         expr.Parse (targets, ParseOptions.AllowItemsNoMetadataAndSplit);
441                         return (string []) expr.ConvertTo (this, typeof (string []));
442                 }
443 #endif
444
445                 [MonoTODO]
446                 public string [] GetConditionedPropertyValues (string propertyName)
447                 {
448                         if (conditionedProperties.ContainsKey (propertyName))
449                                 return conditionedProperties [propertyName].ToArray ();
450                         else
451                                 return new string [0];
452                 }
453
454                 public BuildItemGroup GetEvaluatedItemsByName (string itemName)
455                 {                       
456                         if (needToReevaluate) {
457                                 needToReevaluate = false;
458                                 Reevaluate ();
459                         }
460
461                         if (evaluatedItemsByName.ContainsKey (itemName))
462                                 return evaluatedItemsByName [itemName];
463                         else
464                                 return new BuildItemGroup (this);
465                 }
466
467                 public BuildItemGroup GetEvaluatedItemsByNameIgnoringCondition (string itemName)
468                 {
469                         if (needToReevaluate) {
470                                 needToReevaluate = false;
471                                 Reevaluate ();
472                         }
473
474                         if (evaluatedItemsByNameIgnoringCondition.ContainsKey (itemName))
475                                 return evaluatedItemsByNameIgnoringCondition [itemName];
476                         else
477                                 return new BuildItemGroup (this);
478                 }
479
480                 public string GetEvaluatedProperty (string propertyName)
481                 {
482                         if (needToReevaluate) {
483                                 needToReevaluate = false;
484                                 Reevaluate ();
485                         }
486
487                         if (propertyName == null)
488                                 throw new ArgumentNullException ("propertyName");
489
490                         BuildProperty bp = evaluatedProperties [propertyName];
491
492                         return bp == null ? null : (string) bp;
493                 }
494
495                 [MonoTODO ("We should remember that node and not use XPath to get it")]
496                 public string GetProjectExtensions (string id)
497                 {
498                         if (id == null || id == String.Empty)
499                                 return String.Empty;
500
501                         XmlNode node = xmlDocument.SelectSingleNode (String.Format ("/tns:Project/tns:ProjectExtensions/tns:{0}", id), XmlNamespaceManager);
502
503                         if (node == null)
504                                 return String.Empty;
505                         else
506                                 return node.InnerXml;
507                 }
508
509
510                 public void Load (string projectFileName)
511                 {
512                         Load (projectFileName, ProjectLoadSettings.None);
513                 }
514
515                 public void Load (string projectFileName, ProjectLoadSettings settings)
516                 {
517                         project_load_settings = settings;
518                         if (String.IsNullOrEmpty (projectFileName))
519                                 throw new ArgumentNullException ("projectFileName");
520
521                         if (!File.Exists (projectFileName))
522                                 throw new ArgumentException (String.Format ("Project file {0} not found", projectFileName),
523                                                 "projectFileName");
524
525                         this.fullFileName = Utilities.FromMSBuildPath (Path.GetFullPath (projectFileName));
526                         PushThisFileProperty (fullFileName);
527
528                         string filename = fullFileName;
529                         if (String.Compare (Path.GetExtension (fullFileName), ".sln", true) == 0) {
530                                 Project tmp_project = ParentEngine.CreateNewProject ();
531                                 tmp_project.FullFileName = filename;
532                                 SolutionParser sln_parser = new SolutionParser ();
533                                 sln_parser.ParseSolution (fullFileName, tmp_project, delegate (int errorNumber, string message) {
534                                                 LogWarning (filename, message);
535                                         });
536                                 filename = fullFileName + ".proj";
537                                 try {
538                                         tmp_project.Save (filename);
539                                         ParentEngine.RemoveLoadedProject (tmp_project);
540                                         DoLoad (new StreamReader (filename));
541                                 } finally {
542                                         if (Environment.GetEnvironmentVariable ("XBUILD_EMIT_SOLUTION") == null)
543                                                 File.Delete (filename);
544                                 }
545                         } else {
546                                 DoLoad (new StreamReader (filename));
547                         }
548                 }
549                 
550                 [MonoTODO ("Not tested")]
551                 public void Load (TextReader textReader)
552                 {
553                         Load (textReader, ProjectLoadSettings.None);
554                 }
555
556                 public void Load (TextReader textReader, ProjectLoadSettings projectLoadSettings)
557                 {
558                         project_load_settings = projectLoadSettings;
559                         if (!string.IsNullOrEmpty (fullFileName))
560                                 PushThisFileProperty (fullFileName);
561                         DoLoad (textReader);
562                 }
563
564                 public void LoadXml (string projectXml)
565                 {
566                         LoadXml (projectXml, ProjectLoadSettings.None);
567                 }
568
569                 public void LoadXml (string projectXml, ProjectLoadSettings projectLoadSettings)
570                 {
571                         project_load_settings = projectLoadSettings;
572                         if (!string.IsNullOrEmpty (fullFileName))
573                                 PushThisFileProperty (fullFileName);
574                         DoLoad (new StringReader (projectXml));
575                         MarkProjectAsDirty ();
576                 }
577
578
579                 public void MarkProjectAsDirty ()
580                 {
581                         isDirty = true;
582                         timeOfLastDirty = DateTime.Now;
583                 }
584
585                 [MonoTODO ("Not tested")]
586                 public void RemoveAllItemGroups ()
587                 {
588                         int length = ItemGroups.Count;
589                         BuildItemGroup [] groups = new BuildItemGroup [length];
590                         ItemGroups.CopyTo (groups, 0);
591
592                         for (int i = 0; i < length; i++)
593                                 RemoveItemGroup (groups [i]);
594
595                         MarkProjectAsDirty ();
596                         NeedToReevaluate ();
597                 }
598
599                 [MonoTODO ("Not tested")]
600                 public void RemoveAllPropertyGroups ()
601                 {
602                         int length = PropertyGroups.Count;
603                         BuildPropertyGroup [] groups = new BuildPropertyGroup [length];
604                         PropertyGroups.CopyTo (groups, 0);
605
606                         for (int i = 0; i < length; i++)
607                                 RemovePropertyGroup (groups [i]);
608
609                         MarkProjectAsDirty ();
610                         NeedToReevaluate ();
611                 }
612
613                 [MonoTODO]
614                 public void RemoveItem (BuildItem itemToRemove)
615                 {
616                         if (itemToRemove == null)
617                                 throw new ArgumentNullException ("itemToRemove");
618
619                         if (!itemToRemove.FromXml && !itemToRemove.HasParentItem)
620                                 throw new InvalidOperationException ("The object passed in is not part of the project.");
621
622                         BuildItemGroup big = itemToRemove.ParentItemGroup;
623
624                         if (big.Count == 1) {
625                                 // ParentItemGroup for items from xml and that have parent is the same
626                                 groupingCollection.Remove (big);
627                         } else {
628                                 if (big.ParentProject != this)
629                                         throw new InvalidOperationException ("The object passed in is not part of the project.");
630
631                                 if (itemToRemove.FromXml)
632                                         big.RemoveItem (itemToRemove);
633                                 else
634                                         big.RemoveItem (itemToRemove.ParentItem);
635                         }
636
637                         MarkProjectAsDirty ();
638                         NeedToReevaluate ();
639                 }
640
641                 [MonoTODO ("Not tested")]
642                 public void RemoveItemGroup (BuildItemGroup itemGroupToRemove)
643                 {
644                         if (itemGroupToRemove == null)
645                                 throw new ArgumentNullException ("itemGroupToRemove");
646
647                         groupingCollection.Remove (itemGroupToRemove);
648                         MarkProjectAsDirty ();
649                 }
650                 
651                 [MonoTODO]
652                 // NOTE: does not modify imported projects
653                 public void RemoveItemGroupsWithMatchingCondition (string matchingCondition)
654                 {
655                         throw new NotImplementedException ();
656                 }
657
658                 [MonoTODO]
659                 public void RemoveItemsByName (string itemName)
660                 {
661                         if (itemName == null)
662                                 throw new ArgumentNullException ("itemName");
663
664                         throw new NotImplementedException ();
665                 }
666
667                 [MonoTODO ("Not tested")]
668                 public void RemovePropertyGroup (BuildPropertyGroup propertyGroupToRemove)
669                 {
670                         if (propertyGroupToRemove == null)
671                                 throw new ArgumentNullException ("propertyGroupToRemove");
672
673                         groupingCollection.Remove (propertyGroupToRemove);
674                         MarkProjectAsDirty ();
675                 }
676                 
677                 [MonoTODO]
678                 // NOTE: does not modify imported projects
679                 public void RemovePropertyGroupsWithMatchingCondition (string matchCondition)
680                 {
681                         throw new NotImplementedException ();
682                 }
683
684                 [MonoTODO]
685                 public void ResetBuildStatus ()
686                 {
687                         // hack to allow built targets to be removed
688                         building = true;
689                         Reevaluate ();
690                         building = false;
691                 }
692
693                 public void Save (string projectFileName)
694                 {
695                         Save (projectFileName, Encoding.Default);
696                         isDirty = false;
697                 }
698
699                 [MonoTODO ("Ignores encoding")]
700                 public void Save (string projectFileName, Encoding encoding)
701                 {
702                         xmlDocument.Save (projectFileName);
703                         isDirty = false;
704                 }
705
706                 public void Save (TextWriter outTextWriter)
707                 {
708                         xmlDocument.Save (outTextWriter);
709                         isDirty = false;
710                 }
711
712                 public void SetImportedProperty (string propertyName,
713                                                  string propertyValue,
714                                                  string condition,
715                                                  Project importProject)
716                 {
717                         SetImportedProperty (propertyName, propertyValue, condition, importProject,
718                                 PropertyPosition.UseExistingOrCreateAfterLastPropertyGroup);
719                 }
720
721                 public void SetImportedProperty (string propertyName,
722                                                  string propertyValue,
723                                                  string condition,
724                                                  Project importedProject,
725                                                  PropertyPosition position)
726                 {
727                         SetImportedProperty (propertyName, propertyValue, condition, importedProject,
728                                 PropertyPosition.UseExistingOrCreateAfterLastPropertyGroup, false);
729                 }
730
731                 [MonoTODO]
732                 public void SetImportedProperty (string propertyName,
733                                                  string propertyValue,
734                                                  string condition,
735                                                  Project importedProject,
736                                                  PropertyPosition position,
737                                                  bool treatPropertyValueAsLiteral)
738                 {
739                         throw new NotImplementedException ();
740                 }
741
742                 public void SetProjectExtensions (string id, string xmlText)
743                 {
744                         if (id == null)
745                                 throw new ArgumentNullException ("id");
746                         if (xmlText == null)
747                                 throw new ArgumentNullException ("xmlText");
748
749                         XmlNode projectExtensions, node;
750
751                         projectExtensions = xmlDocument.SelectSingleNode ("/tns:Project/tns:ProjectExtensions", XmlNamespaceManager);
752                         
753                         if (projectExtensions == null) {
754                                 projectExtensions = xmlDocument.CreateElement ("ProjectExtensions", XmlNamespace);
755                                 xmlDocument.DocumentElement.AppendChild (projectExtensions);
756
757                                 node = xmlDocument.CreateElement (id, XmlNamespace);
758                                 node.InnerXml = xmlText;
759                                 projectExtensions.AppendChild (node);
760                         } else {
761                                 node = xmlDocument.SelectSingleNode (String.Format ("/tns:Project/tns:ProjectExtensions/tns:{0}", id), XmlNamespaceManager);
762
763                                 if (node == null) {
764                                         node = xmlDocument.CreateElement (id, XmlNamespace);
765                                         projectExtensions.AppendChild (node);
766                                 }
767                                 
768                                 node.InnerXml = xmlText;
769                                 
770                         }
771
772                         MarkProjectAsDirty ();
773                 }
774                 
775                 public void SetProperty (string propertyName,
776                                          string propertyValue)
777                 {
778                         SetProperty (propertyName, propertyValue, "true",
779                                 PropertyPosition.UseExistingOrCreateAfterLastPropertyGroup, false);
780                 }
781
782                 public void SetProperty (string propertyName,
783                                          string propertyValue,
784                                          string condition)
785                 {
786                         SetProperty (propertyName, propertyValue, condition,
787                                 PropertyPosition.UseExistingOrCreateAfterLastPropertyGroup);
788                 }
789
790                 public void SetProperty (string propertyName,
791                                          string propertyValue,
792                                          string condition,
793                                          PropertyPosition position)
794                 {
795                         SetProperty (propertyName, propertyValue, condition,
796                                 PropertyPosition.UseExistingOrCreateAfterLastPropertyGroup, false);
797                 }
798
799                 [MonoTODO]
800                 public void SetProperty (string propertyName,
801                                          string propertyValue,
802                                          string condition,
803                                          PropertyPosition position,
804                                          bool treatPropertyValueAsLiteral)
805                 {
806                         throw new NotImplementedException ();
807                 }
808
809                 internal void Unload ()
810                 {
811                         unloaded = true;
812                 }
813
814                 internal void CheckUnloaded ()
815                 {
816                         if (unloaded)
817                                 throw new InvalidOperationException ("This project object has been unloaded from the MSBuild engine and is no longer valid.");
818                 }
819
820                 internal void NeedToReevaluate ()
821                 {
822                         needToReevaluate = true;
823                 }
824                                 
825                 // Does the actual loading.
826                 void DoLoad (TextReader textReader)
827                 {
828                         try {
829                                 ParentEngine.RemoveLoadedProject (this);
830         
831                                 xmlDocument.Load (textReader);
832
833                                 if (xmlDocument.DocumentElement.Name == "VisualStudioProject")
834                                         throw new InvalidProjectFileException (String.Format (
835                                                         "Project file '{0}' is a VS2003 project, which is not " +
836                                                         "supported by xbuild. You need to convert it to msbuild " +
837                                                         "format to build with xbuild.", fullFileName));
838
839                                 if (SchemaFile != null) {
840                                         xmlDocument.Schemas.Add (XmlSchema.Read (
841                                                                 new StreamReader (SchemaFile), ValidationCallBack));
842                                         xmlDocument.Validate (ValidationCallBack);
843                                 }
844
845                                 if (xmlDocument.DocumentElement.Name != "Project") {
846                                         throw new InvalidProjectFileException (String.Format (
847                                                 "The element <{0}> is unrecognized, or not supported in this context.", xmlDocument.DocumentElement.Name));
848                                 }
849         
850                                 if (xmlDocument.DocumentElement.GetAttribute ("xmlns") != ns) {
851                                         throw new InvalidProjectFileException (
852                                                 @"The default XML namespace of the project must be the MSBuild XML namespace." + 
853                                                 " If the project is authored in the MSBuild 2003 format, please add " +
854                                                 "xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\" to the <Project> element. " +
855                                                 "If the project has been authored in the old 1.0 or 1.2 format, please convert it to MSBuild 2003 format.  ");
856                                 }
857                                 ProcessXml ();
858                                 ParentEngine.AddLoadedProject (this);
859                         } catch (Exception e) {
860                                 throw new InvalidProjectFileException (String.Format ("{0}: {1}",
861                                                         fullFileName, e.Message), e);
862                         } finally {
863                                 if (textReader != null)
864                                         textReader.Close ();
865                         }
866                 }
867
868                 void Reevaluate ()
869                 {
870                         ProcessXml ();
871                 }
872
873                 void ProcessXml ()
874                 {
875                         groupingCollection = new GroupingCollection (this);
876                         imports = new ImportCollection (groupingCollection);
877                         usingTasks = new UsingTaskCollection (this);
878                         itemGroups = new BuildItemGroupCollection (groupingCollection);
879                         propertyGroups = new BuildPropertyGroupCollection (groupingCollection);
880                         targets = new TargetCollection (this);
881                         last_item_group_containing = new Dictionary <string, BuildItemGroup> ();
882                         
883                         string effective_tools_version = GetToolsVersionToUse (false);
884                         taskDatabase = new TaskDatabase ();
885                         taskDatabase.CopyTasks (ParentEngine.GetDefaultTasks (effective_tools_version));
886
887                         initialTargets = new List<string> ();
888                         defaultTargets = new string [0];
889                         PrepareForEvaluate (effective_tools_version);
890                         ProcessElements (xmlDocument.DocumentElement, null);
891                         
892                         isDirty = false;
893                         Evaluate ();
894                 }
895
896                 void ProcessProjectAttributes (XmlAttributeCollection attributes)
897                 {
898                         foreach (XmlAttribute attr in attributes) {
899                                 switch (attr.Name) {
900                                 case "InitialTargets":
901                                         initialTargets.AddRange (attr.Value.Split (
902                                                                         new char [] {';', ' '},
903                                                                         StringSplitOptions.RemoveEmptyEntries));
904                                         break;
905                                 case "DefaultTargets":
906                                         // first non-empty DefaultTargets found is used
907                                         if (defaultTargets == null || defaultTargets.Length == 0)
908                                                 defaultTargets = attr.Value.Split (new char [] {';', ' '},
909                                                         StringSplitOptions.RemoveEmptyEntries);
910                                         EvaluatedProperties.AddProperty (new BuildProperty ("MSBuildProjectDefaultTargets",
911                                                                 DefaultTargets, PropertyType.Reserved));
912                                         break;
913                                 }
914                         }
915                 }
916
917                 internal void ProcessElements (XmlElement rootElement, ImportedProject ip)
918                 {
919                         ProcessProjectAttributes (rootElement.Attributes);
920                         foreach (XmlNode xn in rootElement.ChildNodes) {
921                                 if (xn is XmlElement) {
922                                         XmlElement xe = (XmlElement) xn;
923                                         switch (xe.Name) {
924                                         case "ProjectExtensions":
925                                                 AddProjectExtensions (xe);
926                                                 break;
927                                         case "Warning":
928                                         case "Message":
929                                         case "Error":
930                                                 AddMessage (xe);
931                                                 break;
932                                         case "Target":
933                                                 AddTarget (xe, ip);
934                                                 break;
935                                         case "UsingTask":
936                                                 AddUsingTask (xe, ip);
937                                                 break;
938                                         case "Import":
939                                                 AddImport (xe, ip, true);
940                                                 break;
941                                         case "ImportGroup":
942                                                 AddImportGroup (xe, ip, true);
943                                                 break;
944                                         case "ItemGroup":
945                                                 AddItemGroup (xe, ip);
946                                                 break;
947                                         case "PropertyGroup":
948                                                 AddPropertyGroup (xe, ip);
949                                                 break;
950                                         case  "Choose":
951                                                 AddChoose (xe, ip);
952                                                 break;
953                                         default:
954                                                 throw new InvalidProjectFileException (String.Format ("Invalid element '{0}' in project file '{1}'.", xe.Name, ip.FullFileName));
955                                         }
956                                 }
957                         }
958                 }
959                 
960                 void PrepareForEvaluate (string effective_tools_version)
961                 {
962                         evaluatedItems = new BuildItemGroup (null, this, null, true);
963                         evaluatedItemsIgnoringCondition = new BuildItemGroup (null, this, null, true);
964                         evaluatedItemsByName = new Dictionary <string, BuildItemGroup> (StringComparer.OrdinalIgnoreCase);
965                         evaluatedItemsByNameIgnoringCondition = new Dictionary <string, BuildItemGroup> (StringComparer.OrdinalIgnoreCase);
966                         if (building && current_settings == BuildSettings.None)
967                                 RemoveBuiltTargets ();
968
969                         InitializeProperties (effective_tools_version);
970                 }
971
972                 void Evaluate ()
973                 {
974                         groupingCollection.Evaluate ();
975
976                         //FIXME: UsingTasks aren't really evaluated. (shouldn't use expressions or anything)
977                         foreach (UsingTask usingTask in UsingTasks)
978                                 usingTask.Evaluate ();
979                 }
980
981                 // Removes entries of all earlier built targets for this project
982                 void RemoveBuiltTargets ()
983                 {
984                         ParentEngine.ClearBuiltTargetsForProject (this);
985                 }
986
987                 void InitializeProperties (string effective_tools_version)
988                 {
989                         BuildProperty bp;
990
991                         evaluatedProperties = new BuildPropertyGroup (null, null, null, true);
992                         conditionedProperties = new Dictionary<string, List<string>> ();
993
994                         foreach (BuildProperty gp in GlobalProperties) {
995                                 bp = new BuildProperty (gp.Name, gp.Value, PropertyType.Global);
996                                 evaluatedProperties.AddProperty (bp);
997                         }
998                         
999                         foreach (BuildProperty gp in GlobalProperties)
1000                                 ParentEngine.GlobalProperties.AddProperty (gp);
1001
1002                         // add properties that we dont have from parent engine's
1003                         // global properties
1004                         foreach (BuildProperty gp in ParentEngine.GlobalProperties) {
1005                                 if (evaluatedProperties [gp.Name] == null) {
1006                                         bp = new BuildProperty (gp.Name, gp.Value, PropertyType.Global);
1007                                         evaluatedProperties.AddProperty (bp);
1008                                 }
1009                         }
1010
1011                         foreach (DictionaryEntry de in Environment.GetEnvironmentVariables ()) {
1012                                 bp = new BuildProperty ((string) de.Key, (string) de.Value, PropertyType.Environment);
1013                                 evaluatedProperties.AddProperty (bp);
1014                         }
1015
1016                         evaluatedProperties.AddProperty (new BuildProperty ("MSBuildProjectFile", Path.GetFileName (fullFileName),
1017                                                 PropertyType.Reserved));
1018                         evaluatedProperties.AddProperty (new BuildProperty ("MSBuildProjectFullPath", fullFileName, PropertyType.Reserved));
1019                         evaluatedProperties.AddProperty (new BuildProperty ("MSBuildProjectName",
1020                                                 Path.GetFileNameWithoutExtension (fullFileName),
1021                                                 PropertyType.Reserved));
1022                         evaluatedProperties.AddProperty (new BuildProperty ("MSBuildProjectExtension",
1023                                                 Path.GetExtension (fullFileName),
1024                                                 PropertyType.Reserved));
1025
1026                         string toolsPath = parentEngine.Toolsets [effective_tools_version].ToolsPath;
1027                         if (toolsPath == null)
1028                                 throw new Exception (String.Format ("Invalid tools version '{0}', no tools path set for this.", effective_tools_version));
1029                         evaluatedProperties.AddProperty (new BuildProperty ("MSBuildBinPath", toolsPath, PropertyType.Reserved));
1030                         evaluatedProperties.AddProperty (new BuildProperty ("MSBuildToolsPath", toolsPath, PropertyType.Reserved));
1031                         evaluatedProperties.AddProperty (new BuildProperty ("MSBuildToolsRoot", Path.GetDirectoryName (toolsPath), PropertyType.Reserved));
1032                         evaluatedProperties.AddProperty (new BuildProperty ("MSBuildToolsVersion", effective_tools_version, PropertyType.Reserved));
1033                         SetExtensionsPathProperties (DefaultExtensionsPath);
1034                         evaluatedProperties.AddProperty (new BuildProperty ("MSBuildProjectDefaultTargets", DefaultTargets, PropertyType.Reserved));
1035                         evaluatedProperties.AddProperty (new BuildProperty ("OS", OS, PropertyType.Environment));
1036 #if XBUILD_12
1037                         // see http://msdn.microsoft.com/en-us/library/vstudio/hh162058(v=vs.120).aspx
1038                         if (effective_tools_version == "12.0") {
1039                                 evaluatedProperties.AddProperty (new BuildProperty ("MSBuildToolsPath32", toolsPath, PropertyType.Reserved));
1040
1041                                 var frameworkToolsPath = ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version451);
1042
1043                                 evaluatedProperties.AddProperty (new BuildProperty ("MSBuildFrameworkToolsPath", frameworkToolsPath, PropertyType.Reserved));
1044                                 evaluatedProperties.AddProperty (new BuildProperty ("MSBuildFrameworkToolsPath32", frameworkToolsPath, PropertyType.Reserved));
1045                         }
1046 #endif
1047                         // FIXME: make some internal method that will work like GetDirectoryName but output String.Empty on null/String.Empty
1048                         string projectDir;
1049                         if (FullFileName == String.Empty)
1050                                 projectDir = Environment.CurrentDirectory;
1051                         else
1052                                 projectDir = Path.GetDirectoryName (FullFileName);
1053
1054                         evaluatedProperties.AddProperty (new BuildProperty ("MSBuildProjectDirectory", projectDir, PropertyType.Reserved));
1055
1056                         if (this_file_property_stack.Count > 0)
1057                                 // Just re-inited the properties, but according to the stack,
1058                                 // we should have a MSBuild*This* property set
1059                                 SetMSBuildThisFileProperties (this_file_property_stack.Peek ());
1060                 }
1061
1062                 internal void SetExtensionsPathProperties (string extn_path)
1063                 {
1064                         if (!String.IsNullOrEmpty (extn_path)) {
1065                                 evaluatedProperties.AddProperty (new BuildProperty ("MSBuildExtensionsPath", extn_path, PropertyType.Reserved));
1066                                 evaluatedProperties.AddProperty (new BuildProperty ("MSBuildExtensionsPath32", extn_path, PropertyType.Reserved));
1067                                 evaluatedProperties.AddProperty (new BuildProperty ("MSBuildExtensionsPath64", extn_path, PropertyType.Reserved));
1068                         }
1069                 }
1070
1071                 // precedence:
1072                 // ToolsVersion property
1073                 // ToolsVersion attribute on the project
1074                 // parentEngine's DefaultToolsVersion
1075                 string GetToolsVersionToUse (bool emitWarning)
1076                 {
1077                         if (!String.IsNullOrEmpty (ToolsVersion))
1078                                 return ToolsVersion;
1079
1080                         if (!HasToolsVersionAttribute)
1081                                 return parentEngine.DefaultToolsVersion;
1082
1083                         if (parentEngine.Toolsets [DefaultToolsVersion] == null) {
1084                                 if (emitWarning)
1085                                         LogWarning (FullFileName, "Project has unknown ToolsVersion '{0}'. Using the default tools version '{1}' instead.",
1086                                                 DefaultToolsVersion, parentEngine.DefaultToolsVersion);
1087                                 return parentEngine.DefaultToolsVersion;
1088                         }
1089
1090                         return DefaultToolsVersion;
1091                 }
1092                 
1093                 void AddProjectExtensions (XmlElement xmlElement)
1094                 {
1095                 }
1096                 
1097                 void AddMessage (XmlElement xmlElement)
1098                 {
1099                 }
1100                 
1101                 void AddTarget (XmlElement xmlElement, ImportedProject importedProject)
1102                 {
1103                         Target target = new Target (xmlElement, this, importedProject);
1104                         targets.AddTarget (target);
1105                         
1106                         if (firstTargetName == null)
1107                                 firstTargetName = target.Name;
1108                 }
1109                 
1110                 void AddUsingTask (XmlElement xmlElement, ImportedProject importedProject)
1111                 {
1112                         UsingTask usingTask;
1113
1114                         usingTask = new UsingTask (xmlElement, this, importedProject);
1115                         UsingTasks.Add (usingTask);
1116                 }
1117
1118                 void AddImport (XmlElement xmlElement, ImportedProject importingProject, bool evaluate_properties)
1119                 {
1120                         // eval all the properties etc till the import
1121                         if (evaluate_properties) {
1122                                 groupingCollection.Evaluate (EvaluationType.Property);
1123                                 groupingCollection.Evaluate (EvaluationType.Choose);
1124                         }
1125                         try {
1126                                 PushThisFileProperty (importingProject != null ? importingProject.FullFileName : FullFileName);
1127
1128                                 string project_attribute = xmlElement.GetAttribute ("Project");
1129                                 if (String.IsNullOrEmpty (project_attribute))
1130                                         throw new InvalidProjectFileException ("The required attribute \"Project\" is missing from element <Import>.");
1131
1132                                 Import.ForEachExtensionPathTillFound (xmlElement, this, importingProject,
1133                                         (importPath, from_source_msg) => AddSingleImport (xmlElement, importPath, importingProject, from_source_msg));
1134                         } finally {
1135                                 PopThisFileProperty ();
1136                         }
1137                 }
1138
1139                 void AddImportGroup (XmlElement xmlElement, ImportedProject importedProject, bool evaluate_properties)
1140                 {
1141                         // eval all the properties etc till the import group
1142                         if (evaluate_properties) {
1143                                 groupingCollection.Evaluate (EvaluationType.Property);
1144                                 groupingCollection.Evaluate (EvaluationType.Choose);
1145                         }
1146                         string condition_attribute = xmlElement.GetAttribute ("Condition");
1147                         if (!ConditionParser.ParseAndEvaluate (condition_attribute, this))
1148                                 return;
1149                         foreach (XmlNode xn in xmlElement.ChildNodes) {
1150                                 if (xn is XmlElement) {
1151                                         XmlElement xe = (XmlElement) xn;
1152                                         switch (xe.Name) {
1153                                         case "Import":
1154                                                 AddImport (xe, importedProject, evaluate_properties);
1155                                                 break;
1156                                         default:
1157                                                 throw new InvalidProjectFileException(String.Format("Invalid element '{0}' inside ImportGroup in project file '{1}'.", xe.Name, importedProject.FullFileName));
1158                                         }
1159                                 }
1160                         }
1161                 }
1162
1163                 bool AddSingleImport (XmlElement xmlElement, string projectPath, ImportedProject importingProject, string from_source_msg)
1164                 {
1165                         Import import = new Import (xmlElement, projectPath, this, importingProject);
1166                         if (!ConditionParser.ParseAndEvaluate (import.Condition, this)) {
1167                                 ParentEngine.LogMessage (MessageImportance.Low,
1168                                                 "Not importing project '{0}' as the condition '{1}' is false",
1169                                                 import.ProjectPath, import.Condition);
1170                                 return false;
1171                         }
1172
1173                         Import existingImport;
1174                         if (Imports.TryGetImport (import, out existingImport)) {
1175                                 if (importingProject == null)
1176                                         LogWarning (fullFileName,
1177                                                         "Cannot import project '{0}' again. It was already imported by " +
1178                                                         "'{1}'. Ignoring.",
1179                                                         projectPath, existingImport.ContainedInProjectFileName);
1180                                 else
1181                                         LogWarning (importingProject != null ? importingProject.FullFileName : fullFileName,
1182                                                 "A circular reference was found involving the import of '{0}'. " +
1183                                                 "It was earlier imported by '{1}'. Only " +
1184                                                 "the first import of this file will be used, ignoring others.",
1185                                                 import.EvaluatedProjectPath, existingImport.ContainedInProjectFileName);
1186
1187                                 return true;
1188                         }
1189
1190                         if (String.Compare (fullFileName, import.EvaluatedProjectPath) == 0) {
1191                                 LogWarning (importingProject != null ? importingProject.FullFileName : fullFileName,
1192                                                 "The main project file was imported here, which creates a circular " +
1193                                                 "reference. Ignoring this import.");
1194
1195                                 return true;
1196                         }
1197
1198                         if (project_load_settings != ProjectLoadSettings.IgnoreMissingImports &&
1199                             !import.CheckEvaluatedProjectPathExists ())
1200                                 return false;
1201
1202                         Imports.Add (import);
1203                         string importingFile = importingProject != null ? importingProject.FullFileName : FullFileName;
1204                         ParentEngine.LogMessage (MessageImportance.Low,
1205                                         "{0}: Importing project {1} {2}",
1206                                         importingFile, import.EvaluatedProjectPath, from_source_msg);
1207
1208                         import.Evaluate (project_load_settings == ProjectLoadSettings.IgnoreMissingImports);
1209                         return true;
1210                 }
1211
1212                 void AddItemGroup (XmlElement xmlElement, ImportedProject importedProject)
1213                 {
1214                         BuildItemGroup big = new BuildItemGroup (xmlElement, this, importedProject, false);
1215                         ItemGroups.Add (big);
1216                 }
1217                 
1218                 void AddPropertyGroup (XmlElement xmlElement, ImportedProject importedProject)
1219                 {
1220                         BuildPropertyGroup bpg = new BuildPropertyGroup (xmlElement, this, importedProject, false);
1221                         PropertyGroups.Add (bpg);
1222                 }
1223                 
1224                 void AddChoose (XmlElement xmlElement, ImportedProject importedProject)
1225                 {
1226                         BuildChoose bc = new BuildChoose (xmlElement, this, importedProject);
1227                         groupingCollection.Add (bc);
1228                 }
1229                 
1230                 static void ValidationCallBack (object sender, ValidationEventArgs e)
1231                 {
1232                         Console.WriteLine ("Validation Error: {0}", e.Message);
1233                 }
1234                 
1235                 public bool BuildEnabled {
1236                         get {
1237                                 return buildEnabled;
1238                         }
1239                         set {
1240                                 buildEnabled = value;
1241                         }
1242                 }
1243
1244                 [MonoTODO]
1245                 public Encoding Encoding {
1246                         get { return encoding; }
1247                 }
1248
1249                 public string DefaultTargets {
1250                         get {
1251                                 return String.Join ("; ", defaultTargets);
1252                         }
1253                         set {
1254                                 xmlDocument.DocumentElement.SetAttribute ("DefaultTargets", value);
1255                                 if (value != null)
1256                                         defaultTargets = value.Split (new char [] {';', ' '},
1257                                                         StringSplitOptions.RemoveEmptyEntries);
1258                         }
1259                 }
1260
1261                 public BuildItemGroup EvaluatedItems {
1262                         get {
1263                                 if (needToReevaluate) {
1264                                         needToReevaluate = false;
1265                                         Reevaluate ();
1266                                 }
1267                                 return evaluatedItems;
1268                         }
1269                 }
1270
1271                 public BuildItemGroup EvaluatedItemsIgnoringCondition {
1272                         get {
1273                                 if (needToReevaluate) {
1274                                         needToReevaluate = false;
1275                                         Reevaluate ();
1276                                 }
1277                                 return evaluatedItemsIgnoringCondition;
1278                         }
1279                 }
1280                 
1281                 internal IDictionary <string, BuildItemGroup> EvaluatedItemsByName {
1282                         get {
1283                                 // FIXME: do we need to do this here?
1284                                 if (needToReevaluate) {
1285                                         needToReevaluate = false;
1286                                         Reevaluate ();
1287                                 }
1288                                 return evaluatedItemsByName;
1289                         }
1290                 }
1291
1292                 internal IEnumerable EvaluatedItemsByNameAsDictionaryEntries {
1293                         get {
1294                                 if (EvaluatedItemsByName.Count == 0)
1295                                         yield break;
1296
1297                                 foreach (KeyValuePair<string, BuildItemGroup> pair in EvaluatedItemsByName) {
1298                                         foreach (BuildItem bi in pair.Value)
1299                                                 yield return new DictionaryEntry (pair.Key, bi.ConvertToITaskItem (null, ExpressionOptions.ExpandItemRefs));
1300                                 }
1301                         }
1302                 }
1303
1304                 internal IDictionary <string, BuildItemGroup> EvaluatedItemsByNameIgnoringCondition {
1305                         get {
1306                                 // FIXME: do we need to do this here?
1307                                 if (needToReevaluate) {
1308                                         needToReevaluate = false;
1309                                         Reevaluate ();
1310                                 }
1311                                 return evaluatedItemsByNameIgnoringCondition;
1312                         }
1313                 }
1314
1315                 // For batching implementation
1316                 Dictionary<string, BuildItemGroup> perBatchItemsByName;
1317                 Dictionary<string, BuildItemGroup> commonItemsByName;
1318
1319                 struct Batch {
1320                         public Dictionary<string, BuildItemGroup> perBatchItemsByName;
1321                         public Dictionary<string, BuildItemGroup> commonItemsByName;
1322
1323                         public Batch (Dictionary<string, BuildItemGroup> perBatchItemsByName, Dictionary<string, BuildItemGroup> commonItemsByName)
1324                         {
1325                                 this.perBatchItemsByName = perBatchItemsByName;
1326                                 this.commonItemsByName = commonItemsByName;
1327                         }
1328                 }
1329
1330                 Stack<Batch> Batches {
1331                         get { return batches; }
1332                 }
1333
1334                 internal void PushBatch (Dictionary<string, BuildItemGroup> perBatchItemsByName, Dictionary<string, BuildItemGroup> commonItemsByName)
1335                 {
1336                         batches.Push (new Batch (perBatchItemsByName, commonItemsByName));
1337                         SetBatchedItems (perBatchItemsByName, commonItemsByName);
1338                 }
1339
1340                 internal void PopBatch ()
1341                 {
1342                         batches.Pop ();
1343                         if (batches.Count > 0) {
1344                                 Batch b = batches.Peek ();
1345                                 SetBatchedItems (b.perBatchItemsByName, b.commonItemsByName);
1346                         } else {
1347                                 SetBatchedItems (null, null);
1348                         }
1349                 }
1350
1351                 void SetBatchedItems (Dictionary<string, BuildItemGroup> perBatchItemsByName, Dictionary<string, BuildItemGroup> commonItemsByName)
1352                 {
1353                         this.perBatchItemsByName = perBatchItemsByName;
1354                         this.commonItemsByName = commonItemsByName;
1355                 }
1356
1357                 // Honors batching
1358                 internal bool TryGetEvaluatedItemByNameBatched (string itemName, out BuildItemGroup group)
1359                 {
1360                         if (perBatchItemsByName != null && perBatchItemsByName.TryGetValue (itemName, out group))
1361                                 return true;
1362
1363                         if (commonItemsByName != null && commonItemsByName.TryGetValue (itemName, out group))
1364                                 return true;
1365
1366                         group = null;
1367                         return EvaluatedItemsByName.TryGetValue (itemName, out group);
1368                 }
1369
1370                 internal string GetMetadataBatched (string itemName, string metadataName)
1371                 {
1372                         BuildItemGroup group = null;
1373                         if (itemName == null) {
1374                                 //unqualified, all items in a batch(bucket) have the
1375                                 //same metadata values
1376                                 group = GetFirst<BuildItemGroup> (perBatchItemsByName.Values);
1377                                 if (group == null)
1378                                         group = GetFirst<BuildItemGroup> (commonItemsByName.Values);
1379                         } else {
1380                                 //qualified
1381                                 TryGetEvaluatedItemByNameBatched (itemName, out group);
1382                         }
1383
1384                         if (group != null) {
1385                                 foreach (BuildItem item in group) {
1386                                         if (item.HasMetadata (metadataName))
1387                                                 return item.GetEvaluatedMetadata (metadataName);
1388                                 }
1389                         }
1390                         return String.Empty;
1391                 }
1392
1393                 internal IEnumerable<BuildItemGroup> GetAllItemGroups ()
1394                 {
1395                         if (perBatchItemsByName == null && commonItemsByName == null)
1396                                 foreach (BuildItemGroup group in EvaluatedItemsByName.Values)
1397                                         yield return group;
1398
1399                         if (perBatchItemsByName != null)
1400                                 foreach (BuildItemGroup group in perBatchItemsByName.Values)
1401                                         yield return group;
1402
1403                         if (commonItemsByName != null)
1404                                 foreach (BuildItemGroup group in commonItemsByName.Values)
1405                                         yield return group;
1406                 }
1407
1408                 T GetFirst<T> (ICollection<T> list)
1409                 {
1410                         if (list == null)
1411                                 return default (T);
1412
1413                         foreach (T t in list)
1414                                 return t;
1415
1416                         return default (T);
1417                 }
1418
1419                 // Used for MSBuild*This* set of properties
1420                 internal void PushThisFileProperty (string full_filename)
1421                 {
1422                         string last_file = this_file_property_stack.Count == 0 ? String.Empty : this_file_property_stack.Peek ();
1423                         this_file_property_stack.Push (full_filename);
1424                         if (last_file != full_filename)
1425                                 // first time, or different from previous one
1426                                 SetMSBuildThisFileProperties (full_filename);
1427                 }
1428
1429                 internal void PopThisFileProperty ()
1430                 {
1431                         string last_file = this_file_property_stack.Pop ();
1432                         if (this_file_property_stack.Count > 0 && last_file != this_file_property_stack.Peek ())
1433                                 SetMSBuildThisFileProperties (this_file_property_stack.Peek ());
1434                 }
1435
1436                 void SetMSBuildThisFileProperties (string full_filename)
1437                 {
1438                         if (String.IsNullOrEmpty (full_filename))
1439                                 return;
1440
1441                         evaluatedProperties.AddProperty (new BuildProperty ("MSBuildThisFile", Path.GetFileName (full_filename), PropertyType.Reserved));
1442                         evaluatedProperties.AddProperty (new BuildProperty ("MSBuildThisFileFullPath", full_filename, PropertyType.Reserved));
1443                         evaluatedProperties.AddProperty (new BuildProperty ("MSBuildThisFileName", Path.GetFileNameWithoutExtension (full_filename), PropertyType.Reserved));
1444                         evaluatedProperties.AddProperty (new BuildProperty ("MSBuildThisFileExtension", Path.GetExtension (full_filename), PropertyType.Reserved));
1445
1446                         string project_dir = Path.GetDirectoryName (full_filename) + Path.DirectorySeparatorChar;
1447                         evaluatedProperties.AddProperty (new BuildProperty ("MSBuildThisFileDirectory", project_dir, PropertyType.Reserved));
1448                         evaluatedProperties.AddProperty (new BuildProperty ("MSBuildThisFileDirectoryNoRoot",
1449                                                 project_dir.Substring (Path.GetPathRoot (project_dir).Length),
1450                                                 PropertyType.Reserved));
1451                 }
1452
1453
1454                 internal void LogWarning (string filename, string message, params object[] messageArgs)
1455                 {
1456                         BuildWarningEventArgs bwea = new BuildWarningEventArgs (
1457                                 null, null, filename, 0, 0, 0, 0, String.Format (message, messageArgs),
1458                                 null, null);
1459                         ParentEngine.EventSource.FireWarningRaised (this, bwea);
1460                 }
1461
1462                 internal void LogError (string filename, string message,
1463                                      params object[] messageArgs)
1464                 {
1465                         BuildErrorEventArgs beea = new BuildErrorEventArgs (
1466                                 null, null, filename, 0, 0, 0, 0, String.Format (message, messageArgs),
1467                                 null, null);
1468                         ParentEngine.EventSource.FireErrorRaised (this, beea);
1469                 }
1470
1471                 internal static string DefaultExtensionsPath {
1472                         get {
1473                                 if (extensions_path == null) {
1474                                         // NOTE: code from mcs/tools/gacutil/driver.cs
1475                                         PropertyInfo gac = typeof (System.Environment).GetProperty (
1476                                                         "GacPath", BindingFlags.Static | BindingFlags.NonPublic);
1477
1478                                         if (gac != null) {
1479                                                 MethodInfo get_gac = gac.GetGetMethod (true);
1480                                                 string gac_path = (string) get_gac.Invoke (null, null);
1481                                                 extensions_path = Path.GetFullPath (Path.Combine (
1482                                                                         gac_path, Path.Combine ("..", "xbuild")));
1483                                         }
1484                                 }
1485                                 return extensions_path;
1486                         }
1487                 }
1488
1489                 public BuildPropertyGroup EvaluatedProperties {
1490                         get {
1491                                 if (needToReevaluate) {
1492                                         needToReevaluate = false;
1493                                         Reevaluate ();
1494                                 }
1495                                 return evaluatedProperties;
1496                         }
1497                 }
1498
1499                 internal IEnumerable EvaluatedPropertiesAsDictionaryEntries {
1500                         get {
1501                                 foreach (BuildProperty bp in EvaluatedProperties)
1502                                         yield return new DictionaryEntry (bp.Name, bp.Value);
1503                         }
1504                 }
1505
1506                 public string FullFileName {
1507                         get { return fullFileName; }
1508                         set { fullFileName = value; }
1509                 }
1510
1511                 public BuildPropertyGroup GlobalProperties {
1512                         get { return globalProperties; }
1513                         set {
1514                                 if (value == null)
1515                                         throw new ArgumentNullException ("value");
1516                                 
1517                                 if (value.FromXml)
1518                                         throw new InvalidOperationException ("GlobalProperties can not be set to persisted property group.");
1519                                 
1520                                 globalProperties = value;
1521                         }
1522                 }
1523
1524                 public bool IsDirty {
1525                         get { return isDirty; }
1526                 }
1527
1528                 public bool IsValidated {
1529                         get { return isValidated; }
1530                         set { isValidated = value; }
1531                 }
1532
1533                 public BuildItemGroupCollection ItemGroups {
1534                         get { return itemGroups; }
1535                 }
1536                 
1537                 public ImportCollection Imports {
1538                         get { return imports; }
1539                 }
1540                 
1541                 public string InitialTargets {
1542                         get {
1543                                 return String.Join ("; ", initialTargets.ToArray ());
1544                         }
1545                         set {
1546                                 initialTargets.Clear ();
1547                                 xmlDocument.DocumentElement.SetAttribute ("InitialTargets", value);
1548                                 if (value != null)
1549                                         initialTargets.AddRange (value.Split (
1550                                                                 new char [] {';', ' '}, StringSplitOptions.RemoveEmptyEntries));
1551                         }
1552                 }
1553
1554                 public Engine ParentEngine {
1555                         get { return parentEngine; }
1556                 }
1557
1558                 public BuildPropertyGroupCollection PropertyGroups {
1559                         get { return propertyGroups; }
1560                 }
1561
1562                 public string SchemaFile {
1563                         get { return schemaFile; }
1564                         set { schemaFile = value; }
1565                 }
1566
1567                 public TargetCollection Targets {
1568                         get { return targets; }
1569                 }
1570
1571                 public DateTime TimeOfLastDirty {
1572                         get { return timeOfLastDirty; }
1573                 }
1574                 
1575                 public UsingTaskCollection UsingTasks {
1576                         get { return usingTasks; }
1577                 }
1578
1579                 [MonoTODO]
1580                 public string Xml {
1581                         get { return xmlDocument.InnerXml; }
1582                 }
1583
1584                 // corresponds to the xml attribute
1585                 public string DefaultToolsVersion {
1586                         get {
1587                                 if (xmlDocument != null)
1588                                         return xmlDocument.DocumentElement.GetAttribute ("ToolsVersion");
1589                                 return null;
1590                         }
1591                         set {
1592                                 if (xmlDocument != null)
1593                                         xmlDocument.DocumentElement.SetAttribute ("ToolsVersion", value);
1594                         }
1595                 }
1596
1597                 public bool HasToolsVersionAttribute {
1598                         get {
1599                                 return xmlDocument != null && xmlDocument.DocumentElement.HasAttribute ("ToolsVersion");
1600                         }
1601                 }
1602
1603                 public string ToolsVersion {
1604                         get; internal set;
1605                 }
1606
1607                 internal Dictionary <string, BuildItemGroup> LastItemGroupContaining {
1608                         get { return last_item_group_containing; }
1609                 }
1610                 
1611                 internal ProjectLoadSettings ProjectLoadSettings {
1612                         get { return project_load_settings; }
1613                         set { project_load_settings = value; }
1614                 }
1615
1616                 internal static XmlNamespaceManager XmlNamespaceManager {
1617                         get {
1618                                 if (manager == null) {
1619                                         manager = new XmlNamespaceManager (new NameTable ());
1620                                         manager.AddNamespace ("tns", ns);
1621                                 }
1622                                 
1623                                 return manager;
1624                         }
1625                 }
1626                 
1627                 internal TaskDatabase TaskDatabase {
1628                         get { return taskDatabase; }
1629                 }
1630                 
1631                 internal XmlDocument XmlDocument {
1632                         get { return xmlDocument; }
1633                 }
1634                 
1635                 internal static string XmlNamespace {
1636                         get { return ns; }
1637                 }
1638
1639                 static string OS {
1640                         get {
1641                                 PlatformID pid = Environment.OSVersion.Platform;
1642                                 switch ((int)pid) {
1643                                 case 128:
1644                                 case 4:
1645                                         return "Unix";
1646                                 case 6:
1647                                         return "OSX";
1648                                 default:
1649                                         return "Windows_NT";
1650                                 }
1651                         }
1652                 }
1653
1654         }
1655 }