6289e0e00a250d3f55d2491c1d6acfa39c65bcf4
[mono.git] / mcs / tools / xbuild / SolutionParser.cs
1 //
2 // SolutionParser.cs: Generates a project file from a solution file.
3 //
4 // Author:
5 //   Jonathan Chambers (joncham@gmail.com)
6 //   Ankit Jain <jankit@novell.com>
7 //   Lluis Sanchez Gual <lluis@novell.com>
8 //
9 // (C) 2009 Jonathan Chambers
10 // Copyright 2008, 2009 Novell, Inc (http://www.novell.com)
11 //
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:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
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.
30
31 #if NET_2_0
32
33 using System;
34 using System.Collections.Generic;
35 using System.Text;
36 using System.Text.RegularExpressions;
37 using System.IO;
38 using Microsoft.Build.BuildEngine;
39
40 namespace Mono.XBuild.CommandLine {
41         class ProjectInfo {
42                 public string Name;
43                 public string FileName;
44
45                 public ProjectInfo (string name, string fileName)
46                 {
47                         Name = name;
48                         FileName = fileName;
49                 }
50
51                 public Dictionary<TargetInfo, TargetInfo> TargetMap = new Dictionary<TargetInfo, TargetInfo> ();
52                 public Dictionary<Guid, ProjectInfo> Dependencies = new Dictionary<Guid, ProjectInfo> ();
53         }
54
55         struct TargetInfo {
56                 public string Configuration;
57                 public string Platform;
58                 public bool Build;
59
60                 public TargetInfo (string configuration, string platform)
61                         : this (configuration, platform, false)
62                 {
63                 }
64
65                 public TargetInfo (string configuration, string platform, bool build)
66                 {
67                         Configuration = configuration;
68                         Platform = platform;
69                         Build = build;
70                 }
71         }
72
73
74         class SolutionParser {
75                 static string[] buildTargets = new string[] { "Build", "Clean", "Rebuild", "Publish" };
76
77                 static string guidExpression = "{[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}}";
78
79                 static Regex projectRegex = new Regex ("Project\\(\"(" + guidExpression + ")\"\\) = \"(.*?)\", \"(.*?)\", \"(" + guidExpression + ")\"(\\s*?)((\\s*?)ProjectSection\\((.*?)\\) = (.*?)EndProjectSection(\\s*?))*(\\s*?)EndProject?", RegexOptions.Singleline);
80                 static Regex projectDependenciesRegex = new Regex ("ProjectSection\\((.*?)\\) = \\w*(.*?)EndProjectSection", RegexOptions.Singleline);
81                 static Regex projectDependencyRegex = new Regex ("\\s*(" + guidExpression + ") = (" + guidExpression + ")");
82
83                 static Regex globalRegex = new Regex ("Global(.*)EndGlobal", RegexOptions.Singleline);
84                 static Regex globalSectionRegex = new Regex ("GlobalSection\\((.*?)\\) = \\w*(.*?)EndGlobalSection", RegexOptions.Singleline);
85
86                 static Regex solutionConfigurationRegex = new Regex ("\\s*(.*?)\\|(.*?) = (.*?)\\|(.+)");
87                 static Regex projectConfigurationActiveCfgRegex = new Regex ("\\s*(" + guidExpression + ")\\.(.+?)\\|(.+?)\\.ActiveCfg = (.+?)\\|(.+)");
88                 static Regex projectConfigurationBuildRegex = new Regex ("\\s*(" + guidExpression + ")\\.(.*?)\\|(.*?)\\.Build\\.0 = (.*?)\\|(.+)");
89
90                 static string solutionFolderGuid = "{2150E333-8FDC-42A3-9474-1A3956D46DE8}";
91                 static string vcprojGuid = "{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}";
92
93                 public void ParseSolution (string file, Project p)
94                 {
95                         AddGeneralSettings (file, p);
96
97                         StreamReader reader = new StreamReader (file);
98                         string line = reader.ReadToEnd ();
99                         line = line.Replace ("\r\n", "\n");
100                         string solutionDir = Path.GetDirectoryName (file);
101
102                         List<TargetInfo> solutionTargets = new List<TargetInfo> ();
103                         Dictionary<Guid, ProjectInfo> projectInfos = new Dictionary<Guid, ProjectInfo> ();
104
105                         Match m = projectRegex.Match (line);
106                         while (m.Success) {
107                                 ProjectInfo projectInfo = new ProjectInfo (m.Groups[2].Value, m.Groups[3].Value);
108                                 if (String.Compare (m.Groups [1].Value, solutionFolderGuid,
109                                                 StringComparison.InvariantCultureIgnoreCase) == 0) {
110                                         // Ignore solution folders
111                                         m = m.NextMatch ();
112                                         continue;
113                                 }
114                                 if (String.Compare (m.Groups [1].Value, vcprojGuid,
115                                                 StringComparison.InvariantCultureIgnoreCase) == 0) {
116                                         // Ignore vcproj 
117                                         ErrorUtilities.ReportWarning (0, string.Format("Ignoring vcproj '{0}'.", projectInfo.Name));
118                                         m = m.NextMatch ();
119                                         continue;
120                                 }
121
122                                 projectInfos.Add (new Guid (m.Groups[4].Value), projectInfo);
123
124                                 Match projectSectionMatch = projectDependenciesRegex.Match (m.Groups[6].Value);
125                                 while (projectSectionMatch.Success) {
126                                         Match projectDependencyMatch = projectDependencyRegex.Match (projectSectionMatch.Value);
127                                         while (projectDependencyMatch.Success) {
128                                                 // we might not have projectInfo available right now, so
129                                                 // set it to null, and fill it in later
130                                                 projectInfo.Dependencies [new Guid (projectDependencyMatch.Groups[1].Value)] = null;
131                                                 projectDependencyMatch = projectDependencyMatch.NextMatch ();
132                                         }
133                                         projectSectionMatch = projectSectionMatch.NextMatch ();
134                                 }
135                                 m = m.NextMatch ();
136                         }
137
138                         foreach (ProjectInfo projectInfo in projectInfos.Values) {
139                                 Project currentProject = p.ParentEngine.CreateNewProject ();
140                                 currentProject.Load (Path.Combine (solutionDir,
141                                                         projectInfo.FileName.Replace ('\\', Path.DirectorySeparatorChar)));
142
143                                 foreach (BuildItem bi in currentProject.GetEvaluatedItemsByName ("ProjectReference")) {
144                                         string projectReferenceGuid = bi.GetEvaluatedMetadata ("Project");
145                                         Guid guid = new Guid (projectReferenceGuid);
146                                         ProjectInfo info = projectInfos [guid];
147                                         if (info != null)
148                                                 // ignore if not found
149                                                 projectInfo.Dependencies [guid] = info;
150                                 }
151                         }
152
153                         // fill in the project info for deps found in the .sln file
154                         foreach (ProjectInfo projectInfo in projectInfos.Values) {
155                                 List<Guid> missingInfos = new List<Guid> ();
156                                 foreach (KeyValuePair<Guid, ProjectInfo> dependency in projectInfo.Dependencies) {
157                                         if (dependency.Value == null)
158                                                 missingInfos.Add (dependency.Key);
159                                 }
160
161                                 foreach (Guid guid in missingInfos) {
162                                         ProjectInfo info;
163                                         if (projectInfos.TryGetValue (guid, out info))
164                                                 projectInfo.Dependencies [guid] = info;
165                                         else
166                                                 projectInfo.Dependencies.Remove (guid);
167                                 }
168                         }
169
170                         Match globalMatch = globalRegex.Match (line);
171                         Match globalSectionMatch = globalSectionRegex.Match (globalMatch.Groups[1].Value);
172                         while (globalSectionMatch.Success) {
173                                 string sectionType = globalSectionMatch.Groups[1].Value;
174                                 switch (sectionType) {
175                                         case "SolutionConfigurationPlatforms":
176                                                 ParseSolutionConfigurationPlatforms (globalSectionMatch.Groups[2].Value, solutionTargets);
177                                                 break;
178                                         case "ProjectConfigurationPlatforms":
179                                                 ParseProjectConfigurationPlatforms (globalSectionMatch.Groups[2].Value, projectInfos);
180                                                 break;
181                                         case "SolutionProperties":
182                                                 ParseSolutionProperties (globalSectionMatch.Groups[2].Value);
183                                                 break;
184                                         case "NestedProjects":
185                                                 break;
186                                         default:
187                                                 ErrorUtilities.ReportWarning (0, string.Format("Don't know how to handle GlobalSection {0}, Ignoring.", sectionType));
188                                                 break;
189                                 }
190                                 globalSectionMatch = globalSectionMatch.NextMatch ();
191                         }
192
193                         int num_levels = AddBuildLevels (p, solutionTargets, projectInfos);
194
195                         AddCurrentSolutionConfigurationContents (p, solutionTargets, projectInfos);
196                         AddValidateSolutionConfiguration (p);
197                         AddProjectTargets (p, solutionTargets, projectInfos);
198                         AddSolutionTargets (p, num_levels);
199                 }
200
201                 void AddGeneralSettings (string solutionFile, Project p)
202                 {
203                         p.DefaultTargets = "Build";
204                         p.InitialTargets = "ValidateSolutionConfiguration";
205                         p.AddNewUsingTaskFromAssemblyName ("CreateTemporaryVCProject", "Microsoft.Build.Tasks, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
206                         p.AddNewUsingTaskFromAssemblyName ("ResolveVCProjectOutput", "Microsoft.Build.Tasks, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
207
208                         BuildPropertyGroup aspNetConfigurationPropertyGroup = p.AddNewPropertyGroup (true);
209                         aspNetConfigurationPropertyGroup.Condition = " ('$(AspNetConfiguration)' == '') ";
210                         aspNetConfigurationPropertyGroup.AddNewProperty ("AspNetConfiguration", "$(Configuration)");
211
212                         string solutionFilePath = Path.GetFullPath (solutionFile);
213                         BuildPropertyGroup solutionPropertyGroup = p.AddNewPropertyGroup (true);
214                         solutionPropertyGroup.AddNewProperty ("SolutionDir", Path.GetDirectoryName (solutionFilePath) + Path.DirectorySeparatorChar);
215                         solutionPropertyGroup.AddNewProperty ("SolutionExt", Path.GetExtension (solutionFile));
216                         solutionPropertyGroup.AddNewProperty ("SolutionFileName", Path.GetFileName (solutionFile));
217                         solutionPropertyGroup.AddNewProperty ("SolutionName", Path.GetFileNameWithoutExtension (solutionFile));
218                         solutionPropertyGroup.AddNewProperty ("SolutionPath", solutionFilePath);
219                 }
220
221                 void ParseSolutionConfigurationPlatforms (string section, List<TargetInfo> solutionTargets)
222                 {
223                         Match solutionConfigurationPlatform = solutionConfigurationRegex.Match (section);
224                         while (solutionConfigurationPlatform.Success) {
225                                 string solutionConfiguration = solutionConfigurationPlatform.Groups[1].Value;
226                                 string solutionPlatform = solutionConfigurationPlatform.Groups[2].Value;
227                                 solutionTargets.Add (new TargetInfo (solutionConfiguration, solutionPlatform));
228                                 solutionConfigurationPlatform = solutionConfigurationPlatform.NextMatch ();
229                         }
230                 }
231
232                 void ParseProjectConfigurationPlatforms (string section, Dictionary<Guid, ProjectInfo> projectInfos)
233                 {
234                         List<Guid> missingGuids = new List<Guid> ();
235                         Match projectConfigurationPlatform = projectConfigurationActiveCfgRegex.Match (section);
236                         while (projectConfigurationPlatform.Success) {
237                                 Guid guid = new Guid (projectConfigurationPlatform.Groups[1].Value);
238                                 ProjectInfo projectInfo;
239                                 if (!projectInfos.TryGetValue (guid, out projectInfo)) {
240                                         if (!missingGuids.Contains (guid)) {
241                                                 ErrorUtilities.ReportWarning (0, string.Format("Failed to find project {0}", guid));
242                                                 missingGuids.Add (guid);
243                                         }
244                                         projectConfigurationPlatform = projectConfigurationPlatform.NextMatch ();
245                                         continue;
246                                 }
247                                 string solConf = projectConfigurationPlatform.Groups[2].Value;
248                                 string solPlat = projectConfigurationPlatform.Groups[3].Value;
249                                 string projConf = projectConfigurationPlatform.Groups[4].Value;
250                                 string projPlat = projectConfigurationPlatform.Groups[5].Value;
251                                 // hack, what are they doing here?
252                                 if (projPlat == "Any CPU")
253                                         projPlat = "AnyCPU";
254                                 projectInfo.TargetMap.Add (new TargetInfo (solConf, solPlat), new TargetInfo (projConf, projPlat));
255                                 projectConfigurationPlatform = projectConfigurationPlatform.NextMatch ();
256                         }
257                         Match projectConfigurationPlatformBuild = projectConfigurationBuildRegex.Match (section);
258                         while (projectConfigurationPlatformBuild.Success) {
259                                 Guid guid = new Guid (projectConfigurationPlatformBuild.Groups[1].Value);
260                                 ProjectInfo projectInfo;
261                                 if (!projectInfos.TryGetValue (guid, out projectInfo)) {
262                                         if (!missingGuids.Contains (guid)) {
263                                                 ErrorUtilities.ReportWarning (0, string.Format("Failed to find project {0}", guid));
264                                                 missingGuids.Add (guid);
265                                         }
266                                         projectConfigurationPlatformBuild = projectConfigurationPlatformBuild.NextMatch ();
267                                         continue;
268                                 }
269                                 string solConf = projectConfigurationPlatformBuild.Groups[2].Value;
270                                 string solPlat = projectConfigurationPlatformBuild.Groups[3].Value;
271                                 string projConf = projectConfigurationPlatformBuild.Groups[4].Value;
272                                 string projPlat = projectConfigurationPlatformBuild.Groups[5].Value;
273                                 // hack, what are they doing here?
274                                 if (projPlat == "Any CPU")
275                                         projPlat = "AnyCPU";
276                                 projectInfo.TargetMap[new TargetInfo (solConf, solPlat)] = new TargetInfo (projConf, projPlat, true);
277                                 projectConfigurationPlatformBuild = projectConfigurationPlatformBuild.NextMatch ();
278                         }
279                 }
280
281                 void ParseSolutionProperties (string section)
282                 {
283                 }
284
285                 void AddCurrentSolutionConfigurationContents (Project p, List<TargetInfo> solutionTargets, Dictionary<Guid, ProjectInfo> projectInfos)
286                 {
287                         AddDefaultSolutionConfiguration (p,
288                                         solutionTargets.Count > 0 ?
289                                                 solutionTargets [0] :
290                                                 new TargetInfo ("Debug", "Any CPU"));
291
292                         foreach (TargetInfo solutionTarget in solutionTargets) {
293                                 BuildPropertyGroup platformPropertyGroup = p.AddNewPropertyGroup (false);
294                                 platformPropertyGroup.Condition = string.Format (
295                                         " ('$(Configuration)' == '{0}') and ('$(Platform)' == '{1}') ",
296                                         solutionTarget.Configuration,
297                                         solutionTarget.Platform
298                                         );
299
300                                 string solutionConfigurationContents = "<SolutionConfiguration xmlns=\"\">";
301                                 foreach (KeyValuePair<Guid, ProjectInfo> projectInfo in projectInfos) {
302                                         foreach (KeyValuePair<TargetInfo, TargetInfo> targetInfo in projectInfo.Value.TargetMap) {
303                                                 if (solutionTarget.Configuration == targetInfo.Key.Configuration && solutionTarget.Platform == targetInfo.Key.Platform) {
304                                                         solutionConfigurationContents += string.Format ("<ProjectConfiguration Project=\"{0}\">{1}|{2}</ProjectConfiguration>",
305                                                                 projectInfo.Key.ToString ("B").ToUpper (), targetInfo.Value.Configuration, targetInfo.Value.Platform);
306                                                 }
307                                         }
308                                 }
309                                 solutionConfigurationContents += "</SolutionConfiguration>";
310
311                                 platformPropertyGroup.AddNewProperty ("CurrentSolutionConfigurationContents", solutionConfigurationContents);
312                         }
313                 }
314
315                 void AddDefaultSolutionConfiguration (Project p, TargetInfo target)
316                 {
317                         BuildPropertyGroup configurationPropertyGroup = p.AddNewPropertyGroup (true);
318                         configurationPropertyGroup.Condition = " '$(Configuration)' == '' ";
319                         configurationPropertyGroup.AddNewProperty ("Configuration", target.Configuration);
320
321                         BuildPropertyGroup platformPropertyGroup = p.AddNewPropertyGroup (true);
322                         platformPropertyGroup.Condition = " '$(Platform)' == '' ";
323                         platformPropertyGroup.AddNewProperty ("Platform", target.Platform);
324                 }
325
326                 void AddWarningForMissingProjectConfiguration (Target target, string slnConfig, string slnPlatform, string projectName)
327                 {
328                         BuildTask task = target.AddNewTask ("Warning");
329                         task.SetParameterValue ("Text",
330                                         String.Format ("The project configuration for project '{0}' corresponding " +
331                                                 "to the solution configuration '{1}|{2}' was not found in the solution file.",
332                                                 projectName, slnConfig, slnPlatform));
333                         task.Condition = String.Format ("('$(Configuration)' == '{0}') and ('$(Platform)' == '{1}')",
334                                                 slnConfig, slnPlatform);
335
336                 }
337
338                 void AddValidateSolutionConfiguration (Project p)
339                 {
340                         Target t = p.Targets.AddNewTarget ("ValidateSolutionConfiguration");
341                         BuildTask task = t.AddNewTask ("Error");
342                         task.SetParameterValue ("Text", "Invalid solution configuration and platform: \"$(Configuration)|$(Platform)\".");
343                         task.Condition = "('$(CurrentSolutionConfigurationContents)' == '') and ('$(SkipInvalidConfigurations)' != 'true')";
344                         task = t.AddNewTask ("Warning");
345                         task.SetParameterValue ("Text", "Invalid solution configuration and platform: \"$(Configuration)|$(Platform)\".");
346                         task.Condition = "('$(CurrentSolutionConfigurationContents)' == '') and ('$(SkipInvalidConfigurations)' == 'true')";
347                         task = t.AddNewTask ("Message");
348                         task.SetParameterValue ("Text", "Building solution configuration \"$(Configuration)|$(Platform)\".");
349                         task.Condition = "'$(CurrentSolutionConfigurationContents)' != ''";
350                 }
351
352                 void AddProjectTargets (Project p, List<TargetInfo> solutionTargets, Dictionary<Guid, ProjectInfo> projectInfos)
353                 {
354                         foreach (KeyValuePair<Guid, ProjectInfo> projectInfo in projectInfos) {
355                                 ProjectInfo project = projectInfo.Value;
356                                 foreach (string buildTarget in buildTargets) {
357                                         string target_name = project.Name +
358                                                 (buildTarget == "Build" ? string.Empty : ":" + buildTarget);
359
360                                         if (IsBuildTargetName (project.Name))
361                                                 target_name = "Solution:" + target_name;
362
363                                         Target target = p.Targets.AddNewTarget (target_name);
364                                         target.Condition = "'$(CurrentSolutionConfigurationContents)' != ''"; 
365
366                                         if (project.Dependencies.Count > 0) {
367                                                 StringBuilder dependencies = new StringBuilder ();
368                                                 foreach (ProjectInfo dependentInfo in project.Dependencies.Values) {
369                                                         if (dependencies.Length > 0)
370                                                                 dependencies.Append (";");
371                                                         if (IsBuildTargetName (dependentInfo.Name))
372                                                                 dependencies.Append ("Solution:");
373                                                         dependencies.Append (dependentInfo.Name);
374                                                         if (buildTarget != "Build")
375                                                                 dependencies.Append (":" + buildTarget);
376                                                 }
377                                                 target.DependsOnTargets = dependencies.ToString ();
378                                         }
379
380                                         foreach (TargetInfo targetInfo in solutionTargets) {
381                                                 BuildTask task = null;
382                                                 TargetInfo projectTargetInfo;
383                                                 if (!project.TargetMap.TryGetValue (targetInfo, out projectTargetInfo)) {
384                                                         AddWarningForMissingProjectConfiguration (target, targetInfo.Configuration,
385                                                                         targetInfo.Platform, project.Name);
386                                                         continue;
387                                                 }
388                                                 if (projectTargetInfo.Build) {
389                                                         task = target.AddNewTask ("MSBuild");
390                                                         task.SetParameterValue ("Projects", project.FileName);
391
392                                                         if (buildTarget != "Build")
393                                                                 task.SetParameterValue ("Targets", buildTarget);
394                                                         task.SetParameterValue ("Properties", string.Format ("Configuration={0}; Platform={1}; BuildingSolutionFile=true; CurrentSolutionConfigurationContents=$(CurrentSolutionConfigurationContents); SolutionDir=$(SolutionDir); SolutionExt=$(SolutionExt); SolutionFileName=$(SolutionFileName); SolutionName=$(SolutionName); SolutionPath=$(SolutionPath)", projectTargetInfo.Configuration, projectTargetInfo.Platform));
395                                                 } else {
396                                                         task = target.AddNewTask ("Message");
397                                                         task.SetParameterValue ("Text", string.Format ("Project \"{0}\" is disabled for solution configuration \"{1}|{2}\".", project.Name, targetInfo.Configuration, targetInfo.Platform));
398                                                 }
399                                                 task.Condition = string.Format (" ('$(Configuration)' == '{0}') and ('$(Platform)' == '{1}') ", targetInfo.Configuration, targetInfo.Platform);
400                                         }
401                                 }
402                         }
403                 }
404
405                 bool IsBuildTargetName (string name)
406                 {
407                         foreach (string tgt in buildTargets)
408                                 if (name == tgt)
409                                         return true;
410                         return false;
411                 }
412
413                 // returns number of levels
414                 int AddBuildLevels (Project p, List<TargetInfo> solutionTargets, Dictionary<Guid, ProjectInfo> projectInfos)
415                 {
416                         List<ProjectInfo>[] infosByLevel = TopologicalSort<ProjectInfo> (projectInfos.Values);
417
418                         foreach (TargetInfo targetInfo in solutionTargets) {
419                                 BuildItemGroup big = p.AddNewItemGroup ();
420                                 big.Condition = String.Format (" ('$(Configuration)' == '{0}') and ('$(Platform)' == '{1}') ",
421                                                 targetInfo.Configuration, targetInfo.Platform);
422
423                                 //FIXME: every level has projects that can be built in parallel.
424                                 //       levels are ordered on the basis of the dependency graph
425
426                                 for (int i = 0; i < infosByLevel.Length; i ++) {
427                                         string build_level = String.Format ("BuildLevel{0}", i);
428                                         string skip_level = String.Format ("SkipLevel{0}", i);
429                                         string missing_level = String.Format ("MissingConfigLevel{0}", i);
430
431                                         foreach (ProjectInfo projectInfo in infosByLevel [i]) {
432                                                 TargetInfo projectTargetInfo;
433                                                 if (!projectInfo.TargetMap.TryGetValue (targetInfo, out projectTargetInfo)) {
434                                                         // missing project config
435                                                         big.AddNewItem (missing_level, projectInfo.Name);
436                                                         continue;
437                                                 }
438
439                                                 if (projectTargetInfo.Build) {
440                                                         BuildItem item = big.AddNewItem (build_level, projectInfo.FileName);
441                                                         item.SetMetadata ("Configuration", projectTargetInfo.Configuration);
442                                                         item.SetMetadata ("Platform", projectTargetInfo.Platform);
443                                                 } else {
444                                                         // build disabled
445                                                         big.AddNewItem (skip_level, projectInfo.Name);
446                                                 }
447                                         }
448                                 }
449                         }
450
451                         return infosByLevel.Length;
452                 }
453
454                 void AddSolutionTargets (Project p, int num_levels)
455                 {
456                         foreach (string buildTarget in buildTargets) {
457                                 Target t = p.Targets.AddNewTarget (buildTarget);
458                                 t.Condition = "'$(CurrentSolutionConfigurationContents)' != ''";
459
460                                 for (int i = 0; i < num_levels; i ++) {
461                                         string level_str = String.Format ("BuildLevel{0}", i);
462                                         BuildTask task = t.AddNewTask ("MSBuild");
463                                         task.SetParameterValue ("Condition", String.Format ("'@({0})' != ''", level_str));
464                                         task.SetParameterValue ("Projects", String.Format ("@({0})", level_str));
465                                         task.SetParameterValue ("Properties",
466                                                 string.Format ("Configuration=%(Configuration); Platform=%(Platform); BuildingSolutionFile=true; CurrentSolutionConfigurationContents=$(CurrentSolutionConfigurationContents); SolutionDir=$(SolutionDir); SolutionExt=$(SolutionExt); SolutionFileName=$(SolutionFileName); SolutionName=$(SolutionName); SolutionPath=$(SolutionPath)"));
467                                         if (buildTarget != "Build")
468                                                 task.SetParameterValue ("Targets", buildTarget);
469                                         //FIXME: change this to BuildInParallel=true, when parallel
470                                         //       build support gets added
471                                         task.SetParameterValue ("RunEachTargetSeparately", "true");
472
473                                         level_str = String.Format ("SkipLevel{0}", i);
474                                         task = t.AddNewTask ("Message");
475                                         task.Condition = String.Format ("'@({0})' != ''", level_str);
476                                         task.SetParameterValue ("Text",
477                                                 String.Format ("The project '%({0}.Identity)' is disabled for solution " +
478                                                         "configuration '$(Configuration)|$(Platform)'.", level_str));
479
480                                         level_str = String.Format ("MissingConfigLevel{0}", i);
481                                         task = t.AddNewTask ("Warning");
482                                         task.Condition = String.Format ("'@({0})' != ''", level_str);
483                                         task.SetParameterValue ("Text",
484                                                 String.Format ("The project configuration for project '%({0}.Identity)' " +
485                                                         "corresponding to the solution configuration " +
486                                                         "'$(Configuration)|$(Platform)' was not found.", level_str));
487                                 }
488                         }
489                 }
490
491                 // Sorts the ProjectInfo dependency graph, to obtain
492                 // a series of build levels with projects. Projects
493                 // in each level can be run parallel (no inter-dependency).
494                 static List<T>[] TopologicalSort<T> (IEnumerable<T> items) where T: ProjectInfo
495                 {
496                         IList<T> allItems;
497                         allItems = items as IList<T>;
498                         if (allItems == null)
499                                 allItems = new List<T> (items);
500
501                         bool[] inserted = new bool[allItems.Count];
502                         bool[] triedToInsert = new bool[allItems.Count];
503                         int[] levels = new int [allItems.Count];
504
505                         int maxdepth = 0;
506                         for (int i = 0; i < allItems.Count; ++i) {
507                                 int d = Insert<T> (i, allItems, levels, inserted, triedToInsert);
508                                 if (d > maxdepth)
509                                         maxdepth = d;
510                         }
511
512                         // Separate out the project infos by build level
513                         List<T>[] infosByLevel = new List<T>[maxdepth];
514                         for (int i = 0; i < levels.Length; i ++) {
515                                 int level = levels [i] - 1;
516                                 if (infosByLevel [level] == null)
517                                         infosByLevel [level] = new List<T> ();
518
519                                 infosByLevel [level].Add (allItems [i]);
520                         }
521
522                         return infosByLevel;
523                 }
524
525                 // returns level# for the project
526                 static int Insert<T> (int index, IList<T> allItems, int[] levels, bool[] inserted, bool[] triedToInsert)
527                         where T: ProjectInfo
528                 {
529                         if (inserted [index])
530                                 return levels [index];
531
532                         if (triedToInsert[index])
533                                 throw new InvalidOperationException ("Cyclic dependency found in the project dependency graph");
534
535                         triedToInsert[index] = true;
536                         ProjectInfo insertItem = allItems[index];
537
538                         int maxdepth = 0;
539                         foreach (ProjectInfo dependency in insertItem.Dependencies.Values) {
540                                 for (int j = 0; j < allItems.Count; ++j) {
541                                         ProjectInfo checkItem = allItems [j];
542                                         if (dependency.FileName == checkItem.FileName) {
543                                                 int d = Insert (j, allItems, levels, inserted, triedToInsert);
544                                                 maxdepth = d > maxdepth ? d : maxdepth;
545                                                 break;
546                                         }
547                                 }
548                         }
549                         levels [index] = maxdepth + 1;
550                         inserted [index] = true;
551
552                         return levels [index];
553                 }
554         }
555 }
556
557 #endif