2 // ProjectCollection.cs
5 // Leszek Ciesielski (skolima@gmail.com)
6 // Rolf Bjarne Kvinge (rolf@xamarin.com)
7 // Atsushi Enomoto (atsushi@xamarin.com)
9 // (C) 2011 Leszek Ciesielski
10 // Copyright (C) 2011,2013 Xamarin Inc.
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using Microsoft.Build.Construction;
33 using Microsoft.Build.Execution;
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Logging;
36 using Microsoft.Build.Utilities;
38 using System.Collections.Generic;
39 using System.Collections.ObjectModel;
43 using System.Reflection;
44 using System.Globalization;
45 using Mono.XBuild.Utilities;
46 using Microsoft.Build.Internal;
48 namespace Microsoft.Build.Evaluation
50 public class ProjectCollection : IDisposable
52 public delegate void ProjectAddedEventHandler (object target, ProjectAddedToProjectCollectionEventArgs args);
54 public class ProjectAddedToProjectCollectionEventArgs : EventArgs
56 public ProjectAddedToProjectCollectionEventArgs (ProjectRootElement project)
59 throw new ArgumentNullException ("project");
60 ProjectRootElement = project;
63 public ProjectRootElement ProjectRootElement { get; private set; }
68 static readonly ProjectCollection global_project_collection;
70 static ProjectCollection ()
73 global_project_collection = new ProjectCollection (new ReadOnlyDictionary<string, string> (new Dictionary<string, string> ()));
75 global_project_collection = new ProjectCollection (new Dictionary<string, string> ());
79 public static string Escape (string unescapedString)
81 return Mono.XBuild.Utilities.MSBuildUtils.Escape (unescapedString);
84 public static string Unescape (string escapedString)
86 return Mono.XBuild.Utilities.MSBuildUtils.Unescape (escapedString);
89 public static ProjectCollection GlobalProjectCollection {
90 get { return global_project_collection; }
93 // semantic model part
95 public ProjectCollection ()
100 public ProjectCollection (IDictionary<string, string> globalProperties)
101 : this (globalProperties, null, ToolsetDefinitionLocations.Registry | ToolsetDefinitionLocations.ConfigurationFile)
105 public ProjectCollection (ToolsetDefinitionLocations toolsetDefinitionLocations)
106 : this (null, null, toolsetDefinitionLocations)
110 public ProjectCollection (IDictionary<string, string> globalProperties, IEnumerable<ILogger> loggers,
111 ToolsetDefinitionLocations toolsetDefinitionLocations)
112 : this (globalProperties, loggers, null, toolsetDefinitionLocations, 1, false)
116 public ProjectCollection (IDictionary<string, string> globalProperties,
117 IEnumerable<ILogger> loggers, IEnumerable<ForwardingLoggerRecord> remoteLoggers,
118 ToolsetDefinitionLocations toolsetDefinitionLocations,
119 int maxNodeCount, bool onlyLogCriticalEvents)
121 global_properties = globalProperties ?? new Dictionary<string, string> ();
122 this.loggers = loggers != null ? loggers.ToList () : new List<ILogger> ();
123 toolset_locations = toolsetDefinitionLocations;
124 MaxNodeCount = maxNodeCount;
125 OnlyLogCriticalEvents = onlyLogCriticalEvents;
127 LoadDefaultToolsets ();
130 [MonoTODO ("not fired yet")]
131 public event ProjectAddedEventHandler ProjectAdded;
132 [MonoTODO ("not fired yet")]
133 public event EventHandler<ProjectChangedEventArgs> ProjectChanged;
134 [MonoTODO ("not fired yet")]
135 public event EventHandler<ProjectCollectionChangedEventArgs> ProjectCollectionChanged;
136 [MonoTODO ("not fired yet")]
137 public event EventHandler<ProjectXmlChangedEventArgs> ProjectXmlChanged;
139 public void AddProject (Project project)
141 this.loaded_projects.Add (project);
142 if (ProjectAdded != null)
143 ProjectAdded (this, new ProjectAddedToProjectCollectionEventArgs (project.Xml));
147 get { return loaded_projects.Count; }
150 string default_tools_version;
151 public string DefaultToolsVersion {
152 get { return default_tools_version; }
154 if (GetToolset (value) == null)
155 throw new InvalidOperationException (string.Format ("Toolset '{0}' does not exist", value));
156 default_tools_version = value;
160 public void Dispose ()
163 GC.SuppressFinalize (this);
166 protected virtual void Dispose (bool disposing)
172 public ICollection<Project> GetLoadedProjects (string fullPath)
174 return LoadedProjects.Where (p => p.FullPath != null && Path.GetFullPath (p.FullPath) == Path.GetFullPath (fullPath)).ToList ();
177 readonly IDictionary<string, string> global_properties;
179 public IDictionary<string, string> GlobalProperties {
180 get { return global_properties; }
183 readonly List<Project> loaded_projects = new List<Project> ();
185 public Project LoadProject (string fileName)
187 return LoadProject (fileName, DefaultToolsVersion);
190 public Project LoadProject (string fileName, string toolsVersion)
192 return LoadProject (fileName, null, toolsVersion);
195 public Project LoadProject (string fileName, IDictionary<string,string> globalProperties, string toolsVersion)
197 var ret = new Project (fileName, globalProperties, toolsVersion);
198 loaded_projects.Add (ret);
202 // These methods somehow don't add the project to ProjectCollection...
203 public Project LoadProject (XmlReader xmlReader)
205 return LoadProject (xmlReader, DefaultToolsVersion);
208 public Project LoadProject (XmlReader xmlReader, string toolsVersion)
210 return LoadProject (xmlReader, null, toolsVersion);
213 public Project LoadProject (XmlReader xmlReader, IDictionary<string,string> globalProperties, string toolsVersion)
215 return new Project (xmlReader, globalProperties, toolsVersion);
218 public ICollection<Project> LoadedProjects {
219 get { return loaded_projects; }
222 readonly List<ILogger> loggers = new List<ILogger> ();
224 public ICollection<ILogger> Loggers {
225 get { return loggers; }
229 public bool OnlyLogCriticalEvents { get; set; }
232 public bool SkipEvaluation { get; set; }
234 readonly ToolsetDefinitionLocations toolset_locations;
235 public ToolsetDefinitionLocations ToolsetLocations {
236 get { return toolset_locations; }
239 readonly List<Toolset> toolsets = new List<Toolset> ();
240 // so what should we do without ToolLocationHelper in Microsoft.Build.Utilities.dll? There is no reference to it in this dll.
241 public ICollection<Toolset> Toolsets {
242 // For ConfigurationFile and None, they cannot be added externally.
243 get { return (ToolsetLocations & ToolsetDefinitionLocations.Registry) != 0 ? toolsets : toolsets.ToList (); }
246 public Toolset GetToolset (string toolsVersion)
248 return Toolsets.FirstOrDefault (t => t.ToolsVersion == toolsVersion);
251 //FIXME: should also support config file, depending on ToolsetLocations
252 void LoadDefaultToolsets ()
254 AddToolset (new Toolset ("2.0",
255 ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version20), this, null));
256 AddToolset (new Toolset ("3.0",
257 ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version30), this, null));
258 AddToolset (new Toolset ("3.5",
259 ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version35), this, null));
261 AddToolset (new Toolset ("4.0",
262 ToolLocationHelper.GetPathToDotNetFramework (TargetDotNetFrameworkVersion.Version40), this, null));
265 AddToolset (new Toolset ("12.0", ToolLocationHelper.GetPathToBuildTools ("12.0"), this, null));
267 default_tools_version = toolsets.First ().ToolsVersion;
270 [MonoTODO ("not verified at all")]
271 public void AddToolset (Toolset toolset)
273 toolsets.Add (toolset);
276 [MonoTODO ("not verified at all")]
277 public void RemoveAllToolsets ()
282 public void RegisterLogger (ILogger logger)
284 loggers.Add (logger);
287 public void RegisterLoggers (IEnumerable<ILogger> loggers)
289 foreach (var logger in loggers)
290 this.loggers.Add (logger);
293 public void UnloadAllProjects ()
295 throw new NotImplementedException ();
298 [MonoTODO ("Not verified at all")]
299 public void UnloadProject (Project project)
301 this.loaded_projects.Remove (project);
304 [MonoTODO ("Not verified at all")]
305 public void UnloadProject (ProjectRootElement projectRootElement)
307 foreach (var proj in loaded_projects.Where (p => p.Xml == projectRootElement).ToArray ())
308 UnloadProject (proj);
311 public static Version Version {
312 get { throw new NotImplementedException (); }
318 public bool DisableMarkDirty { get; set; }
321 public HostServices HostServices { get; set; }
324 public bool IsBuildEnabled { get; set; }
326 internal string BuildStartupDirectory { get; set; }
328 internal int MaxNodeCount { get; private set; }
330 Stack<string> ongoing_imports = new Stack<string> ();
332 internal Stack<string> OngoingImports {
333 get { return ongoing_imports; }
337 internal static IEnumerable<EnvironmentProjectProperty> GetWellKnownProperties (Project project)
339 Func<string,string,EnvironmentProjectProperty> create = (name, value) => new EnvironmentProjectProperty (project, name, value, true);
340 return GetWellKnownProperties (create);
343 internal static IEnumerable<ProjectPropertyInstance> GetWellKnownProperties (ProjectInstance project)
345 Func<string,string,ProjectPropertyInstance> create = (name, value) => new ProjectPropertyInstance (name, true, value);
346 return GetWellKnownProperties (create);
349 static IEnumerable<T> GetWellKnownProperties<T> (Func<string,string,T> create)
351 yield return create ("OS", OS);
352 var ext = Environment.GetEnvironmentVariable ("MSBuildExtensionsPath") ?? DefaultExtensionsPath;
353 yield return create ("MSBuildExtensionsPath", ext);
354 var ext32 = Environment.GetEnvironmentVariable ("MSBuildExtensionsPath32") ?? ext;
355 yield return create ("MSBuildExtensionsPath32", ext32);
356 var ext64 = Environment.GetEnvironmentVariable ("MSBuildExtensionsPath64") ?? ext;
357 yield return create ("MSBuildExtensionsPath64", ext64);
362 PlatformID pid = Environment.OSVersion.Platform;
375 #region Extension Paths resolution
377 static string extensions_path;
378 internal static string DefaultExtensionsPath {
380 if (extensions_path == null) {
381 // NOTE: code from mcs/tools/gacutil/driver.cs
382 PropertyInfo gac = typeof (System.Environment).GetProperty (
383 "GacPath", BindingFlags.Static | BindingFlags.NonPublic);
386 MethodInfo get_gac = gac.GetGetMethod (true);
387 string gac_path = (string) get_gac.Invoke (null, null);
388 extensions_path = Path.GetFullPath (Path.Combine (
389 gac_path, Path.Combine ("..", "xbuild")));
392 return extensions_path;
396 static string DotConfigExtensionsPath = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData),
397 Path.Combine ("xbuild", "tasks"));
398 const string MacOSXExternalXBuildDir = "/Library/Frameworks/Mono.framework/External/xbuild";
399 static string PathSeparatorAsString = Path.PathSeparator.ToString ();
401 // Gives a list of extensions paths to try for $(MSBuildExtensionsPath),
403 internal static IEnumerable<string> GetApplicableExtensionsPaths (Action<string> logMessage)
405 string envvar = String.Join (PathSeparatorAsString, new string [] {
406 // For mac osx, look in the 'External' dir on macosx,
408 MSBuildUtils.RunningOnMac ? MacOSXExternalXBuildDir : String.Empty,
409 DotConfigExtensionsPath,
410 DefaultExtensionsPath});
412 var pathsTable = new Dictionary<string, string> ();
413 foreach (string extn_path in envvar.Split (new char [] {Path.PathSeparator}, StringSplitOptions.RemoveEmptyEntries)) {
414 if (pathsTable.ContainsKey (extn_path))
417 if (!Directory.Exists (extn_path)) {
418 logMessage (string.Format ("Extension path '{0}' not found, ignoring.", extn_path));
422 pathsTable [extn_path] = extn_path;
423 yield return extn_path;
427 internal static string FindFileInSeveralExtensionsPath (ref string extensionsPathOverride, Func<string,string> expandString, string file, Action<string> logMessage)
430 string ex = extensionsPathOverride;
431 Func<bool> action = () => {
432 string path = WindowsCompatibilityExtensions.FindMatchingPath (expandString (file));
433 if (File.Exists (path))
442 foreach (var s in ProjectCollection.GetApplicableExtensionsPaths (logMessage)) {
443 extensionsPathOverride = s;
450 extensionsPathOverride = null;
453 return ret ?? WindowsCompatibilityExtensions.FindMatchingPath (expandString (file));
458 internal IEnumerable<ReservedProjectProperty> GetReservedProperties (Toolset toolset, Project project)
460 Func<string,Func<string>,ReservedProjectProperty> create = (name, value) => new ReservedProjectProperty (project, name, value);
461 return GetReservedProperties<ReservedProjectProperty> (toolset, project.Xml, create, () => project.FullPath);
464 internal IEnumerable<ProjectPropertyInstance> GetReservedProperties (Toolset toolset, ProjectInstance project, ProjectRootElement xml)
466 Func<string,Func<string>,ProjectPropertyInstance> create = (name, value) => new ProjectPropertyInstance (name, true, null, value);
467 return GetReservedProperties<ProjectPropertyInstance> (toolset, xml, create, () => project.FullPath);
470 // seealso http://msdn.microsoft.com/en-us/library/ms164309.aspx
471 IEnumerable<T> GetReservedProperties<T> (Toolset toolset, ProjectRootElement project, Func<string,Func<string>,T> create, Func<string> projectFullPath)
473 yield return create ("MSBuildBinPath", () => toolset.ToolsPath);
474 // FIXME: add MSBuildLastTaskResult
475 // FIXME: add MSBuildNodeCount
476 // FIXME: add MSBuildProgramFiles32
477 yield return create ("MSBuildProjectDefaultTargets", () => project.DefaultTargets);
478 yield return create ("MSBuildProjectDirectory", () => project.DirectoryPath + Path.DirectorySeparatorChar);
479 yield return create ("MSBuildProjectDirectoryNoRoot", () => project.DirectoryPath.Substring (Path.GetPathRoot (project.DirectoryPath).Length));
480 yield return create ("MSBuildProjectExtension", () => Path.GetExtension (project.FullPath));
481 yield return create ("MSBuildProjectFile", () => Path.GetFileName (project.FullPath));
482 yield return create ("MSBuildProjectFullPath", () => project.FullPath);
483 yield return create ("MSBuildProjectName", () => Path.GetFileNameWithoutExtension (project.FullPath));
484 yield return create ("MSBuildStartupDirectory", () => BuildStartupDirectory);
485 yield return create ("MSBuildThisFile", () => Path.GetFileName (GetEvaluationTimeThisFile (projectFullPath)));
486 yield return create ("MSBuildThisFileFullPath", () => GetEvaluationTimeThisFile (projectFullPath));
487 yield return create ("MSBuildThisFileName", () => Path.GetFileNameWithoutExtension (GetEvaluationTimeThisFile (projectFullPath)));
488 yield return create ("MSBuildThisFileExtension", () => Path.GetExtension (GetEvaluationTimeThisFile (projectFullPath)));
490 yield return create ("MSBuildThisFileDirectory", () => Path.GetDirectoryName (GetEvaluationTimeThisFileDirectory (projectFullPath)));
491 yield return create ("MSBuildThisFileDirectoryNoRoot", () => {
492 string dir = GetEvaluationTimeThisFileDirectory (projectFullPath) + Path.DirectorySeparatorChar;
493 return dir.Substring (Path.GetPathRoot (dir).Length);
495 yield return create ("MSBuildToolsPath", () => toolset.ToolsPath);
496 yield return create ("MSBuildToolsVersion", () => toolset.ToolsVersion);
499 // These are required for reserved property, represents dynamically changing property values.
500 // This should resolve to either the project file path or that of the imported file.
501 internal string GetEvaluationTimeThisFileDirectory (Func<string> nonImportingTimeFullPath)
503 var file = GetEvaluationTimeThisFile (nonImportingTimeFullPath);
504 var dir = Path.IsPathRooted (file) ? Path.GetDirectoryName (file) : Directory.GetCurrentDirectory ();
505 return dir + Path.DirectorySeparatorChar;
508 internal string GetEvaluationTimeThisFile (Func<string> nonImportingTimeFullPath)
510 return OngoingImports.Count > 0 ? OngoingImports.Peek () : (nonImportingTimeFullPath () ?? string.Empty);
513 static readonly char [] item_target_sep = {';'};
515 internal static IEnumerable<T> GetAllItems<T> (Func<string,string> expandString, string include, string exclude, Func<string,T> creator, Func<string,ITaskItem> taskItemCreator, string directory, Action<T,string> assignRecurse, Func<ITaskItem,bool> isDuplicate)
517 var includes = expandString (include).Trim ().Split (item_target_sep, StringSplitOptions.RemoveEmptyEntries);
518 var excludes = expandString (exclude).Trim ().Split (item_target_sep, StringSplitOptions.RemoveEmptyEntries);
520 if (includes.Length == 0)
522 if (includes.Length == 1 && includes [0].IndexOf ('*') < 0 && excludes.Length == 0) {
523 // for most case - shortcut.
524 var item = creator (includes [0]);
527 var ds = new Microsoft.Build.BuildEngine.DirectoryScanner () {
528 BaseDirectory = new DirectoryInfo (directory),
529 Includes = includes.Where (s => !string.IsNullOrWhiteSpace (s)).Select (i => taskItemCreator (i)).ToArray (),
530 Excludes = excludes.Where (s => !string.IsNullOrWhiteSpace (s)).Select (e => taskItemCreator (e)).ToArray (),
533 foreach (var taskItem in ds.MatchedItems) {
534 if (isDuplicate (taskItem))
535 continue; // skip duplicate
536 var item = creator (taskItem.ItemSpec);
537 string recurse = taskItem.GetMetadata ("RecursiveDir");
538 assignRecurse (item, recurse);
544 static readonly char [] path_sep = {Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar};
546 internal static string GetWellKnownMetadata (string name, string file, Func<string,string> getFullPath, string recursiveDir)
548 switch (name.ToLower (CultureInfo.InvariantCulture)) {
550 return getFullPath (file);
552 return Path.GetPathRoot (getFullPath (file));
554 return Path.GetFileNameWithoutExtension (file);
556 return Path.GetExtension (file);
558 var idx = file.LastIndexOfAny (path_sep);
559 return idx < 0 ? string.Empty : file.Substring (0, idx + 1);
561 var fp = getFullPath (file);
562 return Path.GetDirectoryName (fp).Substring (Path.GetPathRoot (fp).Length);
568 return new FileInfo (getFullPath (file)).LastWriteTime.ToString ("yyyy-MM-dd HH:mm:ss.fffffff");
570 return new FileInfo (getFullPath (file)).CreationTime.ToString ("yyyy-MM-dd HH:mm:ss.fffffff");
572 return new FileInfo (getFullPath (file)).LastAccessTime.ToString ("yyyy-MM-dd HH:mm:ss.fffffff");